14 #ifndef SST_CORE_UNITALGEBRA_H 15 #define SST_CORE_UNITALGEBRA_H 17 #include "sst/core/decimal_fixedpoint.h" 18 #include "sst/core/serialization/objectMap.h" 19 #include "sst/core/serialization/serialize.h" 20 #include "sst/core/serialization/serializer.h" 21 #include "sst/core/sst_types.h" 22 #include "sst/core/warnmacros.h" 33 using sst_big_num = decimal_fixedpoint<3, 3>;
43 using unit_id_t = uint8_t;
49 static std::recursive_mutex unit_lock;
50 static std::map<std::string, unit_id_t> valid_base_units;
51 static std::map<std::string, std::pair<Units, sst_big_num>> valid_compound_units;
52 static std::map<unit_id_t, std::string> unit_strings;
53 static unit_id_t count;
56 std::vector<unit_id_t> numerator;
57 std::vector<unit_id_t> denominator;
111 static std::string trim(
const std::string& str);
114 void init(
const std::string& val);
142 void print(std::ostream& stream, int32_t precision = 6);
152 std::string
toString(int32_t precision = 6)
const;
164 template <
typename T>
174 template <
typename T>
184 template <
typename T>
194 template <
typename T>
219 bool hasUnits(
const std::string& u)
const;
224 double getDoubleValue()
const;
225 bool isValueZero()
const;
230 SST_SER(unit.numerator);
231 SST_SER(unit.denominator);
235 switch ( ser.mode() ) {
236 case SST::Core::Serialization::serializer::SIZER:
237 case SST::Core::Serialization::serializer::PACK:
244 case SST::Core::Serialization::serializer::UNPACK:
248 value = sst_big_num(s);
251 case SST::Core::Serialization::serializer::MAP:
305 NonMatchingUnits(
const std::string& lhs,
const std::string& rhs,
const std::string& operation);
324 template <
typename T>
340 operator*(UnitAlgebra lhs,
const UnitAlgebra& rhs)
346 template <
typename T>
348 operator/(UnitAlgebra lhs,
const T& rhs)
355 operator/(UnitAlgebra lhs,
const UnitAlgebra& rhs)
361 template <
typename T>
363 operator+(UnitAlgebra lhs,
const T& rhs)
370 operator+(UnitAlgebra lhs,
const UnitAlgebra& rhs)
376 template <
typename T>
378 operator-(UnitAlgebra lhs,
const T& rhs)
385 operator-(UnitAlgebra lhs,
const UnitAlgebra& rhs)
392 operator<<(std::ostream& os,
const UnitAlgebra& r)
399 operator<<(std::ostream& os,
const Units& r)
406 namespace Core::Serialization {
418 std::string
get()
override {
return addr_->toStringBestSI(); }
419 void set_impl(
const std::string& value)
override {
addr_->init(value); }
444 switch ( ser.mode() ) {
445 case serializer::SIZER:
446 case serializer::PACK:
447 case serializer::UNPACK:
448 ua.serialize_order(ser);
450 case serializer::MAP:
453 if ( options & SerOption::map_read_only ) {
454 ser.mapper().setNextObjectReadOnly();
456 ser.mapper().map_primitive(ser.getMapName(), obj_map);
462 SST_FRIEND_SERIALIZE();
470 #endif // SST_CORE_UNITALGEBRA_H Units & operator*=(const Units &v)
Self-multiplication operator.
Definition: unitAlgebra.cc:227
void * getAddr() override
Get the address of the variable represented by the ObjectMap.
Definition: unitAlgebra.h:429
UnitAlgebra & operator/=(const UnitAlgebra &v)
Divide by an argument;.
Definition: unitAlgebra.cc:419
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition: serializer.h:42
std::string toStringBestSI(int32_t precision=6) const
Return a string representation of this value Formats the number using SI-prefixes.
Definition: unitAlgebra.cc:380
bool operator<=(const UnitAlgebra &v) const
Compare if this object is less than, or equal to, the argument.
Definition: unitAlgebra.cc:475
bool operator>=(const UnitAlgebra &v) const
Compare if this object is greater than, or equal to, the argument.
Definition: unitAlgebra.cc:456
std::string toString(int32_t precision=6) const
Create a string representation of this decimal_fixedpoint.
Definition: decimal_fixedpoint.h:446
std::string toString(int32_t precision=6) const
Return a string representation of this value.
Definition: unitAlgebra.cc:372
ObjectMap representing fundamental types, and classes treated as fundamental types.
Definition: objectMap.h:1264
T * addr_
Address of the variable for reading and writing.
Definition: objectMap.h:1270
bool isFundamental() override
Check to see if this ObjectMap represents a fundamental or a class treated as a fundamental.
Definition: unitAlgebra.h:422
bool operator>(const UnitAlgebra &v) const
Compare if this object is greater than the argument.
Definition: unitAlgebra.cc:447
Base serialize class.
Definition: serialize.h:113
Definition: unitAlgebra.h:409
UnitAlgebra & operator+=(const T &v)
Multiply by an argument;.
Definition: unitAlgebra.h:185
Units & operator=(const Units &v)
Assignment operator.
Definition: unitAlgebra.cc:219
virtual void set_impl(const std::string &value) override
Set the value of the object represented as a string.
Definition: objectMap.h:1279
static void registerBaseUnit(const std::string &u)
Create a new Base Unit type.
Definition: unitAlgebra.cc:166
sst_big_num getValue() const
Return the raw value.
Definition: unitAlgebra.h:221
Base class for objects created by the serializer mapping mode used to map the variables for objects...
Definition: objectMap.h:158
Exception for when number couldn't be parsed.
Definition: unitAlgebra.h:286
Units & invert()
Perform a reciprocal operation.
Definition: unitAlgebra.cc:261
bool operator<(const UnitAlgebra &v) const
Compare if this object is less than the argument.
Definition: unitAlgebra.cc:466
UnitAlgebra & operator*=(const T &v)
Multiply by an argument;.
Definition: unitAlgebra.h:165
void print(std::ostream &stream, int32_t precision=6)
Print to an ostream the value.
Definition: unitAlgebra.cc:360
bool operator==(const UnitAlgebra &v) const
Compare if this object is equal to, the argument.
Definition: unitAlgebra.cc:484
Base exception for all exception classes in UnitAlgebra.
Definition: unitAlgebra.h:264
bool operator!=(const Units &lhs) const
Inequality Operator.
Definition: unitAlgebra.h:91
NonMatchingUnits(const std::string &lhs, const std::string &rhs, const std::string &operation)
Definition: unitAlgebra.cc:551
InvalidUnitType(const std::string &type)
Definition: unitAlgebra.cc:543
Exception for when attempting operations between objects that do not have matching base units...
Definition: unitAlgebra.h:297
UnitAlgebra & operator-=(const T &v)
Divide by an argument;.
Definition: unitAlgebra.h:195
InvalidNumberString(const std::string &number)
Definition: unitAlgebra.cc:547
UnitAlgebraException(const std::string &msg)
Definition: unitAlgebra.cc:539
bool operator==(const Units &lhs) const
Equality Operator.
Definition: unitAlgebra.cc:247
Exception for when units are not recognized or are invalid.
Definition: unitAlgebra.h:275
UnitAlgebra & invert()
Apply a reciprocal operation to the object.
Definition: unitAlgebra.cc:498
std::string toString() const
Return a String representation if this Unit.
Definition: unitAlgebra.cc:270
std::string getType() override
Get the type of the variable represented by the ObjectMap.
Definition: unitAlgebra.h:436
bool operator!=(const UnitAlgebra &v) const
Compare if this object is not equal to, the argument.
Definition: unitAlgebra.cc:491
static std::string demangle_name(const char *name)
Static function to demangle type names returned from typeid(T).name()
Definition: objectMap.cc:161
UnitAlgebra & operator/=(const T &v)
Divide by an argument;.
Definition: unitAlgebra.h:175
UnitAlgebra & operator*=(const UnitAlgebra &v)
Multiply by an argument;.
Definition: unitAlgebra.cc:411
UnitAlgebra & operator+=(const UnitAlgebra &v)
Add an argument;.
Definition: unitAlgebra.cc:427
static void registerCompoundUnit(const std::string &u, const std::string &v)
Create a new Compound Unit type.
Definition: unitAlgebra.cc:177
UnitAlgebra & operator-=(const UnitAlgebra &v)
Subtract an argument;.
Definition: unitAlgebra.cc:437
bool hasUnits(const std::string &u) const
Returns true if the units in the parameter string are found in this object.
Definition: unitAlgebra.cc:507
Performs Unit math in full precision.
Definition: unitAlgebra.h:105
void printWithBestSI(std::ostream &stream, int32_t precision=6)
Print to an ostream the value Formats the number using SI-prefixes.
Definition: unitAlgebra.cc:366
Units & operator/=(const Units &v)
Self-division operator.
Definition: unitAlgebra.cc:238
int64_t getRoundedValue() const
Definition: unitAlgebra.cc:515
Helper class internal to UnitAlgebra.
Definition: unitAlgebra.h:40