Flow123d  jenkins-Flow123d-linux-release-multijob-282
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_latex() const;
65 
66  std::string format_text() const;
67 
68  /**
69  * Set flag that unit is undefined.
70  *
71  * Default value is true (set in constructor).
72  * If any exponent is set, @p undef_ flag is unset.
73  * In all fields unit must be defined by user.
74  */
75  void undef(bool val = true);
76 
77  /// Return true if the unit is defined.
78  bool is_def() const;
79 
80 private:
81  /// Values determine positions of exponents in exponents_ vector
82  enum UnitOrder {
92  };
93 
94  /// Variable parts of output format. Used in the @p format method.
95  struct OutputFormat {
96  std::string exp_open, exp_close, delimiter;
97  };
98 
99  /// Generic output formating mtehod.
100  std::string format(OutputFormat form) const;
101 
102  /**
103  * Stores exponents of base SI units in this order:
104  * [m, kg, s, A, K, mol, cd, md]
105  *
106  * where md represents value of exponent depended on dimension (m^{-d})
107  */
109 
110 
111 
112  /**
113  * Flag if object is undefined.
114  *
115  * Value is set on true in constructor, when any exponent is changed, false value is set.
116  */
117  bool undef_;
118 
119  /// Product of two units.
120  friend UnitSI operator *(const UnitSI &a, const UnitSI &b);
121  /// Proportion of two units.
122  friend UnitSI operator /(const UnitSI &a, const UnitSI &b);
123 };
124 
125 
126 /// Product of two units.
127 UnitSI operator *(const UnitSI &a, const UnitSI &b);
128 
129 /// Proportion of two units.
130 UnitSI operator /(const UnitSI &a, const UnitSI &b);
131 
132 
133 #endif /* UNIT_SI_HH_ */
std::string format(OutputFormat form) const
Generic output formating mtehod.
Definition: unit_si.cc:122
bool is_def() const
Return true if the unit is defined.
Definition: unit_si.cc:167
Variable parts of output format. Used in the format method.
Definition: unit_si.hh:95
std::string format_text() const
Definition: unit_si.cc:112
std::vector< int > exponents_
Definition: unit_si.hh:108
static UnitSI & Pa()
Returns Pascal.
Definition: unit_si.cc:39
UnitOrder
Values determine positions of exponents in exponents_ vector.
Definition: unit_si.hh:82
std::string format_latex() const
Definition: unit_si.cc:102
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:185
UnitSI operator*(const UnitSI &a, const UnitSI &b)
Product of two units.
Definition: unit_si.cc:172
UnitSI operator/(const UnitSI &a, const UnitSI &b)
Proportion of two units.
Definition: unit_si.cc:185
std::string delimiter
Definition: unit_si.hh:96
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:172
std::string exp_close
Definition: unit_si.hh:96
static UnitSI & N()
Definition: unit_si.cc:24
bool undef_
Definition: unit_si.hh:117
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:163
std::string exp_open
Definition: unit_si.hh:96
UnitSI()
Constructor.
Definition: unit_si.cc:18