ActiveLib
Loading...
Searching...
No Matches
Unit.h
1
6#ifndef ACTIVE_MEASURE_UNIT
7#define ACTIVE_MEASURE_UNIT
8
9#include "Active/Utility/MathFunctions.h"
10#include "Active/Utility/String.h"
11
12#include <array>
13#include <functional>
14#include <optional>
15
16namespace active::measure {
17
34 template<typename T, typename Type>
35 struct Unit {
36
37 // MARK: - Constructors
38
43 Unit(Type type) : primary{type} {}
49 Unit(Type first, Type second) : primary{primary}, secondary(second) {}
57 Unit(Type type, uint8_t prec, bool isDecimal = true, bool suffixes = true) :
58 primary{type}, precision{prec}, base{isDecimal ? 10.0 : 2.0}, isUnitSuffix{suffixes} {}
66 Unit(Type first, Type second, uint8_t prec, bool isDecimal = true) :
67 primary{first}, secondary{second}, precision{prec}, base{isDecimal ? 10.0 : 2.0} {}
76 Unit(Type first, Type second, Type third, uint8_t prec, bool isDecimal = true) :
77 primary{first}, secondary{second}, tertiary{third}, precision{prec}, base{isDecimal ? 10.0 : 2.0} {}
78
79 // MARK: - Variables
80
82 Type primary;
84 std::optional<Type> secondary;
86 std::optional<Type> tertiary;
88 uint8_t precision = 4;
90 double base = 10.0;
92 bool isUnitSuffix = true;
94 bool isLeadingZero = false;
95
96 // MARK: - Functions (const)
97
102 bool isDecimal() const {
103 return math::isEqual(base, 10.0); //Base 10 numbers are never displayed as fractions
104 }
109 double divisor() const {
110 return pow(base, static_cast<double>(precision));
111 }
116 double eps() const {
117 return 1.0 / divisor();
118 }
119
125 bool isMetric(Type type) const {
126 return T::metric.at(static_cast<size_t>(type));
127 }
135 double simpleRatio(double value, double factor, bool reversed) const {
136 return value * (reversed ? factor : 1.0 / factor);
137 }
145 double conversion(Type type, double value, bool reversed = false) const {
146 //Values are simply multiplied by a fixed ratio by default (some unit types may override with more complex behaviours)
147 return simpleRatio(value, T::conversions.at(static_cast<size_t>(type)), reversed);
148 }
154 utility::String suffix(Type type) const {
155 return T::abbreviations.at(static_cast<size_t>(type));
156 }
162 std::optional<Type> fromTag(const utility::String& text) const {
163 for (auto i = 0; i < T::tags.size(); ++i)
164 if (text == T::tags[i])
165 return static_cast<Type>(i);
166 return std::nullopt;
167 }
173 utility::String toTag(Type type) const {
174 return T::tags.at(static_cast<size_t>(type));
175 }
182 std::optional<std::pair<Type, utility::String::size_type>> findSuffix(const utility::String& text,
183 utility::String::size_type startPos = 0) const {
184 std::optional<std::pair<Type, utility::String::size_type>> result;
185 for (auto i = 0; i < T::abbreviations.size(); ++i) {
186 if (auto pos = text.find(T::abbreviations.at(i), startPos); pos && (!result || (*pos < result->second)))
187 result = std::make_pair(static_cast<Type>(i), *pos);
188 }
189 return result;
190 }
191 };
192
193}
194
195#endif //ACTIVE_MEASURE_UNIT
A Unicode-aware string class.
Definition String.h:51
sizeOption find(const String &toFind, size_type startPos=0) const
Definition String.cpp:1254
char32_t at(size_type index) const
Definition String.cpp:931
std::string::size_type size_type
Class size type.
Definition String.h:65
bool isEqual(double val1, double val2, double prec=eps)
Definition MathFunctions.h:99
Definition AngleUnit.cpp:14
Definition Unit.h:35
double divisor() const
Definition Unit.h:109
bool isLeadingZero
True if a value is displayed with multiple units and zero values (apart from the last unit) should be...
Definition Unit.h:94
Unit(Type first, Type second, uint8_t prec, bool isDecimal=true)
Definition Unit.h:66
bool isUnitSuffix
True if values should be displayed with a unit suffix, e.g. "100.0mm" rather than "100....
Definition Unit.h:92
std::optional< Type > fromTag(const utility::String &text) const
Definition Unit.h:162
utility::String toTag(Type type) const
Definition Unit.h:173
Unit(Type first, Type second, Type third, uint8_t prec, bool isDecimal=true)
Definition Unit.h:76
utility::String suffix(Type type) const
Definition Unit.h:154
bool isDecimal() const
Definition Unit.h:102
std::optional< std::pair< Type, utility::String::size_type > > findSuffix(const utility::String &text, utility::String::size_type startPos=0) const
Definition Unit.h:182
uint8_t precision
The display precision (decimal places for metric or 2^-n for fractional units.
Definition Unit.h:88
double simpleRatio(double value, double factor, bool reversed) const
Definition Unit.h:135
std::optional< Type > tertiary
An optional teriary unit, e.g. for degrees/minutes/seconds combos (nullopt = none,...
Definition Unit.h:86
double base
The numeric base for fractional quantities (10.0 = floating point to specified decimal places,...
Definition Unit.h:90
Unit(Type first, Type second)
Definition Unit.h:49
Unit(Type type)
Definition Unit.h:43
double eps() const
Definition Unit.h:116
Type primary
The primary unit to display.
Definition Unit.h:82
bool isMetric(Type type) const
Definition Unit.h:125
Unit(Type type, uint8_t prec, bool isDecimal=true, bool suffixes=true)
Definition Unit.h:57
double conversion(Type type, double value, bool reversed=false) const
Definition Unit.h:145
std::optional< Type > secondary
An optional secondary unit, e.g. for foot/inch combos (nullopt = none)
Definition Unit.h:84