6#ifndef ACTIVE_MEASURE_UNIT
7#define ACTIVE_MEASURE_UNIT
9#include "Active/Utility/MathFunctions.h"
10#include "Active/Utility/String.h"
34 template<
typename T,
typename Type>
57 Unit(Type type, uint8_t prec,
bool isDecimal =
true,
bool suffixes =
true) :
76 Unit(Type first, Type second, Type third, uint8_t prec,
bool isDecimal =
true) :
126 return T::metric.at(
static_cast<size_t>(type));
135 double simpleRatio(
double value,
double factor,
bool reversed)
const {
136 return value * (reversed ? factor : 1.0 / factor);
145 double conversion(Type type,
double value,
bool reversed =
false)
const {
147 return simpleRatio(value, T::conversions.at(
static_cast<size_t>(type)), reversed);
155 return T::abbreviations.
at(
static_cast<size_t>(type));
163 for (
auto i = 0; i < T::tags.size(); ++i)
164 if (text == T::tags[i])
165 return static_cast<Type
>(i);
174 return T::tags.
at(
static_cast<size_t>(type));
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);
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
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