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/data/Attribute_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_DATA_ATTRIBUTE_IMPL_HPP
7 #define UTILITIES_DATA_ATTRIBUTE_IMPL_HPP
8
9 #include "../UtilitiesAPI.hpp"
10 #include "Attribute.hpp"
11
12 #include "../core/Optional.hpp"
13 #include "../core/String.hpp"
14 #include "../core/Logger.hpp"
15 #include "../core/UUID.hpp"
16
17 namespace pugi {
18 class xml_document;
19 class xml_node;
20 } // namespace pugi
21
22 namespace Json {
23 class Value;
24 }
25
26 namespace openstudio {
27 namespace detail {
28
29 class UTILITIES_API Attribute_Impl : public std::enable_shared_from_this<Attribute_Impl>
30 {
31
32 public:
33 // Note JM 2018-12-04: As far as a I can tell, the only Ctor actually uses are the simple ones,
34 // name, value, optional<string> units
35
36 /// constructors
37 Attribute_Impl(const std::string& name, bool value, const boost::optional<std::string>& units);
38 Attribute_Impl(const openstudio::UUID& uuid, const openstudio::UUID& versionUUID, const std::string& name,
39 const boost::optional<std::string>& displayName, bool value, const boost::optional<std::string>& units,
40 const std::string& source = std::string());
41
42 Attribute_Impl(const std::string& name, double value, const boost::optional<std::string>& units);
43 Attribute_Impl(const openstudio::UUID& uuid, const openstudio::UUID& versionUUID, const std::string& name,
44 const boost::optional<std::string>& displayName, double value, const boost::optional<std::string>& units,
45 const std::string& source = std::string());
46
47 Attribute_Impl(const std::string& name, int value, const boost::optional<std::string>& units);
48 Attribute_Impl(const openstudio::UUID& uuid, const openstudio::UUID& versionUUID, const std::string& name,
49 const boost::optional<std::string>& displayName, int value, const boost::optional<std::string>& units,
50 const std::string& source = std::string());
51
52 Attribute_Impl(const std::string& name, unsigned value, const boost::optional<std::string>& units);
53 Attribute_Impl(const openstudio::UUID& uuid, const openstudio::UUID& versionUUID, const std::string& name,
54 const boost::optional<std::string>& displayName, unsigned value, const boost::optional<std::string>& units,
55 const std::string& source = std::string());
56
57 Attribute_Impl(const std::string& name, const std::string& value, const boost::optional<std::string>& units);
58 Attribute_Impl(const openstudio::UUID& uuid, const openstudio::UUID& versionUUID, const std::string& name,
59 const boost::optional<std::string>& displayName, const std::string& value, const boost::optional<std::string>& units,
60 const std::string& source = std::string());
61
62 Attribute_Impl(const std::string& name, const std::vector<openstudio::Attribute>& value, const boost::optional<std::string>& units);
63 Attribute_Impl(const openstudio::UUID& uuid, const openstudio::UUID& versionUUID, const std::string& name,
64 const boost::optional<std::string>& displayName, const std::vector<openstudio::Attribute>& value,
65 const boost::optional<std::string>& units, const std::string& source = std::string());
66
67 // constructor from xml, throws if required arguments are missing
68 Attribute_Impl(const pugi::xml_node& element);
69
70 Attribute_Impl(const Attribute_Impl& other);
71
72 // Destructor
73 6374 virtual ~Attribute_Impl() = default;
74
75 openstudio::UUID uuid() const;
76
77 openstudio::UUID versionUUID() const;
78
79 /// get the name
80 std::string name() const;
81
82 // ETH@20140414 - displayName should return a std::string (with returnName=true behavior),
83 // and the return type of setDisplayName should be void.
84
85 /// get the display name. if returnName and the display name is empty, will return
86 /// name() instead.
87 boost::optional<std::string> displayName(bool returnName = false) const;
88
89 /// set the display name
90 bool setDisplayName(const std::string& displayName);
91
92 /// clear the display name
93 void clearDisplayName();
94
95 /// get the (optional) data source
96 std::string source() const;
97
98 /// set the data source
99 void setSource(const std::string& source);
100
101 /// clear the data source
102 void clearSource();
103
104 /// get the attribute value type
105 AttributeValueType valueType() const;
106
107 // Check if it has a value set
108 bool hasValue() const;
109
110 /// get value as a bool
111 bool valueAsBoolean() const;
112
113 /// set value. throws if wrong type.
114 void setValue(bool value);
115
116 /// get value as int
117 int valueAsInteger() const;
118
119 /// set value. throws if wrong type.
120 void setValue(int value);
121
122 /// get value as unsigned
123 unsigned valueAsUnsigned() const;
124
125 /// set value. throws if wrong type.
126 void setValue(unsigned value);
127
128 /// get value as double
129 double valueAsDouble() const;
130
131 /// set value. throws if wrong type.
132 void setValue(double value);
133
134 /// get value as string
135 std::string valueAsString() const;
136
137 /// set value. throws if wrong type.
138 void setValue(const char* value);
139
140 /// set value. throws if wrong type.
141 void setValue(const std::string& value);
142
143 /// get value as attribute vector
144 std::vector<Attribute> valueAsAttributeVector() const;
145
146 /// set value. throws if wrong type.
147 void setValue(const std::vector<Attribute>& value);
148
149 /// find child attribute by name
150 boost::optional<Attribute> findChildByName(const std::string& name) const;
151
152 /// get the units
153 boost::optional<std::string> units() const;
154
155 /// set the units
156 bool setUnits(const std::string& units);
157
158 /** If attribute is a simple type (non-vector), prints attribute's value to std::string as
159 * simply as possible. Numeric types print in high precision. Attribute vectors are printed
160 * as XML. */
161 std::string toString() const;
162
163 /// write object and all children to xml
164 pugi::xml_document toXml() const;
165
166 // short_version: if false, will include the uuid, versionuuid and source (if any)
167 Json::Value toJSON(bool short_version) const;
168
169 // Just the value as a Json::Value
170 Json::Value valueAsJSON() const;
171
172 /// comparison
173 bool operator==(const Attribute& other) const;
174
175 protected:
176 friend class openstudio::Attribute;
177
178 /// write values to an xml element
179 /// override in derived classes
180 virtual void writeValues(pugi::xml_node& element) const;
181
182 private:
183 − REGISTER_LOGGER("openstudio.Attribute");
184
185 openstudio::UUID m_uuid;
186 openstudio::UUID m_versionUUID;
187 std::string m_name;
188 boost::optional<std::string> m_displayName;
189 std::string m_source;
190 AttributeValueType m_valueType;
191 OSAttributeVariant m_value;
192 boost::optional<std::string> m_units;
193 };
194
195 } // namespace detail
196 } // namespace openstudio
197
198 #endif // UTILITIES_DATA_ATTRIBUTE_IMPL_HPP
199