GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 48.3% 380 / 0 / 786
Functions: 67.0% 67 / 0 / 100
Branches: 25.4% 357 / 0 / 1408

src/measure/OSArgument.cpp
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 #include "OSArgument.hpp"
7
8 #include "../utilities/idf/WorkspaceObject.hpp"
9
10 #include "../utilities/idd/IddObject.hpp"
11
12 #include "../utilities/core/Assert.hpp"
13 #include "../utilities/core/Containers.hpp"
14 #include "../utilities/core/Compare.hpp"
15
16 #include <boost/lexical_cast.hpp>
17 #include <boost/functional/value_factory.hpp>
18 #include <json/json.h>
19
20 #include <json/value.h>
21 #include <limits>
22 #include <sstream>
23
24 namespace openstudio {
25 namespace measure {
26
27 111 OSArgument OSArgument::clone() const {
28 111 OSArgument result(*this);
29
1/1
✓ Branch 1 taken 111 times.
111 result.m_uuid = createUUID();
30
1/1
✓ Branch 1 taken 111 times.
111 result.m_versionUUID = createUUID();
31 111 return result;
32 ✗ }
33
34 77 OSArgument OSArgument::makeBoolArgument(const std::string& name, bool required, bool modelDependent) {
35
2/2
✓ Branch 1 taken 77 times.
✓ Branch 4 taken 77 times.
77 OSArgument result(name, OSArgumentType::Boolean, required, modelDependent);
36
2/2
✓ Branch 1 taken 77 times.
✓ Branch 4 taken 77 times.
77 result.setDomainType(OSDomainType::Enumeration);
37
1/1
✓ Branch 1 taken 77 times.
77 result.m_choices.emplace_back("true");
38
1/1
✓ Branch 1 taken 77 times.
77 result.m_choices.emplace_back("false");
39 77 return result;
40 ✗ }
41
42 70 OSArgument OSArgument::makeDoubleArgument(const std::string& name, bool required, bool modelDependent) {
43
2/2
✓ Branch 1 taken 70 times.
✓ Branch 4 taken 70 times.
70 OSArgument result(name, OSArgumentType::Double, required, modelDependent);
44
2/2
✓ Branch 1 taken 70 times.
✓ Branch 4 taken 70 times.
70 result.setDomainType(OSDomainType::Interval);
45 70 return result;
46 ✗ }
47
48 15 OSArgument OSArgument::makeIntegerArgument(const std::string& name, bool required, bool modelDependent) {
49
2/2
✓ Branch 1 taken 15 times.
✓ Branch 4 taken 15 times.
15 OSArgument result(name, OSArgumentType::Integer, required, modelDependent);
50
2/2
✓ Branch 1 taken 15 times.
✓ Branch 4 taken 15 times.
15 result.setDomainType(OSDomainType::Interval);
51 15 return result;
52 ✗ }
53
54 20 OSArgument OSArgument::makeStringArgument(const std::string& name, bool required, bool modelDependent) {
55
2/2
✓ Branch 1 taken 20 times.
✓ Branch 4 taken 20 times.
20 OSArgument result(name, OSArgumentType::String, required, modelDependent);
56
2/2
✓ Branch 1 taken 20 times.
✓ Branch 4 taken 20 times.
20 result.setDomainType(OSDomainType::Enumeration);
57 20 return result;
58 ✗ }
59
60 7 OSArgument OSArgument::makeChoiceArgument(const std::string& name, const std::vector<std::string>& choices, bool required, bool modelDependent) {
61 7 return makeChoiceArgument(name, choices, choices, required, modelDependent);
62 }
63
64 15 OSArgument OSArgument::makeChoiceArgument(const std::string& name, const std::vector<std::string>& choices,
65 const std::vector<std::string>& displayNames, bool required, bool modelDependent) {
66
2/2
✓ Branch 1 taken 15 times.
✓ Branch 4 taken 15 times.
15 OSArgument result(name, OSArgumentType::Choice, required, modelDependent);
67
2/2
✓ Branch 1 taken 15 times.
✓ Branch 4 taken 15 times.
15 result.setDomainType(OSDomainType::Enumeration);
68
1/1
✓ Branch 1 taken 15 times.
15 result.m_choices = choices;
69
1/1
✓ Branch 1 taken 15 times.
15 result.m_choiceDisplayNames = displayNames;
70 15 return result;
71 ✗ }
72
73 4 OSArgument OSArgument::makeChoiceArgument(const std::string& name, const std::map<std::string, std::string>& choices_to_display_values_map,
74 bool required, bool modelDependent) {
75
2/2
✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
4 OSArgument result(name, OSArgumentType::Choice, required, modelDependent);
76
2/2
✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
4 result.setDomainType(OSDomainType::Enumeration);
77 4 const size_t size = choices_to_display_values_map.size();
78
1/1
✓ Branch 1 taken 4 times.
4 result.m_choices.reserve(size);
79
1/1
✓ Branch 1 taken 4 times.
4 result.m_choiceDisplayNames.reserve(size);
80
2/2
✓ Branch 7 taken 8 times.
✓ Branch 8 taken 4 times.
12 for (const auto& [k, v] : choices_to_display_values_map) {
81
1/1
✓ Branch 1 taken 8 times.
8 result.m_choices.push_back(k);
82
1/1
✓ Branch 1 taken 8 times.
8 result.m_choiceDisplayNames.push_back(v);
83 }
84 4 return result;
85 ✗ }
86
87 ✗ OSArgument OSArgument::makePathArgument(const std::string& name, bool isRead, const std::string& extension, bool required, bool modelDependent) {
88 ✗ OSArgument result(name, OSArgumentType::Path, required, modelDependent);
89 ✗ result.setDomainType(OSDomainType::Enumeration);
90 ✗ result.m_isRead = isRead;
91 ✗ result.m_extension = extension;
92 ✗ return result;
93 ✗ }
94
95 3 OSArgument OSArgument::makeSeparatorArgument(const std::string& name, bool modelDependent) {
96
2/2
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
3 OSArgument result(name, OSArgumentType::Separator, false, modelDependent);
97 3 return result;
98 }
99
100 //openstudio::UUID OSArgument::uuid() const {
101 // return m_uuid;
102 //}
103
104 //openstudio::UUID OSArgument::versionUUID() const {
105 // return m_versionUUID;
106 //}
107
108 582 std::string OSArgument::name() const {
109 582 return m_name;
110 }
111
112 196 std::string OSArgument::displayName() const {
113 196 return m_displayName;
114 }
115
116 ✗ boost::optional<std::string> OSArgument::description() const {
117 ✗ return m_description;
118 }
119
120 915 OSArgumentType OSArgument::type() const {
121 915 return m_type;
122 }
123
124 ✗ boost::optional<std::string> OSArgument::units() const {
125 ✗ return m_units;
126 }
127
128 191 bool OSArgument::required() const {
129 191 return m_required;
130 }
131
132 1 bool OSArgument::modelDependent() const {
133 1 return m_modelDependent;
134 }
135
136 702 bool OSArgument::hasValue() const {
137 // If index is 0, then its std::monostate (=empty)
138 702 return (m_value.index() != 0);
139 }
140
141 30 bool OSArgument::valueAsBool() const {
142
2/2
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 29 times.
30 if (!hasValue()) {
143
13/13
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
✓ Branch 10 taken 1 time.
✓ Branch 13 taken 1 time.
✓ Branch 19 taken 1 time.
✓ Branch 22 taken 1 time.
✓ Branch 25 taken 1 time.
✓ Branch 28 taken 1 time.
✓ Branch 31 taken 1 time.
✓ Branch 34 taken 1 time.
✓ Branch 38 taken 1 time.
✓ Branch 41 taken 1 time.
1 LOG_AND_THROW("This argument does not have a value set.")
144 }
145
3/4
✓ Branch 1 taken 29 times.
✓ Branch 4 taken 29 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 29 times.
29 if (type() != OSArgumentType::Boolean) {
146 ✗ LOG_AND_THROW("This argument is of type " << type().valueName() << ", not of type Bool.");
147 }
148
149 29 return std::get<bool>(m_value);
150 }
151
152 48 double OSArgument::valueAsDouble() const {
153
2/2
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 47 times.
48 if (!hasValue()) {
154
13/13
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
✓ Branch 10 taken 1 time.
✓ Branch 13 taken 1 time.
✓ Branch 19 taken 1 time.
✓ Branch 22 taken 1 time.
✓ Branch 25 taken 1 time.
✓ Branch 28 taken 1 time.
✓ Branch 31 taken 1 time.
✓ Branch 34 taken 1 time.
✓ Branch 38 taken 1 time.
✓ Branch 41 taken 1 time.
1 LOG_AND_THROW("This argument does not have a value set.")
155 }
156
4/10
✓ Branch 1 taken 47 times.
✓ Branch 4 taken 47 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 47 times.
✗ Branch 10 not taken.
✗ Branch 13 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✓ Branch 19 taken 47 times.
47 if ((type() != OSArgumentType::Double) && (type() != OSArgumentType::Integer)) {
157 ✗ LOG_AND_THROW("This argument is of type " << type().valueName() << ", not of type Double.");
158 }
159
160 47 double result = 0.0;
161
3/4
✓ Branch 1 taken 47 times.
✓ Branch 4 taken 47 times.
✓ Branch 7 taken 47 times.
✗ Branch 8 not taken.
47 if (type() == OSArgumentType::Double) {
162 47 result = std::get<double>(m_value);
163 } else {
164 ✗ result = std::get<int>(m_value);
165 ✗ LOG(Warn, "This argument is of type 'Integer' but returning as a Double as requested. You should consider using valueAsInteger instead");
166 }
167
168 47 return result;
169 }
170
171 10 int OSArgument::valueAsInteger() const {
172
2/2
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 9 times.
10 if (!hasValue()) {
173
13/13
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
✓ Branch 10 taken 1 time.
✓ Branch 13 taken 1 time.
✓ Branch 19 taken 1 time.
✓ Branch 22 taken 1 time.
✓ Branch 25 taken 1 time.
✓ Branch 28 taken 1 time.
✓ Branch 31 taken 1 time.
✓ Branch 34 taken 1 time.
✓ Branch 38 taken 1 time.
✓ Branch 41 taken 1 time.
1 LOG_AND_THROW("This argument does not have a value set.")
174 }
175
3/4
✓ Branch 1 taken 9 times.
✓ Branch 4 taken 9 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 9 times.
9 if (type() != OSArgumentType::Integer) {
176 ✗ LOG_AND_THROW("This argument is of type " << type().valueName() << ", not of type Integer.");
177 }
178
179 9 return std::get<int>(m_value);
180 }
181
182 35 std::string OSArgument::valueAsString() const {
183
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 33 times.
35 if (!hasValue()) {
184
19/19
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
✓ Branch 10 taken 2 times.
✓ Branch 13 taken 2 times.
✓ Branch 17 taken 2 times.
✓ Branch 20 taken 2 times.
✓ Branch 23 taken 2 times.
✓ Branch 29 taken 2 times.
✓ Branch 32 taken 2 times.
✓ Branch 35 taken 2 times.
✓ Branch 38 taken 2 times.
✓ Branch 41 taken 2 times.
✓ Branch 44 taken 2 times.
✓ Branch 47 taken 2 times.
✓ Branch 50 taken 2 times.
✓ Branch 53 taken 2 times.
✓ Branch 58 taken 2 times.
✓ Branch 61 taken 2 times.
2 LOG_AND_THROW("Argument " << name() << " has no value.");
185 }
186
187 33 return printOSArgumentVariant(m_value);
188 }
189
190 ✗ openstudio::path OSArgument::valueAsPath() const {
191 ✗ if (!hasValue()) {
192 ✗ LOG_AND_THROW("This argument does not have a value set.")
193 }
194 ✗ if (type() != OSArgumentType::Path) {
195 ✗ LOG_AND_THROW("This argument is of type " << type().valueName() << ", not of type Path.");
196 }
197
198 ✗ return std::get<openstudio::path>(m_value);
199 }
200
201 49 Json::Value OSArgument::valueAsJSON() const {
202 49 return argumentVariantToJSONValue(m_value);
203 }
204
205 878 bool OSArgument::hasDefaultValue() const {
206 878 return (m_defaultValue.index() != 0);
207 }
208
209 70 bool OSArgument::defaultValueAsBool() const {
210
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 70 times.
70 if (!hasDefaultValue()) {
211 ✗ LOG_AND_THROW("This argument does not have a default value set.")
212 }
213
3/4
✓ Branch 1 taken 70 times.
✓ Branch 4 taken 70 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 70 times.
70 if (type() != OSArgumentType::Boolean) {
214 ✗ LOG_AND_THROW("This argument is of type " << type().valueName() << ", not of type Bool.");
215 }
216
217 70 return std::get<bool>(m_defaultValue);
218 }
219
220 54 double OSArgument::defaultValueAsDouble() const {
221
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 54 times.
54 if (!hasDefaultValue()) {
222 ✗ LOG_AND_THROW("This argument does not have a default value set.")
223 }
224
3/4
✓ Branch 1 taken 54 times.
✓ Branch 4 taken 54 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 54 times.
54 if (type() != OSArgumentType::Double) {
225 ✗ LOG_AND_THROW("This argument is of type " << type().valueName() << ", not of type Double.");
226 }
227
228 54 return std::get<double>(m_defaultValue);
229 }
230
231 ✗ int OSArgument::defaultValueAsInteger() const {
232 ✗ if (!hasDefaultValue()) {
233 ✗ LOG_AND_THROW("This argument does not have a default value set.")
234 }
235 ✗ if (type() != OSArgumentType::Integer) {
236 ✗ LOG_AND_THROW("This argument is of type " << type().valueName() << ", not of type Integer.");
237 }
238
239 ✗ return std::get<int>(m_defaultValue);
240 }
241
242 4 std::string OSArgument::defaultValueAsString() const {
243
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 if (!hasDefaultValue()) {
244 ✗ LOG_AND_THROW("Argument " << name() << " does not have a default value.");
245 }
246
247 4 return printOSArgumentVariant(m_defaultValue);
248 }
249
250 ✗ openstudio::path OSArgument::defaultValueAsPath() const {
251 ✗ if (!hasDefaultValue()) {
252 ✗ LOG_AND_THROW("This argument does not have a default value set.")
253 }
254 ✗ if (type() != OSArgumentType::Path) {
255 ✗ LOG_AND_THROW("This argument is of type " << type().valueName() << ", not of type Path.");
256 }
257
258 ✗ return std::get<openstudio::path>(m_defaultValue);
259 }
260
261 ✗ Json::Value OSArgument::defaultValueAsJSON() const {
262 ✗ return argumentVariantToJSONValue(m_defaultValue);
263 }
264
265 561 bool OSArgument::hasDomain() const {
266 561 return !m_domain.empty();
267 }
268
269 201 OSDomainType OSArgument::domainType() const {
270 201 return m_domainType;
271 }
272
273 ✗ std::vector<bool> OSArgument::domainAsBool() const {
274 ✗ if (!hasDomain()) {
275 ✗ LOG_AND_THROW("No domain set for OSArgument '" << name() << "'.");
276 }
277 ✗ if (type() != OSArgumentType::Boolean) {
278 ✗ LOG_AND_THROW("This argument is of type " << type().valueName() << ", not of type Bool.");
279 }
280
281 ✗ std::vector<bool> result;
282 ✗ result.reserve(m_domain.size());
283 ✗ for (const OSArgumentVariant& value : m_domain) {
284 ✗ result.push_back(std::get<bool>(value));
285 }
286 ✗ return result;
287 ✗ }
288
289 20 std::vector<double> OSArgument::domainAsDouble() const {
290
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
20 if (!hasDomain()) {
291 ✗ LOG_AND_THROW("No domain set for OSArgument '" << name() << "'.");
292 }
293
4/10
✓ Branch 1 taken 20 times.
✓ Branch 4 taken 20 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 20 times.
✗ Branch 10 not taken.
✗ Branch 13 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✓ Branch 19 taken 20 times.
20 if ((type() != OSArgumentType::Double) && (type() != OSArgumentType::Integer)) {
294 ✗ LOG_AND_THROW("This argument is of type " << type().valueName() << ", not of type Double.");
295 }
296
297 20 std::vector<double> result;
298
299
3/4
✓ Branch 1 taken 20 times.
✓ Branch 4 taken 20 times.
✓ Branch 7 taken 20 times.
✗ Branch 8 not taken.
20 if (type() == OSArgumentType::Double) {
300
2/2
✓ Branch 5 taken 40 times.
✓ Branch 6 taken 20 times.
60 for (const OSArgumentVariant& value : m_domain) {
301
2/2
✓ Branch 1 taken 40 times.
✓ Branch 4 taken 40 times.
40 result.push_back(std::get<double>(value));
302 }
303 } else {
304 // It's an int
305 ✗ LOG(Warn, "This argument is of type 'Integer' but returning Domain as a Double as requested. "
306 "You should consider using domainAsInteger instead");
307 ✗ for (const OSArgumentVariant& value : m_domain) {
308 ✗ result.push_back(std::get<int>(value));
309 }
310 }
311 20 return result;
312 ✗ }
313
314 19 std::vector<int> OSArgument::domainAsInteger() const {
315
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
19 if (!hasDomain()) {
316 ✗ LOG_AND_THROW("No domain set for OSArgument '" << name() << "'.");
317 }
318
3/4
✓ Branch 1 taken 19 times.
✓ Branch 4 taken 19 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 19 times.
19 if (type() != OSArgumentType::Integer) {
319 ✗ LOG_AND_THROW("This argument is of type " << type().valueName() << ", not of type Integer.");
320 }
321
322 19 std::vector<int> result;
323
1/1
✓ Branch 2 taken 19 times.
19 result.reserve(m_domain.size());
324
2/2
✓ Branch 5 taken 38 times.
✓ Branch 6 taken 19 times.
57 for (const OSArgumentVariant& value : m_domain) {
325
2/2
✓ Branch 1 taken 38 times.
✓ Branch 4 taken 38 times.
38 result.push_back(std::get<int>(value));
326 }
327 19 return result;
328 ✗ }
329
330 ✗ std::vector<openstudio::path> OSArgument::domainAsPath() const {
331 ✗ if (!hasDomain()) {
332 ✗ LOG_AND_THROW("No domain set for OSArgument '" << name() << "'.");
333 }
334 ✗ if (type() != OSArgumentType::Path) {
335 ✗ LOG_AND_THROW("This argument is of type " << type().valueName() << ", not of type Path.");
336 }
337
338 ✗ std::vector<openstudio::path> result;
339 ✗ result.reserve(m_domain.size());
340 ✗ for (const OSArgumentVariant& value : m_domain) {
341 ✗ result.push_back(std::get<openstudio::path>(value));
342 }
343 ✗ return result;
344 ✗ }
345
346 ✗ std::vector<std::string> OSArgument::domainAsString() const {
347 ✗ if (!hasDomain()) {
348 ✗ LOG_AND_THROW("No domain set for OSArgument '" << name() << "'.");
349 }
350
351 // TODO: add check for arg type?
352 ✗ std::vector<std::string> result;
353 ✗ result.reserve(m_domain.size());
354 ✗ for (const OSArgumentVariant& value : m_domain) {
355 ✗ result.push_back(printOSArgumentVariant(value));
356 }
357 ✗ return result;
358 ✗ }
359
360 ✗ Json::Value OSArgument::domainAsJSON() const {
361 ✗ if (!hasDomain()) {
362 ✗ return Json::nullValue;
363 }
364
365 ✗ Json::Value root(Json::arrayValue);
366 ✗ for (const OSArgumentVariant& value : m_domain) {
367 ✗ root.append(argumentVariantToJSONValue(value));
368 }
369
370 ✗ return root;
371 ✗ }
372
373 19 std::vector<std::string> OSArgument::choiceValues() const {
374 19 return m_choices;
375 }
376
377 3 std::vector<std::string> OSArgument::choiceValueDisplayNames() const {
378 3 return m_choiceDisplayNames;
379 }
380
381 ✗ std::string OSArgument::valueDisplayName() const {
382 ✗ std::string valueString = valueAsString();
383 ✗ const int index = int(std::find(m_choices.begin(), m_choices.end(), valueString) - m_choices.begin());
384 ✗ if (index < int(m_choiceDisplayNames.size())) {
385 ✗ return m_choiceDisplayNames[index];
386 }
387 ✗ return valueString;
388 ✗ }
389
390 ✗ std::string OSArgument::defaultValueDisplayName() const {
391 ✗ std::string defaultValueString = defaultValueAsString();
392 ✗ const int index = int(std::find(m_choices.begin(), m_choices.end(), defaultValueString) - m_choices.begin());
393 ✗ if (index < int(m_choiceDisplayNames.size())) {
394 ✗ return m_choiceDisplayNames[index];
395 }
396 ✗ return defaultValueString;
397 ✗ }
398
399 ✗ bool OSArgument::isRead() const {
400 ✗ return m_isRead;
401 }
402
403 ✗ std::string OSArgument::extension() const {
404 ✗ return m_extension;
405 }
406
407 120 void OSArgument::setDisplayName(const std::string& displayName) {
408 120 m_displayName = displayName;
409 120 onChange();
410 120 }
411
412 76 void OSArgument::setDescription(const std::string& description) {
413 76 m_description = description;
414 76 onChange();
415 76 }
416
417 ✗ void OSArgument::setUnits(const std::string& units) {
418 ✗ m_units = units;
419 ✗ onChange();
420 ✗ }
421
422 30 bool OSArgument::setValue(bool value) {
423 30 bool result = false;
424
2/3
✓ Branch 1 taken 30 times.
✓ Branch 4 taken 30 times.
✗ Branch 5 not taken.
30 if (m_type == OSArgumentType::Boolean) {
425 30 m_value = value;
426
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
30 OS_ASSERT(hasValue());
427 30 onChange();
428 30 result = true;
429 }
430 30 return result;
431 }
432
433 29 bool OSArgument::setValue(double value) {
434 29 bool result = false;
435
3/3
✓ Branch 1 taken 29 times.
✓ Branch 4 taken 27 times.
✓ Branch 5 taken 2 times.
29 if (m_type == OSArgumentType::Double) {
436 27 m_value = value;
437
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
27 OS_ASSERT(hasValue());
438 27 onChange();
439 27 result = true;
440
441
2/3
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
2 } else if (m_type == OSArgumentType::Integer) {
442 // Let a double be assigned to an int, only if said double is really an integer expressed as a double
443 2 const int test = (int)std::floor(value);
444
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (test == value) {
445 2 result = setValue(test);
446 } else {
447 ✗ result = false;
448 }
449 }
450
451 29 return result;
452 }
453
454 26 bool OSArgument::setValue(int value) {
455 26 bool result = false;
456
3/3
✓ Branch 1 taken 26 times.
✓ Branch 4 taken 15 times.
✓ Branch 5 taken 11 times.
26 if (m_type == OSArgumentType::Integer) {
457 15 m_value = value;
458 15 result = true;
459
460
2/3
✓ Branch 1 taken 11 times.
✓ Branch 4 taken 11 times.
✗ Branch 5 not taken.
11 } else if (m_type == OSArgumentType::Double) {
461 // If we pass an int, but our type is double, we let it happen
462 11 m_value.emplace<double>(value);
463 11 result = true;
464 }
465
466
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 if (result) {
467
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 26 times.
26 OS_ASSERT(hasValue());
468 26 onChange();
469 }
470
471 26 return result;
472 }
473
474 ✗ bool OSArgument::setValue(const openstudio::path& value) {
475 ✗ bool result = false;
476 ✗ if (m_type == OSArgumentType::Path) {
477 ✗ m_value = value;
478 ✗ OS_ASSERT(hasValue());
479 ✗ onChange();
480 ✗ result = true;
481 }
482 ✗ return result;
483 }
484
485 4 bool OSArgument::setValue(const char* value) {
486
2/2
✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
8 return setValue(std::string(value));
487 }
488
489 22 bool OSArgument::setValue(const std::string& value) {
490 22 const bool result = setStringInternal(m_value, value);
491
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 if (result) {
492
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 22 times.
22 OS_ASSERT(hasValue());
493 22 onChange();
494 }
495 22 return result;
496 }
497
498 5 void OSArgument::clearValue() {
499 5 m_value = OSArgumentVariant();
500
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
5 OS_ASSERT(!hasValue());
501 5 onChange();
502 5 }
503
504 46 bool OSArgument::setDefaultValue(bool defaultValue) {
505 46 bool result = false;
506
2/3
✓ Branch 1 taken 46 times.
✓ Branch 4 taken 46 times.
✗ Branch 5 not taken.
46 if (m_type == OSArgumentType::Boolean) {
507 46 m_defaultValue = defaultValue;
508
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 46 times.
46 OS_ASSERT(hasDefaultValue());
509 46 onChange();
510 46 result = true;
511 }
512 46 return result;
513 }
514
515 37 bool OSArgument::setDefaultValue(double defaultValue) {
516 37 bool result = false;
517
2/3
✓ Branch 1 taken 37 times.
✓ Branch 4 taken 37 times.
✗ Branch 5 not taken.
37 if (m_type == OSArgumentType::Double) {
518 37 m_defaultValue = defaultValue;
519
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 37 times.
37 OS_ASSERT(hasDefaultValue());
520 37 onChange();
521 37 result = true;
522 ✗ } else if (m_type == OSArgumentType::Integer) {
523 // Let a double be assigned to an int, only if said double is really an integer expressed as a double
524 ✗ const int test = (int)std::floor(defaultValue);
525 ✗ if (test == defaultValue) {
526 ✗ result = setDefaultValue(test);
527 } else {
528 ✗ result = false;
529 }
530 }
531 37 return result;
532 }
533
534 ✗ bool OSArgument::setDefaultValue(int defaultValue) {
535 ✗ bool result = false;
536 ✗ if (m_type == OSArgumentType::Integer) {
537 ✗ m_defaultValue = defaultValue;
538 ✗ result = true;
539
540 ✗ } else if (m_type == OSArgumentType::Double) {
541 // If we pass an int, but our type is double, we let it happen
542 ✗ m_defaultValue.emplace<double>(defaultValue);
543 ✗ result = true;
544 }
545
546 ✗ if (result) {
547 ✗ OS_ASSERT(hasDefaultValue());
548 ✗ onChange();
549 }
550
551 ✗ return result;
552 }
553
554 ✗ bool OSArgument::setDefaultValue(const openstudio::path& defaultValue) {
555 ✗ bool result = false;
556 ✗ if (m_type == OSArgumentType::Path) {
557 ✗ m_defaultValue = defaultValue;
558 ✗ OS_ASSERT(hasDefaultValue());
559 ✗ onChange();
560 ✗ result = true;
561 }
562 ✗ return result;
563 }
564
565 ✗ bool OSArgument::setDefaultValue(const char* defaultValue) {
566 ✗ return setDefaultValue(std::string(defaultValue));
567 }
568
569 ✗ bool OSArgument::setDefaultValue(const std::string& defaultValue) {
570 ✗ const bool result = setStringInternal(m_defaultValue, defaultValue);
571 ✗ if (result) {
572 ✗ OS_ASSERT(hasDefaultValue());
573 ✗ onChange();
574 }
575 ✗ return result;
576 }
577
578 201 bool OSArgument::setDomainType(const OSDomainType& domainType) {
579
3/3
✓ Branch 1 taken 201 times.
✓ Branch 4 taken 116 times.
✓ Branch 5 taken 85 times.
201 if (domainType != this->domainType()) {
580 // check whether domainType makes sense for type()
581
1/3
✗ Branch 1 not taken.
✓ Branch 2 taken 116 times.
✗ Branch 3 not taken.
116 switch (domainType.value()) {
582 ✗ case OSDomainType::Interval:
583 ✗ switch (m_type.value()) {
584 ✗ case OSArgumentType::Boolean:
585 case OSArgumentType::String:
586 case OSArgumentType::Choice:
587 case OSArgumentType::Path:
588 ✗ LOG(Info, "OSDomainType " << domainType.valueName() << " does not make sense for " << "arguments of type " << m_type.valueName()
589 << ", such as " << name() << ".");
590 ✗ return false;
591 ✗ default:
592 ✗ break;
593 }
594 ✗ break;
595 116 case OSDomainType::Enumeration:
596 116 break;
597 ✗ default:
598 ✗ OS_ASSERT(false);
599 }
600
601 // if so, change and clear current domain
602 116 m_domainType = domainType;
603 116 m_domain.clear();
604 116 onChange();
605 }
606 201 return true;
607 }
608
609 ✗ bool OSArgument::setDomain(const std::vector<bool>& domain) {
610 ✗ bool result(false);
611 ✗ if (m_type == OSArgumentType::Boolean) {
612 ✗ OS_ASSERT(m_domainType == OSDomainType::Enumeration);
613 // could check for uniqueness, but pass on that for now
614 ✗ m_domain.clear();
615 ✗ for (bool value : domain) {
616 ✗ m_domain.emplace_back(value);
617 }
618 ✗ onChange();
619 ✗ result = true;
620 }
621 ✗ return result;
622 }
623
624 ✗ bool OSArgument::setDomain(const std::vector<double>& domain) {
625 ✗ bool result(false);
626 ✗ if (m_type == OSArgumentType::Double) {
627 ✗ if ((m_domainType != OSDomainType::Interval) || (domain.size() == 2u)) {
628 // could check for uniqueness, min < max, but pass on that for now
629 ✗ m_domain.clear();
630 ✗ for (double value : domain) {
631 ✗ m_domain.emplace_back(value);
632 }
633 ✗ onChange();
634 ✗ result = true;
635 }
636 }
637 ✗ return result;
638 }
639
640 ✗ bool OSArgument::setDomain(const std::vector<int>& domain) {
641 ✗ bool result(false);
642 ✗ if (m_type == OSArgumentType::Integer) {
643 ✗ if ((m_domainType != OSDomainType::Interval) || (domain.size() == 2u)) {
644 // could check for uniqueness, min < max, but pass on that for now
645 ✗ m_domain.clear();
646 ✗ for (int value : domain) {
647 ✗ m_domain.emplace_back(value);
648 }
649 ✗ onChange();
650 ✗ result = true;
651 }
652 ✗ } else if (m_type == OSArgumentType::Double) {
653 ✗ if ((m_domainType != OSDomainType::Interval) || (domain.size() == 2u)) {
654 // could check for uniqueness, min < max, but pass on that for now
655 ✗ m_domain.clear();
656 ✗ for (int value : domain) {
657 ✗ m_domain.emplace_back(double(value));
658 }
659 ✗ onChange();
660 ✗ result = true;
661 }
662 }
663 ✗ return result;
664 }
665
666 ✗ bool OSArgument::setDomain(const std::vector<openstudio::path>& domain) {
667 ✗ bool result(false);
668 ✗ if (m_type == OSArgumentType::Path) {
669 ✗ OS_ASSERT(m_domainType == OSDomainType::Enumeration);
670 // could check for uniqueness, but pass on that for now
671 ✗ m_domain.clear();
672 ✗ for (const openstudio::path& value : domain) {
673 ✗ m_domain.emplace_back(value);
674 }
675 ✗ onChange();
676 ✗ result = true;
677 }
678 ✗ return result;
679 }
680
681 ✗ bool OSArgument::setDomain(const std::vector<std::string>& domain) {
682 ✗ bool result(false);
683 ✗ if ((m_domainType != OSDomainType::Interval) || (domain.size() == 2u)) {
684 // Store the original, in case we fail to set one element
685 ✗ const std::vector<OSArgumentVariant> originalDomain = m_domain;
686 ✗ m_domain.clear();
687 ✗ for (const std::string& value : domain) {
688 ✗ OSArgumentVariant newValue;
689 ✗ result = setStringInternal(newValue, value);
690 ✗ if (!result) {
691 // Restore the original domain
692 ✗ m_domain = originalDomain;
693 ✗ break;
694 }
695 ✗ m_domain.push_back(newValue);
696 ✗ }
697 ✗ if (result) {
698 ✗ onChange();
699 }
700 ✗ }
701 ✗ return result;
702 }
703
704 ✗ void OSArgument::clearDomain() {
705 ✗ m_domain.clear();
706 ✗ }
707
708 2 bool OSArgument::setMinValue(double minValue) { // NOLINT(misc-no-recursion)
709
2/3
✓ Branch 1 taken 2 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
2 if (m_type == OSArgumentType::Integer) {
710 ✗ const int test = (int)std::floor(minValue);
711 ✗ if (test == minValue) {
712 // If int expressed as double (eg: 1.0 when type = int)
713 // Then we call the int overload instead
714 ✗ return setMinValue(test);
715 }
716 ✗ return false;
717
3/3
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 1 time.
✓ Branch 5 taken 1 time.
2 } else if (m_type != OSArgumentType::Double) {
718 1 return false;
719 }
720
721 1 double maxValue = std::numeric_limits<double>::max();
722 // Try to fetch the existing maxValue
723
3/8
✓ Branch 1 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 6 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✓ Branch 12 taken 1 time.
1 if (hasDomain() && (m_domainType == OSDomainType::Interval)) {
724 ✗ std::vector<double> domain = domainAsDouble();
725 ✗ if (domain.size() == 2) {
726 ✗ maxValue = domain[1];
727 }
728 ✗ }
729
730
1/1
✓ Branch 1 taken 1 time.
1 m_domainType = OSDomainType::Interval;
731 1 m_domain.clear();
732
1/1
✓ Branch 1 taken 1 time.
1 m_domain.emplace_back(minValue);
733
1/1
✓ Branch 1 taken 1 time.
1 m_domain.emplace_back(maxValue);
734
735
1/1
✓ Branch 1 taken 1 time.
1 onChange();
736
737 1 return true;
738 }
739
740 8 bool OSArgument::setMinValue(int minValue) { // NOLINT(misc-no-recursion)
741
2/3
✓ Branch 1 taken 8 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 8 times.
8 if (m_type == OSArgumentType::Double) {
742 ✗ auto test = (double)minValue;
743 ✗ return setMinValue(test);
744
2/3
✓ Branch 1 taken 8 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 8 times.
8 } else if (m_type != OSArgumentType::Integer) {
745 ✗ return false;
746 }
747
748 8 int maxValue = std::numeric_limits<int>::max();
749
3/8
✓ Branch 1 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 6 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✓ Branch 12 taken 8 times.
8 if (hasDomain() && (m_domainType == OSDomainType::Interval)) {
750 ✗ std::vector<int> domain = domainAsInteger();
751 ✗ if (domain.size() == 2) {
752 ✗ maxValue = domain[1];
753 }
754 ✗ }
755
756
1/1
✓ Branch 1 taken 8 times.
8 m_domainType = OSDomainType::Interval;
757 8 m_domain.clear();
758
1/1
✓ Branch 1 taken 8 times.
8 m_domain.emplace_back(minValue);
759
1/1
✓ Branch 1 taken 8 times.
8 m_domain.emplace_back(maxValue);
760
761
1/1
✓ Branch 1 taken 8 times.
8 onChange();
762
763 8 return true;
764 }
765
766 9 bool OSArgument::setMaxValue(double maxValue) { // NOLINT(misc-no-recursion)
767
3/3
✓ Branch 1 taken 9 times.
✓ Branch 4 taken 1 time.
✓ Branch 5 taken 8 times.
9 if (m_type == OSArgumentType::Integer) {
768 1 const int test = (int)std::floor(maxValue);
769
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 if (test == maxValue) {
770
1/1
✓ Branch 1 taken 1 time.
1 return setMaxValue(test);
771 }
772 ✗ return false;
773
2/3
✓ Branch 1 taken 8 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 8 times.
8 } else if (m_type != OSArgumentType::Double) {
774 ✗ return false;
775 }
776
777 8 double minValue = std::numeric_limits<double>::lowest();
778
7/8
✓ Branch 1 taken 8 times.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 7 times.
✓ Branch 6 taken 1 time.
✓ Branch 9 taken 1 time.
✗ Branch 10 not taken.
✓ Branch 11 taken 1 time.
✓ Branch 12 taken 7 times.
8 if (hasDomain() && (m_domainType == OSDomainType::Interval)) {
779
1/1
✓ Branch 1 taken 1 time.
1 std::vector<double> domain = domainAsDouble();
780
1/2
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
1 if (domain.size() == 2) {
781 1 minValue = domain[0];
782 }
783 1 }
784
785
1/1
✓ Branch 1 taken 8 times.
8 m_domainType = OSDomainType::Interval;
786 8 m_domain.clear();
787
1/1
✓ Branch 1 taken 8 times.
8 m_domain.emplace_back(minValue);
788
1/1
✓ Branch 1 taken 8 times.
8 m_domain.emplace_back(maxValue);
789
790
1/1
✓ Branch 1 taken 8 times.
8 onChange();
791
792 8 return true;
793 }
794
795 3 bool OSArgument::setMaxValue(int maxValue) { // NOLINT(misc-no-recursion)
796
3/3
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 1 time.
✓ Branch 5 taken 2 times.
3 if (m_type == OSArgumentType::Double) {
797 1 auto test = (double)maxValue;
798
1/1
✓ Branch 1 taken 1 time.
1 return setMaxValue(test);
799
3/3
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 1 time.
✓ Branch 5 taken 1 time.
2 } else if (m_type != OSArgumentType::Integer) {
800 1 return false;
801 }
802
803 1 int minValue = std::numeric_limits<int>::lowest();
804
5/8
✓ Branch 1 taken 1 time.
✓ Branch 3 taken 1 time.
✗ Branch 4 not taken.
✓ Branch 6 taken 1 time.
✓ Branch 9 taken 1 time.
✗ Branch 10 not taken.
✓ Branch 11 taken 1 time.
✗ Branch 12 not taken.
1 if (hasDomain() && (m_domainType == OSDomainType::Interval)) {
805
1/1
✓ Branch 1 taken 1 time.
1 std::vector<int> domain = domainAsInteger();
806
1/2
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
1 if (domain.size() == 2) {
807 1 minValue = domain[0];
808 }
809 1 }
810
811
1/1
✓ Branch 1 taken 1 time.
1 m_domainType = OSDomainType::Interval;
812 1 m_domain.clear();
813
1/1
✓ Branch 1 taken 1 time.
1 m_domain.emplace_back(minValue);
814
1/1
✓ Branch 1 taken 1 time.
1 m_domain.emplace_back(maxValue);
815
816
1/1
✓ Branch 1 taken 1 time.
1 onChange();
817
818 1 return true;
819 }
820
821 2 void OSArgument::setRequired(bool required) {
822 2 m_required = required;
823 2 }
824
825 22 bool OSArgument::setStringInternal(OSArgumentVariant& argVar, const std::string& value) {
826 22 bool result = false;
827
828
2/3
✓ Branch 1 taken 22 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 22 times.
22 if (m_type == OSArgumentType::Boolean) {
829 ✗ if (openstudio::istringEqual(value, "true")) {
830 ✗ argVar = true;
831 ✗ result = true;
832 ✗ } else if (openstudio::istringEqual(value, "false")) {
833 ✗ argVar = false;
834 ✗ result = true;
835 } else {
836 ✗ LOG(Debug, "Unknown value '" << value << "' for argument of type Bool.");
837 }
838
2/3
✓ Branch 1 taken 22 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 22 times.
22 } else if (m_type == OSArgumentType::Double) {
839 try {
840 ✗ auto const double_val = std::stod(value, nullptr);
841 ✗ argVar = double_val;
842 ✗ result = true;
843 ✗ } catch (std::exception&) {
844 ✗ LOG(Debug, "Unable to convert value '" << value << "' to argument of type Double.");
845 ✗ }
846
2/3
✓ Branch 1 taken 22 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 22 times.
22 } else if (m_type == OSArgumentType::Integer) {
847 try {
848 ✗ auto const int_val = std::stoi(value, nullptr);
849 ✗ argVar = int_val;
850 ✗ result = true;
851 ✗ } catch (std::exception&) {
852 ✗ LOG(Debug, "Unable to convert value '" << value << "' to argument of type Integer.");
853 ✗ }
854
3/3
✓ Branch 1 taken 22 times.
✓ Branch 4 taken 11 times.
✓ Branch 5 taken 11 times.
22 } else if (m_type == OSArgumentType::String) {
855 11 argVar = value;
856 11 result = true;
857
2/3
✓ Branch 1 taken 11 times.
✓ Branch 4 taken 11 times.
✗ Branch 5 not taken.
11 } else if (m_type == OSArgumentType::Choice) {
858
2/3
✓ Branch 4 taken 11 times.
✓ Branch 7 taken 11 times.
✗ Branch 8 not taken.
11 if (std::find(m_choices.begin(), m_choices.end(), value) != m_choices.end()) {
859 11 argVar = value;
860 11 result = true;
861 } else {
862 // can also set using display name
863 ✗ auto it = std::find(m_choiceDisplayNames.begin(), m_choiceDisplayNames.end(), value);
864 ✗ if (it != m_choiceDisplayNames.end()) {
865 ✗ const int index = int(it - m_choiceDisplayNames.begin());
866 ✗ if (index < int(m_choices.size())) {
867 ✗ argVar = m_choices[index];
868 ✗ result = true;
869 }
870 } else {
871 ✗ LOG(Debug, "Unable to find choice '" << value << "' in either choice list or choice display names list for argument of type Choice.");
872 }
873 }
874 ✗ } else if (m_type == OSArgumentType::Path) {
875 ✗ const openstudio::path temp = toPath(value);
876 ✗ argVar = temp;
877 ✗ result = true;
878 ✗ }
879
880 // Don't really need to handle the other case, as it can only be a separator
881
882 22 return result;
883 }
884
885 98 std::string OSArgument::print() const {
886
1/1
✓ Branch 1 taken 98 times.
98 std::stringstream ss;
887
888 // name
889
2/2
✓ Branch 1 taken 98 times.
✓ Branch 4 taken 98 times.
98 ss << name();
890
2/3
✓ Branch 1 taken 98 times.
✓ Branch 5 taken 98 times.
✗ Branch 6 not taken.
98 if (!displayName().empty()) {
891
4/4
✓ Branch 1 taken 98 times.
✓ Branch 4 taken 98 times.
✓ Branch 7 taken 98 times.
✓ Branch 10 taken 98 times.
98 ss << " (" << displayName() << ")";
892 }
893
1/1
✓ Branch 1 taken 98 times.
98 ss << '\n';
894
895 // type and required
896
4/4
✓ Branch 1 taken 98 times.
✓ Branch 4 taken 98 times.
✓ Branch 7 taken 98 times.
✓ Branch 10 taken 98 times.
98 ss << type().valueName() << ", ";
897
3/3
✓ Branch 1 taken 98 times.
✓ Branch 3 taken 90 times.
✓ Branch 4 taken 8 times.
98 if (required()) {
898
1/1
✓ Branch 1 taken 90 times.
90 ss << "Required";
899 } else {
900
1/1
✓ Branch 1 taken 8 times.
8 ss << "Optional";
901 }
902
1/1
✓ Branch 1 taken 98 times.
98 ss << '\n';
903
904 // value
905
1/1
✓ Branch 1 taken 98 times.
98 ss << "Value: ";
906
3/3
✓ Branch 1 taken 98 times.
✓ Branch 3 taken 22 times.
✓ Branch 4 taken 76 times.
98 if (hasValue()) {
907
3/3
✓ Branch 1 taken 22 times.
✓ Branch 4 taken 22 times.
✓ Branch 7 taken 22 times.
22 ss << printValue(false) << " ";
908 }
909
3/3
✓ Branch 1 taken 98 times.
✓ Branch 3 taken 78 times.
✓ Branch 4 taken 20 times.
98 if (hasDefaultValue()) {
910
4/4
✓ Branch 1 taken 78 times.
✓ Branch 4 taken 78 times.
✓ Branch 7 taken 78 times.
✓ Branch 10 taken 78 times.
78 ss << "(" << printDefaultValue() << ")";
911 }
912
1/1
✓ Branch 1 taken 98 times.
98 ss << '\n';
913
914
2/3
✓ Branch 1 taken 98 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 98 times.
98 if (m_type.value() == OSArgumentType::Choice) {
915 ✗ ss << "Choices:" << '\n';
916 ✗ const int dnn = m_choiceDisplayNames.size();
917 ✗ for (int i = 0, n = m_choices.size(); i < n; ++i) {
918 ✗ ss << " " << m_choices[i];
919 ✗ if ((i < dnn) && (!m_choiceDisplayNames[i].empty())) {
920 ✗ ss << " (" << m_choiceDisplayNames[i] << ")";
921 }
922 ✗ ss << '\n';
923 }
924 }
925
926
3/3
✓ Branch 1 taken 98 times.
✓ Branch 3 taken 20 times.
✓ Branch 4 taken 78 times.
98 if (hasDomain()) {
927
3/3
✓ Branch 1 taken 20 times.
✓ Branch 4 taken 20 times.
✓ Branch 7 taken 20 times.
20 ss << m_domainType.valueName() << " Domain: ";
928
2/3
✓ Branch 1 taken 20 times.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
20 if (m_domainType == OSDomainType::Interval) {
929
1/3
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
✗ Branch 4 not taken.
20 OS_ASSERT(m_domain.size() == 2u);
930
8/8
✓ Branch 1 taken 20 times.
✓ Branch 5 taken 20 times.
✓ Branch 8 taken 20 times.
✓ Branch 11 taken 20 times.
✓ Branch 15 taken 20 times.
✓ Branch 18 taken 20 times.
✓ Branch 21 taken 20 times.
✓ Branch 24 taken 20 times.
20 ss << "[" << printOSArgumentVariant(m_domain[0]) << ", " << printOSArgumentVariant(m_domain[1]) << "]" << '\n';
931 } else {
932 ✗ ss << '\n';
933 ✗ for (const OSArgumentVariant& value : m_domain) {
934 ✗ ss << " " << printOSArgumentVariant(value) << '\n';
935 }
936 }
937 }
938
939
1/1
✓ Branch 1 taken 98 times.
196 return ss.str();
940 98 }
941
942 55 std::string OSArgument::printValue(bool printDefault) const {
943 55 std::string result;
944
945 55 OSArgumentVariant toPrint;
946
3/3
✓ Branch 1 taken 55 times.
✓ Branch 3 taken 40 times.
✓ Branch 4 taken 15 times.
55 if (hasValue()) {
947
1/1
✓ Branch 1 taken 40 times.
40 toPrint = m_value;
948
4/7
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 15 times.
✓ Branch 5 taken 15 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 15 times.
✗ Branch 8 not taken.
15 } else if (printDefault && hasDefaultValue()) {
949
1/1
✓ Branch 1 taken 15 times.
15 toPrint = m_defaultValue;
950 }
951
952 // If not std::monostate (= empty)
953
1/2
✓ Branch 1 taken 55 times.
✗ Branch 2 not taken.
55 if (toPrint.index() != 0) {
954
1/1
✓ Branch 1 taken 55 times.
55 result = printOSArgumentVariant(toPrint);
955 }
956
957 110 return result;
958 55 }
959
960 78 std::string OSArgument::printDefaultValue() const {
961 78 std::string result;
962
963
2/3
✓ Branch 1 taken 78 times.
✓ Branch 3 taken 78 times.
✗ Branch 4 not taken.
78 if (hasDefaultValue()) {
964
1/1
✓ Branch 1 taken 78 times.
78 result = printOSArgumentVariant(m_defaultValue);
965 }
966
967 78 return result;
968 ✗ }
969
970 // ostream operators for OSArgument
971 98 std::ostream& operator<<(std::ostream& os, const OSArgument& arg) {
972
2/2
✓ Branch 1 taken 98 times.
✓ Branch 4 taken 98 times.
98 os << arg.print();
973 98 return os;
974 }
975
976 210 std::string OSArgument::printOSArgumentVariant(const OSArgumentVariant& argVar) {
977
1/3
✗ Branch 1 not taken.
✓ Branch 2 taken 210 times.
✗ Branch 4 not taken.
210 OS_ASSERT(argVar.index() != 0);
978
1/1
✓ Branch 1 taken 210 times.
210 std::stringstream ss;
979
980 // We use std::visit, filtering out the case where it's monostate
981 // Aside from monostate, every possible type is streamable
982 210 std::visit(
983 210 [&ss](const auto& arg) {
984 // Needed to properly compare the types
985 using T = std::decay_t<decltype(arg)>;
986 if constexpr (std::is_same_v<T, std::monostate>) {
987 // No-op
988 } else if constexpr (std::is_same_v<T, bool>) {
989 68 ss << std::boolalpha << arg;
990 } else {
991 142 ss << arg;
992 }
993 210 },
994 argVar);
995
996
1/1
✓ Branch 1 taken 210 times.
420 return ss.str();
997 210 }
998
999 // helper constant for the visitor below so we static assert we didn't miss a type
1000 template <class>
1001 inline constexpr bool always_false_v = false;
1002
1003 49 Json::Value OSArgument::argumentVariantToJSONValue(const OSArgumentVariant& argVar) {
1004 return std::visit(
1005 49 [](auto&& arg) -> Json::Value {
1006 using T = std::decay_t<decltype(arg)>;
1007 if constexpr (std::is_same_v<T, std::monostate>) {
1008 20 return Json::nullValue;
1009 } else if constexpr (std::is_same_v<T, bool>) { // NOLINT(bugprone-branch-clone)
1010 6 return arg;
1011 } else if constexpr (std::is_same_v<T, double>) {
1012 7 return arg;
1013 } else if constexpr (std::is_same_v<T, int>) {
1014 5 return arg;
1015 } else if constexpr (std::is_same_v<T, std::string>) {
1016 11 return arg;
1017 } else if constexpr (std::is_same_v<T, openstudio::path>) {
1018 ✗ return openstudio::toString(arg);
1019 } else {
1020 static_assert(always_false_v<T>, "non-exhaustive visitor!");
1021 }
1022 },
1023
1/1
✓ Branch 1 taken 49 times.
98 argVar);
1024 }
1025
1026
2/2
✓ Branch 6 taken 293 times.
✓ Branch 12 taken 293 times.
293 OSArgument::OSArgument() : m_uuid(createUUID()), m_versionUUID(createUUID()) {}
1027
1028 204 OSArgument::OSArgument(const std::string& name, const OSArgumentType& type, bool required, bool modelDependent)
1029 204 : m_uuid(createUUID()),
1030 204 m_versionUUID(createUUID()),
1031 204 m_name(name),
1032
1/1
✓ Branch 1 taken 204 times.
204 m_displayName(name),
1033 204 m_type(type),
1034 204 m_required(required),
1035
1/1
✓ Branch 5 taken 204 times.
204 m_modelDependent(modelDependent) {}
1036
1037 523 void OSArgument::onChange() {
1038 523 m_versionUUID = createUUID();
1039 523 }
1040
1041 7 OSArgument makeChoiceArgumentOfWorkspaceObjects(const std::string& name, const IddObjectType& iddObjectType, const Workspace& workspace,
1042 bool required) {
1043
1044
1/1
✓ Branch 1 taken 7 times.
7 const std::vector<WorkspaceObject> objects = workspace.getObjectsByType(iddObjectType);
1045 7 std::vector<std::pair<std::string, std::string>> intermediate;
1046
1/1
✓ Branch 2 taken 7 times.
7 intermediate.reserve(objects.size());
1047
1048
2/2
✓ Branch 5 taken 5 times.
✓ Branch 6 taken 7 times.
12 for (const WorkspaceObject& object : objects) {
1049 5 std::string objectName;
1050
2/3
✓ Branch 1 taken 5 times.
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
10 if (object.name()) {
1051
3/3
✓ Branch 1 taken 5 times.
✓ Branch 4 taken 5 times.
✓ Branch 7 taken 5 times.
5 objectName = object.name().get();
1052 } else {
1053 ✗ objectName = object.iddObject().type().valueName();
1054 }
1055
3/3
✓ Branch 1 taken 5 times.
✓ Branch 4 taken 5 times.
✓ Branch 7 taken 5 times.
5 intermediate.emplace_back(toString(object.handle()), objectName);
1056 5 }
1057
1058
1/1
✓ Branch 3 taken 7 times.
7 std::sort(intermediate.begin(), intermediate.end(), SecondOfPairLess<std::pair<std::string, std::string>>());
1059
1060 7 const int n = intermediate.size();
1061
1/1
✓ Branch 1 taken 7 times.
14 StringVector choices(n);
1062
1/1
✓ Branch 1 taken 7 times.
7 StringVector displayNames(n);
1063
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 7 times.
12 for (int i = 0; i < n; ++i) {
1064
1/1
✓ Branch 3 taken 5 times.
5 choices[i] = intermediate[i].first;
1065
1/1
✓ Branch 3 taken 5 times.
5 displayNames[i] = intermediate[i].second;
1066 }
1067
1068
1/1
✓ Branch 1 taken 7 times.
14 return OSArgument::makeChoiceArgument(name, choices, displayNames, required);
1069 7 }
1070
1071 ✗ OSArgument makeChoiceArgumentOfWorkspaceObjects(const std::string& name, const std::string& referenceName, const Workspace& workspace,
1072 bool required) {
1073
1074 ✗ const std::vector<WorkspaceObject> objects = workspace.getObjectsByReference(referenceName);
1075 ✗ std::vector<std::pair<std::string, std::string>> intermediate;
1076 ✗ intermediate.reserve(objects.size());
1077 ✗ for (const WorkspaceObject& object : objects) {
1078 ✗ intermediate.emplace_back(toString(object.handle()), object.name().get());
1079 }
1080
1081 ✗ std::sort(intermediate.begin(), intermediate.end(), SecondOfPairLess<std::pair<std::string, std::string>>());
1082
1083 ✗ const int n = intermediate.size();
1084 ✗ StringVector choices(n);
1085 ✗ StringVector displayNames(n);
1086 ✗ for (int i = 0; i < n; ++i) {
1087 ✗ choices[i] = intermediate[i].first;
1088 ✗ displayNames[i] = intermediate[i].second;
1089 }
1090
1091 ✗ return OSArgument::makeChoiceArgument(name, choices, displayNames, required);
1092 ✗ }
1093
1094 17 std::map<std::string, OSArgument> convertOSArgumentVectorToMap(const std::vector<OSArgument>& arguments) {
1095 17 std::map<std::string, OSArgument> argMap;
1096
2/2
✓ Branch 4 taken 43 times.
✓ Branch 5 taken 17 times.
60 for (const OSArgument& arg : arguments) {
1097
3/3
✓ Branch 1 taken 43 times.
✓ Branch 4 taken 43 times.
✓ Branch 8 taken 43 times.
43 argMap.insert(std::make_pair(arg.name(), arg.clone()));
1098 }
1099 17 return argMap;
1100 ✗ }
1101
1102 ✗ Json::Value OSArgument::toJSON() const {
1103 ✗ Json::Value root;
1104 ✗ root["name"] = m_name;
1105 // root["uid"] = openstudio::removeBraces(m_uuid);
1106 // root["versionuuid"] = openstudio::removeBraces(m_versionUUID);
1107 ✗ root["display_name"] = m_displayName;
1108 ✗ if (m_description) {
1109 ✗ root["description"] = *m_description;
1110 }
1111 ✗ root["type"] = m_type.valueName();
1112 ✗ root["required"] = m_required;
1113 ✗ root["model_dependent"] = m_modelDependent;
1114
1115 ✗ if (m_type == OSArgumentType::Boolean) {
1116 ✗ if (hasDefaultValue()) {
1117 ✗ root["default_value"] = defaultValueAsBool();
1118 }
1119 ✗ if (hasValue()) {
1120 ✗ root["value"] = valueAsBool();
1121 }
1122 ✗ } else if (m_type == OSArgumentType::Double) {
1123 ✗ if (m_units) {
1124 ✗ root["units"] = *m_units;
1125 }
1126 ✗ if (hasDefaultValue()) {
1127 ✗ root["default_value"] = defaultValueAsDouble();
1128 }
1129 ✗ if (hasValue()) {
1130 ✗ root["value"] = valueAsDouble();
1131 }
1132 ✗ if (hasDomain()) {
1133 ✗ auto domain = domainAsDouble();
1134 ✗ if (domain.size() != 2) {
1135 ✗ LOG_AND_THROW("Domain for a double should be of size 2!");
1136 }
1137 ✗ auto& minValue = domain.front();
1138 ✗ auto& maxValue = domain.back();
1139 ✗ if (minValue != std::numeric_limits<double>::lowest()) {
1140 ✗ root["min_value"] = minValue;
1141 }
1142 ✗ if (maxValue != std::numeric_limits<double>::max()) {
1143 ✗ root["max_value"] = maxValue;
1144 }
1145 ✗ }
1146
1147 ✗ } else if (m_type == OSArgumentType::Quantity) {
1148 // Note: This is heavily deprecated, and the std::variant can't contain it
1149 ✗ if (m_units) {
1150 ✗ root["units"] = *m_units;
1151 }
1152 ✗ if (hasDefaultValue()) {
1153 ✗ root["default_value"] = defaultValueAsDouble();
1154 }
1155 ✗ if (hasValue()) {
1156 ✗ root["value"] = valueAsDouble();
1157 }
1158
1159 ✗ } else if (m_type == OSArgumentType::Integer) {
1160 ✗ if (m_units) {
1161 ✗ root["units"] = *m_units;
1162 }
1163 ✗ if (hasDefaultValue()) {
1164 ✗ root["default_value"] = defaultValueAsInteger();
1165 }
1166 ✗ if (hasValue()) {
1167 ✗ root["value"] = valueAsInteger();
1168 }
1169 ✗ if (hasDomain()) {
1170 ✗ auto domain = domainAsInteger();
1171 ✗ if (domain.size() != 2) {
1172 ✗ LOG_AND_THROW("Domain for an integer should be of size 2!");
1173 }
1174 ✗ auto& minValue = domain.front();
1175 ✗ auto& maxValue = domain.back();
1176 ✗ if (minValue != std::numeric_limits<int>::lowest()) {
1177 ✗ root["min_value"] = minValue;
1178 }
1179 ✗ if (maxValue != std::numeric_limits<int>::max()) {
1180 ✗ root["max_value"] = maxValue;
1181 }
1182 ✗ }
1183 ✗ } else if (m_type == OSArgumentType::String) {
1184 ✗ if (hasDefaultValue()) {
1185 ✗ root["default_value"] = defaultValueAsString();
1186 }
1187 ✗ if (hasValue()) {
1188 ✗ root["value"] = valueAsString();
1189 }
1190 ✗ } else if (m_type == OSArgumentType::Choice) {
1191 ✗ if (hasDefaultValue()) {
1192 ✗ root["default_value"] = defaultValueAsString();
1193 }
1194 ✗ if (hasValue()) {
1195 ✗ root["value"] = valueAsString();
1196 }
1197 ✗ auto& choiceValues = root["choice_values"];
1198 ✗ choiceValues = Json::arrayValue; // Without this line, it's "null" (/nil in Ruby) if m_choices is empty, while old CLI had it as `[]`
1199 ✗ for (const auto& choice : m_choices) {
1200 ✗ choiceValues.append(choice);
1201 }
1202 ✗ auto& choiceDisplayNames = root["choice_display_names"];
1203 ✗ choiceDisplayNames = Json::arrayValue;
1204 ✗ for (const auto& choice : m_choiceDisplayNames) {
1205 ✗ choiceDisplayNames.append(choice);
1206 }
1207
1208 ✗ } else if (m_type == OSArgumentType::Path) {
1209 ✗ if (hasDefaultValue()) {
1210 ✗ root["default_value"] = openstudio::toString(defaultValueAsPath());
1211 }
1212 ✗ if (hasValue()) {
1213 ✗ root["value"] = openstudio::toString(valueAsPath());
1214 }
1215 ✗ } else if (m_type == OSArgumentType::Separator) {
1216 // No-op
1217 } else {
1218 ✗ LOG_AND_THROW("Unhandled type");
1219 }
1220
1221 ✗ return root;
1222 ✗ }
1223
1224 ✗ std::string OSArgument::toJSONString() const {
1225 ✗ return toJSON().toStyledString();
1226 }
1227
1228 } // namespace measure
1229 } // namespace openstudio
1230