Upcasting facility refactored by adding additional alias templates

This commit is contained in:
Mateusz Pusz
2019-04-09 16:53:34 +02:00
parent 373e70f2b1
commit fe158440bb
8 changed files with 69 additions and 52 deletions
+14 -4
View File
@@ -110,20 +110,30 @@ namespace units {
template<Ratio Ratio1, Ratio Ratio2>
using common_ratio_t = typename common_ratio<Ratio1, Ratio2>::type;
// upcast_base
// upcasting
template<typename BaseType>
struct upcast_base {
using base_type = BaseType;
};
// upcasting_traits
template<typename T>
concept bool Upcastable =
requires {
typename T::base_type;
} &&
std::experimental::ranges::DerivedFrom<T, upcast_base<typename T::base_type>>;
template<Upcastable T>
using upcast_from = typename T::base_type;
template<typename T>
struct upcasting_traits : std::type_identity<T> {};
using upcast_to = std::type_identity<T>;
template<typename T>
struct upcasting_traits : upcast_to<T> {};
template<typename T>
using upcasting_traits_t = typename upcasting_traits<T>::type;
} // namespace units
+1 -2
View File
@@ -101,8 +101,7 @@ namespace units {
template<typename T>
concept bool Dimension =
std::is_empty_v<T> &&
detail::is_dimension<typename T::base_type> &&
std::experimental::ranges::DerivedFrom<T, typename T::base_type>;
detail::is_dimension<upcast_from<T>>;
// dim_invert
+7 -7
View File
@@ -28,25 +28,25 @@
namespace units {
struct dimension_frequency : make_dimension_t<exp<base_dim_time, -1>> {};
template<> struct upcasting_traits<typename dimension_frequency::base_type> : std::type_identity<dimension_frequency> {};
template<> struct upcasting_traits<upcast_from<dimension_frequency>> : upcast_to<dimension_frequency> {};
struct millihertz : unit<dimension_frequency, std::milli> {};
template<> struct upcasting_traits<typename millihertz::base_type> : std::type_identity<millihertz> {};
template<> struct upcasting_traits<upcast_from<millihertz>> : upcast_to<millihertz> {};
struct hertz : unit<dimension_frequency, std::ratio<1>> {};
template<> struct upcasting_traits<typename hertz::base_type> : std::type_identity<hertz> {};
template<> struct upcasting_traits<upcast_from<hertz>> : upcast_to<hertz> {};
struct kilohertz : unit<dimension_frequency, std::kilo> {};
template<> struct upcasting_traits<typename kilohertz::base_type> : std::type_identity<kilohertz> {};
template<> struct upcasting_traits<upcast_from<kilohertz>> : upcast_to<kilohertz> {};
struct megahertz : unit<dimension_frequency, std::mega> {};
template<> struct upcasting_traits<typename megahertz::base_type> : std::type_identity<megahertz> {};
template<> struct upcasting_traits<upcast_from<megahertz>> : upcast_to<megahertz> {};
struct gigahertz : unit<dimension_frequency, std::giga> {};
template<> struct upcasting_traits<typename gigahertz::base_type> : std::type_identity<gigahertz> {};
template<> struct upcasting_traits<upcast_from<gigahertz>> : upcast_to<gigahertz> {};
struct terahertz : unit<dimension_frequency, std::tera> {};
template<> struct upcasting_traits<typename terahertz::base_type> : std::type_identity<terahertz> {};
template<> struct upcasting_traits<upcast_from<terahertz>> : upcast_to<terahertz> {};
template<Unit U = hertz, Number Rep = double>
using frequency = quantity<dimension_frequency, U, Rep>;
+9 -9
View File
@@ -29,33 +29,33 @@ namespace units {
// dimension
struct dimension_length : make_dimension_t<exp<base_dim_length, 1>> {};
template<> struct upcasting_traits<typename dimension_length::base_type> : std::type_identity<dimension_length> {};
template<> struct upcasting_traits<upcast_from<dimension_length>> : upcast_to<dimension_length> {};
// SI units
struct millimeter : unit<dimension_length, std::milli> {};
template<> struct upcasting_traits<typename millimeter::base_type> : std::type_identity<millimeter> {};
template<> struct upcasting_traits<upcast_from<millimeter>> : upcast_to<millimeter> {};
struct centimeter : unit<dimension_length, std::ratio<1, 100>> {};
template<> struct upcasting_traits<typename centimeter::base_type> : std::type_identity<centimeter> {};
template<> struct upcasting_traits<upcast_from<centimeter>> : upcast_to<centimeter> {};
struct meter : unit<dimension_length, std::ratio<1>> {};
template<> struct upcasting_traits<typename meter::base_type> : std::type_identity<meter> {};
template<> struct upcasting_traits<upcast_from<meter>> : upcast_to<meter> {};
struct kilometer : unit<dimension_length, std::kilo> {};
template<> struct upcasting_traits<typename kilometer::base_type> : std::type_identity<kilometer> {};
template<> struct upcasting_traits<upcast_from<kilometer>> : upcast_to<kilometer> {};
// US customary units
struct yard : unit<dimension_length, std::ratio<9'144, 10'000>> {};
template<> struct upcasting_traits<typename yard::base_type> : std::type_identity<yard> {};
template<> struct upcasting_traits<upcast_from<yard>> : upcast_to<yard> {};
struct foot : unit<dimension_length, std::ratio_multiply<std::ratio<1, 3>, yard::ratio>> {};
template<> struct upcasting_traits<typename foot::base_type> : std::type_identity<foot> {};
template<> struct upcasting_traits<upcast_from<foot>> : upcast_to<foot> {};
struct inch : unit<dimension_length, std::ratio_multiply<std::ratio<1, 12>, foot::ratio>> {};
template<> struct upcasting_traits<typename inch::base_type> : std::type_identity<inch> {};
template<> struct upcasting_traits<upcast_from<inch>> : upcast_to<inch> {};
struct mile : unit<dimension_length, std::ratio_multiply<std::ratio<1'760>, yard::ratio>> {};
template<> struct upcasting_traits<typename mile::base_type> : std::type_identity<mile> {};
template<> struct upcasting_traits<upcast_from<mile>> : upcast_to<mile> {};
// length
template<Unit U = meter, Number Rep = double>
+7 -7
View File
@@ -28,25 +28,25 @@
namespace units {
struct dimension_time : make_dimension_t<exp<base_dim_time, 1>> {};
template<> struct upcasting_traits<typename dimension_time::base_type> : std::type_identity<dimension_time> {};
template<> struct upcasting_traits<upcast_from<dimension_time>> : upcast_to<dimension_time> {};
struct nanosecond : unit<dimension_time, std::nano> {};
template<> struct upcasting_traits<typename nanosecond::base_type> : std::type_identity<nanosecond> {};
template<> struct upcasting_traits<upcast_from<nanosecond>> : upcast_to<nanosecond> {};
struct microsecond : unit<dimension_time, std::micro> {};
template<> struct upcasting_traits<typename microsecond::base_type> : std::type_identity<microsecond> {};
template<> struct upcasting_traits<upcast_from<microsecond>> : upcast_to<microsecond> {};
struct millisecond : unit<dimension_time, std::milli> {};
template<> struct upcasting_traits<typename millisecond::base_type> : std::type_identity<millisecond> {};
template<> struct upcasting_traits<upcast_from<millisecond>> : upcast_to<millisecond> {};
struct second : unit<dimension_time, std::ratio<1>> {};
template<> struct upcasting_traits<typename second::base_type> : std::type_identity<second> {};
template<> struct upcasting_traits<upcast_from<second>> : upcast_to<second> {};
struct minute : unit<dimension_time, std::ratio<60>> {};
template<> struct upcasting_traits<typename minute::base_type> : std::type_identity<minute> {};
template<> struct upcasting_traits<upcast_from<minute>> : upcast_to<minute> {};
struct hour : unit<dimension_time, std::ratio<3600>> {};
template<> struct upcasting_traits<typename hour::base_type> : std::type_identity<hour> {};
template<> struct upcasting_traits<upcast_from<hour>> : upcast_to<hour> {};
template<Unit U = second, Number Rep = double>
using time = quantity<dimension_time, U, Rep>;
+4 -4
View File
@@ -29,16 +29,16 @@
namespace units {
struct dimension_velocity : make_dimension_t<exp<base_dim_length, 1>, exp<base_dim_time, -1>> {};
template<> struct upcasting_traits<typename dimension_velocity::base_type> : std::type_identity<dimension_velocity> {};
template<> struct upcasting_traits<upcast_from<dimension_velocity>> : upcast_to<dimension_velocity> {};
struct meter_per_second : unit<dimension_velocity, std::ratio<1>> {};
template<> struct upcasting_traits<typename meter_per_second::base_type> : std::type_identity<meter_per_second> {};
template<> struct upcasting_traits<upcast_from<meter_per_second>> : upcast_to<meter_per_second> {};
struct kilometer_per_hour : unit<dimension_velocity, std::ratio_divide<kilometer::ratio, hour::ratio>> {};
template<> struct upcasting_traits<typename kilometer_per_hour::base_type> : std::type_identity<kilometer_per_hour> {};
template<> struct upcasting_traits<upcast_from<kilometer_per_hour>> : upcast_to<kilometer_per_hour> {};
struct mile_per_hour : unit<dimension_velocity, std::ratio_divide<mile::ratio, hour::ratio>> {};
template<> struct upcasting_traits<typename mile_per_hour::base_type> : std::type_identity<mile_per_hour> {};
template<> struct upcasting_traits<upcast_from<mile_per_hour>> : upcast_to<mile_per_hour> {};
template<Unit U = meter_per_second, Number Rep = double>
using velocity = quantity<dimension_velocity, U, Rep>;
+1 -2
View File
@@ -49,7 +49,6 @@ namespace units {
template<typename T>
concept bool Unit =
std::is_empty_v<T> &&
detail::is_unit<typename T::base_type> &&
std::experimental::ranges::DerivedFrom<T, typename T::base_type>;
detail::is_unit<upcast_from<T>>;
} // namespace units