14 #ifndef SST_CORE_UNITALGEBRA_H
15 #define SST_CORE_UNITALGEBRA_H
17 #include "sst/core/decimal_fixedpoint.h"
18 #include "sst/core/serialization/serializable.h"
19 #include "sst/core/serialization/serializer.h"
20 #include "sst/core/sst_types.h"
21 #include "sst/core/warnmacros.h"
31 typedef decimal_fixedpoint<3, 3> sst_big_num;
41 typedef uint8_t unit_id_t;
47 static std::recursive_mutex unit_lock;
48 static std::map<std::string, unit_id_t> valid_base_units;
49 static std::map<std::string, std::pair<Units, sst_big_num>> valid_compound_units;
50 static std::map<unit_id_t, std::string> unit_strings;
51 static unit_id_t count;
52 static bool initialized;
54 static bool initialize();
57 std::vector<unit_id_t> numerator;
58 std::vector<unit_id_t> denominator;
114 static std::string trim(
const std::string& str);
117 void init(
const std::string& val);
142 void print(std::ostream& stream);
159 template <
typename T>
169 template <
typename T>
179 template <
typename T>
189 template <
typename T>
214 bool hasUnits(
const std::string& u)
const;
219 double getDoubleValue()
const;
220 bool isValueZero()
const;
226 ser& unit.denominator;
230 switch ( ser.mode() ) {
231 case SST::Core::Serialization::serializer::SIZER:
232 case SST::Core::Serialization::serializer::PACK:
239 case SST::Core::Serialization::serializer::UNPACK:
243 value = sst_big_num(s);
266 template <
typename T>
268 operator*(UnitAlgebra lhs,
const T& rhs)
282 operator*(UnitAlgebra lhs,
const UnitAlgebra& rhs)
288 template <
typename T>
290 operator/(UnitAlgebra lhs,
const T& rhs)
297 operator/(UnitAlgebra lhs,
const UnitAlgebra& rhs)
303 template <
typename T>
305 operator+(UnitAlgebra lhs,
const T& rhs)
312 operator+(UnitAlgebra lhs,
const UnitAlgebra& rhs)
318 template <
typename T>
320 operator-(UnitAlgebra lhs,
const T& rhs)
327 operator-(UnitAlgebra lhs,
const UnitAlgebra& rhs)
334 operator<<(std::ostream& os,
const UnitAlgebra& r)
341 operator<<(std::ostream& os,
const Units& r)
Definition: serializable.h:139
Definition: serializable.h:119
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition: serializer.h:35
Performs Unit math in full precision.
Definition: unitAlgebra.h:109
sst_big_num getValue() const
Return the raw value.
Definition: unitAlgebra.h:216
bool operator==(const UnitAlgebra &v) const
Compare if this object is equal to, the argument.
Definition: unitAlgebra.cc:528
UnitAlgebra & operator+=(const UnitAlgebra &v)
Add an argument;.
Definition: unitAlgebra.cc:442
bool operator>=(const UnitAlgebra &v) const
Compare if this object is greater than, or equal to, the argument.
Definition: unitAlgebra.cc:486
std::string toString() const
Return a string representation of this value.
Definition: unitAlgebra.cc:389
UnitAlgebra & operator*=(const UnitAlgebra &v)
Multiply by an argument;.
Definition: unitAlgebra.cc:426
UnitAlgebra & operator+=(const T &v)
Multiply by an argument;.
Definition: unitAlgebra.h:180
UnitAlgebra & invert()
Apply a reciprocal operation to the object.
Definition: unitAlgebra.cc:542
bool hasUnits(const std::string &u) const
Returns true if the units in the parameter string are found in this object.
Definition: unitAlgebra.cc:551
bool operator!=(const UnitAlgebra &v) const
Compare if this object is not equal to, the argument.
Definition: unitAlgebra.cc:535
bool operator<=(const UnitAlgebra &v) const
Compare if this object is less than, or equal to, the argument.
Definition: unitAlgebra.cc:514
void printWithBestSI(std::ostream &stream)
Print to an ostream the value Formats the number using SI-prefixes.
Definition: unitAlgebra.cc:383
UnitAlgebra(const UnitAlgebra &)=default
Copy constructor.
UnitAlgebra & operator-=(const UnitAlgebra &v)
Subtract an argument;.
Definition: unitAlgebra.cc:457
UnitAlgebra & operator/=(const UnitAlgebra &v)
Divide by an argument;.
Definition: unitAlgebra.cc:434
int64_t getRoundedValue() const
Return the rounded value as a 64bit integer.
Definition: unitAlgebra.cc:561
std::string toStringBestSI() const
Return a string representation of this value Formats the number using SI-prefixes.
Definition: unitAlgebra.cc:397
bool operator>(const UnitAlgebra &v) const
Compare if this object is greater than the argument.
Definition: unitAlgebra.cc:472
bool operator<(const UnitAlgebra &v) const
Compare if this object is less than the argument.
Definition: unitAlgebra.cc:500
UnitAlgebra & operator*=(const T &v)
Multiply by an argument;.
Definition: unitAlgebra.h:160
void print(std::ostream &stream)
Print to an ostream the value.
Definition: unitAlgebra.cc:377
UnitAlgebra & operator-=(const T &v)
Divide by an argument;.
Definition: unitAlgebra.h:190
UnitAlgebra & operator/=(const T &v)
Divide by an argument;.
Definition: unitAlgebra.h:170
Helper class internal to UnitAlgebra.
Definition: unitAlgebra.h:39
Units(const Units &)=default
Copy constructor.
Units & operator=(const Units &v)
Assignment operator.
Definition: unitAlgebra.cc:237
Units & operator/=(const Units &v)
Self-division operator.
Definition: unitAlgebra.cc:256
static void registerBaseUnit(const std::string &u)
Create a new Base Unit type.
Definition: unitAlgebra.cc:184
static void registerCompoundUnit(const std::string &u, const std::string &v)
Create a new Compound Unit type.
Definition: unitAlgebra.cc:195
std::string toString() const
Return a String representation if this Unit.
Definition: unitAlgebra.cc:288
Units & operator*=(const Units &v)
Self-multiplication operator.
Definition: unitAlgebra.cc:245
bool operator==(const Units &lhs) const
Equality Operator.
Definition: unitAlgebra.cc:265
bool operator!=(const Units &lhs) const
Inequality Operator.
Definition: unitAlgebra.h:92
Units & invert()
Perform a reciprocal operation.
Definition: unitAlgebra.cc:279
std::string toString(int32_t precision=6) const
Create a string representation of this decimal_fixedpoint.
Definition: decimal_fixedpoint.h:440