Text formatting enabled + directory tree refactoring

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

View File

@@ -22,8 +22,8 @@
#pragma once #pragma once
#include <units/bits/fixed_string.h> #include <units/bits/external/fixed_string.h>
#include <units/bits/unit_concept.h> #include <units/unit_concept.h>
#include <type_traits> #include <type_traits>
namespace units { namespace units {

View File

@@ -22,8 +22,8 @@
#pragma once #pragma once
#include <units/bits/hacks.h> #include <units/bits/external/hacks.h>
#include <units/bits/numeric_concepts.h> #include <units/bits/external/numeric_concepts.h>
#include <units/customization_points.h> #include <units/customization_points.h>
#include <units/ratio.h> #include <units/ratio.h>

View File

@@ -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

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

View File

@@ -200,7 +200,7 @@ struct dimension_sqrt_impl;
template<BaseDimension D> template<BaseDimension D>
struct dimension_sqrt_impl<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> template<BaseDimension D>
@@ -210,7 +210,7 @@ struct dimension_sqrt_impl<derived_dimension<exp<D, 2>>> {
template<DerivedDimension D> template<DerivedDimension D>
struct dimension_sqrt_impl<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> template<typename... Es>
@@ -221,7 +221,7 @@ struct dimension_sqrt_impl<derived_dimension<Es...>> {
} // namespace detail } // namespace detail
template<Dimension D> 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 // dimension_pow
namespace detail { namespace detail {

View File

@@ -22,7 +22,7 @@
#pragma once #pragma once
#include <units/bits/hacks.h> #include <units/bits/external/hacks.h>
#include <type_traits> #include <type_traits>
namespace units { namespace units {

View File

@@ -22,7 +22,7 @@
#pragma once #pragma once
#include <units/bits/hacks.h> #include <units/bits/external/hacks.h>
// P1813 - A Concept Design for the Numeric Algorithms // P1813 - A Concept Design for the Numeric Algorithms

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

View File

@@ -22,7 +22,7 @@
#pragma once #pragma once
#include <units/bits/type_traits.h> #include <units/bits/external/type_traits.h>
namespace units { namespace units {

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

View File

@@ -23,9 +23,9 @@
#pragma once #pragma once
#include <units/base_dimension.h> #include <units/base_dimension.h>
#include <units/bits/downcasting.h> #include <units/bits/external/downcasting.h>
#include <units/bits/fixed_string.h> #include <units/bits/external/fixed_string.h>
#include <units/bits/type_list.h> #include <units/bits/external/type_list.h>
#include <units/ratio.h> #include <units/ratio.h>
#include <ratio> #include <ratio>
@@ -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>; 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 } // namespace units

View File

@@ -22,7 +22,7 @@
#pragma once #pragma once
#include <units/bits/customization_points.h> #include <units/customization_points.h>
#include <units/quantity.h> #include <units/quantity.h>
#include <fmt/format.h> #include <fmt/format.h>
#include <string_view> #include <string_view>
@@ -116,20 +116,20 @@ namespace units {
return format_to(out, treat_as_floating_point<Rep> ? "{:" + sign_text + "g}" : "{:" + sign_text + "}", val); 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) 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 { struct units_formatter {
OutputIt out; OutputIt out;
Rep val; Rep val;
fmt::sign_t sign; fmt::sign_t sign;
int precision; 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) out(o), val(q.count()), sign(s), precision(prec)
{ {
} }
@@ -147,7 +147,7 @@ namespace units {
void on_quantity_unit() 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 } // namespace units
template<typename Unit, typename Rep, typename CharT> template<typename Dimension, typename Unit, typename Rep, typename CharT>
struct fmt::formatter<units::quantity<Unit, Rep>, CharT> { struct fmt::formatter<units::quantity<Dimension, Unit, Rep>, CharT> {
private: private:
using quantity = units::quantity<Unit, Rep>; using quantity = units::quantity<Dimension, Unit, Rep>;
using iterator = fmt::basic_parse_context<CharT>::iterator; using iterator = fmt::basic_parse_context<CharT>::iterator;
using arg_ref_type = fmt::internal::arg_ref<CharT>; using arg_ref_type = fmt::internal::arg_ref<CharT>;
@@ -293,7 +293,7 @@ public:
} }
template<typename FormatContext> 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(); 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 // 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 = units::detail::format_units_quantity_value(out, q.count(), specs.sign, precision);
*out++ = CharT(' '); *out++ = CharT(' ');
units::detail::format_units_quantity_unit<Unit>(out); units::detail::format_units_quantity_unit<Dimension, Unit>(out);
} }
else { else {
// user provided format // user provided format

View File

@@ -23,7 +23,7 @@
#pragma once #pragma once
#include <units/base_dimension.h> #include <units/base_dimension.h>
#include <units/bits/type_traits.h> #include <units/bits/external/type_traits.h>
#include <units/quantity.h> #include <units/quantity.h>
#include <units/unit.h> #include <units/unit.h>

View File

@@ -22,8 +22,8 @@
#pragma once #pragma once
#include <units/bits/downcasting.h> #include <units/bits/external/downcasting.h>
#include <units/bits/fixed_string.h> #include <units/bits/external/fixed_string.h>
#include <units/ratio.h> #include <units/ratio.h>
namespace units { namespace units {

View File

@@ -22,8 +22,9 @@
#pragma once #pragma once
#include <units/concepts.h> #include <units/bits/concepts.h>
#include <units/dimension_op.h> #include <units/bits/dimension_op.h>
#include <units/bits/unit_text.h>
#include <limits> #include <limits>
#include <ostream> #include <ostream>
@@ -441,7 +442,7 @@ public:
template<class CharT, class Traits> template<class CharT, class Traits>
friend std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const quantity& q) 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>();
} }
}; };

View File

@@ -22,7 +22,7 @@
#pragma once #pragma once
#include <units/bits/hacks.h> #include <units/bits/external/hacks.h>
#include <type_traits> #include <type_traits>
#include <numeric> #include <numeric>
#include <cstdint> #include <cstdint>

View File

@@ -22,10 +22,13 @@
#pragma once #pragma once
#include <units/bits/downcasting.h> #include <units/bits/deduced_symbol_text.h>
#include <units/bits/fixed_string.h> #include <units/bits/deduced_unit.h>
#include <units/bits/type_traits.h> #include <units/bits/external/downcasting.h>
#include <units/bits/unit_concept.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/derived_dimension.h>
#include <units/prefix.h> #include <units/prefix.h>
#include <units/ratio.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> {}; 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 * 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. * 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 * @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> {}; struct unknown_unit : unit<unknown_unit> {};
@@ -144,65 +147,13 @@ struct prefixed_unit :
using prefix_type = P::prefix_type; 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 * @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 * 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 * derived dimension. The number and order of provided units should match the recipe of the
* dimension. * 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 Child inherited class type used by the downcasting facility (CRTP Idiom)
* @tparam Dim a derived dimension recipe to use for deduction * @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)) (U::is_named && (URest::is_named && ... && true))
struct deduced_unit : downcast_child<Child, detail::deduced_unit<Dim, U, URest...>> { struct deduced_unit : downcast_child<Child, detail::deduced_unit<Dim, U, URest...>> {
static constexpr bool is_named = false; 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; using prefix_type = no_prefix;
}; };

View File

@@ -22,11 +22,12 @@
#pragma once #pragma once
#include <units/bits/type_traits.h> #include <units/bits/external/type_traits.h>
#include <units/ratio.h> #include <units/ratio.h>
namespace units { namespace units {
// UnitRatio
template<typename R> template<typename R>
concept UnitRatio = Ratio<R> && (R::num * R::den > 0); concept UnitRatio = Ratio<R> && (R::num * R::den > 0);

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

View File

@@ -20,6 +20,6 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE. # SOFTWARE.
#add_subdirectory(unit_test/runtime) add_subdirectory(unit_test/runtime)
add_subdirectory(unit_test/static) add_subdirectory(unit_test/static)
#add_subdirectory(metabench) #add_subdirectory(metabench)

View File

@@ -20,43 +20,13 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE. // SOFTWARE.
#include <units/quantity.h> #include <units/data/information.h>
#include <catch2/catch.hpp> #include <catch2/catch.hpp>
#include <sstream> #include <sstream>
namespace data { using namespace units::data;
struct base_dim_digital_information : units::base_dimension<"digital information", "b"> {}; TEST_CASE("operator<< on a data quantity", "[text][ostream]")
struct digital_information : units::derived_dimension<digital_information, units::exp<base_dim_digital_information, 1>> {};
template<typename T>
concept DigitalInformation = units::QuantityOf<T, digital_information>;
struct data_prefix : units::prefix_type {};
struct kibi : units::prefix<kibi, data_prefix, units::ratio< 1'024>, "Ki"> {};
struct mebi : units::prefix<mebi, data_prefix, units::ratio<1'048'576>, "Mi"> {};
struct bit : units::named_coherent_derived_unit<bit, digital_information, "b", data_prefix> {};
struct kilobit : units::prefixed_derived_unit<kilobit, kibi, bit> {};
struct byte : units::named_scaled_derived_unit<byte, digital_information, "B", units::ratio<8>, data_prefix> {};
struct kilobyte : units::prefixed_derived_unit<kilobyte, kibi, byte> {};
inline namespace literals {
constexpr auto operator""_b(unsigned long long l) { return units::quantity<bit, std::int64_t>(l); }
constexpr auto operator""_Kib(unsigned long long l) { return units::quantity<kilobit, std::int64_t>(l); }
constexpr auto operator""_B(unsigned long long l) { return units::quantity<byte, std::int64_t>(l); }
constexpr auto operator""_KiB(unsigned long long l) { return units::quantity<kilobyte, std::int64_t>(l); }
}
}
using namespace data;
TEST_CASE("operator<< on a custom quantity", "[text][ostream]")
{ {
std::stringstream stream; std::stringstream stream;
@@ -64,25 +34,25 @@ TEST_CASE("operator<< on a custom quantity", "[text][ostream]")
{ {
SECTION("named unit") SECTION("named unit")
{ {
stream << 64_B; stream << 64B;
REQUIRE(stream.str() == "64 B"); REQUIRE(stream.str() == "64 B");
} }
SECTION("prefixed coherent unit") SECTION("prefixed coherent unit")
{ {
stream << 256_Kib; stream << 256Kib;
REQUIRE(stream.str() == "256 Kib"); REQUIRE(stream.str() == "256 Kib");
} }
SECTION("prefixed non-coherent unit") SECTION("prefixed non-coherent unit")
{ {
stream << 1024_KiB; stream << 1024KiB;
REQUIRE(stream.str() == "1024 KiB"); REQUIRE(stream.str() == "1024 KiB");
} }
SECTION("other unit matching prefix") SECTION("other unit matching prefix")
{ {
stream << 8_Kib * 8_Kib / 2_b; stream << 8Kib * 8Kib / 2b;
REQUIRE(stream.str() == "32 Mib"); REQUIRE(stream.str() == "32 Mib");
} }
} }

View File

@@ -20,18 +20,20 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE. // SOFTWARE.
#include "units/dimensions/area.h" #include "units/physical/si/area.h"
#include "units/dimensions/frequency.h" #include "units/physical/si/frequency.h"
#include "units/dimensions/power.h" #include "units/physical/si/power.h"
#include "units/dimensions/velocity.h" #include "units/physical/si/pressure.h"
#include "units/dimensions/volume.h" #include "units/physical/si/velocity.h"
#include "units/dimensions/surface_tension.h" #include "units/physical/si/volume.h"
#include "units/physical/si/surface_tension.h"
#include "units/format.h" #include "units/format.h"
#include "units/math.h" #include "units/math.h"
#include <catch2/catch.hpp> #include <catch2/catch.hpp>
#include <sstream> #include <sstream>
using namespace units; using namespace units;
using namespace units::si;
using namespace Catch::Matchers; using namespace Catch::Matchers;
TEST_CASE("operator<< on a quantity", "[text][ostream][fmt]") TEST_CASE("operator<< on a quantity", "[text][ostream][fmt]")
@@ -108,7 +110,7 @@ TEST_CASE("operator<< on a quantity", "[text][ostream][fmt]")
{ {
SECTION("in terms of base units") SECTION("in terms of base units")
{ {
const quantity<unit<length, ratio<1'000'000>>> q(123); const length<scaled_unit<metre, ratio<1'000'000>>> q(123);
stream << q; stream << q;
SECTION("iostream") SECTION("iostream")
@@ -129,7 +131,7 @@ TEST_CASE("operator<< on a quantity", "[text][ostream][fmt]")
SECTION("in terms of derived units") SECTION("in terms of derived units")
{ {
const quantity<unit<energy, ratio<1, 100>>> q(60); const energy<scaled_unit<joule, ratio<1, 100>>> q(60);
stream << q; stream << q;
SECTION("iostream") SECTION("iostream")
@@ -242,8 +244,8 @@ TEST_CASE("operator<< on a quantity", "[text][ostream][fmt]")
SECTION("surface tension") SECTION("surface tension")
{ {
struct newton_per_centimetre : deduced_derived_unit<newton_per_centimetre, surface_tension, newton, centimetre> {}; struct newton_per_centimetre : deduced_unit<newton_per_centimetre, dim_surface_tension, newton, centimetre> {};
const quantity<newton_per_centimetre> q(123); const surface_tension<newton_per_centimetre> q(123);
stream << q; stream << q;
SECTION("iostream") SECTION("iostream")
@@ -636,17 +638,17 @@ TEST_CASE("format string with only %Q should print quantity value only", "[text]
SECTION("nan") SECTION("nan")
{ {
CHECK(fmt::format("{:%Q}", quantity<metre>(std::numeric_limits<double>::quiet_NaN())) == "nan"); CHECK(fmt::format("{:%Q}", length<metre>(std::numeric_limits<double>::quiet_NaN())) == "nan");
} }
SECTION("inf") SECTION("inf")
{ {
CHECK(fmt::format("{:%Q}", quantity<metre>(std::numeric_limits<double>::infinity())) == "inf"); CHECK(fmt::format("{:%Q}", length<metre>(std::numeric_limits<double>::infinity())) == "inf");
} }
SECTION("-inf") SECTION("-inf")
{ {
CHECK(fmt::format("{:%Q}", quantity<metre>(-std::numeric_limits<double>::infinity())) == "-inf"); CHECK(fmt::format("{:%Q}", length<metre>(-std::numeric_limits<double>::infinity())) == "-inf");
} }
} }
} }
@@ -742,8 +744,8 @@ TEST_CASE("fill and align specification", "[text][fmt]")
TEST_CASE("sign specification", "[text][fmt]") TEST_CASE("sign specification", "[text][fmt]")
{ {
quantity<metre> inf(std::numeric_limits<double>::infinity()); length<metre> inf(std::numeric_limits<double>::infinity());
quantity<metre> nan(std::numeric_limits<double>::quiet_NaN()); length<metre> nan(std::numeric_limits<double>::quiet_NaN());
SECTION("default format {} on a quantity") SECTION("default format {} on a quantity")
{ {

View File

@@ -20,16 +20,16 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE. // SOFTWARE.
#include "units/dimensions/area.h" #include "units/physical/si/area.h"
#include "units/dimensions/frequency.h" #include "units/physical/si/frequency.h"
#include "units/dimensions/power.h" #include "units/physical/si/power.h"
#include "units/dimensions/velocity.h" #include "units/physical/si/velocity.h"
#include "units/dimensions/volume.h" #include "units/physical/si/volume.h"
#include "units/dimensions/surface_tension.h" #include "units/physical/si/surface_tension.h"
#include "units/format.h" #include "units/format.h"
#include <catch2/catch.hpp> #include <catch2/catch.hpp>
using namespace units; using namespace units::si;
TEST_CASE("fmt::format on synthesized unit symbols", "[text][fmt]") TEST_CASE("fmt::format on synthesized unit symbols", "[text][fmt]")
{ {

View File

@@ -21,73 +21,35 @@
// SOFTWARE. // SOFTWARE.
#include "units/math.h" #include "units/math.h"
#include "units/dimensions/area.h" #include "units/physical/si/area.h"
#include "units/dimensions/volume.h" #include "units/physical/si/volume.h"
#include <catch2/catch.hpp> #include <catch2/catch.hpp>
using namespace units; using namespace units;
using namespace units::si;
// classical // classical
TEST_CASE("pow<N>() on quantity changes the value and the dimension accordingly", "[math][pow]") TEST_CASE("'pow<N>()' on quantity changes the value and the dimension accordingly", "[math][pow]")
{ {
SECTION ("'pow<0>(q)' returns '1'") {
CHECK(pow<0>(2m) == 1); CHECK(pow<0>(2m) == 1);
}
SECTION ("'pow<1>(q)' returns 'q'") {
CHECK(pow<1>(2m) == 2m); CHECK(pow<1>(2m) == 2m);
}
SECTION ("'pow<2>(q)' squares both the value and a dimension") {
CHECK(pow<2>(2m) == 4sq_m); CHECK(pow<2>(2m) == 4sq_m);
}
SECTION ("'pow<3>(q)' cubes both the value and a dimension") {
CHECK(pow<3>(2m) == 8cub_m); CHECK(pow<3>(2m) == 8cub_m);
}
} }
TEST_CASE("sqrt() on quantity changes the value and the dimension accordingly", "[math][sqrt]") TEST_CASE("'sqrt()' on quantity changes the value and the dimension accordingly", "[math][sqrt]")
{ {
REQUIRE(sqrt(4sq_m) == 2m); REQUIRE(sqrt(4sq_m) == 2m);
} }
// BDD style
SCENARIO("quantities should work with pow<N>()", "[math][pow]")
{
GIVEN("A quantity q") {
auto q = 2m;
REQUIRE(q.count() == 2);
WHEN("pow<1>(q) is called") {
auto res = pow<1>(q);
THEN("the same quantity is received") {
REQUIRE(res == q);
}
}
WHEN("pow<2>(q) is called") {
auto res = pow<2>(q);
THEN("both the value and dimension is raised to the exponent 2") {
REQUIRE(res == 4sq_m);
}
}
WHEN("pow<3>(q) is called") {
auto res = pow<3>(q);
THEN("both the value and dimension is raised to the exponent 3") {
REQUIRE(res == 8cub_m);
}
}
}
}
SCENARIO("quantities should work with sqrt()", "[math][sqrt]")
{
GIVEN("A quantity q") {
auto q = 4sq_m;
REQUIRE(q.count() == 4);
WHEN("sqrt(q) is called") {
auto res = sqrt(q);
THEN("both the value and dimension are square rooted") {
REQUIRE(res == 2m);
}
}
}
}

View File

@@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE. // SOFTWARE.
#include "units/dimension_op.h" #include "units/bits/dimension_op.h"
#include "units/unit.h" #include "units/unit.h"
#include <utility> #include <utility>

View File

@@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE. // SOFTWARE.
#include "units/bits/type_list.h" #include "units/bits/external/type_list.h"
#include "units/derived_dimension.h" #include "units/derived_dimension.h"
#include "units/unit.h" #include "units/unit.h"
#include <utility> #include <utility>

View File

@@ -53,6 +53,6 @@ static_assert(std::is_same_v<downcast<scaled_unit<metre_per_second, ratio_divide
static_assert(centimetre::symbol == "cm"); static_assert(centimetre::symbol == "cm");
static_assert(kilometre::symbol == "km"); static_assert(kilometre::symbol == "km");
static_assert(kilometre_per_hour::symbol == ""); static_assert(kilometre_per_hour::symbol == "km/h");
} // namespace } // namespace