TBTK
|
Need a break? Support the development by playing Polarity Puzzles |
Base class for Quantitis. More...
#include <Quantity.h>
Classes | |
class | ConversionTable |
Public Types | |
using | Unit = Units |
using | Exponent = Exponents |
Public Member Functions | |
Quantity () | |
Quantity (double value) | |
Public Member Functions inherited from TBTK::Real | |
Real () | |
constexpr | Real (double value) |
Real (const std::string &serialization, Serializable::Mode mode) | |
constexpr | operator double () const |
Real & | operator= (double rhs) |
Real & | operator+= (const Real &rhs) |
Real & | operator-= (const Real &rhs) |
Real & | operator*= (const Real &rhs) |
Real & | operator/= (const Real &rhs) |
std::string | serialize (Serializable::Mode mode) const |
Static Public Member Functions | |
static std::string | getUnitString (Unit unit) |
static Unit | getUnit (const std::string &unit) |
static double | getConversionFactor (Unit unit) |
Static Protected Attributes | |
static class TBTK::Quantity::Quantity::ConversionTable | conversionTable |
A Quantity is a Real number that carries compile time information about units. While it is possible to create Quantity object, their main purpose is to act as template parameter for the UnitHandler functions.
Note: The Quantity template is not meant to be instatiated directly. Instead the Base and Derived classes that inherit from Quantity are meant to be instatiated. However, the syntax for instantiating such Quantities is the same as instantiating a Quantity directly. The instantiation procedure is therefore outlined here.
To instantiate a Quantity, we first need to define a set of units for the Quantity. This is done with the help of an enum class.
We also need to define the exponents for the base units Angle, Charge, Count, Energy, Length, Temperature, and Time. In the case of voltage, this is V = J/C, or Energy/Charge.
Having defined these two enum classes, we can define Voltage using
Note that when instantiating Base and Derived units, Quantity in the line above is replaced by Base and Derived, respectively.
With these definitions, the following symbols that are relevant for UnitHandler calls are defined:
We also need to define a table that can be used to convert between string and enum class representations of the units. The conversion table should also contain a conversion factor that specifies the amount in the given unit that corresponds to a single unit of the Quantity in the default base units. The default base units are rad, C, pcs, eV, m, K, and s.
The unit for voltage is V = J/C = 1.602176634e-19. We can therefore define the conversion table as follows.
Notes:
The conversionTable is defined as follows. (Note the empty curly brace, these are necessary to ensure that the conversion table is created.)
The following code is then put into the function initializeDerivedQuantities() to initialize the conversion table.
The conversion tables for Base units are similarly initialized in initializeBaseQuantities().
It is not possible to define custom Derived units and initialize them from another function that initializeDerivedQuantities(). Custom defined Derived units therefore either have to be defined using hard coded constants or be defined by extending the Derived.h and Derived.cpp files directly.
|
inline |
Default constructor.
|
inline |
|
static |
Get the conversion factor for converting from the reference unit to the given unit.
unit | The unit to get the conversion factor for. |
|
static |
Convert a string to a Unit.
unit | A unit string. |
|
static |
Get unit string for the given Unit.
unit | The unit to get the string for. |