Files
mp-units/src/include/units/prefix.h
T

77 lines
2.7 KiB
C++
Raw Normal View History

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
#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
/**
* @brief The base for all prefix types
*
2019-12-01 19:47:58 +01:00
* Every prefix type should inherit from this type to satisfy PrefixType concept.
*/
struct prefix_type {};
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-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.
*/
struct no_prefix : prefix_type {};
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
template<PrefixType PT, Ratio R>
struct prefix_base : downcast_base<prefix_base<PT, R>> {
using prefix_type = PT;
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-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-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
*/
template<typename Child, PrefixType PT, basic_fixed_string Symbol, Ratio R>
requires (!std::same_as<PT, no_prefix>)
struct prefix : downcast_child<Child, detail::prefix_base<PT, ratio<R::num, R::den, R::exp>>> {
2019-12-01 19:47:58 +01:00
static constexpr auto symbol = Symbol;
};
2019-11-04 06:37:50 +00:00
} // namespace units