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"
44 using unit_id_t = uint8_t;
47 friend class UnitAlgebra;
50 static std::recursive_mutex unit_lock;
51 static std::map<std::string, unit_id_t> valid_base_units;
52 static std::map<std::string, std::pair<Units, sst_big_num>> valid_compound_units;
53 static std::map<unit_id_t, std::string> unit_strings;
54 static unit_id_t count;
57 std::vector<unit_id_t> numerator;
58 std::vector<unit_id_t> denominator;
62 void addUnit(
const std::string&, sst_big_num& multiplier,
bool invert);
76 Units(
const std::string& units, sst_big_num& multiplier);
78 virtual ~Units() =
default;
112 static std::string trim(
const std::string& str);
115 void init(
const std::string& val);
117 UnitAlgebra() =
default;
133 UnitAlgebra(
const std::string& val);
134 virtual ~UnitAlgebra() =
default;
143 void print(std::ostream& stream, int32_t precision = 6);
153 std::string
toString(int32_t precision = 6)
const;
160 UnitAlgebra& operator=(
const std::string& v);
163 UnitAlgebra&
operator*=(
const UnitAlgebra& v);
165 template <
typename T>
175 template <
typename T>
185 template <
typename T>
195 template <
typename T>
220 bool hasUnits(
const std::string& u)
const;
225 double getDoubleValue()
const;
226 bool isValueZero()
const;
231 SST_SER(unit.numerator);
232 SST_SER(unit.denominator);
236 switch ( ser.mode() ) {
237 case SST::Core::Serialization::serializer::SIZER:
238 case SST::Core::Serialization::serializer::PACK:
245 case SST::Core::Serialization::serializer::UNPACK:
249 value = sst_big_num(s);
252 case SST::Core::Serialization::serializer::MAP:
306 NonMatchingUnits(
const std::string& lhs,
const std::string& rhs,
const std::string& operation);
341operator*(UnitAlgebra lhs,
const UnitAlgebra& rhs)
400operator<<(std::ostream& os,
const Units& r)
407namespace Core::Serialization {
419 std::string
get() const final
override {
return addr_->toStringBestSI(); }
420 void set_impl(
const std::string& value)
final override {
addr_->init(value); }
445 switch ( ser.mode() ) {
446 case serializer::SIZER:
447 case serializer::PACK:
448 case serializer::UNPACK:
449 ua.serialize_order(ser);
451 case serializer::MAP:
454 if ( SerOption::is_set(options, SerOption::map_read_only) ) obj_map->
setReadOnly();
455 ser.mapper().map_object(ser.getMapName(), obj_map);
461 SST_FRIEND_SERIALIZE();
void * getAddr() const final override
Get the address of the variable represented by the ObjectMap.
Definition unitAlgebra.h:430
std::string get() const final override
Get the value of the variable as a string.
Definition unitAlgebra.h:419
std::string getType() const override
Get the type of the variable represented by the ObjectMap.
Definition unitAlgebra.h:437
bool isFundamental() const final override
Check to see if this ObjectMap represents a fundamental or a class treated as a fundamental.
Definition unitAlgebra.h:423
UnitAlgebra * addr_
Address of the variable for reading and writing.
Definition unitAlgebra.h:416
ObjectMap representing fundamental types, and classes treated as fundamental types.
Definition objectMap.h:1230
REF * addr_
Address of the variable for reading and writing.
Definition objectMap.h:1235
virtual void set_impl(const std::string &value) override
Set the value of the object represented as a string.
Definition objectMap.h:1244
Base class for objects created by the serializer mapping mode used to map the variables for objects.
Definition objectMap.h:188
ObjectMap()=default
Default constructor primarily used for the "top" object in the hierarchy.
static std::string demangle_name(const char *name)
Static function to demangle type names returned from typeid(T).name().
Definition objectMap.cc:152
void setReadOnly(bool state=true)
Set the read-only state of the object.
Definition objectMap.h:263
Base serialize class.
Definition serialize.h:132
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition serializer.h:43
InvalidNumberString(const std::string &number)
Definition unitAlgebra.cc:539
InvalidUnitType(const std::string &type)
Definition unitAlgebra.cc:535
NonMatchingUnits(const std::string &lhs, const std::string &rhs, const std::string &operation)
Definition unitAlgebra.cc:543
UnitAlgebraException(const std::string &msg)
Definition unitAlgebra.cc:531
Performs Unit math in full precision.
Definition unitAlgebra.h:107
void print(std::ostream &stream, int32_t precision=6)
Print to an ostream the value.
Definition unitAlgebra.cc:352
sst_big_num getValue() const
Return the raw value.
Definition unitAlgebra.h:222
bool operator==(const UnitAlgebra &v) const
Compare if this object is equal to, the argument.
Definition unitAlgebra.cc:476
UnitAlgebra & operator/=(const T &v)
Divide by an argument;.
Definition unitAlgebra.h:176
UnitAlgebra & operator+=(const UnitAlgebra &v)
Add an argument;.
Definition unitAlgebra.cc:419
bool operator>=(const UnitAlgebra &v) const
Compare if this object is greater than, or equal to, the argument.
Definition unitAlgebra.cc:448
UnitAlgebra & operator*=(const UnitAlgebra &v)
Multiply by an argument;.
Definition unitAlgebra.cc:403
UnitAlgebra & invert()
Apply a reciprocal operation to the object.
Definition unitAlgebra.cc:490
bool hasUnits(const std::string &u) const
Returns true if the units in the parameter string are found in this object.
Definition unitAlgebra.cc:499
bool operator!=(const UnitAlgebra &v) const
Compare if this object is not equal to, the argument.
Definition unitAlgebra.cc:483
bool operator<=(const UnitAlgebra &v) const
Compare if this object is less than, or equal to, the argument.
Definition unitAlgebra.cc:467
std::string toStringBestSI(int32_t precision=6) const
Return a string representation of this value Formats the number using SI-prefixes.
Definition unitAlgebra.cc:372
std::string toString(int32_t precision=6) const
Return a string representation of this value.
Definition unitAlgebra.cc:364
UnitAlgebra(const UnitAlgebra &)=default
Copy constructor.
UnitAlgebra & operator-=(const UnitAlgebra &v)
Subtract an argument;.
Definition unitAlgebra.cc:429
UnitAlgebra & operator*=(const T &v)
Multiply by an argument;.
Definition unitAlgebra.h:166
void printWithBestSI(std::ostream &stream, int32_t precision=6)
Print to an ostream the value Formats the number using SI-prefixes.
Definition unitAlgebra.cc:358
UnitAlgebra & operator/=(const UnitAlgebra &v)
Divide by an argument;.
Definition unitAlgebra.cc:411
int64_t getRoundedValue() const
Definition unitAlgebra.cc:507
bool operator>(const UnitAlgebra &v) const
Compare if this object is greater than the argument.
Definition unitAlgebra.cc:439
UnitAlgebra & operator+=(const T &v)
Multiply by an argument;.
Definition unitAlgebra.h:186
bool operator<(const UnitAlgebra &v) const
Compare if this object is less than the argument.
Definition unitAlgebra.cc:458
UnitAlgebra & operator-=(const T &v)
Divide by an argument;.
Definition unitAlgebra.h:196
Helper class internal to UnitAlgebra.
Definition unitAlgebra.h:42
Units & operator=(const Units &v)=default
Assignment operator.
Units(const Units &)=default
Copy constructor.
std::string toString() const
Return a String representation if this Unit.
Definition unitAlgebra.cc:262
Units & operator/=(const Units &v)
Self-division operator.
Definition unitAlgebra.cc:230
static void registerBaseUnit(const std::string &u)
Create a new Base Unit type.
Definition unitAlgebra.cc:166
static void registerCompoundUnit(const std::string &u, const std::string &v)
Create a new Compound Unit type.
Definition unitAlgebra.cc:177
Units & operator*=(const Units &v)
Self-multiplication operator.
Definition unitAlgebra.cc:219
bool operator==(const Units &lhs) const
Equality Operator.
Definition unitAlgebra.cc:239
bool operator!=(const Units &lhs) const
Inequality Operator.
Definition unitAlgebra.h:92
Units(const std::string &units, sst_big_num &multiplier)
Create a new instantiation of a Units with a base unit string, and multiplier.
Definition unitAlgebra.cc:187
Units & invert()
Perform a reciprocal operation.
Definition unitAlgebra.cc:253
Class that implements a decimal fixed-point number.
Definition decimal_fixedpoint.h:45
std::string toString(int32_t precision=6) const
Create a string representation of this decimal_fixedpoint.
Definition decimal_fixedpoint.h:447