helics 3.7.0
Loading...
Searching...
No Matches
helicsTypes.hpp
Go to the documentation of this file.
1/*
2Copyright (c) 2017-2026,
3Battelle Memorial Institute; Lawrence Livermore National Security, LLC; Alliance for Energy
4Innovation LLC. See the top-level NOTICE for additional details. All rights reserved.
5SPDX-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>
21namespace helics {
22using IdentifierType = std::uint32_t;
23using std::int32_t;
24using std::int64_t;
25
27 static_cast<IdentifierType>(-1);
28
30enum class Identifiers : char {
31 PUBLICATION = 'p',
32 INPUT = 'i',
33 FILTER = 'f',
34 ENDPOINT = 'e',
35 QUERY = 'q',
36 TRANSLATORS = 't'
37};
38
41 LOCAL,
42 GLOBAL,
43};
44
51template<typename BaseType, Identifiers ID, BaseType invalidValue>
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
88namespace std {
90template<typename BaseType, helics::Identifiers ID, BaseType invalidValue>
91struct hash<helics::IdentifierId<BaseType, ID, invalidValue>> {
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
103namespace helics {
104using PublicationId = IdentifierId<IdentifierType, Identifiers::PUBLICATION, invalid_id_value>;
105using InputId = IdentifierId<IdentifierType, Identifiers::INPUT, invalid_id_value>;
106
107using QueryId = IdentifierId<IdentifierType, Identifiers::QUERY, invalid_id_value>;
108
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
147template<class X>
148inline constexpr const char* typeNameString()
149{
150 // this will probably not be the same on all platforms
151 return typeid(X).name();
152}
153namespace 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
175template<>
176inline constexpr const char* typeNameString<std::vector<std::string>>()
177{
178 return typestrings::svecstr;
179}
180template<>
181inline constexpr const char* typeNameString<std::vector<double>>()
182{
183 return typestrings::dvecstr;
184}
185
186template<>
187inline constexpr const char* typeNameString<std::vector<std::complex<double>>>()
188{
189 return typestrings::cvecstr;
190}
191
193template<>
194inline constexpr const char* typeNameString<double>()
195{
196 return typestrings::doublestr;
197}
198
200template<>
201inline constexpr const char* typeNameString<float>()
202{
203 return typestrings::floatstr;
204}
205
207template<>
208inline constexpr const char* typeNameString<bool>()
209{
210 return typestrings::boolstr;
211}
212
214template<>
215inline constexpr const char* typeNameString<char>()
216{
217 return typestrings::charstr;
218}
220template<>
221inline constexpr const char* typeNameString<unsigned char>()
222{
223 return typestrings::ucharstr;
224}
226template<>
227inline constexpr const char* typeNameString<std::int32_t>()
228{
229 return typestrings::i32str;
230}
232template<>
233inline constexpr const char* typeNameString<std::uint32_t>()
234{
235 return typestrings::ui32str;
236}
238template<>
239inline constexpr const char* typeNameString<int64_t>()
240{
241 return typestrings::i64str;
242}
244template<>
245inline constexpr const char* typeNameString<std::uint64_t>()
246{
247 return typestrings::ui64str;
248}
250template<>
251inline constexpr const char* typeNameString<std::complex<float>>()
252{
253 return typestrings::cfloatstr;
254}
256template<>
257inline constexpr const char* typeNameString<std::complex<double>>()
258{
259 return typestrings::cdoublestr;
260}
261template<>
262inline constexpr const char* typeNameString<std::string>()
263{
264 return typestrings::strstr;
265}
266
267template<>
268inline constexpr const char* typeNameString<NamedPoint>()
269{
270 return typestrings::npstr;
271}
273enum 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
292inline constexpr bool isBytesType(DataType type)
293{
294 return (type == DataType::HELICS_ANY) || (type == DataType::HELICS_CUSTOM);
295}
296
298HELICS_CXX_EXPORT const std::string& typeNameStringRef(DataType type);
299
301HELICS_CXX_EXPORT DataType getTypeFromString(std::string_view typeName);
302
305HELICS_CXX_EXPORT std::string_view getCleanedTypeName(std::string_view typeName);
306
308HELICS_CXX_EXPORT std::string helicsComplexString(double real, double imag);
310HELICS_CXX_EXPORT std::string helicsComplexString(std::complex<double> val);
313HELICS_CXX_EXPORT std::string helicsVectorString(const std::vector<double>& val);
315HELICS_CXX_EXPORT std::string helicsIntString(std::int64_t val);
317HELICS_CXX_EXPORT std::string helicsDoubleString(double val);
320HELICS_CXX_EXPORT std::string helicsVectorString(const double* vals, size_t size);
323HELICS_CXX_EXPORT std::string
324 helicsComplexVectorString(const std::vector<std::complex<double>>& val);
328HELICS_CXX_EXPORT std::string helicsNamedPointString(const NamedPoint& point);
329HELICS_CXX_EXPORT std::string helicsNamedPointString(std::string_view pointName, double val);
331HELICS_CXX_EXPORT std::complex<double> helicsGetComplex(std::string_view val);
333HELICS_CXX_EXPORT std::vector<double> helicsGetVector(std::string_view val);
334HELICS_CXX_EXPORT void helicsGetVector(std::string_view val, std::vector<double>& data);
335
337HELICS_CXX_EXPORT std::vector<std::complex<double>> helicsGetComplexVector(std::string_view val);
338
340HELICS_CXX_EXPORT void helicsGetComplexVector(std::string_view val,
341 std::vector<std::complex<double>>& data);
342
344HELICS_CXX_EXPORT NamedPoint helicsGetNamedPoint(std::string_view val);
346HELICS_CXX_EXPORT std::int64_t getIntFromString(std::string_view val);
348HELICS_CXX_EXPORT double getDoubleFromString(std::string_view val);
350HELICS_CXX_EXPORT std::complex<double> getComplexFromString(std::string_view val);
352HELICS_CXX_EXPORT bool helicsBoolValue(std::string_view val);
354HELICS_CXX_EXPORT double vectorNorm(const std::vector<double>& vec);
356HELICS_CXX_EXPORT double vectorNorm(const std::vector<std::complex<double>>& vec);
361HELICS_CXX_EXPORT SmallBuffer typeConvert(DataType type, double val);
362HELICS_CXX_EXPORT SmallBuffer typeConvert(DataType type, int64_t val);
363HELICS_CXX_EXPORT SmallBuffer typeConvert(DataType type, std::string_view val);
364HELICS_CXX_EXPORT SmallBuffer typeConvert(DataType type, const std::vector<double>& val);
365HELICS_CXX_EXPORT SmallBuffer typeConvert(DataType type, const double* vals, size_t size);
366HELICS_CXX_EXPORT SmallBuffer typeConvertComplex(DataType type, const double* vals, size_t size);
367HELICS_CXX_EXPORT SmallBuffer typeConvert(DataType type,
368 const std::vector<std::complex<double>>& val);
369HELICS_CXX_EXPORT SmallBuffer typeConvert(DataType type, const std::complex<double>& val);
370HELICS_CXX_EXPORT SmallBuffer typeConvert(DataType type, const NamedPoint& val);
371HELICS_CXX_EXPORT SmallBuffer typeConvert(DataType type, std::string_view str, double val);
372HELICS_CXX_EXPORT SmallBuffer typeConvert(DataType type, bool val);
373HELICS_CXX_EXPORT SmallBuffer typeConvert(DataType type, char val);
374HELICS_CXX_EXPORT SmallBuffer typeConvert(DataType type, Time val);
375
377template<class X>
379{
380 return DataType::HELICS_CUSTOM;
381}
382
383template<>
384constexpr DataType helicsType<int64_t>()
385{
386 return DataType::HELICS_INT;
387}
388
389template<>
390constexpr DataType helicsType<bool>()
391{
392 return DataType::HELICS_BOOL;
393}
394
395template<>
396constexpr DataType helicsType<std::string>()
397{
398 return DataType::HELICS_STRING;
399}
400
401template<>
402constexpr DataType helicsType<NamedPoint>()
403{
404 return DataType::HELICS_NAMED_POINT;
405}
406template<>
407constexpr DataType helicsType<double>()
408{
409 return DataType::HELICS_DOUBLE;
410}
411
412template<>
413constexpr DataType helicsType<Time>()
414{
415 return DataType::HELICS_TIME;
416}
417
418template<>
419constexpr DataType helicsType<std::complex<double>>()
420{
421 return DataType::HELICS_COMPLEX;
422}
423
424template<>
425constexpr DataType helicsType<std::vector<double>>()
426{
427 return DataType::HELICS_VECTOR;
428}
429
430template<>
431constexpr 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
437template<class X>
438constexpr bool isConvertableType()
439{
440 return false;
441}
442
443template<>
444constexpr bool isConvertableType<float>()
445{
446 return true;
447}
448
449template<>
450constexpr bool isConvertableType<long double>()
451{
452 return true;
453}
454
455template<>
456constexpr bool isConvertableType<int32_t>()
457{
458 return true;
459}
460
461template<>
462constexpr bool isConvertableType<int16_t>()
463{
464 return true;
465}
466
467template<>
468constexpr bool isConvertableType<uint16_t>()
469{
470 return true;
471}
472
473template<>
474constexpr bool isConvertableType<char>()
475{
476 return true;
477}
478
479template<>
480constexpr bool isConvertableType<unsigned char>()
481{
482 return true;
483}
484
485template<>
486constexpr bool isConvertableType<uint64_t>()
487{
488 return true;
489}
490
492template<class X>
493inline X invalidValue()
494{
495 return X();
496}
497
499constexpr double invalidDouble = HELICS_INVALID_DOUBLE;
500
501template<>
502constexpr double invalidValue<double>()
503{
504 return invalidDouble;
505}
506
507template<>
508constexpr int64_t invalidValue<int64_t>()
509{
510 return (std::numeric_limits<int64_t>::min)();
511}
512
513template<>
514constexpr uint64_t invalidValue<uint64_t>()
515{
516 return (std::numeric_limits<uint64_t>::max)();
517}
518
519template<>
520constexpr Time invalidValue<Time>()
521{
522 return Time::minVal();
523}
524
525template<>
526inline NamedPoint invalidValue<NamedPoint>()
527{
528 return {std::string(), std::nan("0")};
529}
530
531template<>
532constexpr std::complex<double> invalidValue<std::complex<double>>()
533{
534 return {invalidValue<double>(), 0.0};
535}
537template<typename T>
538using remove_cv_ref = std::remove_cv_t<std::remove_reference_t<T>>;
539
540constexpr int primaryType = 0;
541constexpr int convertibleType = 1;
542constexpr int nonConvertibleType = 2;
547template<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
constexpr IdentifierId(const IdentifierId &id) noexcept
Definition helicsTypes.hpp:64
IdentifierId & operator=(BaseType val) noexcept
Definition helicsTypes.hpp:66
bool operator!=(IdentifierId id) const noexcept
Definition helicsTypes.hpp:79
bool operator<(IdentifierId id) const noexcept
Definition helicsTypes.hpp:81
IdentifierId & operator=(const IdentifierId &id)=default
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:74
@ HELICS_DATA_TYPE_DOUBLE
Definition helics_enums.h:72
@ HELICS_DATA_TYPE_STRING
Definition helics_enums.h:70
@ HELICS_DATA_TYPE_TIME
Definition helics_enums.h:86
@ HELICS_DATA_TYPE_COMPLEX_VECTOR
Definition helics_enums.h:80
@ HELICS_DATA_TYPE_RAW
Definition helics_enums.h:90
@ HELICS_DATA_TYPE_NAMED_POINT
Definition helics_enums.h:82
@ HELICS_DATA_TYPE_JSON
Definition helics_enums.h:92
@ HELICS_DATA_TYPE_COMPLEX
Definition helics_enums.h:76
@ HELICS_DATA_TYPE_CHAR
Definition helics_enums.h:88
@ HELICS_DATA_TYPE_ANY
Definition helics_enums.h:97
@ HELICS_DATA_TYPE_MULTI
Definition helics_enums.h:94
@ HELICS_DATA_TYPE_VECTOR
Definition helics_enums.h:78
@ HELICS_DATA_TYPE_BOOLEAN
Definition helics_enums.h:84
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:658
DataType
Definition helicsTypes.hpp:273
NamedPoint helicsGetNamedPoint(std::string_view val)
Definition helicsTypes.cpp:417
constexpr const char * typeNameString()
Definition helicsTypes.hpp:148
std::string helicsVectorString(const std::vector< double > &val)
Definition helicsTypes.cpp:373
std::vector< std::complex< double > > helicsGetComplexVector(std::string_view val)
Definition helicsTypes.cpp:410
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
std::vector< double > helicsGetVector(std::string_view val)
Definition helicsTypes.cpp:403
const std::string & typeNameStringRef(DataType type)
Definition helicsTypes.cpp:64
std::string_view getCleanedTypeName(std::string_view typeName)
Definition helicsTypes.cpp:274
constexpr const char * typeNameString< std::int32_t >()
Definition helicsTypes.hpp:227
std::int64_t getIntFromString(std::string_view val)
Definition helicsTypes.cpp:492
constexpr const char * typeNameString< float >()
Definition helicsTypes.hpp:201
std::complex< double > helicsGetComplex(std::string_view val)
Definition helicsTypes.cpp:304
SmallBuffer typeConvert(DataType type, double val)
Definition helicsTypes.cpp:745
std::string helicsDoubleString(double val)
Definition helicsTypes.cpp:368
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< unsigned char >()
Definition helicsTypes.hpp:221
DataType getTypeFromString(std::string_view typeName)
Definition helicsTypes.cpp:248
constexpr const char * typeNameString< char >()
Definition helicsTypes.hpp:215
constexpr const char * typeNameString< std::uint32_t >()
Definition helicsTypes.hpp:233
Identifiers
Definition helicsTypes.hpp:30
constexpr const char * typeNameString< bool >()
Definition helicsTypes.hpp:208
std::string helicsIntString(std::int64_t val)
Definition helicsTypes.cpp:363
std::string helicsComplexVectorString(const std::vector< std::complex< double > > &val)
Definition helicsTypes.cpp:383
constexpr const char * typeNameString< double >()
Definition helicsTypes.hpp:194
double getDoubleFromString(std::string_view val)
Definition helicsTypes.cpp:503
constexpr const char * typeNameString< int64_t >()
Definition helicsTypes.hpp:239
std::string helicsNamedPointString(const NamedPoint &point)
Definition helicsTypes.cpp:388
constexpr const char * typeNameString< std::uint64_t >()
Definition helicsTypes.hpp:245
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:109
std::string helicsComplexString(double real, double imag)
Definition helicsTypes.cpp:131
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::complex< double > getComplexFromString(std::string_view val)
Definition helicsTypes.cpp:474
STL namespace.
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