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/ScaleFactory.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_SCALEFACTORY_HPP
7 #define UTILITIES_UNITS_SCALEFACTORY_HPP
8
9 #include "../UtilitiesAPI.hpp"
10 #include "../core/Logger.hpp"
11
12 #include "Scale.hpp"
13
14 #include <ostream>
15 #include <set>
16 #include <map>
17
18 namespace openstudio {
19
20 /** Singleton that creates scales based on std::string or exponent.
21 * Access using ScaleFactory::instance(). */
22 class UTILITIES_API ScaleFactory
23 {
24 public:
25 static ScaleFactory& instance();
26
27 ScaleFactory(const ScaleFactory& other) = delete;
28 ScaleFactory(ScaleFactory&& other) = delete;
29 ScaleFactory& operator=(const ScaleFactory&) = delete;
30 ScaleFactory& operator=(ScaleFactory&&) = delete;
31
32 /** Store a scale callback function for future lookup using the scale exponent or
33 * abbreviation. By default, SI scales are registered during factory construction. */
34 bool registerScale(ScaleConstant scale);
35
36 /** Returns a vector of Scale objects, one for each scale registered in the factory. */
37 std::vector<Scale> registeredScales();
38
39 /** Outputs the scales registered in the factory. */
40 friend UTILITIES_API std::ostream& operator<<(std::ostream& os, const ScaleFactory& factory);
41
42 /** Creates a scale from an exponent. Throws if the factory does not have any scales registered.
43 * Returns ScaleConstant with ().value == 0.0 if exponent not in factory. */
44 ScaleConstant createScale(int exponent) const;
45
46 /** Creates a scale from an abbreviation. Throws if the factory does not have any scales registered.
47 * Returns ScaleConstant with ().value == 0.0 if abbr not in factory. */
48 ScaleConstant createScale(const std::string& abbr) const;
49
50 private:
51 − REGISTER_LOGGER("openstudio.units.ScaleFactory");
52 /// Private constructor initializes ScaleFactory with standard SI scales.
53 ScaleFactory();
54 1887 ~ScaleFactory() = default;
55
56 using ExponentLookupMap = std::map<int, ScaleConstant>;
57 using AbbreviationLookupMap = std::map<std::string, ScaleConstant>;
58 ExponentLookupMap m_exponentMap;
59 AbbreviationLookupMap m_abbreviationMap;
60
61 ScaleConstant m_recoverFromFailedCreate() const;
62 };
63
64 /** Prints scales that are registered in the factory (and thus available for use in
65 * Units and Quantities). */
66 UTILITIES_API std::ostream& operator<<(std::ostream& os, const ScaleFactory& factory);
67
68 /** Wrapper around << for SWIG bindings. Prints each scale registered in ScaleFactory,
69 * and thus available for use in Units, Quantities, and bare Scale operations. */
70 UTILITIES_API std::string printScales();
71
72 /** If possible, extracts scale abbreviation from the front of str, returning abbreviation in .first
73 * and remainder of string in .second. Otherwise, returns empty strings. */
74 UTILITIES_API std::pair<std::string, std::string> extractScaleAbbreviation(const std::string& str);
75
76 /** Return type for scale multiplication and division. See the corresponding operators
77 * for its use. */
78 // DLM: this is ill-conceived and easy to misuse (which is what was happening) in #2240
79 // all operations (e.g. *) between two Scales should return a Scale
80 using ScaleOpReturnType = std::pair<ScaleConstant, double>;
81
82 /** Multiplication of scales. If result implied by adding exponents is not
83 * available, .second is amount multiplication of values should be multiplied
84 * by so result will be correct when using .first. */
85 UTILITIES_API ScaleOpReturnType operator*(const Scale& firstScale, const Scale& secondScale);
86
87 /** Division of scales. If result implied by subtracting exponents is not
88 * available, .second is amount final value should be multiplied by so result
89 * will be correct when using .first. */
90 UTILITIES_API ScaleOpReturnType operator/(const Scale& firstScale, const Scale& secondScale);
91
92 /** Raise scale to an integer power. If result implied by the operation is not
93 * available, .second is amount final value should be multiplied by so result
94 * will be correct when using .first. */
95 UTILITIES_API ScaleOpReturnType pow(const Scale& scale, int expNum, int expDenom = 1);
96
97 /** Returns scale with desiredExponent, if possible. Otherwise backtracks to nearby
98 * scale, and returns that scale along with a factor that the value should be
99 * multiplied by to make the end result consistent with the returned scale. */
100 UTILITIES_API ScaleOpReturnType scaleOpHelperFunction(int desiredExponent);
101
102 } // namespace openstudio
103
104 #endif // UTILITIES_UNITS_SCALEFACTORY_HPP
105