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/UnitFactory.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_UNITFACTORY_HPP
7 #define UTILITIES_UNITS_UNITFACTORY_HPP
8
9 #include "../UtilitiesAPI.hpp"
10
11 #include "Unit.hpp"
12
13 #include "../core/Logger.hpp"
14
15 #include <boost/functional.hpp>
16
17 #include <set>
18 #include <map>
19
20 namespace openstudio {
21
22 /** Singleton that creates units based on std::string representation.
23 * Access using UnitFactory::instance(). The class keeps up with standard
24 * unit strings, pseudonyms for those strings, and pretty strings.
25 *
26 * Standard unit strings use base units like kg, m, s, ft, etc. and follow
27 * LaTeX formatting. For example, the standard unit std::string for SI energy is
28 * <pre>kg*m^2/s^2</pre>.
29 *
30 * Also provides a function for looking up pretty units without having to
31 * create a unit. */
32 class UTILITIES_API UnitFactory
33 {
34 public:
35 static UnitFactory& instance();
36
37 UnitFactory(const UnitFactory& other) = delete;
38 UnitFactory(UnitFactory&& other) = delete;
39 UnitFactory& operator=(const UnitFactory&) = delete;
40 UnitFactory& operator=(UnitFactory&&) = delete;
41
42 /** Units are created by functions that accept no arguments and return
43 * something that can be converted into a Unit::Ptr. */
44 using CreateUnitCallback = boost::function<Unit()>;
45
46 // FACTORY MAINTENANCE
47
48 /** Associate a unit creation function with a standard string. Also registers
49 * the pretty string if one exists. Returns false if standardString or prettyString is
50 * already used as a key in one or more internal maps.
51 *
52 * Option to specify a unit system is for conflict resolution. For instance, seconds
53 * is a base unit in SI and IP. For efficiency, only specify system if you expect a
54 * conflict. */
55 bool registerUnit(CreateUnitCallback createFn, UnitSystem system = UnitSystem::Mixed);
56
57 /** Register alternative symbols for standardString. For instance, J is a pseudonym for
58 * kg*m^2/s^2. Returns false if equivalentString is already used as a key in the internal map. */
59 bool registerEquivalentString(const std::string& equivalentString, const std::string& standardString);
60
61 // FACTORY USE
62
63 /** Parses unitStr to create a unit based on the internal factory maps. The maps are populated
64 * using the functions registerUnit and registerEquivalentString. Specify system to alert the
65 * factory of your preferred unit system. The returned pointer is not guaranteed to be from
66 * that system, but if there is a conflict, it will be preferred. */
67 boost::optional<Unit> createUnit(const std::string& unitString, UnitSystem system = UnitSystem::Mixed) const;
68
69 /** Returns the prettyString registered for standardString. Returns the empty std::string for
70 * unregistered standardStrings. */
71 std::string lookupPrettyString(const std::string& standardString) const;
72
73 private:
74 − REGISTER_LOGGER("openstudio.units.UnitFactory");
75 UnitFactory();
76 100 ~UnitFactory() = default;
77
78 using ResultCacheMap = std::map<std::string, boost::optional<Unit>>;
79
80 mutable ResultCacheMap m_resultCacheMap;
81
82 using StandardStringCallbackMap = std::map<std::string, CreateUnitCallback>;
83 using CallbackMapMap = std::map<UnitSystem, StandardStringCallbackMap>;
84
85 CallbackMapMap m_callbackMaps;
86
87 using PrettyStringLookupMap = std::map<std::string, std::string>;
88 using StandardStringLookupMap = std::map<std::string, std::vector<std::string>>;
89
90 StandardStringLookupMap m_standardStringLookupMap; // look up alias, get back standardString
91 PrettyStringLookupMap m_prettyStringLookupMap; // look up standardString, get back prettyString
92
93 // helper create function--does simple lookups
94 boost::optional<Unit> createUnitSimple(const std::string& unitString, UnitSystem system = UnitSystem::Mixed) const;
95 };
96
97 /** Returns the best match UnitSystem for unitString, in priority order SI, IP, ..., Mixed
98 * \relates UnitFactory */
99 UTILITIES_API UnitSystem getSystem(const std::string& unitString);
100
101 /** Returns true if unitString can be created as a unit in system. \relates UnitFactory */
102 UTILITIES_API bool isInSystem(const std::string& unitString, UnitSystem system);
103
104 /** Returns true if any of unitString's baseUnits are registered in the factory.
105 * \relates UnitFactory */
106 UTILITIES_API bool containsRegisteredBaseUnit(const std::string& unitString);
107
108 /** Extracts a unitString from text, if possible. Only returns strings that contain at least one
109 * registered base unit. \relates UnitFactory */
110 UTILITIES_API std::string extractUnitString(const std::string& text);
111
112 /** Converts unitString to standard form. Right now, does conversion like people/100 m^2 -->
113 * c(people/m^2). \relates UnitFactory */
114 UTILITIES_API std::string convertToStandardForm(const std::string& unitString);
115
116 /** Replaces unitString in text, using same finder algorithm as extractUnitString.
117 * \relates UnitFactory */
118 UTILITIES_API std::string replaceUnitString(const std::string& text, const std::string& newUnitString);
119
120 /** Returns true if unitString can be turned into a Unit. \relates UnitFactory */
121 UTILITIES_API bool isUnitString(const std::string& unitString);
122
123 /** Returns a Unit of the type that corresponds to system. \relates UnitFactory */
124 UTILITIES_API Unit createDimensionlessUnit(UnitSystem system);
125
126 /** Creates a Unit corresponding to unitString (using UnitFactory). User may specify preferred
127 * UnitSystem, and can cast the result to the appropriate class, as desired.
128 * \relates UnitFactory */
129 UTILITIES_API boost::optional<Unit> createUnit(const std::string& unitString, UnitSystem system);
130
131 /** Creates a Unit corresponding to unitString (using UnitFactory). Attempts to find the best
132 * UnitSystem for unitString, in priority order SI, IP, ..., Mixed. \relates UnitFactory */
133 UTILITIES_API boost::optional<Unit> createUnit(const std::string& unitString);
134
135 /** Returns true if uStr1 and uStr2 evaluate to the same unit. \relates UnitFactory */
136 UTILITIES_API bool unitStringsEqual(const std::string& uStr1, const std::string& uStr2);
137
138 /** In IP units, power density is often expressed as W/ft^2, which is actually a
139 * mixed unit. \relates UnitFactory */
140 UTILITIES_API Unit createIPPowerDensity();
141
142 /** Make sure crgal^3/min shows up as gal/min. \relates UnitFactory */
143 UTILITIES_API Unit createGPMVolumetricFlowrate();
144
145 /** In IP units, pressure is often expressed as lb_f/in^2, which is actually a mixed
146 * unit. \relates UnitFactory */
147 UTILITIES_API Unit createIPPressure();
148
149 } // namespace openstudio
150
151 #endif // UTILITIES_UNITS_UNITFACTORY_HPP
152