helics  3.5.2
helicsTypes.hpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2017-2024,
3 Battelle Memorial Institute; Lawrence Livermore National Security, LLC; Alliance for Sustainable
4 Energy, LLC. See the top-level NOTICE for additional details. All rights reserved.
5 SPDX-License-Identifier: BSD-3-Clause
6 */
7 #pragma once
8 #include "../core/core-data.hpp"
9 #include "helics_cxx_export.h"
10 
11 #include <complex>
12 #include <limits>
13 #include <string>
14 #include <string_view>
15 #include <typeinfo>
16 #include <utility>
17 #include <vector>
21 namespace helics {
22 using IdentifierType = std::uint32_t;
23 using std::int32_t;
24 using std::int64_t;
25 
27  static_cast<IdentifierType>(-1);
28 
30 enum class Identifiers : char {
31  PUBLICATION = 'p',
32  INPUT = 'i',
33  FILTER = 'f',
34  ENDPOINT = 'e',
35  QUERY = 'q',
36  TRANSLATORS = 't'
37 };
38 
40 enum class InterfaceVisibility {
41  LOCAL,
42  GLOBAL,
43 };
44 
51 template<typename BaseType, Identifiers ID, BaseType invalidValue>
52 class IdentifierId {
53  private:
54  BaseType ivalue = invalidValue;
55 
56  public:
57  static const Identifiers identity{ID};
58  using UnderlyingType = BaseType;
60  constexpr IdentifierId() noexcept: ivalue(invalidValue) {}
62  constexpr explicit IdentifierId(BaseType val) noexcept: ivalue(val) {}
64  constexpr IdentifierId(const IdentifierId& id) noexcept: ivalue(id.ivalue) {}
66  IdentifierId& operator=(BaseType val) noexcept
67  {
68  ivalue = val;
69  return *this;
70  }
72  IdentifierId& operator=(const IdentifierId& id) = default;
73 
75  BaseType value() const noexcept { return ivalue; };
77  bool operator==(IdentifierId id) const noexcept { return (ivalue == id.ivalue); }
79  bool operator!=(IdentifierId id) const noexcept { return (ivalue != id.ivalue); }
81  bool operator<(IdentifierId id) const noexcept { return (ivalue < id.ivalue); }
82  // check if the current value is not the invalidValue
83  bool isValid() const noexcept { return (ivalue != invalidValue); }
84 };
85 } // namespace helics
86 
87 // specialize std::hash
88 namespace std {
90 template<typename BaseType, helics::Identifiers ID, BaseType invalidValue>
91 struct hash<helics::IdentifierId<BaseType, ID, invalidValue>> {
92  using argument_type =
94  using result_type = std::size_t;
96  result_type operator()(argument_type const& key) const noexcept
97  {
98  return std::hash<BaseType>{}(key.value());
99  }
100 };
101 } // namespace std
102 
103 namespace helics {
104 using PublicationId = IdentifierId<IdentifierType, Identifiers::PUBLICATION, invalid_id_value>;
105 using InputId = IdentifierId<IdentifierType, Identifiers::INPUT, invalid_id_value>;
106 
107 using QueryId = IdentifierId<IdentifierType, Identifiers::QUERY, invalid_id_value>;
108 
110 class NamedPoint {
111  public:
112  std::string name;
113  double value =
114  std::numeric_limits<double>::quiet_NaN();
116  NamedPoint() = default;
118  NamedPoint(std::string_view valname, double valval): name(valname), value(valval) {}
123  bool operator==(const NamedPoint& np) const
124  {
125  return ((std::isnan(value)) && (std::isnan(np.value))) ?
126  (name == np.name) :
127  ((value == np.value) && (name == np.name));
128  }
129  bool operator!=(const NamedPoint& np) const { return !operator==(np); }
133  bool operator<(const NamedPoint& np) const
134  {
135  return (name == np.name) ? (name < np.name) : (value < np.value);
136  }
140  bool operator>(const NamedPoint& np) const
141  {
142  return (name == np.name) ? (name > np.name) : (value > np.value);
143  }
144 };
145 
147 template<class X>
148 inline constexpr const char* typeNameString()
149 {
150  // this will probably not be the same on all platforms
151  return typeid(X).name();
152 }
153 namespace typestrings {
154  constexpr auto svecstr = "string_vector";
155  constexpr auto dvecstr = "double_vector";
156  constexpr auto cvecstr = "complex_vector";
157 
158  constexpr auto doublestr = "double";
159  constexpr auto floatstr = "float";
160  constexpr auto boolstr = "bool";
161 
162  constexpr auto charstr = "char";
163  constexpr auto ucharstr = "uchar";
164  constexpr auto i32str = "int32";
165  constexpr auto ui32str = "uint32";
166  constexpr auto i64str = "int64";
167  constexpr auto ui64str = "uint64";
168 
169  constexpr auto cfloatstr = "complex_f";
170  constexpr auto cdoublestr = "complex";
171  constexpr auto npstr = "named_point";
172  constexpr auto strstr = "string";
173 } // namespace typestrings
174 
175 template<>
176 inline constexpr const char* typeNameString<std::vector<std::string>>()
177 {
178  return typestrings::svecstr;
179 }
180 template<>
181 inline constexpr const char* typeNameString<std::vector<double>>()
182 {
183  return typestrings::dvecstr;
184 }
185 
186 template<>
187 inline constexpr const char* typeNameString<std::vector<std::complex<double>>>()
188 {
189  return typestrings::cvecstr;
190 }
191 
193 template<>
194 inline constexpr const char* typeNameString<double>()
195 {
196  return typestrings::doublestr;
197 }
198 
200 template<>
201 inline constexpr const char* typeNameString<float>()
202 {
203  return typestrings::floatstr;
204 }
205 
207 template<>
208 inline constexpr const char* typeNameString<bool>()
209 {
210  return typestrings::boolstr;
211 }
212 
214 template<>
215 inline constexpr const char* typeNameString<char>()
216 {
217  return typestrings::charstr;
218 }
220 template<>
221 inline constexpr const char* typeNameString<unsigned char>()
222 {
223  return typestrings::ucharstr;
224 }
226 template<>
227 inline constexpr const char* typeNameString<std::int32_t>()
228 {
229  return typestrings::i32str;
230 }
232 template<>
233 inline constexpr const char* typeNameString<std::uint32_t>()
234 {
235  return typestrings::ui32str;
236 }
238 template<>
239 inline constexpr const char* typeNameString<int64_t>()
240 {
241  return typestrings::i64str;
242 }
244 template<>
245 inline constexpr const char* typeNameString<std::uint64_t>()
246 {
247  return typestrings::ui64str;
248 }
250 template<>
251 inline constexpr const char* typeNameString<std::complex<float>>()
252 {
253  return typestrings::cfloatstr;
254 }
256 template<>
257 inline constexpr const char* typeNameString<std::complex<double>>()
258 {
259  return typestrings::cdoublestr;
260 }
261 template<>
262 inline constexpr const char* typeNameString<std::string>()
263 {
264  return typestrings::strstr;
265 }
266 
267 template<>
268 inline constexpr const char* typeNameString<NamedPoint>()
269 {
270  return typestrings::npstr;
271 }
273 enum class DataType : int {
274  HELICS_UNKNOWN = HELICS_DATA_TYPE_UNKNOWN,
275  HELICS_STRING = HELICS_DATA_TYPE_STRING,
276  HELICS_DOUBLE = HELICS_DATA_TYPE_DOUBLE,
277  HELICS_INT = HELICS_DATA_TYPE_INT,
278 
279  HELICS_COMPLEX = HELICS_DATA_TYPE_COMPLEX,
280  HELICS_VECTOR = HELICS_DATA_TYPE_VECTOR,
281  HELICS_COMPLEX_VECTOR = HELICS_DATA_TYPE_COMPLEX_VECTOR,
282  HELICS_NAMED_POINT = HELICS_DATA_TYPE_NAMED_POINT,
283  HELICS_BOOL = HELICS_DATA_TYPE_BOOLEAN,
284  HELICS_TIME = HELICS_DATA_TYPE_TIME,
285  HELICS_CHAR = HELICS_DATA_TYPE_CHAR,
286  HELICS_CUSTOM = HELICS_DATA_TYPE_RAW,
287  HELICS_ANY = HELICS_DATA_TYPE_ANY,
288  HELICS_JSON = HELICS_DATA_TYPE_JSON,
289  HELICS_MULTI = HELICS_DATA_TYPE_MULTI,
290 };
291 
292 inline constexpr bool isBytesType(DataType type)
293 {
294  return (type == DataType::HELICS_ANY) || (type == DataType::HELICS_CUSTOM);
295 }
296 
298 HELICS_CXX_EXPORT const std::string& typeNameStringRef(DataType type);
299 
301 HELICS_CXX_EXPORT DataType getTypeFromString(std::string_view typeName);
302 
305 HELICS_CXX_EXPORT std::string_view getCleanedTypeName(std::string_view typeName);
306 
308 HELICS_CXX_EXPORT std::string helicsComplexString(double real, double imag);
310 HELICS_CXX_EXPORT std::string helicsComplexString(std::complex<double> val);
313 HELICS_CXX_EXPORT std::string helicsVectorString(const std::vector<double>& val);
315 HELICS_CXX_EXPORT std::string helicsIntString(std::int64_t val);
317 HELICS_CXX_EXPORT std::string helicsDoubleString(double val);
320 HELICS_CXX_EXPORT std::string helicsVectorString(const double* vals, size_t size);
323 HELICS_CXX_EXPORT std::string
324  helicsComplexVectorString(const std::vector<std::complex<double>>& val);
328 HELICS_CXX_EXPORT std::string helicsNamedPointString(const NamedPoint& point);
329 HELICS_CXX_EXPORT std::string helicsNamedPointString(std::string_view pointName, double val);
331 HELICS_CXX_EXPORT std::complex<double> helicsGetComplex(std::string_view val);
333 HELICS_CXX_EXPORT std::vector<double> helicsGetVector(std::string_view val);
334 HELICS_CXX_EXPORT void helicsGetVector(std::string_view val, std::vector<double>& data);
335 
337 HELICS_CXX_EXPORT std::vector<std::complex<double>> helicsGetComplexVector(std::string_view val);
338 
340 HELICS_CXX_EXPORT void helicsGetComplexVector(std::string_view val,
341  std::vector<std::complex<double>>& data);
342 
344 HELICS_CXX_EXPORT NamedPoint helicsGetNamedPoint(std::string_view val);
346 HELICS_CXX_EXPORT std::int64_t getIntFromString(std::string_view val);
348 HELICS_CXX_EXPORT double getDoubleFromString(std::string_view val);
350 HELICS_CXX_EXPORT std::complex<double> getComplexFromString(std::string_view val);
352 HELICS_CXX_EXPORT bool helicsBoolValue(std::string_view val);
354 HELICS_CXX_EXPORT double vectorNorm(const std::vector<double>& vec);
356 HELICS_CXX_EXPORT double vectorNorm(const std::vector<std::complex<double>>& vec);
361 HELICS_CXX_EXPORT SmallBuffer typeConvert(DataType type, double val);
362 HELICS_CXX_EXPORT SmallBuffer typeConvert(DataType type, int64_t val);
363 HELICS_CXX_EXPORT SmallBuffer typeConvert(DataType type, std::string_view val);
364 HELICS_CXX_EXPORT SmallBuffer typeConvert(DataType type, const std::vector<double>& val);
365 HELICS_CXX_EXPORT SmallBuffer typeConvert(DataType type, const double* vals, size_t size);
366 HELICS_CXX_EXPORT SmallBuffer typeConvertComplex(DataType type, const double* vals, size_t size);
367 HELICS_CXX_EXPORT SmallBuffer typeConvert(DataType type,
368  const std::vector<std::complex<double>>& val);
369 HELICS_CXX_EXPORT SmallBuffer typeConvert(DataType type, const std::complex<double>& val);
370 HELICS_CXX_EXPORT SmallBuffer typeConvert(DataType type, const NamedPoint& val);
371 HELICS_CXX_EXPORT SmallBuffer typeConvert(DataType type, std::string_view str, double val);
372 HELICS_CXX_EXPORT SmallBuffer typeConvert(DataType type, bool val);
373 HELICS_CXX_EXPORT SmallBuffer typeConvert(DataType type, char val);
374 HELICS_CXX_EXPORT SmallBuffer typeConvert(DataType type, Time val);
375 
377 template<class X>
378 constexpr DataType helicsType()
379 {
380  return DataType::HELICS_CUSTOM;
381 }
382 
383 template<>
384 constexpr DataType helicsType<int64_t>()
385 {
386  return DataType::HELICS_INT;
387 }
388 
389 template<>
390 constexpr DataType helicsType<bool>()
391 {
392  return DataType::HELICS_BOOL;
393 }
394 
395 template<>
396 constexpr DataType helicsType<std::string>()
397 {
398  return DataType::HELICS_STRING;
399 }
400 
401 template<>
402 constexpr DataType helicsType<NamedPoint>()
403 {
404  return DataType::HELICS_NAMED_POINT;
405 }
406 template<>
407 constexpr DataType helicsType<double>()
408 {
409  return DataType::HELICS_DOUBLE;
410 }
411 
412 template<>
413 constexpr DataType helicsType<Time>()
414 {
415  return DataType::HELICS_TIME;
416 }
417 
418 template<>
419 constexpr DataType helicsType<std::complex<double>>()
420 {
421  return DataType::HELICS_COMPLEX;
422 }
423 
424 template<>
425 constexpr DataType helicsType<std::vector<double>>()
426 {
427  return DataType::HELICS_VECTOR;
428 }
429 
430 template<>
431 constexpr DataType helicsType<std::vector<std::complex<double>>>()
432 {
433  return DataType::HELICS_COMPLEX_VECTOR;
434 }
435 
436 // check if the type is directly convertible to a base HelicsType
437 template<class X>
438 constexpr bool isConvertableType()
439 {
440  return false;
441 }
442 
443 template<>
444 constexpr bool isConvertableType<float>()
445 {
446  return true;
447 }
448 
449 template<>
450 constexpr bool isConvertableType<long double>()
451 {
452  return true;
453 }
454 
455 template<>
456 constexpr bool isConvertableType<int32_t>()
457 {
458  return true;
459 }
460 
461 template<>
462 constexpr bool isConvertableType<int16_t>()
463 {
464  return true;
465 }
466 
467 template<>
468 constexpr bool isConvertableType<uint16_t>()
469 {
470  return true;
471 }
472 
473 template<>
474 constexpr bool isConvertableType<char>()
475 {
476  return true;
477 }
478 
479 template<>
480 constexpr bool isConvertableType<unsigned char>()
481 {
482  return true;
483 }
484 
485 template<>
486 constexpr bool isConvertableType<uint64_t>()
487 {
488  return true;
489 }
490 
492 template<class X>
493 inline X invalidValue()
494 {
495  return X();
496 }
497 
499 constexpr double invalidDouble = -1e49;
500 
501 template<>
502 constexpr double invalidValue<double>()
503 {
504  return invalidDouble;
505 }
506 
507 template<>
508 constexpr int64_t invalidValue<int64_t>()
509 {
510  return (std::numeric_limits<int64_t>::min)();
511 }
512 
513 template<>
514 constexpr uint64_t invalidValue<uint64_t>()
515 {
516  return (std::numeric_limits<uint64_t>::max)();
517 }
518 
519 template<>
520 constexpr Time invalidValue<Time>()
521 {
522  return Time::minVal();
523 }
524 
525 template<>
526 inline NamedPoint invalidValue<NamedPoint>()
527 {
528  return {std::string(), std::nan("0")};
529 }
530 
531 template<>
532 constexpr std::complex<double> invalidValue<std::complex<double>>()
533 {
534  return {invalidValue<double>(), 0.0};
535 }
537 template<typename T>
538 using remove_cv_ref = std::remove_cv_t<std::remove_reference_t<T>>;
539 
540 constexpr int primaryType = 0;
541 constexpr int convertibleType = 1;
542 constexpr int nonConvertibleType = 2;
547 template<typename X>
549  std::conditional_t<helicsType<remove_cv_ref<X>>() != DataType::HELICS_CUSTOM,
550  std::integral_constant<int, primaryType>,
551  std::conditional_t<isConvertableType<remove_cv_ref<X>>(),
552  std::integral_constant<int, convertibleType>,
553  std::integral_constant<int, nonConvertibleType>>>;
554 } // namespace helics
Definition: helicsTypes.hpp:52
BaseType value() const noexcept
Definition: helicsTypes.hpp:75
IdentifierId & operator=(BaseType val) noexcept
Definition: helicsTypes.hpp:66
constexpr IdentifierId(const IdentifierId &id) noexcept
Definition: helicsTypes.hpp:64
IdentifierId & operator=(const IdentifierId &id)=default
bool operator!=(IdentifierId id) const noexcept
Definition: helicsTypes.hpp:79
bool operator<(IdentifierId id) const noexcept
Definition: helicsTypes.hpp:81
constexpr IdentifierId() noexcept
Definition: helicsTypes.hpp:60
static const Identifiers identity
the type of the identifier
Definition: helicsTypes.hpp:57
bool operator==(IdentifierId id) const noexcept
Definition: helicsTypes.hpp:77
constexpr IdentifierId(BaseType val) noexcept
Definition: helicsTypes.hpp:62
Definition: helicsTypes.hpp:110
double value
the data value for the named point
Definition: helicsTypes.hpp:113
bool operator<(const NamedPoint &np) const
Definition: helicsTypes.hpp:133
bool operator==(const NamedPoint &np) const
Definition: helicsTypes.hpp:123
bool operator>(const NamedPoint &np) const
Definition: helicsTypes.hpp:140
NamedPoint(std::string_view valname, double valval)
Definition: helicsTypes.hpp:118
std::string name
the text value for the named point
Definition: helicsTypes.hpp:112
NamedPoint()=default
@ HELICS_DATA_TYPE_INT
Definition: helics_enums.h:72
@ HELICS_DATA_TYPE_DOUBLE
Definition: helics_enums.h:70
@ HELICS_DATA_TYPE_STRING
Definition: helics_enums.h:68
@ HELICS_DATA_TYPE_TIME
Definition: helics_enums.h:84
@ HELICS_DATA_TYPE_COMPLEX_VECTOR
Definition: helics_enums.h:78
@ HELICS_DATA_TYPE_RAW
Definition: helics_enums.h:88
@ HELICS_DATA_TYPE_NAMED_POINT
Definition: helics_enums.h:80
@ HELICS_DATA_TYPE_JSON
Definition: helics_enums.h:90
@ HELICS_DATA_TYPE_COMPLEX
Definition: helics_enums.h:74
@ HELICS_DATA_TYPE_CHAR
Definition: helics_enums.h:86
@ HELICS_DATA_TYPE_ANY
Definition: helics_enums.h:95
@ HELICS_DATA_TYPE_MULTI
Definition: helics_enums.h:92
@ HELICS_DATA_TYPE_VECTOR
Definition: helics_enums.h:76
@ HELICS_DATA_TYPE_BOOLEAN
Definition: helics_enums.h:82
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
constexpr DataType helicsType()
Definition: helicsTypes.hpp:378
bool helicsBoolValue(std::string_view val)
Definition: helicsTypes.cpp:645
DataType
Definition: helicsTypes.hpp:273
NamedPoint helicsGetNamedPoint(std::string_view val)
Definition: helicsTypes.cpp:407
constexpr const char * typeNameString< unsigned char >()
Definition: helicsTypes.hpp:221
std::string helicsVectorString(const std::vector< double > &val)
Definition: helicsTypes.cpp:363
std::vector< std::complex< double > > helicsGetComplexVector(std::string_view val)
Definition: helicsTypes.cpp:400
constexpr double invalidDouble
defined constant for an invalid value as a double
Definition: helicsTypes.hpp:499
constexpr IdentifierType invalid_id_value
defining an invalid id value
Definition: helicsTypes.hpp:26
constexpr const char * typeNameString< int64_t >()
Definition: helicsTypes.hpp:239
std::vector< double > helicsGetVector(std::string_view val)
Definition: helicsTypes.cpp:393
constexpr const char * typeNameString< double >()
Definition: helicsTypes.hpp:194
const std::string & typeNameStringRef(DataType type)
Definition: helicsTypes.cpp:58
constexpr const char * typeNameString< bool >()
Definition: helicsTypes.hpp:208
std::string_view getCleanedTypeName(std::string_view typeName)
Definition: helicsTypes.cpp:264
std::int64_t getIntFromString(std::string_view val)
Definition: helicsTypes.cpp:481
std::complex< double > helicsGetComplex(std::string_view val)
Definition: helicsTypes.cpp:294
SmallBuffer typeConvert(DataType type, double val)
Definition: helicsTypes.cpp:732
std::string helicsDoubleString(double val)
Definition: helicsTypes.cpp:358
std::remove_cv_t< std::remove_reference_t< T > > remove_cv_ref
Helper template to remove const volatile references.
Definition: helicsTypes.hpp:538
constexpr const char * typeNameString< float >()
Definition: helicsTypes.hpp:201
constexpr const char * typeNameString()
Definition: helicsTypes.hpp:148
DataType getTypeFromString(std::string_view typeName)
Definition: helicsTypes.cpp:238
Identifiers
Definition: helicsTypes.hpp:30
std::string helicsIntString(std::int64_t val)
Definition: helicsTypes.cpp:353
constexpr const char * typeNameString< char >()
Definition: helicsTypes.hpp:215
double getDoubleFromString(std::string_view val)
Definition: helicsTypes.cpp:492
std::string helicsNamedPointString(const NamedPoint &point)
Definition: helicsTypes.cpp:378
std::uint32_t IdentifierType
specify the underlying type used in the identifiers
Definition: helicsTypes.hpp:22
double vectorNorm(const std::vector< double > &vec)
Definition: helicsTypes.cpp:103
std::string helicsComplexString(double real, double imag)
Definition: helicsTypes.cpp:121
InterfaceVisibility
Definition: helicsTypes.hpp:40
X invalidValue()
Definition: helicsTypes.hpp:493
std::conditional_t< helicsType< remove_cv_ref< X > >() !=DataType::HELICS_CUSTOM, std::integral_constant< int, primaryType >, std::conditional_t< isConvertableType< remove_cv_ref< X > >(), std::integral_constant< int, convertibleType >, std::integral_constant< int, nonConvertibleType > >> typeCategory
Definition: helicsTypes.hpp:553
TimeRepresentation< count_time< 9 > > Time
Definition: helicsTime.hpp:27
std::string helicsComplexVectorString(const std::vector< std::complex< double >> &val)
Definition: helicsTypes.cpp:373
std::complex< double > getComplexFromString(std::string_view val)
Definition: helicsTypes.cpp:463
std::size_t result_type
the result type of the hash code
Definition: helicsTypes.hpp:94
result_type operator()(argument_type const &key) const noexcept
Definition: helicsTypes.hpp:96