Flow123d  last_with_con_2.0.0-4-g42e6930
unit_si.hh
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file unit_si.hh
15  * @brief
16  */
17 
18 #ifndef UNIT_SI_HH_
19 #define UNIT_SI_HH_
20 
21 #include <vector>
22 #include <string>
23 
24 
25 /**
26  * @brief Class for representation SI units of Fields.
27  *
28  * Units are set through exponents of basic SI units. These exponents are set in methods with same
29  * name as unit symbols (e.g. kg(), K() etc).
30  *
31  * Class contains method that provides formated string representing full unit symbol (usable in
32  * LaTeX output).
33  *
34  * UnitSI object contains flag that says if it is defined
35  * - undefined object can't be formated
36  * - if any exponent is set, flag is set to defined
37  *
38  * Class contains static methods that return frequently used derived units (Watt, Pascal etc).
39  */
40 class UnitSI {
41 public:
42  /// Constructor
43  UnitSI();
44 
45  /// Methods return frequently used derived units
46  /// Returns Newton
47  static UnitSI & N();
48  /// Returns Joule
49  static UnitSI & J();
50  /// Returns Watt
51  static UnitSI & W();
52  /// Returns Pascal
53  static UnitSI & Pa();
54  /// Returns dimensionless unit
55  static UnitSI & dimensionless();
56  /// Returns dimensionless unit
57  static UnitSI & one();
58 
59  /// Methods set values of exponents for SI units with similar name
60  UnitSI & m(int exp = 1);
61  UnitSI & kg(int exp = 1);
62  UnitSI & s(int exp = 1);
63  UnitSI & A(int exp = 1);
64  UnitSI & K(int exp = 1);
65  UnitSI & mol(int exp = 1);
66  UnitSI & cd(int exp = 1);
67  /// The dimension dependent meter: md^y = m^(yd), where 'd' is dimension.
68  UnitSI & md(int exp = -1);
69 
70  /**
71  * Makes unit description string in Latex format, e.g. "$[m.kg^{2}.s^{-2}]$"
72  *
73  * Have assert for undefined units.
74  */
75  std::string format_latex() const;
76 
77  std::string format_text() const;
78 
79  /**
80  * Machine readable JSON format. Units are stored as a record with keys given by
81  * SI units strings (corresponding to setters). Values of the keys are integer.
82  */
83  std::string json() const;
84 
85  /**
86  * Set flag that unit is undefined.
87  *
88  * Default value is true (set in constructor).
89  * If any exponent is set, @p undef_ flag is unset.
90  * In all fields unit must be defined by user.
91  */
92  void undef(bool val = true);
93 
94  /// Return true if the unit is defined.
95  bool is_def() const;
96 
97 private:
98  /// Values determine positions of exponents in exponents_ vector
99  enum UnitOrder {
109  };
110 
111  /// Variable parts of output format. Used in the @p format method.
112  struct OutputFormat {
113  std::string exp_open, exp_close, delimiter;
114  };
115 
116  /**
117  * Symbols for individual units. Can not use static variable due to usage in static initialization.
118  */
119  static const std::string &unit_symbol(unsigned int idx);
120 
121  /// Generic output formating method.
122  std::string format(OutputFormat form) const;
123 
124 
125  /**
126  * Stores exponents of base SI units in the order given by the
127  * UnitOrder enum
128  *
129  * where md represents value of exponent depended on dimension (m^{-d})
130  */
132 
133 
134  /**
135  * Flag if object is undefined.
136  *
137  * Value is set on true in constructor, when any exponent is changed, false value is set.
138  */
139  bool undef_;
140 
141  /// Product of two units.
142  friend UnitSI operator *(const UnitSI &a, const UnitSI &b);
143  /// Proportion of two units.
144  friend UnitSI operator /(const UnitSI &a, const UnitSI &b);
145 };
146 
147 
148 /// Product of two units.
149 UnitSI operator *(const UnitSI &a, const UnitSI &b);
150 
151 /// Proportion of two units.
152 UnitSI operator /(const UnitSI &a, const UnitSI &b);
153 
154 
155 #endif /* UNIT_SI_HH_ */
std::string format(OutputFormat form) const
Generic output formating method.
Definition: unit_si.cc:142
bool is_def() const
Return true if the unit is defined.
Definition: unit_si.cc:199
Variable parts of output format. Used in the format method.
Definition: unit_si.hh:112
std::string format_text() const
Definition: unit_si.cc:125
std::vector< int > exponents_
Definition: unit_si.hh:131
static UnitSI & Pa()
Returns Pascal.
Definition: unit_si.cc:48
static UnitSI & one()
Returns dimensionless unit.
Definition: unit_si.cc:58
UnitOrder
Values determine positions of exponents in exponents_ vector.
Definition: unit_si.hh:99
std::string format_latex() const
Definition: unit_si.cc:115
UnitSI & K(int exp=1)
Definition: unit_si.cc:86
UnitSI & A(int exp=1)
Definition: unit_si.cc:80
friend UnitSI operator/(const UnitSI &a, const UnitSI &b)
Proportion of two units.
Definition: unit_si.cc:217
std::string delimiter
Definition: unit_si.hh:113
UnitSI & cd(int exp=1)
Definition: unit_si.cc:98
static UnitSI & W()
Returns Watt.
Definition: unit_si.cc:43
UnitSI & s(int exp=1)
Definition: unit_si.cc:74
UnitSI & kg(int exp=1)
Definition: unit_si.cc:68
static UnitSI & J()
Returns Joule.
Definition: unit_si.cc:38
friend UnitSI operator*(const UnitSI &a, const UnitSI &b)
Product of two units.
Definition: unit_si.cc:204
std::string exp_close
Definition: unit_si.hh:113
static UnitSI & N()
Definition: unit_si.cc:33
static const std::string & unit_symbol(unsigned int idx)
Definition: unit_si.cc:135
bool undef_
Definition: unit_si.hh:139
UnitSI & mol(int exp=1)
Definition: unit_si.cc:92
UnitSI & m(int exp=1)
Methods set values of exponents for SI units with similar name.
Definition: unit_si.cc:62
Class for representation SI units of Fields.
Definition: unit_si.hh:40
UnitSI & md(int exp=-1)
The dimension dependent meter: md^y = m^(yd), where &#39;d&#39; is dimension.
Definition: unit_si.cc:104
static UnitSI & dimensionless()
Returns dimensionless unit.
Definition: unit_si.cc:53
void undef(bool val=true)
Definition: unit_si.cc:195
std::string exp_open
Definition: unit_si.hh:113
UnitSI()
Constructor.
Definition: unit_si.cc:27
std::string json() const
Definition: unit_si.cc:181