2019-11-04 06:37:50 +00:00
|
|
|
// The MIT License (MIT)
|
|
|
|
|
//
|
|
|
|
|
// Copyright (c) 2018 Mateusz Pusz
|
|
|
|
|
//
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
|
//
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
// copies or substantial portions of the Software.
|
|
|
|
|
//
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
// SOFTWARE.
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2019-12-11 08:07:13 +01:00
|
|
|
#include <units/bits/external/downcasting.h>
|
|
|
|
|
#include <units/bits/external/fixed_string.h>
|
2019-11-04 06:37:50 +00:00
|
|
|
#include <units/ratio.h>
|
|
|
|
|
|
|
|
|
|
namespace units {
|
|
|
|
|
|
2019-12-01 19:47:58 +01:00
|
|
|
/**
|
2020-03-19 14:44:56 +01:00
|
|
|
* @brief The base for all prefix families
|
2019-12-27 18:49:43 +00:00
|
|
|
*
|
2020-03-19 14:44:56 +01:00
|
|
|
* Every prefix family should inherit from this type to satisfy PrefixFamily concept.
|
2019-12-01 19:47:58 +01:00
|
|
|
*/
|
2020-03-19 14:44:56 +01:00
|
|
|
struct prefix_family {};
|
2019-11-08 16:51:45 +00:00
|
|
|
|
2019-12-01 19:47:58 +01:00
|
|
|
/**
|
|
|
|
|
* @brief No prefix possible for the unit
|
2019-12-27 18:49:43 +00:00
|
|
|
*
|
2019-12-01 19:47:58 +01:00
|
|
|
* This is a special prefix type tag specifying that the unit can not be scaled with any kind
|
|
|
|
|
* of the prefix.
|
|
|
|
|
*/
|
2020-03-19 14:44:56 +01:00
|
|
|
struct no_prefix : prefix_family {};
|
2019-11-04 06:37:50 +00:00
|
|
|
|
2019-12-01 19:47:58 +01:00
|
|
|
namespace detail {
|
2019-11-04 06:37:50 +00:00
|
|
|
|
2020-03-19 14:44:56 +01:00
|
|
|
template<PrefixFamily PT, Ratio R>
|
2019-12-01 19:47:58 +01:00
|
|
|
struct prefix_base : downcast_base<prefix_base<PT, R>> {
|
2020-03-19 14:44:56 +01:00
|
|
|
using prefix_family = PT;
|
2019-12-01 19:47:58 +01:00
|
|
|
using ratio = R;
|
|
|
|
|
};
|
2019-11-04 06:37:50 +00:00
|
|
|
|
2019-12-01 19:47:58 +01:00
|
|
|
} // namespace detail
|
2019-11-04 06:37:50 +00:00
|
|
|
|
2019-12-01 19:47:58 +01:00
|
|
|
/**
|
|
|
|
|
* @brief A prefix used to scale units
|
2019-12-27 18:49:43 +00:00
|
|
|
*
|
2019-12-01 19:47:58 +01:00
|
|
|
* Data from a prefix class is used in two cases:
|
|
|
|
|
* - when defining a prefixed_unit its ratio is used to scale the reference unit and its
|
|
|
|
|
* symbol is used to prepend to the symbol of referenced unit
|
|
|
|
|
* - when printing the symbol of a scaled unit that was not predefined by the user but its
|
|
|
|
|
* factor matches ratio of a prefix from the specified prefix family, its symbol will be
|
|
|
|
|
* prepended to the symbol of the unit
|
2019-12-27 18:49:43 +00:00
|
|
|
*
|
2019-12-01 19:47:58 +01:00
|
|
|
* @tparam Child inherited class type used by the downcasting facility (CRTP Idiom)
|
|
|
|
|
* @tparam PT a type of prefix family
|
|
|
|
|
* @tparam Symbol a text representation of the prefix
|
|
|
|
|
* @tparam R factor to be used to scale a unit
|
|
|
|
|
*/
|
2020-03-19 14:44:56 +01:00
|
|
|
template<typename Child, PrefixFamily PT, basic_fixed_string Symbol, Ratio R>
|
2019-12-12 13:17:31 +01:00
|
|
|
requires (!std::same_as<PT, no_prefix>)
|
2020-02-08 19:49:36 +01:00
|
|
|
struct prefix : downcast_child<Child, detail::prefix_base<PT, R>> {
|
2019-12-01 19:47:58 +01:00
|
|
|
static constexpr auto symbol = Symbol;
|
|
|
|
|
};
|
2019-11-04 06:37:50 +00:00
|
|
|
|
|
|
|
|
} // namespace units
|