Flow123d  jenkins-Flow123d-windows32-release-multijob-51
unit_si.hh
Go to the documentation of this file.
1 /*
2  * unit_si.hh
3  *
4  * Created on: Aug 30, 2012
5  * Author: jb
6  */
7 
8 
9 #ifndef UNIT_SI_HH_
10 #define UNIT_SI_HH_
11 
12 #include <vector>
13 #include <string>
14 
15 
16 /**
17  * @brief Class for representation SI units of Fields.
18  *
19  * Units are set through exponents of basic SI units. These exponents are set in methods with same
20  * name as unit symbols (e.g. kg(), K() etc).
21  *
22  * Class contains method that provides formated string representing full unit symbol (usable in
23  * LaTeX output).
24  *
25  * UnitSI object contains flag that says if it is defined
26  * - undefined object can't be formated
27  * - if any exponent is set, flag is set to defined
28  *
29  * Class contains static methods that return frequently used derived units (Watt, Pascal etc).
30  */
31 class UnitSI {
32 public:
33  /// Constructor
34  UnitSI();
35 
36  /// Methods return frequently used derived units
37  /// Returns Newton
38  static UnitSI & N();
39  /// Returns Joule
40  static UnitSI & J();
41  /// Returns Watt
42  static UnitSI & W();
43  /// Returns Pascal
44  static UnitSI & Pa();
45  /// Returns dimensionless unit
46  static UnitSI & dimensionless();
47 
48  /// Methods set values of exponents for SI units with similar name
49  UnitSI & m(int exp = 1);
50  UnitSI & kg(int exp = 1);
51  UnitSI & s(int exp = 1);
52  UnitSI & A(int exp = 1);
53  UnitSI & K(int exp = 1);
54  UnitSI & mol(int exp = 1);
55  UnitSI & cd(int exp = 1);
56  /// Method sets value of exponent for m^{-d}, where d is dimension of region
57  UnitSI & md(int exp = -1);
58 
59  /**
60  * Makes unit description string in Latex format, e.g. "$[m.kg^{2}.s^{-2}]$"
61  *
62  * Have assert for undefined units.
63  */
64  std::string format() const;
65 
66  /**
67  * Set flag that unit is undefined.
68  *
69  * Default value is true (set in constructor).
70  * If any exponent is set, @p undef_ flag is unset.
71  * In all fields unit must be defined by user.
72  */
73  void undef(bool val = true);
74 
75  /// Return true if the unit is defined.
76  bool is_def() const;
77 
78 private:
79  /// Values determine positions of exponents in exponents_ vector
80  enum UnitOrder {
89  };
90 
91  /**
92  * Stores exponents of base SI units in this order:
93  * [m, kg, s, A, K, mol, cd, md]
94  *
95  * where md represents value of exponent depended on dimension (m^{-d})
96  */
98 
99  /**
100  * Flag if object is undefined.
101  *
102  * Value is set on true in constructor, when any exponent is changed, false value is set.
103  */
104  bool undef_;
105 
106  /// Product of two units.
107  friend UnitSI operator *(const UnitSI &a, const UnitSI &b);
108  /// Proportion of two units.
109  friend UnitSI operator /(const UnitSI &a, const UnitSI &b);
110 };
111 
112 
113 /// Product of two units.
114 UnitSI operator *(const UnitSI &a, const UnitSI &b);
115 
116 /// Proportion of two units.
117 UnitSI operator /(const UnitSI &a, const UnitSI &b);
118 
119 
120 #endif /* UNIT_SI_HH_ */
bool is_def() const
Return true if the unit is defined.
Definition: unit_si.cc:145
std::vector< int > exponents_
Definition: unit_si.hh:97
static UnitSI & Pa()
Returns Pascal.
Definition: unit_si.cc:39
UnitSI operator*(const UnitSI &a, const UnitSI &b)
Product of two units.
Definition: unit_si.cc:150
UnitOrder
Values determine positions of exponents in exponents_ vector.
Definition: unit_si.hh:80
std::string format() const
Definition: unit_si.cc:97
UnitSI & K(int exp=1)
Definition: unit_si.cc:73
UnitSI & A(int exp=1)
Definition: unit_si.cc:67
friend UnitSI operator/(const UnitSI &a, const UnitSI &b)
Proportion of two units.
Definition: unit_si.cc:163
UnitSI & cd(int exp=1)
Definition: unit_si.cc:85
static UnitSI & W()
Returns Watt.
Definition: unit_si.cc:34
UnitSI & s(int exp=1)
Definition: unit_si.cc:61
UnitSI & kg(int exp=1)
Definition: unit_si.cc:55
static UnitSI & J()
Returns Joule.
Definition: unit_si.cc:29
friend UnitSI operator*(const UnitSI &a, const UnitSI &b)
Product of two units.
Definition: unit_si.cc:150
UnitSI operator/(const UnitSI &a, const UnitSI &b)
Proportion of two units.
Definition: unit_si.cc:163
static UnitSI & N()
Definition: unit_si.cc:24
bool undef_
Definition: unit_si.hh:104
UnitSI & mol(int exp=1)
Definition: unit_si.cc:79
UnitSI & m(int exp=1)
Methods set values of exponents for SI units with similar name.
Definition: unit_si.cc:49
Class for representation SI units of Fields.
Definition: unit_si.hh:31
UnitSI & md(int exp=-1)
Method sets value of exponent for m^{-d}, where d is dimension of region.
Definition: unit_si.cc:91
static UnitSI & dimensionless()
Returns dimensionless unit.
Definition: unit_si.cc:44
void undef(bool val=true)
Definition: unit_si.cc:141
UnitSI()
Constructor.
Definition: unit_si.cc:18