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);
145 void print(std::ostream& stream, int32_t precision = 6);
155 std::string
toString(int32_t precision = 6)
const;
167 template <
typename T>
177 template <
typename T>
187 template <
typename T>
197 template <
typename T>
222 bool hasUnits(
const std::string& u)
const;
227 double getDoubleValue()
const;
228 bool isValueZero()
const;
234 ser& unit.denominator;
238 switch ( ser.mode() ) {
239 case SST::Core::Serialization::serializer::SIZER:
240 case SST::Core::Serialization::serializer::PACK:
247 case SST::Core::Serialization::serializer::UNPACK:
251 value = sst_big_num(s);
265 class UnitAlgebraException :
public std::logic_error
271 UnitAlgebraException(
const std::string& msg);
306 NonMatchingUnits(
const std::string& lhs,
const std::string& rhs,
const std::string& operation);
325 template <
typename T>
341 operator*(UnitAlgebra lhs,
const UnitAlgebra& rhs)
347 template <
typename T>
349 operator/(UnitAlgebra lhs,
const T& rhs)
356 operator/(UnitAlgebra lhs,
const UnitAlgebra& rhs)
362 template <
typename T>
364 operator+(UnitAlgebra lhs,
const T& rhs)
371 operator+(UnitAlgebra lhs,
const UnitAlgebra& rhs)
377 template <
typename T>
379 operator-(UnitAlgebra lhs,
const T& rhs)
386 operator-(UnitAlgebra lhs,
const UnitAlgebra& rhs)
393 operator<<(std::ostream& os,
const UnitAlgebra& r)
400 operator<<(std::ostream& os,
const Units& r)
408 #endif // SST_CORE_UNITALGEBRA_H Units & operator*=(const Units &v)
Self-multiplication operator.
Definition: unitAlgebra.cc:244
UnitAlgebra & operator/=(const UnitAlgebra &v)
Divide by an argument;.
Definition: unitAlgebra.cc:432
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition: serializer.h:35
bool operator<=(const UnitAlgebra &v) const
Compare if this object is less than, or equal to, the argument.
Definition: unitAlgebra.cc:480
bool operator>=(const UnitAlgebra &v) const
Compare if this object is greater than, or equal to, the argument.
Definition: unitAlgebra.cc:463
std::string toString(int32_t precision=6) const
Create a string representation of this decimal_fixedpoint.
Definition: decimal_fixedpoint.h:440
bool operator>(const UnitAlgebra &v) const
Compare if this object is greater than the argument.
Definition: unitAlgebra.cc:456
UnitAlgebra & operator+=(const T &v)
Multiply by an argument;.
Definition: unitAlgebra.h:188
Units & operator=(const Units &v)
Assignment operator.
Definition: unitAlgebra.cc:236
static void registerBaseUnit(const std::string &u)
Create a new Base Unit type.
Definition: unitAlgebra.cc:183
sst_big_num getValue() const
Return the raw value.
Definition: unitAlgebra.h:224
Definition: serializable.h:118
Exception for when number couldn't be parsed.
Definition: unitAlgebra.h:287
Units & invert()
Perform a reciprocal operation.
Definition: unitAlgebra.cc:278
bool operator<(const UnitAlgebra &v) const
Compare if this object is less than the argument.
Definition: unitAlgebra.cc:473
UnitAlgebra & operator*=(const T &v)
Multiply by an argument;.
Definition: unitAlgebra.h:168
void print(std::ostream &stream, int32_t precision=6)
Print to an ostream the value.
Definition: unitAlgebra.cc:375
bool operator==(const UnitAlgebra &v) const
Compare if this object is equal to, the argument.
Definition: unitAlgebra.cc:487
bool operator!=(const Units &lhs) const
Inequality Operator.
Definition: unitAlgebra.h:92
NonMatchingUnits(const std::string &lhs, const std::string &rhs, const std::string &operation)
Definition: unitAlgebra.cc:554
InvalidUnitType(const std::string &type)
Definition: unitAlgebra.cc:546
Exception for when attempting operations between objects that do not have matching base units...
Definition: unitAlgebra.h:298
UnitAlgebra & operator-=(const T &v)
Divide by an argument;.
Definition: unitAlgebra.h:198
InvalidNumberString(const std::string &number)
Definition: unitAlgebra.cc:550
bool operator==(const Units &lhs) const
Equality Operator.
Definition: unitAlgebra.cc:264
Base exception for all exception classes in UnitAlgebra.
Definition: unitAlgebra.h:276
UnitAlgebra & invert()
Apply a reciprocal operation to the object.
Definition: unitAlgebra.cc:501
bool operator!=(const UnitAlgebra &v) const
Compare if this object is not equal to, the argument.
Definition: unitAlgebra.cc:494
UnitAlgebra & operator/=(const T &v)
Divide by an argument;.
Definition: unitAlgebra.h:178
UnitAlgebra & operator*=(const UnitAlgebra &v)
Multiply by an argument;.
Definition: unitAlgebra.cc:424
UnitAlgebra & operator+=(const UnitAlgebra &v)
Add an argument;.
Definition: unitAlgebra.cc:440
std::string toString() const
Return a String representation if this Unit.
Definition: unitAlgebra.cc:287
static void registerCompoundUnit(const std::string &u, const std::string &v)
Create a new Compound Unit type.
Definition: unitAlgebra.cc:194
UnitAlgebra & operator-=(const UnitAlgebra &v)
Subtract an argument;.
Definition: unitAlgebra.cc:448
std::string toStringBestSI(int32_t precision=6) const
Return a string representation of this value Formats the number using SI-prefixes.
Definition: unitAlgebra.cc:395
bool hasUnits(const std::string &u) const
Returns true if the units in the parameter string are found in this object.
Definition: unitAlgebra.cc:510
Performs Unit math in full precision.
Definition: unitAlgebra.h:106
void printWithBestSI(std::ostream &stream, int32_t precision=6)
Print to an ostream the value Formats the number using SI-prefixes.
Definition: unitAlgebra.cc:381
Units & operator/=(const Units &v)
Self-division operator.
Definition: unitAlgebra.cc:255
int64_t getRoundedValue() const
Definition: unitAlgebra.cc:520
std::string toString(int32_t precision=6) const
Return a string representation of this value.
Definition: unitAlgebra.cc:387
Helper class internal to UnitAlgebra.
Definition: unitAlgebra.h:38
Definition: serializable.h:138