GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 100.0% 1 / 1 / 2
Functions: 100.0% 1 / 1 / 2
Branches: -% 0 / 1 / 1

src/utilities/units/Unit_Impl.hpp
Line Branch Exec Source
1 /***********************************************************************************************************************
2 * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC.
3 * See also https://openstudio.net/license
4 ***********************************************************************************************************************/
5
6 #ifndef UTILITIES_UNITS_UNIT_IMPL_HPP
7 #define UTILITIES_UNITS_UNIT_IMPL_HPP
8
9 #include "../UtilitiesAPI.hpp"
10
11 #include "Unit.hpp"
12 #include "Scale.hpp"
13
14 #include <boost/smart_ptr.hpp>
15
16 namespace openstudio {
17 namespace detail {
18
19 /** Unit_Impl is the implementation class for Unit.*/
20 class UTILITIES_API Unit_Impl : public std::enable_shared_from_this<Unit_Impl>
21 {
22 public:
23 /** @name Constructors and Destructors */
24 //@{
25
26 /** Default constructor. Can set non-zero exponent (base 10) for the scale, and prettyString
27 * (alias for standardString, as in J = kg*m^2/s^2). The actual scale stored with Unit depends
28 * on what scales are registered in ScaleFactory. Throws if there is no entry for
29 * scaleExponent in ScaleFactory. Example: \verbatim
30 openstudio::Unit u(-2,"J");
31 u.setBaseUnitExponent("kg",1);
32 u.setBaseUnitExponent("m",2);
33 u.setBaseUnitExponent("s",-2);
34 std::cout << u; // would print cJ, using default openstudio::ScaleFactory initialization \endverbatim
35 * Note that this is not the preferred method for constructing Joules. See openstudio::SIUnit,
36 * and openstudio::UnitFactory for alternatives. */
37 Unit_Impl(int scaleExponent = 0, const std::string& prettyString = "");
38
39 /** Constructor using scale abbreviations registered in ScaleFactory. For instance, pass
40 * in "k" to access kilo = 10^3 (using the default ScaleFactory initialization). Throws if
41 * there is no entry for scaleAbbreviation in ScaleFactory. */
42 Unit_Impl(const std::string& scaleAbbreviation, const std::string& prettyString = "");
43
44 2841217 virtual ~Unit_Impl() = default;
45
46 /** Deep copy constructor. */
47 virtual Unit clone() const;
48
49 /** Deep copy constructor that discards system designation. Postcondition: system() == UnitSystem::Mixed. */
50 Unit cloneToMixed() const;
51
52 //@}
53 /** @name Base Units and Scale */
54 //@{
55
56 /// Returns base units already available in Unit.
57 std::vector<std::string> baseUnits() const;
58
59 /// Returns true if baseUnit is available in Unit.
60 bool isBaseUnit(const std::string& baseUnit) const;
61
62 /// Base unit exponent getter. Returns 0 if baseUnit not present. */
63 int baseUnitExponent(const std::string& baseUnit) const;
64
65 /// Sets baseUnit^exponent. If baseUnit not yet present, is added and return value is true.
66 virtual void setBaseUnitExponent(const std::string& baseUnit, int exponent);
67
68 /// Scale getter.
69 Scale scale() const;
70
71 /** Sets the scale to the one registered in ScaleFactory for 10^scaleExponent. Returns true
72 * if successful, false if no Scale with exponent == scaleExponent exists in ScaleFactory.
73 * In that case, Unit remains unchanged. */
74 bool setScale(int scaleExponent);
75
76 /** Sets the scale to the one registered in ScaleFactory under scaleAbbreviation. Returns true
77 * if successful, false if no Scale with abbr == scaleAbbreviation exists in ScaleFactory.
78 * In that case, Unit remains unchanged. */
79 bool setScale(const std::string& scaleAbbreviation);
80
81 //@}
82 /** @name Serialization */
83 //@{
84
85 /** Returns the standard output string for this unit. Version withScale==false is used as a
86 * signature elsewhere in the utility. LaTeX formatting is used. Examples of valid strings:
87 *
88 * <ul>
89 * <li>kg*m/s^2</li>
90 * <li>Btu/h</li>
91 * </ul>
92 *
93 * Units with positive exponents come first, then '/', then units with negative exponents.
94 * ^ indicates a non-unity exponent.
95 *
96 * If withScale==true (the default since this is probably preferred by users), scale
97 * abbreviation is prefixed. Prefers km^2 to M(m^2), kBtu/ft^2 to k(Btu/ft^2).
98 */
99 std::string standardString(bool withScale = true) const;
100
101 /** Returns the preferred output string for this unit. For instance, the prettyString
102 * for SI energy is 'J' for Joules, but the standard string is 'kg*m^2/s^2'.
103 *
104 * If withScale==true (the default since this is probably preferred by users), scale
105 * abbreviation is prefixed. Prefers km^2 to M(m^2), kBtu/ft^2 to k(Btu/ft^2). */
106 std::string prettyString(bool withScale = true) const;
107
108 /** Setter for prettyString (do not include scale abbreviation). openstudio::Unit attempts
109 * to keep up with prettyStrings, but makes no promises. Precondition: isCompoundUnit(str).
110 * Otherwise throws. */
111 void setPrettyString(const std::string& str);
112
113 /** Returns prettyString(withScale) if it exists; otherwise returns standardString(withScale). */
114 std::string print(bool withScale = true) const;
115
116 //@}
117 /** @name Unit System */
118 //@{
119
120 /** Getter for this Unit's system */
121 UnitSystem system() const;
122
123 //@}
124 /** @name Mathematical Operators */
125 //@{
126
127 /** Equality for units. Does not require identical systems, scales, or prettyStrings. */
128 virtual bool operator==(const Unit& rUnit) const;
129
130 /** Multiply-assign operator. Adds exponents on baseUnits and scales. Returned scale exponent
131 * may differ from expectation based on initialization of openstudio::ScaleFactory, see
132 * Scale operators declared in ScaleFactory.hpp. */
133 virtual void operator*=(const Unit& rUnit);
134
135 /** Divide-assign operator. Subtracts exponents on rUnit's baseUnits and scales from lUnits's.
136 * Returned scale exponent may differ from expectation based on initialization of
137 * openstudio::ScaleFactory, see Scale operators declared in ScaleFactory.hpp. */
138 virtual void operator/=(const Unit& rUnit);
139
140 /** Raise Unit to a rational power. Throws openstudio::Exception if expDenom is not a common divisor
141 * for all baseUnit and scale exponents. Returned scale exponent may differ from expectation
142 * based on initialization of openstudio::ScaleFactory, see Scale operators declared in
143 * ScaleFactory.hpp. */
144 void pow(int expNum, int expDenom = 1, bool okToCallFactory = true);
145
146 //@}
147 /** @name Type Casting */
148 //@{
149
150 /** Get a public object that wraps this impl.*/
151 template <typename T>
152 T getPublicObject() const {
153 T result(std::dynamic_pointer_cast<typename T::ImplType>(std::const_pointer_cast<Unit_Impl>(shared_from_this())));
154 return result;
155 }
156
157 //@}
158 protected:
159 using UnitElement = std::pair<std::string, int>;
160 std::vector<UnitElement> m_units;
161 ScaleConstant m_scale;
162 std::string m_prettyString;
163 UnitSystem m_system;
164
165 // constructor for derived classes
166 Unit_Impl(int scaleExponent, const std::string& prettyString, int numBaseUnits, UnitSystem system);
167
168 // constructor for derived classes
169 Unit_Impl(const std::string& scaleAbbreviation, const std::string& prettyString, int numBaseUnits, UnitSystem system);
170
171 std::vector<UnitElement>::iterator findBaseUnit(const std::string&);
172
173 std::vector<UnitElement>::const_iterator findBaseUnit(const std::string&) const;
174
175 private:
176 − REGISTER_LOGGER("openstudio.units.Unit");
177 };
178
179 } // namespace detail
180
181 } // namespace openstudio
182
183 #endif // UTILITIES_UNITS_UNIT_IMPL_HPP
184