GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 82.4% 56 / 1 / 69
Functions: 81.8% 18 / 2 / 24
Branches: 81.0% 34 / 1 / 43

src/utilities/idf/IdfObject_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_IDF_IDFOBJECT_IMPL_HPP
7 #define UTILITIES_IDF_IDFOBJECT_IMPL_HPP
8
9 #include <utilities/UtilitiesAPI.hpp>
10 #include <utilities/idf/Handle.hpp>
11 #include <utilities/idf/IdfObjectDiff.hpp>
12 #include <utilities/idd/IddObject.hpp>
13
14 #include <utilities/core/Logger.hpp>
15 #include <utilities/core/Containers.hpp>
16 #include <nano/nano_signal_slot.hpp> // Signal-Slot replacement
17
18 #include <boost/optional.hpp>
19
20 #include <string>
21 #include <ostream>
22 #include <vector>
23
24 namespace openstudio {
25
26 // forward declarations
27 class IdfObject;
28 class IdfExtensibleGroup;
29 struct IdfObjectImplLess;
30 class StrictnessLevel;
31 class ValidityReport;
32 class DataError;
33 class Quantity;
34 class OSOptionalQuantity;
35
36 // private namespace
37 namespace detail {
38
39 /** Implementation of IdfObject. */
40 class UTILITIES_API IdfObject_Impl
41 : public std::enable_shared_from_this<IdfObject_Impl>
42 , public Nano::Observer
43 {
44 public:
45 /** @name Constructors */
46 //@{
47
48 /** Copy constructor, used for cloning. */
49 IdfObject_Impl(const IdfObject_Impl& other, bool keepHandle = false);
50
51 /** Constructor from type. Equivalent to IdfObject(IddFactory::instance.iddObject(type)). */
52 explicit IdfObject_Impl(IddObjectType type, bool fastName = false);
53
54 /** Constructor from iddObject. */
55 explicit IdfObject_Impl(const IddObject& iddObject, bool fastName = false);
56
57 /** Constructor from underlying data. Used by WorkspaceObject_Impl. */
58 IdfObject_Impl(const Handle& handle, const std::string& comment, const IddObject& iddObject, const StringVector& fields,
59 const StringVector& fieldComments);
60
61 4924781 virtual ~IdfObject_Impl() = default;
62
63 //@}
64 /** @name Getters */
65 //@{
66
67 /** Returns the handle associated with the object. */
68 Handle handle() const;
69
70 /** Get this object's IddObject. */
71 IddObject iddObject() const;
72
73 /** Returns the comment block associated with the object. */
74 std::string comment() const;
75
76 /** Returns the comment associated with field index, if one exists. Optionally, if returnDefault
77 * is passed in as true, and no field comment exists for index, fieldComment will return a
78 * comment-ized version of the IddField name, following a commonly used Idf convention. */
79 boost::optional<std::string> fieldComment(unsigned index, bool returnDefault = false) const;
80
81 /** Get the name of the object, if it exists. Optionally, if returnDefault is passed in as true,
82 * name will return the default name, if it exists. */
83 boost::optional<std::string> name(bool returnDefault = false) const;
84
85 /** Returns the name if set or returns an empty string. Optionally, if returnDefault is passed in as true,
86 * name will return the default name, if it exists. */
87 std::string nameString(bool returnDefault = false) const;
88
89 /** Get a brief description of this object. Always includes iddObject().type(), and includes
90 * name() if available and not empty. */
91 std::string briefDescription() const;
92
93 /** Returns true if the field is empty. */
94 bool isEmpty(unsigned index) const;
95
96 /** Get the value of field index, if index < numFields(). Optionally, if returnDefault is
97 * passed in as true, getString will return the default value for non-existent
98 * (non-extensible) fields and fields with empty data, if a default exists.
99 * If the default does not exist or is not requested and the field is empty
100 * then returnUninitializedEmpty is used to determine if an uninitialized result is returned.
101 */
102 virtual boost::optional<std::string> getString(unsigned index, bool returnDefault = false, bool returnUninitializedEmpty = false) const;
103
104 /** Like getString except for reference fields getString will return the name of the referenced object.
105 * This method, getField, will always return the string value of the field.
106 * For IdfObject, this is the same as getString */
107 virtual boost::optional<std::string> getField(unsigned index, bool returnDefault = false) const;
108
109 /** Get the value of the field at index, converted to double, if possible. Returns an
110 * uninitialized object if the conversion is unsuccessful for any reason. Logs a warning
111 * if the conversion fails, the field is RealType, and the field is not equal to
112 * 'autocalculate', 'autosize', or the empty string.
113 *
114 * Optionally, if returnDefault is passed in as true, getDouble will return the default
115 * value for non-existent (non-extensible) fields and fields with empty data, if a
116 * real-valued default exists. */
117 boost::optional<double> getDouble(unsigned index, bool returnDefault = false) const;
118
119 /** Returns the Quantity at index, if possible. Uses markup in IDD to determine the units. If
120 * returnIP == true, uses the IP unit mark-up in the IDD to perform a conversion. */
121 OSOptionalQuantity getQuantity(unsigned index, bool returnDefault = false, bool returnIP = false) const;
122
123 /** Get the value of the field at index, converted to unsigned, if possible. Returns an
124 * uninitialized object if the conversion is unsuccessful for any reason. Logs a warning
125 * if the conversion fails, the field is IntegerType, there is a minBound >= 0, and the
126 * field is not equal to 'autocalculate', 'autosize', or the empty string.
127 *
128 * Optionally, if returnDefault is passed in as true, getUnsigned will return the default
129 * value for non-existent (non-extensible) fields and fields with empty data, if an
130 * unsigned default exists. */
131 boost::optional<unsigned> getUnsigned(unsigned index, bool returnDefault = false) const;
132
133 /** Get value of the field at index, converted to int, if possible. Returns an
134 * uninitialized object if the conversion is unsuccessful for any reason. Logs a warning
135 * if the conversion fails, the field is IntegerType, and the field is not equal to
136 * 'autocalculate', 'autosize', or the empty string.
137 *
138 * Optionally, if returnDefault is passed in as true, getInt will return the default
139 * value for non-existent (non-extensible) fields and fields with empty data, if an
140 * int default exists. */
141 boost::optional<int> getInt(unsigned index, bool returnDefault = false) const;
142
143 /** Returns the IdfExtensibleGroup at groupIndex, if it exists. Otherwise, return value will be
144 * .empty(). */
145 IdfExtensibleGroup getExtensibleGroup(unsigned groupIndex) const;
146
147 /** Returns this object's IdfExtensibleGroups. */
148 std::vector<IdfExtensibleGroup> extensibleGroups() const;
149
150 //@}
151 /** @name Setters */
152 //@{
153
154 /** Sets the comment block associated with the object. Will prepend comment characters as
155 * necessary. */
156 void setComment(const std::string& msg);
157 void setComment(const std::string& msg, bool checkValidity);
158
159 /** Sets the comment associated with field index, if that field exists. Otherwise returns
160 * false. */
161 bool setFieldComment(unsigned index, const std::string& cmnt);
162 bool setFieldComment(unsigned index, const std::string& cmnt, bool checkValidity);
163
164 /** Sets the name field if it exists, returning the actual name string set. Returns false
165 * otherwise. The return value and newName may differ (by an appended integer) if a
166 * conflict with newName was detected. Name conflicts will not be automatically avoided
167 * in IDF (text-only) mode. */
168 virtual boost::optional<std::string> setName(const std::string& newName);
169 virtual boost::optional<std::string> setName(const std::string& newName, bool checkValidity);
170
171 /** Sets the name field to a new, unique name, if the name field exists and (is empty or
172 * overwrite == true). Returns false otherwise. Created name is returned. */
173 virtual boost::optional<std::string> createName();
174 virtual boost::optional<std::string> createName(bool overwrite);
175
176 /** Sets the field at index to value, if possible. Returns false if the value cannot be set for
177 * any reason. (Perhaps index >= numFields().) */
178 virtual bool setString(unsigned index, const std::string& value);
179 virtual bool setString(unsigned index, const std::string& value, bool checkValidity);
180
181 /** Sets the field at index to value, if possible. Returns false if the value cannot be set for
182 * any reason. (Perhaps index >= numFields(), the field is not RealType, or the value is out of
183 * bounds per IddField.properties()). */
184 bool setDouble(unsigned index, double value);
185 bool setDouble(unsigned index, double value, bool checkValidity);
186
187 /** Sets the field at index to q, if possible. Returns false if the units of q cannot be converted
188 * to getUnits(index). */
189 bool setQuantity(unsigned index, const Quantity& q);
190 bool setQuantity(unsigned index, const Quantity& q, bool checkValidity);
191
192 /** Sets the field at index to value, if possible. Returns false if the value cannot be set for
193 * any reasons. (Perhaps index >= numFields(), the field is not IntegerType, or the value is out
194 * of bounds per IddField.properties()). */
195 bool setUnsigned(unsigned index, unsigned value);
196 bool setUnsigned(unsigned index, unsigned value, bool checkValidity);
197
198 /** Sets the field at index to value, if possible. Returns false if the value cannot be set for
199 * any reasons. (Perhaps index >= numFields(), the field is not IntegerType, or the value is out
200 * of bounds per IddField.properties()). */
201 bool setInt(unsigned index, int value);
202 bool setInt(unsigned index, int value, bool checkValidity);
203
204 /** Pushes a new field onto the object, if possible. Returns false if the operation cannot
205 * be completed for any reason. */
206 virtual bool pushString();
207 virtual bool pushString(bool checkValidity);
208 virtual bool pushString(const std::string& value);
209 virtual bool pushString(const std::string& value, bool checkValidity);
210
211 /** Pushes a new extensible group onto the object, if possible. Return value will be empty() if
212 * the operation cannot be completed for any reason. */
213 IdfExtensibleGroup pushExtensibleGroup(const std::vector<std::string>& values);
214 IdfExtensibleGroup pushExtensibleGroup(const std::vector<std::string>& values, bool checkValidity);
215
216 /** Inserts a new extensible group at groupIndex, if possible. Return value will be empty() if
217 * the operation cannot be completed for any reason. */
218 IdfExtensibleGroup insertExtensibleGroup(unsigned groupIndex, const std::vector<std::string>& values);
219 IdfExtensibleGroup insertExtensibleGroup(unsigned groupIndex, const std::vector<std::string>& values, bool checkValidity);
220
221 /** Pops the final extensible group from the object, if possible. Returns the popped data if
222 * successful. Otherwise, the returned vector will be empty. Internally IdfObject might call
223 * this function to rewind an operation that has gone bad half way through a calculation. */
224 virtual std::vector<std::string> popExtensibleGroup();
225 virtual std::vector<std::string> popExtensibleGroup(bool checkValidity);
226
227 /** Erases the extensible group at groupIndex, if possible. Returns the erased data, and
228 * preserves the relative order of the remaining data if successful. Otherwise, the returned
229 * vector will be empty. */
230 std::vector<std::string> eraseExtensibleGroup(unsigned groupIndex);
231 std::vector<std::string> eraseExtensibleGroup(unsigned groupIndex, bool checkValidity);
232
233 /** Pops all \link IdfExtensibleGroup IdfExtensibleGroups\endlink from the object, if possible.
234 * Returns popped data if successful. Otherwise, the returned vector will be empty. */
235 std::vector<std::vector<std::string>> clearExtensibleGroups();
236 std::vector<std::vector<std::string>> clearExtensibleGroups(bool checkValidity);
237
238 // Helper to initialize all fields (if the object currently displays less than numFields), optionally filling the default for a given object
239 void initializeFields(bool fill_default = false);
240
241 //@}
242 /** @name Queries */
243 //@{
244
245 /** Returns the current number of fields in the object. */
246 unsigned numFields() const;
247
248 /** Returns the current number of non-extensible fields in the object. */
249 unsigned numNonextensibleFields() const;
250
251 /** Returns the minimum number of fields an object of this type must have to be valid. */
252 unsigned minFields() const;
253
254 /** Returns the maximum number of fields an object of this type can have and still be valid. */
255 boost::optional<unsigned> maxFields() const;
256
257 /** Returns the number of extensible groups in the object. */
258 unsigned numExtensibleGroups() const;
259
260 /** Returns the minimum number of \link ExtensibleGroup ExtensibleGroups\endlink an object of
261 * this type must have to be valid. */
262 unsigned minExtensibleGroups() const;
263
264 /** Returns the maximum number of \link ExtensibleGroup ExtensibleGroups\endlink an object of
265 * this type must have to be valid. */
266 boost::optional<unsigned> maxExtensibleGroups() const;
267
268 /** Returns true if index is in objectListFields(). */
269 bool isObjectListField(unsigned index) const;
270
271 /** Returns the indices of all fields that can point to other objects. WorkspaceObject manages
272 * all \object-list fields whose reference lists are unambiguously associated with an index-0
273 * name field. */
274 std::vector<unsigned> objectListFields() const;
275
276 /** Returns true if index is in dataFields(). */
277 bool isDataField(unsigned index) const;
278
279 /** Returns the indices of all fields that are not managedObjectListFields(). */
280 std::vector<unsigned> dataFields() const;
281
282 /** Returns the indices of all fields that are required, as defined by the IddObject. */
283 std::vector<unsigned> requiredFields() const;
284
285 /** Returns true if this IdfObject is valid, that is, has no errors at or below level. */
286 bool isValid(StrictnessLevel level) const;
287
288 bool isValid(StrictnessLevel level, bool checkNames) const;
289
290 /** Returns a ValidityReport for this IdfObject containing all errors at or below level. */
291 ValidityReport validityReport(StrictnessLevel level, bool checkNames) const;
292
293 /** Checks for equality of data (non-managedObjectList) fields. Prerequisite: iddObject()s
294 * must be equal. For objects with handle fields, checks for equality of handle OR equality
295 * of name. */
296 bool dataFieldsEqual(const IdfObject& other) const;
297
298 /** Checks for equality of objectListFields(). Prerequisite: iddObject()s must be
299 * equal. */
300 bool objectListFieldsEqual(const IdfObject& other) const;
301
302 /** Returns true if for all objectListFields() that for both objects are non-empty,
303 * the values are equal (using the same criteria as objectListFieldsEqual).
304 * Prerequisite: iddObject()s must be equal. */
305 bool objectListFieldsNonConflicting(const IdfObject& other) const;
306
307 //@}
308 /** @name Serialization */
309 //@{
310
311 /** Constructor from text. Parses text and queries the IddFactory for its IddObject. May create
312 * an invalid object. (May even be invalid at enums::Strictness level None.) */
313 static std::shared_ptr<IdfObject_Impl> load(const std::string& text);
314
315 /** Constructor from text and an explicit iddObject. May create an invalid object. (May even
316 * be invalid at enums::Strictness level None.) */
317 static std::shared_ptr<IdfObject_Impl> load(const std::string& text, const IddObject& iddObject);
318
319 /** Serialize this object to os as Idf text. */
320 std::ostream& print(std::ostream& os) const;
321
322 /** Serialize just the preceding comments and name of this IdfObject in the format used by
323 * full object print. If hasFields, the name is followed by a ','. Otherwise, the name is
324 * followed by a ';'. */
325 std::ostream& printName(std::ostream& os, bool hasFields = true) const;
326
327 /** Serialize field index in the format used by full object print. If not isLastField, the
328 * field value is followed by a ','. Otherwise, the object is ended by using a ';'. */
329 std::ostream& printField(std::ostream& os, unsigned index, bool isLastField = false) const;
330
331 //@}
332 /** @name Type Casting */
333 //@{
334
335 /** Get an object that wraps this impl. */
336 template <typename T>
337 6857909 T getObject() const {
338
32/40
openstudio::IdfObject openstudio::detail::IdfObject_Impl::getObject<openstudio::IdfObject>() const:
✓ Branch 1 taken 993077 times.
✓ Branch 6 taken 993077 times.
openstudio::WorkspaceObject openstudio::detail::IdfObject_Impl::getObject<openstudio::WorkspaceObject>() const:
✓ Branch 1 taken 87718 times.
✓ Branch 6 taken 87718 times.
openstudio::model::AdditionalProperties openstudio::detail::IdfObject_Impl::getObject<openstudio::model::AdditionalProperties>() const:
✓ Branch 1 taken 4 times.
✓ Branch 6 taken 4 times.
openstudio::model::BoilerHotWater openstudio::detail::IdfObject_Impl::getObject<openstudio::model::BoilerHotWater>() const:
✓ Branch 1 taken 26 times.
✓ Branch 6 taken 26 times.
openstudio::model::Construction openstudio::detail::IdfObject_Impl::getObject<openstudio::model::Construction>() const:
✗ Branch 1 not taken.
✗ Branch 6 not taken.
openstudio::model::Curve openstudio::detail::IdfObject_Impl::getObject<openstudio::model::Curve>() const:
✓ Branch 1 taken 269 times.
✓ Branch 6 taken 269 times.
openstudio::model::ExternalFile openstudio::detail::IdfObject_Impl::getObject<openstudio::model::ExternalFile>() const:
✓ Branch 1 taken 41 times.
✓ Branch 6 taken 41 times.
openstudio::model::HVACComponent openstudio::detail::IdfObject_Impl::getObject<openstudio::model::HVACComponent>() const:
✓ Branch 1 taken 300 times.
✓ Branch 6 taken 300 times.
openstudio::model::Material openstudio::detail::IdfObject_Impl::getObject<openstudio::model::Material>() const:
✗ Branch 1 not taken.
✗ Branch 6 not taken.
openstudio::model::ModelObject openstudio::detail::IdfObject_Impl::getObject<openstudio::model::ModelObject>() const:
✗ Branch 1 not taken.
✗ Branch 6 not taken.
openstudio::model::OtherEquipment openstudio::detail::IdfObject_Impl::getObject<openstudio::model::OtherEquipment>() const:
✓ Branch 1 taken 4 times.
✓ Branch 6 taken 4 times.
openstudio::model::ParentObject openstudio::detail::IdfObject_Impl::getObject<openstudio::model::ParentObject>() const:
✓ Branch 1 taken 1837 times.
✓ Branch 6 taken 1837 times.
openstudio::model::RefrigerationCondenserCascade openstudio::detail::IdfObject_Impl::getObject<openstudio::model::RefrigerationCondenserCascade>() const:
✓ Branch 1 taken 36 times.
✓ Branch 6 taken 36 times.
openstudio::model::Schedule openstudio::detail::IdfObject_Impl::getObject<openstudio::model::Schedule>() const:
✓ Branch 1 taken 12992 times.
✓ Branch 6 taken 12992 times.
openstudio::model::ScheduleBase openstudio::detail::IdfObject_Impl::getObject<openstudio::model::ScheduleBase>() const:
✗ Branch 1 not taken.
✗ Branch 6 not taken.
openstudio::model::ScheduleRule openstudio::detail::IdfObject_Impl::getObject<openstudio::model::ScheduleRule>() const:
✓ Branch 1 taken 19614 times.
✓ Branch 6 taken 19614 times.
openstudio::model::StandardsInformationConstruction openstudio::detail::IdfObject_Impl::getObject<openstudio::model::StandardsInformationConstruction>() const:
✓ Branch 1 taken 370 times.
✓ Branch 6 taken 370 times.
openstudio::model::StandardsInformationMaterial openstudio::detail::IdfObject_Impl::getObject<openstudio::model::StandardsInformationMaterial>() const:
✓ Branch 1 taken 3 times.
✓ Branch 6 taken 3 times.
openstudio::model::SteamEquipment openstudio::detail::IdfObject_Impl::getObject<openstudio::model::SteamEquipment>() const:
✓ Branch 1 taken 4 times.
✓ Branch 6 taken 4 times.
openstudio::model::TableMultiVariableLookup openstudio::detail::IdfObject_Impl::getObject<openstudio::model::TableMultiVariableLookup>() const:
✓ Branch 1 taken 410 times.
✓ Branch 6 taken 410 times.
6857909 T result(std::dynamic_pointer_cast<typename T::ImplType>(std::const_pointer_cast<IdfObject_Impl>(shared_from_this())));
339 6857909 return result;
340 }
341
342 //@}
343 /** @name Signal Helpers */
344 //@{
345
346 /** Emits signals after batch update and error checking is complete, clears the diffs */
347 virtual void emitChangeSignals();
348
349 //@}
350
351 //@}
352 /** @name Nano Signals */
353 //@{
354
355 // Emitted on any change--any field, any comment.
356 Nano::Signal<void()> onChange;
357
358 // Emitted if name field changed.
359 Nano::Signal<void()> onNameChange;
360
361 // Emitted when any dataFields() are changed (WorkspaceObject and higher), or when any field
362 // data is changed in Idf mode.
363 Nano::Signal<void()> onDataChange;
364
365 protected:
366 friend class openstudio::IdfObject;
367
368 // handle
369 Handle m_handle;
370
371 // user comment
372 std::string m_comment;
373
374 // idd object definition
375 IddObject m_iddObject;
376
377 // idf fields
378 std::vector<std::string> m_fields;
379 std::vector<std::string> m_fieldComments; // only populated if encounter non-empty, non-default comment
380
381 // idf differences
382 std::vector<IdfObjectDiff> m_diffs;
383
384 // GETTER HELPERS
385
386 std::vector<std::string> fields() const;
387
388 std::vector<std::string> fieldComments() const;
389
390 virtual OSOptionalQuantity getQuantityFromDouble(unsigned index, boost::optional<double> value, bool returnIP) const;
391
392 virtual boost::optional<double> getDoubleFromQuantity(unsigned index, const Quantity& q) const;
393
394 // QUERY HELPERS
395
396 virtual void populateValidityReport(ValidityReport& report, bool checkNames) const;
397
398 virtual std::vector<DataError> fieldDataIsValid(unsigned index, const StrictnessLevel& level) const;
399
400 /** Checks Integer, Real and Choice fields. */
401 virtual bool fieldDataIsCorrectType(unsigned index) const;
402
403 virtual bool fieldIsNonnullIfRequired(unsigned index) const;
404
405 private:
406
2/2
✓ Branch 6 taken 302 times.
✓ Branch 10 taken 302 times.
302 IdfObject_Impl() = default;
407
408 // CONSTRUCTION HELPERS
409
410 /** Minimal constructor from iddObject for use by IdfObject_Impl::load. */
411 IdfObject_Impl(const IddObject& iddObject, bool fastName, bool minimal);
412
413 // Resizes m_fields to m_iddObject.numFieldsInDefaultObject(). Called to make sure that
414 // IdfObject satisfies Strictness::None.
415 void resizeToMinFields();
416
417 /* Parse IdfObject text. If getIddFromFactory, will first search for the IddObject using the
418 * IddFactory, otherwise, assumes that m_iddObject was provided and is correct. (Will log
419 * warning if the names do not match.) */
420 void parse(const std::string& text, bool getIddFromFactory);
421
422 // parse fields
423 void parseFields(const std::string& text);
424
425 // GETTER AND SETTER HELPERS
426
427 /** Set this object's IddObject to iddObject. */
428 bool setIddObject(const IddObject& iddObject);
429
430 // remove any indices that are outside m_fields' range
431 UnsignedVector trimFieldIndices(const UnsignedVector& indices) const;
432
433 // repeat indices as many times as necessary to fill out extensible groups in m_fields
434 UnsignedVector repeatExtensibleIndices(const UnsignedVector& indices) const;
435
436 // QUERY HELPERS
437
438 bool fieldDataIsWithinBounds(unsigned index) const;
439
440 /** Check fieldValue against bounds in iddField. */
441 bool withinBounds(double fieldValue, const IddField& iddField) const;
442
443 // convert a user string to one that can be written to file
444 std::string encodeString(const std::string& value) const;
445
446 // convert a string in file to one the use sees
447 std::string decodeString(const std::string& string) const;
448
449 // configure logging
450 − REGISTER_LOGGER("utilities.idf.IdfObject");
451 };
452
453 using IdfObject_ImplPtr = std::shared_ptr<IdfObject_Impl>;
454
455 using IdfObject_ImplPtrVector = std::vector<IdfObject_ImplPtr>;
456
457 } // namespace detail
458
459 } // namespace openstudio
460
461 #endif // UTILITIES_IDF_IDFOBJECT_IMPL_HPP
462