Debian Codegraph @

packages/seacas/libraries/ioss/src/Ioss_Property.C

raw
1 // Copyright(C) 1999-2021, 2023, 2025 National Technology & Engineering Solutions
2 // of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
3 // NTESS, the U.S. Government retains certain rights in this software.
4 //
5 // See packages/seacas/LICENSE for details
6 #include "Ioss_GroupingEntity.h"
7 #include "Ioss_Property.h"
8 #include "Ioss_Utils.h"
9 #include <assert.h>
10 #include <fmt/ostream.h>
11 #include <ostream>
12 #include <string>
13
14 namespace {
15 std::string type_string(Ioss::Property::BasicType type)
16 {
17 switch (type) {
18 case Ioss::Property::INVALID: return {"invalid"};
19 case Ioss::Property::REAL: return {"real"};
20 case Ioss::Property::INTEGER: return {"integer"};
21 case Ioss::Property::POINTER: return {"pointer"};
22 case Ioss::Property::STRING: return {"string"};
23 case Ioss::Property::VEC_INTEGER: return {"vector<int>"};
24 case Ioss::Property::VEC_DOUBLE: return {"vector<double>"};
25 }
26 return {"internal error"};
27 }
28
29 void error_message(const Ioss::Property &property, const std::string &requested_type)
30 {
31 std::ostringstream errmsg;
32 fmt::print(errmsg,
33 "ERROR: For property named '{}', code requested value of type '{}', but property "
34 "type is '{}'. Types must match\n",
35 property.get_name(), requested_type, type_string(property.get_type()));
36 IOSS_ERROR(errmsg);
37 }
38 } // namespace
39
40 /** \brief Create an INTEGER type property.
41 *
42 * \param[in] name The property name.
43 * \param[in] value The property value.
44 * \param[in] origin The origin of the property - IMPLICIT, or EXTERNAL, or ATTRIBUTE
45 */
46 Ioss::Property::Property(std::string name, int value, Origin origin)
47 : name_(std::move(name)), type_(INTEGER), origin_(origin), data_(static_cast<int64_t>(value))
48 {
49 }
50
51 /** \brief Create an INTEGER type property using an int64_t variable.
52 *
53 * \param[in] name The property name.
54 * \param[in] value The property value.
55 * \param[in] origin The origin of the property - IMPLICIT, or EXTERNAL, or ATTRIBUTE
56 */
57 Ioss::Property::Property(std::string name, int64_t value, Origin origin)
58 : name_(std::move(name)), type_(INTEGER), origin_(origin), data_(value)
59 {
60 }
61
62 /** \brief Create a REAL type property.
63 *
64 * \param[in] name The property name.
65 * \param[in] value The property value.
66 * \param[in] origin The origin of the property - IMPLICIT, or EXTERNAL, or ATTRIBUTE
67 */
68 Ioss::Property::Property(std::string name, double value, Origin origin)
69 : name_(std::move(name)), type_(REAL), origin_(origin), data_(value)
70 {
71 }
72
73 /** \brief Create a STRING type property.
74 *
75 * \param[in] name The property name.
76 * \param[in] value The property value.
77 * \param[in] origin The origin of the property - IMPLICIT, or EXTERNAL, or ATTRIBUTE
78 */
79 Ioss::Property::Property(std::string name, const std::string &value, Origin origin)
80 : name_(std::move(name)), type_(STRING), origin_(origin), data_(value)
81 {
82 }
83
84 /** \brief Create a VEC_INTEGER type property.
85 *
86 * \param[in] name The property name.
87 * \param[in] value The property value.
88 * \param[in] origin The origin of the property - IMPLICIT, or EXTERNAL, or ATTRIBUTE
89 */
90 Ioss::Property::Property(std::string name, const std::vector<int> &value, Origin origin)
91 : name_(std::move(name)), type_(VEC_INTEGER), origin_(origin), data_(value)
92 {
93 }
94
95 /** \brief Create a VEC_DOUBLE type property.
96 *
97 * \param[in] name The property name.
98 * \param[in] value The property value.
99 * \param[in] origin The origin of the property - IMPLICIT, or EXTERNAL, or ATTRIBUTE
100 */
101 Ioss::Property::Property(std::string name, const std::vector<double> &value, Origin origin)
102 : name_(std::move(name)), type_(VEC_DOUBLE), origin_(origin), data_(value)
103 {
104 }
105
106 /** \brief Create a STRING type property from const char* argument.
107 *
108 * \param[in] name The property name.
109 * \param[in] value The property value.
110 * \param[in] origin The origin of the property - IMPLICIT, or EXTERNAL, or ATTRIBUTE
111 */
112 Ioss::Property::Property(std::string name, const char *value, Origin origin)
113 : name_(std::move(name)), type_(STRING), origin_(origin), data_(std::string(value))
114 {
115 }
116
117 /** \brief Create a POINTER type property.
118 *
119 * \param[in] name The property name.
120 * \param[in] value The property value.
121 * \param[in] origin The origin of the property - IMPLICIT, or EXTERNAL, or ATTRIBUTE
122 */
123 Ioss::Property::Property(std::string name, void *value, Origin origin)
124 : name_(std::move(name)), type_(POINTER), origin_(origin), data_(value)
125 {
126 }
127
128 /** \brief Set implicit property with a specified type.
129 *
130 * \param[in] ge The property value.
131 * \param[in] name The property name.
132 * \param[in] type The property type.
133 */
134 Ioss::Property::Property(const Ioss::GroupingEntity *ge, std::string name, const BasicType type)
135 : name_(std::move(name)), type_(type), origin_(IMPLICIT), data_(ge)
136 {
137 }
138
139 bool Ioss::Property::operator==(const Ioss::Property &rhs) const
140 {
141 if (this->name_ != rhs.name_) {
142 return false;
143 }
144
145 if (this->type_ != rhs.type_) {
146 return false;
147 }
148
149 if (this->origin_ != rhs.origin_) {
150 return false;
151 }
152
153 switch (this->type_) {
154 case INVALID: break;
155 case REAL:
156 double r_lhs, r_rhs;
157 this->get_value(&r_lhs);
158 rhs.get_value(&r_rhs);
159 if (r_lhs != r_rhs) {
160 return false;
161 }
162 break;
163 case INTEGER:
164 int64_t i_lhs, i_rhs;
165 this->get_value(&i_lhs);
166 rhs.get_value(&i_rhs);
167 if (i_lhs != i_rhs) {
168 return false;
169 }
170 break;
171 case POINTER:
172 void *p_lhs, *p_rhs;
173 this->get_value(p_lhs);
174 rhs.get_value(p_rhs);
175 if (p_lhs != p_rhs) {
176 return false;
177 }
178 break;
179 case VEC_DOUBLE: {
180 auto rh = rhs.get_vec_double();
181 auto lh = get_vec_double();
182 if (lh != rh) {
183 return false;
184 }
185 } break;
186 case VEC_INTEGER: {
187 auto rh = rhs.get_vec_int();
188 auto lh = get_vec_int();
189 if (lh != rh) {
190 return false;
191 }
192 } break;
193 case STRING:
194 std::string s_lhs, s_rhs;
195 this->get_value(&s_lhs);
196 rhs.get_value(&s_rhs);
197 if (s_lhs != s_rhs) {
198 return false;
199 }
200 break;
201 }
202 return true;
203 }
204
205 bool Ioss::Property::operator!=(const Ioss::Property &rhs) const { return !(*this == rhs); }
206
207 /** \brief Get the property value if it is of type STRING.
208 *
209 * \returns The STRING-type property value
210 */
211 std::string Ioss::Property::get_string() const
212 {
213 std::string value;
214 bool valid = get_value(&value);
215 if (!valid) {
216 error_message(*this, "string");
217 }
218 return value;
219 }
220
221 /** \brief Get the property value if it is of type VEC_DOUBLE.
222 *
223 * \returns The VEC_DOUBLE-type property value
224 */
225 std::vector<double> Ioss::Property::get_vec_double() const
226 {
227 std::vector<double> value;
228 bool valid = get_value(&value);
229 if (!valid) {
230 error_message(*this, "vector<double>");
231 }
232 return value;
233 }
234
235 /** \brief Get the property value if it is of type VEC_INT.
236 *
237 * \returns The VEC_INT-type property value
238 */
239 std::vector<int> Ioss::Property::get_vec_int() const
240 {
241 std::vector<int> value;
242 bool valid = get_value(&value);
243 if (!valid) {
244 error_message(*this, "vector<int>");
245 }
246 return value;
247 }
248
249 /** \brief Get the property value if it is of type INTEGER.
250 *
251 * \returns The INTEGER-type property value
252 */
253 int64_t Ioss::Property::get_int() const
254 {
255 int64_t value;
256 bool valid = get_value(&value);
257 if (!valid) {
258 error_message(*this, "int");
259 }
260 return value;
261 }
262
263 /** \brief Get the property value if it is of type REAL.
264 *
265 * \returns The REAL-type property value.
266 */
267 double Ioss::Property::get_real() const
268 {
269 double value;
270 bool valid = get_value(&value);
271 if (!valid) {
272 error_message(*this, "real");
273 }
274 return value;
275 }
276
277 /** \brief Get the property value if it is of type POINTER.
278 *
279 * \returns The POINTER-type property value.
280 */
281 void *Ioss::Property::get_pointer() const
282 {
283 void *value = nullptr;
284 bool valid = get_value(value);
285 if (!valid) {
286 error_message(*this, "pointer");
287 }
288 return value;
289 }
290
291 bool Ioss::Property::get_value(int64_t *value) const
292 {
293 bool valid_request = type_ == INTEGER;
294 if (is_explicit()) {
295 assert(std::holds_alternative<int64_t>(data_));
296 *value = std::get<int64_t>(data_);
297 }
298 else {
299 assert(std::holds_alternative<const Ioss::GroupingEntity *>(data_));
300 const auto *ge = std::get<const Ioss::GroupingEntity *>(data_);
301 const auto implicit = ge->get_implicit_property(name_);
302 valid_request = implicit.get_value(value);
303 }
304 return valid_request;
305 }
306
307 bool Ioss::Property::get_value(double *value) const
308 {
309 bool valid_request = type_ == REAL;
310 if (is_explicit()) {
311 assert(std::holds_alternative<double>(data_));
312 *value = std::get<double>(data_);
313 }
314 else {
315 assert(std::holds_alternative<const Ioss::GroupingEntity *>(data_));
316 const auto *ge = std::get<const Ioss::GroupingEntity *>(data_);
317 const auto implicit = ge->get_implicit_property(name_);
318 valid_request = implicit.get_value(value);
319 }
320 return valid_request;
321 }
322
323 bool Ioss::Property::get_value(std::string *value) const
324 {
325 bool valid_request = type_ == STRING;
326 if (is_explicit()) {
327 assert(std::holds_alternative<std::string>(data_));
328 *value = std::get<std::string>(data_);
329 }
330 else {
331 assert(std::holds_alternative<const Ioss::GroupingEntity *>(data_));
332 const auto *ge = std::get<const Ioss::GroupingEntity *>(data_);
333 const auto implicit = ge->get_implicit_property(name_);
334 valid_request = implicit.get_value(value);
335 }
336 return valid_request;
337 }
338
339 bool Ioss::Property::get_value(std::vector<int> *value) const
340 {
341 bool valid_request = type_ == VEC_INTEGER;
342 if (is_explicit()) {
343 assert(std::holds_alternative<std::vector<int>>(data_));
344 auto ivec = std::get<std::vector<int>>(data_);
345 std::copy(ivec.begin(), ivec.end(), std::back_inserter(*value));
346 }
347 else {
348 assert(std::holds_alternative<const Ioss::GroupingEntity *>(data_));
349 const auto *ge = std::get<const Ioss::GroupingEntity *>(data_);
350 const auto implicit = ge->get_implicit_property(name_);
351 valid_request = implicit.get_value(value);
352 }
353 return valid_request;
354 }
355
356 bool Ioss::Property::get_value(std::vector<double> *value) const
357 {
358 bool valid_request = type_ == VEC_DOUBLE;
359 if (is_explicit()) {
360 assert(std::holds_alternative<std::vector<double>>(data_));
361 auto dvec = std::get<std::vector<double>>(data_);
362 std::copy(dvec.begin(), dvec.end(), std::back_inserter(*value));
363 }
364 else {
365 assert(std::holds_alternative<const Ioss::GroupingEntity *>(data_));
366 const auto *ge = std::get<const Ioss::GroupingEntity *>(data_);
367 const auto implicit = ge->get_implicit_property(name_);
368 valid_request = implicit.get_value(value);
369 }
370 return valid_request;
371 }
372
373 bool Ioss::Property::get_value(void *&value) const
374 {
375 bool valid_request = type_ == POINTER;
376 if (is_explicit()) {
377 assert(std::holds_alternative<void *>(data_));
378 value = std::get<void *>(data_);
379 }
380 else {
381 assert(std::holds_alternative<const Ioss::GroupingEntity *>(data_));
382 const auto *ge = std::get<const Ioss::GroupingEntity *>(data_);
383 const auto implicit = ge->get_implicit_property(name_);
384 valid_request = implicit.get_value(value);
385 }
386 return valid_request;
387 }