Text formatting enabled + directory tree refactoring

This commit is contained in:
Mateusz Pusz committed 2019-12-11 08:07:13 +01:00
1 parent 8ffc46ba75
commit bc1901f4f0
30 files changed
+507 -236

No files matched your search

+2 -2
View File
@@ -22,8 +22,8 @@
#pragma once
#include <units/bits/fixed_string.h>
#include <units/bits/unit_concept.h>
#include <units/bits/external/fixed_string.h>
#include <units/unit_concept.h>
#include <type_traits>
namespace units {
@@ -22,8 +22,8 @@
#pragma once
#include <units/bits/hacks.h>
#include <units/bits/numeric_concepts.h>
#include <units/bits/external/hacks.h>
#include <units/bits/external/numeric_concepts.h>
#include <units/customization_points.h>
#include <units/ratio.h>
@@ -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
+72
View File
@@ -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
@@ -200,7 +200,7 @@ struct dimension_sqrt_impl;
template<BaseDimension D>
struct dimension_sqrt_impl<D> {
using type = derived_dimension<exp<D, 1, 2>>;
using type = downcast_dimension<derived_dimension<exp<D, 1, 2>>>;
};
template<BaseDimension D>
@@ -210,7 +210,7 @@ struct dimension_sqrt_impl<derived_dimension<exp<D, 2>>> {
template<DerivedDimension D>
struct dimension_sqrt_impl<D> {
using type = dimension_sqrt_impl<typename D::downcast_base_type>;
using type = dimension_sqrt_impl<typename D::downcast_base_type>::type;
};
template<typename... Es>
@@ -221,7 +221,7 @@ struct dimension_sqrt_impl<derived_dimension<Es...>> {
} // namespace detail
template<Dimension D>
using dimension_sqrt = detail::dimension_sqrt_impl<typename D::downcast_base_type>::type;
using dimension_sqrt = detail::dimension_sqrt_impl<D>::type;
// dimension_pow
namespace detail {
@@ -22,7 +22,7 @@
#pragma once
#include <units/bits/hacks.h>
#include <units/bits/external/hacks.h>
#include <type_traits>
namespace units {
File renamed without changes.
File renamed without changes.
@@ -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
View File
@@ -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
@@ -22,7 +22,7 @@
#pragma once
#include <units/bits/type_traits.h>
#include <units/bits/external/type_traits.h>
namespace units {
File renamed without changes.
+129
View File
@@ -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
+4 -24
View File
@@ -23,9 +23,9 @@
#pragma once
#include <units/base_dimension.h>
#include <units/bits/downcasting.h>
#include <units/bits/fixed_string.h>
#include <units/bits/type_list.h>
#include <units/bits/external/downcasting.h>
#include <units/bits/external/fixed_string.h>
#include <units/bits/external/type_list.h>
#include <units/ratio.h>
#include <ratio>
@@ -264,7 +264,7 @@ using base_units_ratio = base_units_ratio_impl<D>::type;
* The implementation is responsible for unpacking all of the dimensions into a list containing only base dimensions
* and their factors and putting them to the other (private) units::derived_dimension class template partial
* specialization.
*
*
* @note User should always use only this partial specialization to create derived dimensions.
*
* @tparam Child inherited class type used by the downcasting facility (CRTP Idiom)
@@ -279,24 +279,4 @@ struct derived_dimension<Child, U, E, ERest...> : downcast_child<Child, typename
using base_units_ratio = detail::base_units_ratio<typename downcast_child<Child, typename detail::make_dimension<E, ERest...>>::downcast_base_type>;
};
namespace detail {
template<Dimension D>
struct dimension_unit_impl;
template<BaseDimension D>
struct dimension_unit_impl<D> {
using type = D::base_unit;
};
template<DerivedDimension D>
struct dimension_unit_impl<D> {
using type = D::coherent_unit;
};
}
template<Dimension D>
using dimension_unit = detail::dimension_unit_impl<D>::type;
} // namespace units
+11 -11
View File
@@ -22,7 +22,7 @@
#pragma once
#include <units/bits/customization_points.h>
#include <units/customization_points.h>
#include <units/quantity.h>
#include <fmt/format.h>
#include <string_view>
@@ -116,20 +116,20 @@ namespace units {
return format_to(out, treat_as_floating_point<Rep> ? "{:" + sign_text + "g}" : "{:" + sign_text + "}", val);
}
template<typename Unit, typename OutputIt>
template<typename Dimension, typename Unit, typename OutputIt>
inline static OutputIt format_units_quantity_unit(OutputIt out)
{
return format_to(out, "{}", unit_text<Unit>().c_str());
return format_to(out, "{}", unit_text<Dimension, Unit>().c_str());
}
template<typename OutputIt, typename Unit, typename Rep>
template<typename OutputIt, typename Dimension, typename Unit, typename Rep>
struct units_formatter {
OutputIt out;
Rep val;
fmt::sign_t sign;
int precision;
explicit units_formatter(OutputIt o, quantity<Unit, Rep> q, fmt::sign_t s, int prec):
explicit units_formatter(OutputIt o, quantity<Dimension, Unit, Rep> q, fmt::sign_t s, int prec):
out(o), val(q.count()), sign(s), precision(prec)
{
}
@@ -147,7 +147,7 @@ namespace units {
void on_quantity_unit()
{
out = format_units_quantity_unit<Unit>(out);
out = format_units_quantity_unit<Dimension, Unit>(out);
}
};
@@ -155,10 +155,10 @@ namespace units {
} // namespace units
template<typename Unit, typename Rep, typename CharT>
struct fmt::formatter<units::quantity<Unit, Rep>, CharT> {
template<typename Dimension, typename Unit, typename Rep, typename CharT>
struct fmt::formatter<units::quantity<Dimension, Unit, Rep>, CharT> {
private:
using quantity = units::quantity<Unit, Rep>;
using quantity = units::quantity<Dimension, Unit, Rep>;
using iterator = fmt::basic_parse_context<CharT>::iterator;
using arg_ref_type = fmt::internal::arg_ref<CharT>;
@@ -293,7 +293,7 @@ public:
}
template<typename FormatContext>
auto format(const units::quantity<Unit, Rep>& q, FormatContext& ctx)
auto format(const units::quantity<Dimension, Unit, Rep>& q, FormatContext& ctx)
{
auto begin = format_str.begin(), end = format_str.end();
@@ -310,7 +310,7 @@ public:
// default format should print value followed by the unit separeted with 1 space
out = units::detail::format_units_quantity_value(out, q.count(), specs.sign, precision);
*out++ = CharT(' ');
units::detail::format_units_quantity_unit<Unit>(out);
units::detail::format_units_quantity_unit<Dimension, Unit>(out);
}
else {
// user provided format
+1 -1
View File
@@ -23,7 +23,7 @@
#pragma once
#include <units/base_dimension.h>
#include <units/bits/type_traits.h>
#include <units/bits/external/type_traits.h>
#include <units/quantity.h>
#include <units/unit.h>
+2 -2
View File
@@ -22,8 +22,8 @@
#pragma once
#include <units/bits/downcasting.h>
#include <units/bits/fixed_string.h>
#include <units/bits/external/downcasting.h>
#include <units/bits/external/fixed_string.h>
#include <units/ratio.h>
namespace units {
+4 -3
View File
@@ -22,8 +22,9 @@
#pragma once
#include <units/concepts.h>
#include <units/dimension_op.h>
#include <units/bits/concepts.h>
#include <units/bits/dimension_op.h>
#include <units/bits/unit_text.h>
#include <limits>
#include <ostream>
@@ -441,7 +442,7 @@ public:
template<class CharT, class Traits>
friend std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const quantity& q)
{
return os << q.count(); // << " " << detail::unit_text<quantity::unit>(); TODO add support
return os << q.count() << " " << detail::unit_text<quantity::dimension, quantity::unit>();
}
};
+1 -1
View File
@@ -22,7 +22,7 @@
#pragma once
#include <units/bits/hacks.h>
#include <units/bits/external/hacks.h>
#include <type_traits>
#include <numeric>
#include <cstdint>
+13 -62
View File
@@ -22,10 +22,13 @@
#pragma once
#include <units/bits/downcasting.h>
#include <units/bits/fixed_string.h>
#include <units/bits/type_traits.h>
#include <units/bits/unit_concept.h>
#include <units/bits/deduced_symbol_text.h>
#include <units/bits/deduced_unit.h>
#include <units/bits/external/downcasting.h>
#include <units/bits/external/fixed_string.h>
#include <units/bits/external/text_tools.h>
#include <units/bits/external/type_traits.h>
#include <units/unit_of_concept.h>
#include <units/derived_dimension.h>
#include <units/prefix.h>
#include <units/ratio.h>
@@ -60,7 +63,7 @@ template<Unit U1, Unit U2>
struct same_unit_reference : std::is_same<typename U1::reference, typename U2::reference> {};
/**
* @brief A starting point for a new hierarchy of units
* @brief An unnamed unit
*
* Defines a new unnamed (in most cases coherent) derived unit of a specific derived dimension
* and it should be passed in this dimension's definition.
@@ -76,7 +79,7 @@ struct unit : downcast_child<Child, scaled_unit<Child, ratio<1>>> {
/**
* @brief Unknown unit
*
* Used as a reference unit of an unknown dimension.
* Used as a coherent unit of an unknown dimension.
*/
struct unknown_unit : unit<unknown_unit> {};
@@ -144,65 +147,13 @@ struct prefixed_unit :
using prefix_type = P::prefix_type;
};
// UnitOf
template<typename U, typename D>
concept UnitOf =
Unit<U> &&
Dimension<D> &&
std::same_as<typename U::reference, typename dimension_unit<D>::reference>;
namespace 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 detail
/**
* @brief A unit with a deduced ratio and symbol
*
* Defines a new unit with a deduced ratio and symbol based on the recipe from the provided
* derived dimension. The number and order of provided units should match the recipe of
* dimension.
* derived dimension. The number and order of provided units should match the recipe of the
* derived dimension. All of the units provided should also be a named ones so it is possible
* to create a deduced symbol text.
*
* @tparam Child inherited class type used by the downcasting facility (CRTP Idiom)
* @tparam Dim a derived dimension recipe to use for deduction
@@ -214,7 +165,7 @@ template<typename Child, DerivedDimension Dim, Unit U, Unit... URest>
(U::is_named && (URest::is_named && ... && true))
struct deduced_unit : downcast_child<Child, detail::deduced_unit<Dim, U, URest...>> {
static constexpr bool is_named = false;
static constexpr auto symbol = basic_fixed_string{""}; // detail::deduced_symbol_text<Dim, U, Us...>(); // TODO implement this
static constexpr auto symbol = detail::deduced_symbol_text<typename Dim::recipe, U, URest...>();
using prefix_type = no_prefix;
};
@@ -22,11 +22,12 @@
#pragma once
#include <units/bits/type_traits.h>
#include <units/bits/external/type_traits.h>
#include <units/ratio.h>
namespace units {
// UnitRatio
template<typename R>
concept UnitRatio = Ratio<R> && (R::num * R::den > 0);
+57
View File
@@ -0,0 +1,57 @@
// 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>
namespace units {
namespace detail {
template<Dimension D>
struct dimension_unit_impl;
template<BaseDimension D>
struct dimension_unit_impl<D> {
using type = D::base_unit;
};
template<DerivedDimension D>
struct dimension_unit_impl<D> {
using type = D::coherent_unit;
};
} // namespace detail
template<Dimension D>
using dimension_unit = detail::dimension_unit_impl<D>::type;
// UnitOf
template<typename U, typename D>
concept UnitOf =
Unit<U> &&
Dimension<D> &&
std::same_as<typename U::reference, typename dimension_unit<D>::reference>;
// same_unit_reference<typename U, typename dimension_unit<D>> // TODO check if this works
} // namespace units