forked from mpusz/mp-units
Text formatting enabled + directory tree refactoring
This commit is contained in:
@@ -22,19 +22,32 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <units/bits/type_traits.h>
|
||||
#include <units/bits/external/hacks.h>
|
||||
#include <units/bits/external/numeric_concepts.h>
|
||||
#include <units/customization_points.h>
|
||||
#include <units/ratio.h>
|
||||
|
||||
namespace units {
|
||||
|
||||
template<typename R>
|
||||
concept UnitRatio = Ratio<R> && (R::num * R::den > 0);
|
||||
namespace detail {
|
||||
|
||||
template<typename U, UnitRatio R>
|
||||
struct scaled_unit;
|
||||
template<typename T, typename U = T>
|
||||
concept basic_arithmetic = // exposition only
|
||||
std::magma<std::ranges::plus, T, U> &&
|
||||
std::magma<std::ranges::minus, T, U> &&
|
||||
std::magma<std::ranges::times, T, U> &&
|
||||
std::magma<std::ranges::divided_by, T, U>;
|
||||
|
||||
// Unit
|
||||
template<typename T>
|
||||
concept Unit = is_derived_from_instantiation<T, scaled_unit>;
|
||||
template<typename From, typename To>
|
||||
concept safe_convertible = // exposition only
|
||||
std::convertible_to<From, To> &&
|
||||
(treat_as_floating_point<To> || (!treat_as_floating_point<From>));
|
||||
|
||||
template<typename Rep, typename unit_from, typename unit_to>
|
||||
concept safe_divisible = // exposition only
|
||||
treat_as_floating_point<Rep> ||
|
||||
ratio_divide<typename unit_from::ratio, typename unit_to::ratio>::den == 1;
|
||||
|
||||
}
|
||||
|
||||
} // namespace units
|
||||
@@ -0,0 +1,82 @@
|
||||
// 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/fixed_string.h>
|
||||
#include <units/bits/external/text_tools.h>
|
||||
#include <units/derived_dimension.h>
|
||||
|
||||
namespace units::detail {
|
||||
|
||||
template<bool Divide, std::size_t Idx>
|
||||
constexpr auto operator_text()
|
||||
{
|
||||
if constexpr(Idx == 0) {
|
||||
if constexpr(Divide) {
|
||||
return basic_fixed_string("1/");
|
||||
}
|
||||
else {
|
||||
return basic_fixed_string("");
|
||||
}
|
||||
}
|
||||
else {
|
||||
if constexpr(Divide) {
|
||||
return basic_fixed_string("/");
|
||||
}
|
||||
else {
|
||||
return basic_fixed_string("⋅");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template<typename E, basic_fixed_string Symbol, std::size_t Idx>
|
||||
constexpr auto exp_text()
|
||||
{
|
||||
// get calculation operator + symbol
|
||||
const auto txt = operator_text<E::num < 0, Idx>() + Symbol;
|
||||
if constexpr(E::den != 1) {
|
||||
// add root part
|
||||
return txt + basic_fixed_string("^(") + regular<abs(E::num)>() + basic_fixed_string("/") + regular<E::den>() + basic_fixed_string(")");
|
||||
}
|
||||
else if constexpr(abs(E::num) != 1) {
|
||||
// add exponent part
|
||||
return txt + superscript<abs(E::num)>();
|
||||
}
|
||||
else {
|
||||
return txt;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename... Us, typename... Es, std::size_t... Idxs>
|
||||
constexpr auto deduced_symbol_text(derived_dimension<Es...>, std::index_sequence<Idxs...>)
|
||||
{
|
||||
return (exp_text<Es, Us::symbol, Idxs>() + ...);
|
||||
}
|
||||
|
||||
template<DerivedDimension Dim, Unit... Us>
|
||||
constexpr auto deduced_symbol_text()
|
||||
{
|
||||
return deduced_symbol_text<Us...>(Dim(), std::index_sequence_for<Us...>());
|
||||
}
|
||||
|
||||
} // namespace units::detail
|
||||
@@ -0,0 +1,72 @@
|
||||
// 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/unit_of_concept.h>
|
||||
#include <units/derived_dimension.h>
|
||||
|
||||
namespace units::detail {
|
||||
|
||||
// same_scaled_units
|
||||
template<DerivedDimension D, Unit... Us>
|
||||
inline constexpr bool same_scaled_units = false;
|
||||
|
||||
template<typename... Es, Unit... Us>
|
||||
inline constexpr bool same_scaled_units<derived_dimension<Es...>, Us...> = (UnitOf<Us, typename Es::dimension> && ...);
|
||||
|
||||
// deduced_unit
|
||||
template<typename Result, int UnitExpNum, int UnitExpDen, typename UnitRatio>
|
||||
struct ratio_op;
|
||||
|
||||
template<typename Result, int UnitExpDen, typename UnitRatio>
|
||||
struct ratio_op<Result, 0, UnitExpDen, UnitRatio> {
|
||||
using ratio = Result;
|
||||
};
|
||||
|
||||
template<typename Result, int UnitExpNum, int UnitExpDen, typename UnitRatio>
|
||||
struct ratio_op {
|
||||
using calc_ratio =
|
||||
conditional<(UnitExpNum * UnitExpDen > 0), ratio_multiply<Result, UnitRatio>, ratio_divide<Result, UnitRatio>>;
|
||||
static constexpr int value = (UnitExpNum * UnitExpDen > 0) ? (UnitExpNum - UnitExpDen) : (UnitExpNum + UnitExpDen);
|
||||
using ratio = ratio_op<calc_ratio, value, UnitExpDen, UnitRatio>::ratio;
|
||||
};
|
||||
|
||||
template<DerivedDimension D, Unit... Us>
|
||||
struct derived_ratio;
|
||||
|
||||
template<Unit... Us>
|
||||
struct derived_ratio<derived_dimension<>, Us...> {
|
||||
using ratio = ::units::ratio<1>;
|
||||
};
|
||||
|
||||
template<typename E, typename... ERest, Unit U, Unit... URest>
|
||||
struct derived_ratio<derived_dimension<E, ERest...>, U, URest...> {
|
||||
using rest_ratio = derived_ratio<derived_dimension<ERest...>, URest...>::ratio;
|
||||
using ratio = ratio_op<rest_ratio, E::num, E::den, typename U::ratio>::ratio;
|
||||
};
|
||||
|
||||
template<DerivedDimension D, Unit... Us>
|
||||
using deduced_unit =
|
||||
scaled_unit<typename D::coherent_unit::reference, typename detail::derived_ratio<typename D::recipe, Us...>::ratio>;
|
||||
|
||||
} // namespace units::detail
|
||||
@@ -0,0 +1,262 @@
|
||||
// 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/derived_dimension.h>
|
||||
#include <units/unit.h>
|
||||
|
||||
namespace units {
|
||||
|
||||
// equivalent_dim
|
||||
namespace detail {
|
||||
|
||||
template<Dimension D1, Dimension D2>
|
||||
struct equivalent_dim_impl : std::false_type {};
|
||||
|
||||
template<BaseDimension D1, BaseDimension D2>
|
||||
struct equivalent_base_dim : std::conjunction<std::bool_constant<D1::name == D2::name>,
|
||||
same_unit_reference<typename D1::base_unit, typename D2::base_unit>> {};
|
||||
|
||||
template<BaseDimension D1, BaseDimension D2>
|
||||
struct equivalent_dim_impl<D1, D2> : std::disjunction<std::is_same<D1, D2>, equivalent_base_dim<D1, D2>> {};
|
||||
|
||||
template<Exponent E1, Exponent E2>
|
||||
struct equivalent_exp : std::false_type {};
|
||||
|
||||
template<BaseDimension Dim1, int Num, int Den, BaseDimension Dim2>
|
||||
struct equivalent_exp<exp<Dim1, Num, Den>, exp<Dim2, Num, Den>> : equivalent_dim_impl<Dim1, Dim2> {};
|
||||
|
||||
template<DerivedDimension D1, DerivedDimension D2>
|
||||
struct equivalent_derived_dim : std::false_type {};
|
||||
|
||||
template<typename... Es1, typename... Es2>
|
||||
requires (sizeof...(Es1) == sizeof...(Es2))
|
||||
struct equivalent_derived_dim<derived_dimension<Es1...>, derived_dimension<Es2...>> : std::conjunction<equivalent_exp<Es1, Es2>...> {};
|
||||
|
||||
template<DerivedDimension D1, DerivedDimension D2>
|
||||
struct equivalent_dim_impl<D1, D2> : std::disjunction<std::is_same<D1, D2>, equivalent_derived_dim<downcast_base_t<D1>, downcast_base_t<D2>>> {};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template<Dimension D1, Dimension D2>
|
||||
inline constexpr bool equivalent_dim = detail::equivalent_dim_impl<D1, D2>::value;
|
||||
|
||||
/**
|
||||
* @brief Unknown dimension
|
||||
*
|
||||
* Sometimes a temporary partial result of a complex calculation may not result in a predefined
|
||||
* dimension. In such a case an `unknown_dimension` is created with a coherent unit of `unknown_unit`
|
||||
* and ratio<1>.
|
||||
*
|
||||
* @tparam Es zero or more exponents of a derived dimension
|
||||
*/
|
||||
template<Exponent... Es>
|
||||
struct unknown_dimension : derived_dimension<unknown_dimension<Es...>, scaled_unit<unknown_unit, ratio<1>>, Es...> {
|
||||
using coherent_unit = scaled_unit<unknown_unit, ratio<1>>;
|
||||
};
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<DerivedDimension D>
|
||||
struct check_unknown {
|
||||
using type = D;
|
||||
};
|
||||
|
||||
// downcast did not find a user predefined type
|
||||
template<typename... Es>
|
||||
struct check_unknown<derived_dimension<Es...>> {
|
||||
using type = unknown_dimension<Es...>;
|
||||
};
|
||||
|
||||
template<Dimension D>
|
||||
struct downcast_dimension_impl;
|
||||
|
||||
template<BaseDimension D>
|
||||
struct downcast_dimension_impl<D> {
|
||||
using type = D;
|
||||
};
|
||||
|
||||
template<DerivedDimension D>
|
||||
struct downcast_dimension_impl<D> {
|
||||
using type = check_unknown<downcast<D>>::type;
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template<Dimension D>
|
||||
using downcast_dimension = detail::downcast_dimension_impl<D>::type;
|
||||
|
||||
// dim_invert
|
||||
namespace detail {
|
||||
|
||||
template<Dimension D>
|
||||
struct dim_invert_impl;
|
||||
|
||||
template<BaseDimension D>
|
||||
struct dim_invert_impl<D> {
|
||||
using type = downcast_dimension<derived_dimension<exp<D, -1>>>;
|
||||
};
|
||||
|
||||
template<BaseDimension D>
|
||||
struct dim_invert_impl<derived_dimension<exp<D, -1>>> {
|
||||
using type = D;
|
||||
};
|
||||
|
||||
template<typename... Es>
|
||||
struct dim_invert_impl<derived_dimension<Es...>> {
|
||||
using type = downcast_dimension<derived_dimension<exp_invert<Es>...>>;
|
||||
};
|
||||
|
||||
template<DerivedDimension D>
|
||||
struct dim_invert_impl<D> : dim_invert_impl<downcast_base_t<D>> {
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template<Dimension D>
|
||||
using dim_invert = detail::dim_invert_impl<D>::type;
|
||||
|
||||
// dimension_multiply
|
||||
namespace detail {
|
||||
|
||||
template<Dimension D>
|
||||
struct dim_unpack {
|
||||
using type = D;
|
||||
};
|
||||
|
||||
template<BaseDimension D>
|
||||
struct dim_unpack<derived_dimension<exp<D, 1>>> {
|
||||
using type = D;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Merges 2 sorted derived dimensions into one units::derived_dimension
|
||||
*
|
||||
* A result of a dimensional calculation may result with many exponents of the same base dimension orginated
|
||||
* from different parts of the equation. As the exponents lists of both operands it is enough to merge them
|
||||
* into one list and consolidate duplicates. Also it is possible that final exponents list will contain only
|
||||
* one element being a base dimension with exponent 1. In such a case the final dimension should be the base
|
||||
* dimension itself.
|
||||
*/
|
||||
template<Dimension D1, Dimension D2>
|
||||
using merge_dimension = dim_unpack<typename dim_consolidate<type_list_merge_sorted<D1, D2, exp_less>>::type>::type;
|
||||
|
||||
template<Dimension D1, Dimension D2>
|
||||
struct dimension_multiply_impl;
|
||||
|
||||
template<BaseDimension D1, BaseDimension D2>
|
||||
struct dimension_multiply_impl<D1, D2> {
|
||||
using type = downcast_dimension<merge_dimension<derived_dimension<exp<D1, 1>>, derived_dimension<exp<D2, 1>>>>;
|
||||
};
|
||||
|
||||
template<BaseDimension D1, DerivedDimension D2>
|
||||
struct dimension_multiply_impl<D1, D2> {
|
||||
using type = downcast_dimension<merge_dimension<derived_dimension<exp<D1, 1>>, typename D2::downcast_base_type>>;
|
||||
};
|
||||
|
||||
template<DerivedDimension D1, BaseDimension D2>
|
||||
struct dimension_multiply_impl<D1, D2> {
|
||||
using type = dimension_multiply_impl<D2, D1>::type;
|
||||
};
|
||||
|
||||
template<DerivedDimension D1, DerivedDimension D2>
|
||||
struct dimension_multiply_impl<D1, D2> {
|
||||
using type = downcast_dimension<merge_dimension<typename D1::downcast_base_type, typename D2::downcast_base_type>>;
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template<Dimension D1, Dimension D2>
|
||||
using dimension_multiply = detail::dimension_multiply_impl<D1, D2>::type;
|
||||
|
||||
template<Dimension D1, Dimension D2>
|
||||
using dimension_divide = detail::dimension_multiply_impl<D1, dim_invert<D2>>::type;
|
||||
|
||||
// dimension_sqrt
|
||||
namespace detail {
|
||||
|
||||
template<Dimension D>
|
||||
struct dimension_sqrt_impl;
|
||||
|
||||
template<BaseDimension D>
|
||||
struct dimension_sqrt_impl<D> {
|
||||
using type = downcast_dimension<derived_dimension<exp<D, 1, 2>>>;
|
||||
};
|
||||
|
||||
template<BaseDimension D>
|
||||
struct dimension_sqrt_impl<derived_dimension<exp<D, 2>>> {
|
||||
using type = D;
|
||||
};
|
||||
|
||||
template<DerivedDimension D>
|
||||
struct dimension_sqrt_impl<D> {
|
||||
using type = dimension_sqrt_impl<typename D::downcast_base_type>::type;
|
||||
};
|
||||
|
||||
template<typename... Es>
|
||||
struct dimension_sqrt_impl<derived_dimension<Es...>> {
|
||||
using type = downcast_dimension<derived_dimension<exp_multiply<Es, 1, 2>...>>;
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template<Dimension D>
|
||||
using dimension_sqrt = detail::dimension_sqrt_impl<D>::type;
|
||||
|
||||
// dimension_pow
|
||||
namespace detail {
|
||||
|
||||
template<Dimension D, std::size_t N>
|
||||
struct dimension_pow_impl;
|
||||
|
||||
template<BaseDimension D, std::size_t N>
|
||||
struct dimension_pow_impl<D, N> {
|
||||
using type = downcast_dimension<derived_dimension<exp<D, N>>>;
|
||||
};
|
||||
|
||||
template<BaseDimension D>
|
||||
struct dimension_pow_impl<D, 1> {
|
||||
using type = D;
|
||||
};
|
||||
|
||||
template<BaseDimension D, std::size_t N>
|
||||
struct dimension_pow_impl<derived_dimension<exp<D, 1, N>>, N> {
|
||||
using type = D;
|
||||
};
|
||||
|
||||
template<DerivedDimension D, std::size_t N>
|
||||
struct dimension_pow_impl<D, N> {
|
||||
using type = dimension_pow_impl<downcast_base_t<D>, N>::type;
|
||||
};
|
||||
|
||||
template<typename... Es, std::size_t N>
|
||||
struct dimension_pow_impl<derived_dimension<Es...>, N> {
|
||||
using type = downcast_dimension<derived_dimension<exp_multiply<Es, N, 1>...>>;
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template<Dimension D, std::size_t N>
|
||||
using dimension_pow = detail::dimension_pow_impl<D, N>::type;
|
||||
|
||||
} // namespace units
|
||||
+1
-1
@@ -22,7 +22,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <units/bits/hacks.h>
|
||||
#include <units/bits/external/hacks.h>
|
||||
#include <type_traits>
|
||||
|
||||
namespace units {
|
||||
Vendored
+1
-1
@@ -22,7 +22,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <units/bits/hacks.h>
|
||||
#include <units/bits/external/hacks.h>
|
||||
|
||||
// P1813 - A Concept Design for the Numeric Algorithms
|
||||
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
// 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/fixed_string.h>
|
||||
|
||||
namespace units::detail {
|
||||
|
||||
template<int Value>
|
||||
requires (0 <= Value) && (Value < 10)
|
||||
inline constexpr basic_fixed_string superscript_number = "";
|
||||
|
||||
template<> inline constexpr basic_fixed_string superscript_number<0> = "\u2070";
|
||||
template<> inline constexpr basic_fixed_string superscript_number<1> = "\u00b9";
|
||||
template<> inline constexpr basic_fixed_string superscript_number<2> = "\u00b2";
|
||||
template<> inline constexpr basic_fixed_string superscript_number<3> = "\u00b3";
|
||||
template<> inline constexpr basic_fixed_string superscript_number<4> = "\u2074";
|
||||
template<> inline constexpr basic_fixed_string superscript_number<5> = "\u2075";
|
||||
template<> inline constexpr basic_fixed_string superscript_number<6> = "\u2076";
|
||||
template<> inline constexpr basic_fixed_string superscript_number<7> = "\u2077";
|
||||
template<> inline constexpr basic_fixed_string superscript_number<8> = "\u2078";
|
||||
template<> inline constexpr basic_fixed_string superscript_number<9> = "\u2079";
|
||||
|
||||
template<int Value>
|
||||
requires (Value >= 0)
|
||||
constexpr auto superscript()
|
||||
{
|
||||
if constexpr(Value < 10)
|
||||
return superscript_number<Value>;
|
||||
else
|
||||
return superscript<Value / 10>() + superscript<Value % 10>();
|
||||
}
|
||||
|
||||
template<int Value>
|
||||
requires (Value >= 0)
|
||||
constexpr auto regular()
|
||||
{
|
||||
if constexpr(Value < 10)
|
||||
return basic_fixed_string(static_cast<char>('0' + Value));
|
||||
else
|
||||
return regular<Value / 10>() + regular<Value % 10>();
|
||||
}
|
||||
|
||||
} // namespace units::detail
|
||||
+1
-1
@@ -22,7 +22,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <units/bits/type_traits.h>
|
||||
#include <units/bits/external/type_traits.h>
|
||||
|
||||
namespace units {
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
// 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/deduced_symbol_text.h>
|
||||
#include <units/derived_dimension.h>
|
||||
|
||||
namespace units::detail {
|
||||
|
||||
template<typename Ratio>
|
||||
constexpr auto ratio_text()
|
||||
{
|
||||
if constexpr(Ratio::num != 1 || Ratio::den != 1) {
|
||||
auto txt = basic_fixed_string("[") + regular<Ratio::num>();
|
||||
if constexpr(Ratio::den == 1) {
|
||||
return txt + basic_fixed_string("]");
|
||||
}
|
||||
else {
|
||||
return txt + basic_fixed_string("/") + regular<Ratio::den>() + basic_fixed_string("]");
|
||||
}
|
||||
}
|
||||
else {
|
||||
return basic_fixed_string("");
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Ratio, typename PrefixType>
|
||||
constexpr auto prefix_or_ratio_text()
|
||||
{
|
||||
if constexpr(Ratio::num == 1 && Ratio::den == 1) {
|
||||
// no ratio/prefix
|
||||
return basic_fixed_string("");
|
||||
}
|
||||
else {
|
||||
if constexpr (!std::same_as<PrefixType, no_prefix>) {
|
||||
// try to form a prefix
|
||||
using prefix = downcast<detail::prefix_base<PrefixType, Ratio>>;
|
||||
|
||||
if constexpr(!std::same_as<prefix, prefix_base<PrefixType, Ratio>>) {
|
||||
// print as a prefixed unit
|
||||
return prefix::symbol;
|
||||
}
|
||||
else {
|
||||
// print as a ratio of the coherent unit
|
||||
return ratio_text<Ratio>();
|
||||
}
|
||||
}
|
||||
else {
|
||||
// print as a ratio of the coherent unit
|
||||
return ratio_text<Ratio>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template<typename... Es, std::size_t... Idxs>
|
||||
constexpr auto derived_dimension_unit_text(derived_dimension<Es...>, std::index_sequence<Idxs...>)
|
||||
{
|
||||
return (exp_text<Es, dimension_unit<typename Es::dimension>::symbol, Idxs>() + ...);
|
||||
}
|
||||
|
||||
template<typename... Es>
|
||||
constexpr auto derived_dimension_unit_text(derived_dimension<Es...> d)
|
||||
{
|
||||
return derived_dimension_unit_text(d, std::index_sequence_for<Es...>());
|
||||
}
|
||||
|
||||
template<typename... Es>
|
||||
constexpr bool all_named(derived_dimension<Es...>)
|
||||
{
|
||||
return (dimension_unit<typename Es::dimension>::is_named && ...);
|
||||
}
|
||||
|
||||
template<Dimension Dim>
|
||||
constexpr auto derived_dimension_unit_text()
|
||||
{
|
||||
using recipe = typename Dim::recipe;
|
||||
if constexpr(all_named(recipe()))
|
||||
return derived_dimension_unit_text(recipe());
|
||||
else
|
||||
return derived_dimension_unit_text(Dim());
|
||||
}
|
||||
|
||||
// TODO Inline below concept when switched to gcc-10
|
||||
template<typename T>
|
||||
concept has_symbol = requires{ T::symbol; };
|
||||
|
||||
template<Dimension Dim, Unit U>
|
||||
constexpr auto unit_text()
|
||||
{
|
||||
if constexpr(has_symbol<U>) {
|
||||
// already has a symbol so print it
|
||||
return U::symbol;
|
||||
}
|
||||
else {
|
||||
// print as a prefix or ratio of a reference unit
|
||||
auto prefix_txt = prefix_or_ratio_text<typename U::ratio, typename U::reference::prefix_type>();
|
||||
|
||||
if constexpr(has_symbol<typename U::reference>) {
|
||||
// use predefined reference unit symbol
|
||||
return prefix_txt + U::reference::symbol;
|
||||
}
|
||||
else {
|
||||
// use derived dimension ingredients to create a unit symbol
|
||||
return prefix_txt + derived_dimension_unit_text<Dim>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace units::detail
|
||||
Reference in New Issue
Block a user