Phyth v0.1.0
A dimensional safe C++ header-only mechanics and electromagnetics simulation library.
Loading...
Searching...
No Matches
Unit.hpp
Go to the documentation of this file.
1#ifndef PHYTH_UNIT_HPP
2#define PHYTH_UNIT_HPP
3
4#include <ratio>
5
6namespace Phyth {
40 template<typename DimT, typename Scale = std::ratio<1>, typename TagT = void>
41 struct Unit {
44 using ScaleT = Scale;
45
55 static constexpr double scale = static_cast<double>(Scale::num) / Scale::den;
56
58 Unit() = default;
59 };
60
70 template<typename T>
71 struct is_unit : std::false_type {
72 };
73
74 template<typename DimT, typename Scale, typename TagT>
75 struct is_unit<Unit<DimT, Scale, TagT> > : std::true_type {
76 };
77
78 template<typename T>
79 inline constexpr bool is_unit_v = is_unit<T>::value;
80
81 template<typename U1, typename U2>
82 struct UnitMulBin {
83 using type = Unit<
85 typename std::ratio_multiply<typename U1::ScaleT, typename U2::ScaleT>::type
86 >;
87 };
88
89 template<typename U1, typename U2>
90 struct UnitDivBin {
91 using type = Unit<
93 typename std::ratio_divide<typename U1::ScaleT, typename U2::ScaleT>::type
94 >;
95 };
96
97 template<template<typename, typename> class F, typename... Ts>
98 struct FoldLeft;
99
100 template<template<typename, typename> class F, typename T>
101 struct FoldLeft<F, T> {
102 using type = T;
103 };
104
105 template<template<typename, typename> class F,
106 typename T1, typename T2, typename... Rest>
107 struct FoldLeft<F, T1, T2, Rest...> {
108 using type = typename FoldLeft<
109 F,
110 typename F<T1, T2>::type,
111 Rest...
112 >::type;
113 };
114
115 template<template<typename, typename> class F, typename... Ts>
116 using FoldLeftT = typename FoldLeft<F, Ts...>::type;
117
118 template<typename... Us>
120
121 template<typename... Us>
123
124 template <typename U, typename Ratio>
125 struct UnitRec {
126 using type = Unit<
128 std::ratio_multiply<typename U::ScaleT, Ratio>
129 >;
130 };
131
132 template <typename U, typename Ratio = std::ratio<1>>
134}
135
136#endif // PHYTH_UNIT_HPP
FoldLeftT< UnitMulBin, Us... > UnitMulT
Definition Unit.hpp:119
typename DimDiv< D1, D2 >::type DimDivT
Convenience alias for DimDiv.
Definition Dimension.hpp:91
FoldLeftT< UnitDivBin, Us... > UnitDivT
Definition Unit.hpp:122
typename DimMul< D1, D2 >::type DimMulT
Convenience alias for DimMul.
Definition Dimension.hpp:71
typename UnitRec< U, Ratio >::type UnitRecT
Definition Unit.hpp:133
typename FoldLeft< F, Ts... >::type FoldLeftT
Definition Unit.hpp:116
typename FoldLeft< F, typename F< T1, T2 >::type, Rest... >::type type
Definition Unit.hpp:112
constexpr bool is_unit_v
Definition Unit.hpp:79
Physical unit with dimension and scale factor relative to SI base.
Definition Unit.hpp:41
Scale ScaleT
Definition Unit.hpp:44
Unit()=default
Default constructor.
DimT DimensionT
Definition Unit.hpp:43
static constexpr double scale
Scale factor relative to the SI base unit.
Definition Unit.hpp:55
Trait: check if a type is a Unit instantiation.
Definition Unit.hpp:71