helics  2.8.1
helicsTypes.hpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2017-2021,
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 <typeinfo>
15 #include <utility>
16 #include <vector>
20 namespace helics {
21 using identifier_type = std::uint32_t;
22 using std::int32_t;
23 using std::int64_t;
24 
26  static_cast<identifier_type>(-1);
27 
29 enum class identifiers : char {
30  publication,
31  input,
32  filter,
33  endpoint,
34  query,
35 };
36 
39  local,
40  global,
41 };
42 
43 constexpr interface_visibility GLOBAL = interface_visibility::global;
44 constexpr interface_visibility LOCAL = interface_visibility::local;
45 
51 template<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 identifier_id_t() noexcept: ivalue(invalidValue) {}
62  constexpr explicit identifier_id_t(BaseType val) noexcept: ivalue(val) {}
64  constexpr identifier_id_t(const identifier_id_t& id) noexcept: ivalue(id.ivalue) {}
66  identifier_id_t& operator=(BaseType val) noexcept
67  {
68  ivalue = val;
69  return *this;
70  }
72  identifier_id_t& operator=(const identifier_id_t& id) = default;
73 
75  BaseType value() const noexcept { return ivalue; };
77  bool operator==(identifier_id_t id) const noexcept { return (ivalue == id.ivalue); }
79  bool operator!=(identifier_id_t id) const noexcept { return (ivalue != id.ivalue); }
81  bool operator<(identifier_id_t 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::identifier_id_t<BaseType, ID, invalidValue>> {
92  using argument_type =
94  using result_type = std::size_t;
95 
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 publication_id_t =
105  identifier_id_t<identifier_type, identifiers::publication, invalid_id_value>;
106 using input_id_t = identifier_id_t<identifier_type, identifiers::input, invalid_id_value>;
107 
108 using query_id_t = identifier_id_t<identifier_type, identifiers::query, invalid_id_value>;
109 
111 class NamedPoint {
112  public:
113  std::string name;
114  double value =
115  std::numeric_limits<double>::quiet_NaN();
116 
117  NamedPoint() = default;
119  NamedPoint(std::string valname, double valval): name(std::move(valname)), value(valval) {}
124  bool operator==(const NamedPoint& np) const
125  {
126  return ((std::isnan(value)) && (std::isnan(np.value))) ?
127  (name == np.name) :
128  ((value == np.value) && (name == np.name));
129  }
130  bool operator!=(const NamedPoint& np) const { return !operator==(np); }
134  bool operator<(const NamedPoint& np) const
135  {
136  return (name == np.name) ? (name < np.name) : (value < np.value);
137  }
141  bool operator>(const NamedPoint& np) const
142  {
143  return (name == np.name) ? (name > np.name) : (value > np.value);
144  }
145 };
146 
148 template<class X>
149 inline constexpr const char* typeNameString()
150 {
151  // this will probably not be the same on all platforms
152  return typeid(X).name();
153 }
154 namespace typestrings {
155  constexpr auto svecstr = "string_vector";
156  constexpr auto dvecstr = "double_vector";
157  constexpr auto cvecstr = "complex_vector";
158 
159  constexpr auto doublestr = "double";
160  constexpr auto floatstr = "float";
161  constexpr auto boolstr = "bool";
162 
163  constexpr auto charstr = "char";
164  constexpr auto ucharstr = "uchar";
165  constexpr auto i32str = "int32";
166  constexpr auto ui32str = "uint32";
167  constexpr auto i64str = "int64";
168  constexpr auto ui64str = "uint64";
169 
170  constexpr auto cfloatstr = "complex_f";
171  constexpr auto cdoublestr = "complex";
172  constexpr auto npstr = "named_point";
173  constexpr auto strstr = "string";
174 } // namespace typestrings
175 
176 template<>
177 inline constexpr const char* typeNameString<std::vector<std::string>>()
178 {
179  return typestrings::svecstr;
180 }
181 template<>
182 inline constexpr const char* typeNameString<std::vector<double>>()
183 {
184  return typestrings::dvecstr;
185 }
186 
187 template<>
188 inline constexpr const char* typeNameString<std::vector<std::complex<double>>>()
189 {
190  return typestrings::cvecstr;
191 }
192 
194 template<>
195 inline constexpr const char* typeNameString<double>()
196 {
197  return typestrings::doublestr;
198 }
199 
201 template<>
202 inline constexpr const char* typeNameString<float>()
203 {
204  return typestrings::floatstr;
205 }
206 
208 template<>
209 inline constexpr const char* typeNameString<bool>()
210 {
211  return typestrings::boolstr;
212 }
213 
215 template<>
216 inline constexpr const char* typeNameString<char>()
217 {
218  return typestrings::charstr;
219 }
221 template<>
222 inline constexpr const char* typeNameString<unsigned char>()
223 {
224  return typestrings::ucharstr;
225 }
227 template<>
228 inline constexpr const char* typeNameString<std::int32_t>()
229 {
230  return typestrings::i32str;
231 }
233 template<>
234 inline constexpr const char* typeNameString<std::uint32_t>()
235 {
236  return typestrings::ui32str;
237 }
239 template<>
240 inline constexpr const char* typeNameString<int64_t>()
241 {
242  return typestrings::i64str;
243 }
245 template<>
246 inline constexpr const char* typeNameString<std::uint64_t>()
247 {
248  return typestrings::ui64str;
249 }
251 template<>
252 inline constexpr const char* typeNameString<std::complex<float>>()
253 {
254  return typestrings::cfloatstr;
255 }
257 template<>
258 inline constexpr const char* typeNameString<std::complex<double>>()
259 {
260  return typestrings::cdoublestr;
261 }
262 template<>
263 inline constexpr const char* typeNameString<std::string>()
264 {
265  return typestrings::strstr;
266 }
267 
268 template<>
269 inline constexpr const char* typeNameString<NamedPoint>()
270 {
271  return typestrings::npstr;
272 }
273 
275 enum class data_type : int {
276  helics_string = helics_data_type_string,
277  helics_double = helics_data_type_double,
278  helics_int = helics_data_type_int,
279 
281  helics_vector = helics_data_type_vector,
282  helics_complex_vector = helics_data_type_complex_vector,
283  helics_named_point = helics_data_type_named_point,
286  helics_custom = helics_data_type_raw,
287  helics_any = helics_data_type_any,
288  helics_multi = helics_data_type_multi,
289  helics_json = helics_data_type_json,
290  helics_unknown = 262355,
291 
292 };
293 
294 inline constexpr bool isRawType(data_type type)
295 {
296  return (type == data_type::helics_any) || (type == data_type::helics_custom);
297 }
298 
300 HELICS_CXX_EXPORT const std::string& typeNameStringRef(data_type type);
301 
303 HELICS_CXX_EXPORT data_type getTypeFromString(const std::string& typeName);
304 
306 HELICS_CXX_EXPORT std::string helicsComplexString(double real, double imag);
308 HELICS_CXX_EXPORT std::string helicsComplexString(std::complex<double> val);
311 HELICS_CXX_EXPORT std::string helicsVectorString(const std::vector<double>& val);
314 HELICS_CXX_EXPORT std::string helicsVectorString(const double* vals, size_t size);
317 HELICS_CXX_EXPORT std::string
318  helicsComplexVectorString(const std::vector<std::complex<double>>& val);
322 HELICS_CXX_EXPORT std::string helicsNamedPointString(const NamedPoint& point);
323 HELICS_CXX_EXPORT std::string helicsNamedPointString(const std::string& pointName, double val);
324 HELICS_CXX_EXPORT std::string helicsNamedPointString(const char* pointName, double val);
326 HELICS_CXX_EXPORT std::complex<double> helicsGetComplex(const std::string& val);
328 HELICS_CXX_EXPORT std::vector<double> helicsGetVector(const std::string& val);
329 HELICS_CXX_EXPORT void helicsGetVector(const std::string& val, std::vector<double>& data);
330 
332 HELICS_CXX_EXPORT std::vector<std::complex<double>> helicsGetComplexVector(const std::string& val);
333 
335 HELICS_CXX_EXPORT void helicsGetComplexVector(const std::string& val,
336  std::vector<std::complex<double>>& data);
337 
339 HELICS_CXX_EXPORT NamedPoint helicsGetNamedPoint(const std::string& val);
341 HELICS_CXX_EXPORT double getDoubleFromString(const std::string& val);
343 HELICS_CXX_EXPORT std::complex<double> getComplexFromString(const std::string& val);
345 HELICS_CXX_EXPORT bool helicsBoolValue(const std::string& val);
347 HELICS_CXX_EXPORT double vectorNorm(const std::vector<double>& vec);
349 HELICS_CXX_EXPORT double vectorNorm(const std::vector<std::complex<double>>& vec);
354 HELICS_CXX_EXPORT data_block typeConvert(data_type type, double val);
355 HELICS_CXX_EXPORT data_block typeConvert(data_type type, int64_t val);
356 HELICS_CXX_EXPORT data_block typeConvert(data_type type, const char* val);
357 HELICS_CXX_EXPORT data_block typeConvert(data_type type, const std::string& val);
358 HELICS_CXX_EXPORT data_block typeConvert(data_type type, const std::vector<double>& val);
359 HELICS_CXX_EXPORT data_block typeConvert(data_type type, const double* vals, size_t size);
360 HELICS_CXX_EXPORT data_block typeConvert(data_type type,
361  const std::vector<std::complex<double>>& val);
362 HELICS_CXX_EXPORT data_block typeConvert(data_type type, const std::complex<double>& val);
363 HELICS_CXX_EXPORT data_block typeConvert(data_type type, const NamedPoint& val);
364 HELICS_CXX_EXPORT data_block typeConvert(data_type type, const char* str, double val);
365 HELICS_CXX_EXPORT data_block typeConvert(data_type type, const std::string& str, double val);
366 HELICS_CXX_EXPORT data_block typeConvert(data_type type, bool val);
367 
369 template<class X>
371 {
372  return data_type::helics_custom;
373 }
374 
375 template<>
376 constexpr data_type helicsType<int64_t>()
377 {
378  return data_type::helics_int;
379 }
380 
381 template<>
382 constexpr data_type helicsType<bool>()
383 {
384  return data_type::helics_bool;
385 }
386 
387 template<>
388 constexpr data_type helicsType<std::string>()
389 {
390  return data_type::helics_string;
391 }
392 
393 template<>
394 constexpr data_type helicsType<NamedPoint>()
395 {
396  return data_type::helics_named_point;
397 }
398 template<>
399 constexpr data_type helicsType<double>()
400 {
401  return data_type::helics_double;
402 }
403 
404 template<>
405 constexpr data_type helicsType<Time>()
406 {
407  return data_type::helics_time;
408 }
409 
410 template<>
411 constexpr data_type helicsType<std::complex<double>>()
412 {
413  return data_type::helics_complex;
414 }
415 
416 template<>
417 constexpr data_type helicsType<std::vector<double>>()
418 {
419  return data_type::helics_vector;
420 }
421 
422 template<>
423 constexpr data_type helicsType<std::vector<std::complex<double>>>()
424 {
425  return data_type::helics_complex_vector;
426 }
427 
428 // check if the type is directly convertible to a base HelicsType
429 template<class X>
430 constexpr bool isConvertableType()
431 {
432  return false;
433 }
434 
435 template<>
436 constexpr bool isConvertableType<float>()
437 {
438  return true;
439 }
440 
441 template<>
442 constexpr bool isConvertableType<long double>()
443 {
444  return true;
445 }
446 
447 template<>
448 constexpr bool isConvertableType<int32_t>()
449 {
450  return true;
451 }
452 
453 template<>
454 constexpr bool isConvertableType<int16_t>()
455 {
456  return true;
457 }
458 
459 template<>
460 constexpr bool isConvertableType<uint16_t>()
461 {
462  return true;
463 }
464 
465 template<>
466 constexpr bool isConvertableType<char>()
467 {
468  return true;
469 }
470 
471 template<>
472 constexpr bool isConvertableType<unsigned char>()
473 {
474  return true;
475 }
476 
477 template<>
478 constexpr bool isConvertableType<uint64_t>()
479 {
480  return true;
481 }
482 
484 template<class X>
485 inline X invalidValue()
486 {
487  return X();
488 }
489 
491 constexpr double invalidDouble = -1e48;
492 
493 template<>
494 constexpr double invalidValue<double>()
495 {
496  return invalidDouble;
497 }
498 
499 template<>
500 constexpr int64_t invalidValue<int64_t>()
501 {
502  return (std::numeric_limits<int64_t>::min)();
503 }
504 
505 template<>
506 constexpr uint64_t invalidValue<uint64_t>()
507 {
508  return (std::numeric_limits<uint64_t>::max)();
509 }
510 
511 template<>
512 constexpr Time invalidValue<Time>()
513 {
514  return Time::minVal();
515 }
516 
517 template<>
518 inline NamedPoint invalidValue<NamedPoint>()
519 {
520  return {std::string(), std::nan("0")};
521 }
522 
523 template<>
524 constexpr std::complex<double> invalidValue<std::complex<double>>()
525 {
526  return {invalidValue<double>(), 0.0};
527 }
529 template<typename T>
530 using remove_cv_ref = std::remove_cv_t<std::remove_reference_t<T>>;
531 
532 constexpr int primaryType = 0;
533 constexpr int convertibleType = 1;
534 constexpr int nonConvertibleType = 2;
539 template<typename X>
540 using typeCategory =
541  std::conditional_t<helicsType<remove_cv_ref<X>>() != data_type::helics_custom,
542  std::integral_constant<int, primaryType>,
543  std::conditional_t<isConvertableType<remove_cv_ref<X>>(),
544  std::integral_constant<int, convertibleType>,
545  std::integral_constant<int, nonConvertibleType>>>;
546 } // namespace helics
generateJsonString
std::string generateJsonString(const Json::Value &block)
Definition: JsonProcessingFunctions.cpp:97
data
@ data
print timing+data transmissions
Definition: loggingHelper.hpp:30
helics::identifier_id_t::value
BaseType value() const noexcept
Definition: helicsTypes.hpp:75
helicsTypes.hpp
helics_data_type_raw
@ helics_data_type_raw
Definition: helics_enums.h:71
helics::NamedPoint::name
std::string name
the text value for the named point
Definition: helicsTypes.hpp:113
helics::NamedPoint
Definition: helicsTypes.hpp:111
helics::identifier_type
std::uint32_t identifier_type
specify the underlying type used in the identifiers
Definition: helicsTypes.hpp:21
helics::identifier_id_t::operator==
bool operator==(identifier_id_t id) const noexcept
Definition: helicsTypes.hpp:77
helics::data_block
Definition: core-data.hpp:31
std::hash< helics::identifier_id_t< BaseType, ID, invalidValue > >::result_type
std::size_t result_type
the result type of the hash code
Definition: helicsTypes.hpp:94
helics::NamedPoint::NamedPoint
NamedPoint()=default
helics::helicsComplexVectorString
std::string helicsComplexVectorString(const std::vector< std::complex< double >> &val)
Definition: helicsTypes.cpp:282
helics::typeNameString< unsigned char >
constexpr const char * typeNameString< unsigned char >()
Definition: helicsTypes.hpp:222
helics::invalidValue
X invalidValue()
Definition: helicsTypes.hpp:485
helics::ValueConverter
Definition: ValueConverter.hpp:26
helics::helicsType
constexpr data_type helicsType()
Definition: helicsTypes.hpp:370
helics::typeNameString
constexpr const char * typeNameString()
Definition: helicsTypes.hpp:149
helics::NamedPoint::value
double value
the data value for the named point
Definition: helicsTypes.hpp:114
helics::helicsGetVector
std::vector< double > helicsGetVector(const std::string &val)
Definition: helicsTypes.cpp:334
helics::Time
TimeRepresentation< count_time< 9 > > Time
Definition: helics-time.hpp:27
helics_time
double helics_time
Definition: api-data.h:81
helics::identifiers
identifiers
Definition: helicsTypes.hpp:29
helics::typeNameString< char >
constexpr const char * typeNameString< char >()
Definition: helicsTypes.hpp:216
helics_data_type_multi
@ helics_data_type_multi
Definition: helics_enums.h:75
helics::typeNameString< bool >
constexpr const char * typeNameString< bool >()
Definition: helicsTypes.hpp:209
helics::invalid_id_value
constexpr identifier_type invalid_id_value
defining an invalid id value
Definition: helicsTypes.hpp:25
helics::helicsGetNamedPoint
NamedPoint helicsGetNamedPoint(const std::string &val)
Definition: helicsTypes.cpp:348
helics::vectorNorm
double vectorNorm(const std::vector< double > &vec)
Definition: helicsTypes.cpp:66
helics::NamedPoint::operator==
bool operator==(const NamedPoint &np) const
Definition: helicsTypes.hpp:124
helics_data_type_any
@ helics_data_type_any
Definition: helics_enums.h:77
helics::typeNameStringRef
const std::string & typeNameStringRef(data_type type)
Definition: helicsTypes.cpp:27
helics_complex
Definition: api-data.h:137
helics::helicsBoolValue
bool helicsBoolValue(const std::string &val)
Definition: helicsTypes.cpp:528
helics::typeCategory
std::conditional_t< helicsType< remove_cv_ref< X > >() !=data_type::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:545
helics::identifier_id_t
Definition: helicsTypes.hpp:52
helics::invalidDouble
constexpr double invalidDouble
defined constant for an invalid value as a double
Definition: helicsTypes.hpp:491
helics::helicsVectorString
std::string helicsVectorString(const std::vector< double > &val)
Definition: helicsTypes.cpp:244
std::hash< helics::identifier_id_t< BaseType, ID, invalidValue > >::operator()
result_type operator()(argument_type const &key) const noexcept
Definition: helicsTypes.hpp:96
helics_data_type_complex_vector
@ helics_data_type_complex_vector
Definition: helics_enums.h:63
helics_data_type_named_point
@ helics_data_type_named_point
Definition: helics_enums.h:65
helics::identifier_id_t::operator!=
bool operator!=(identifier_id_t id) const noexcept
Definition: helicsTypes.hpp:79
helics::typeNameString< int64_t >
constexpr const char * typeNameString< int64_t >()
Definition: helicsTypes.hpp:240
helics_data_type_double
@ helics_data_type_double
Definition: helics_enums.h:55
helics_data_type_int
@ helics_data_type_int
Definition: helics_enums.h:57
helics::NamedPoint::operator>
bool operator>(const NamedPoint &np) const
Definition: helicsTypes.hpp:141
helics::helicsGetComplexVector
std::vector< std::complex< double > > helicsGetComplexVector(const std::string &val)
Definition: helicsTypes.cpp:341
helics::remove_cv_ref
std::remove_cv_t< std::remove_reference_t< T > > remove_cv_ref
Helper template to remove const volatile references.
Definition: helicsTypes.hpp:530
helics_data_type_json
@ helics_data_type_json
Definition: helics_enums.h:73
helics::helicsComplexString
std::string helicsComplexString(double real, double imag)
Definition: helicsTypes.cpp:79
helics::identifier_id_t::identity
static const identifiers identity
the type of the identifier
Definition: helicsTypes.hpp:57
helics_data_type_string
@ helics_data_type_string
Definition: helics_enums.h:53
helics::typeNameString< float >
constexpr const char * typeNameString< float >()
Definition: helicsTypes.hpp:202
helics_data_type_vector
@ helics_data_type_vector
Definition: helics_enums.h:61
helics::NamedPoint::NamedPoint
NamedPoint(std::string valname, double valval)
Definition: helicsTypes.hpp:119
helics
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
helics::data_type
data_type
Definition: helicsTypes.hpp:275
helics_data_type_complex
@ helics_data_type_complex
Definition: helics_enums.h:59
helics_data_type_time
@ helics_data_type_time
Definition: helics_enums.h:69
helics::getDoubleFromString
double getDoubleFromString(const std::string &val)
Definition: helicsTypes.cpp:416
helics::identifier_id_t::identifier_id_t
constexpr identifier_id_t(BaseType val) noexcept
Definition: helicsTypes.hpp:62
helics::getComplexFromString
std::complex< double > getComplexFromString(const std::string &val)
Definition: helicsTypes.cpp:398
helics_complex
struct helics_complex helics_complex
helics::getTypeFromString
data_type getTypeFromString(const std::string &typeName)
Definition: helicsTypes.cpp:187
helics::typeNameString< double >
constexpr const char * typeNameString< double >()
Definition: helicsTypes.hpp:195
helics::typeConvert
data_block typeConvert(data_type type, double val)
Definition: helicsTypes.cpp:610
helics::identifier_id_t::operator<
bool operator<(identifier_id_t id) const noexcept
Definition: helicsTypes.hpp:81
helics::identifier_id_t::identifier_id_t
constexpr identifier_id_t(const identifier_id_t &id) noexcept
Definition: helicsTypes.hpp:64
helics::interface_visibility
interface_visibility
Definition: helicsTypes.hpp:38
helics::helicsNamedPointString
std::string helicsNamedPointString(const NamedPoint &point)
Definition: helicsTypes.cpp:300
helics_bool
int helics_bool
Definition: api-data.h:92
helics::identifier_id_t::identifier_id_t
constexpr identifier_id_t() noexcept
Definition: helicsTypes.hpp:60
ValueConverter.hpp
helics_data_type_boolean
@ helics_data_type_boolean
Definition: helics_enums.h:67
helics::identifier_id_t::operator=
identifier_id_t & operator=(BaseType val) noexcept
Definition: helicsTypes.hpp:66
helics::NamedPoint::operator<
bool operator<(const NamedPoint &np) const
Definition: helicsTypes.hpp:134
helics::helicsGetComplex
std::complex< double > helicsGetComplex(const std::string &val)
Definition: helicsTypes.cpp:207