helics  2.8.1
ValueConverter.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 
15 #include "../core/Core.hpp"
16 #include "data_view.hpp"
17 #include "helicsTypes.hpp"
18 
19 #include <string>
20 #include <utility>
21 #include <vector>
22 
23 namespace helics {
25 template<class X>
27  public:
28  using baseType = X;
30  static data_block convert(const X& val);
31 
33  static void convert(const X& val, data_block& store);
34 
36  static void convert(const X* vals, size_t size, data_block& store);
37 
39  static data_block convert(const X* vals, size_t size);
40 
42  static X interpret(const data_view& block);
43 
45  static void interpret(const data_view& block, X& val);
46 
48  static std::string type() { return typeNameString<X>(); }
49 };
50 
52 template<>
53 class ValueConverter<std::string> {
54  public:
55  using baseType = std::string;
56  static data_block convert(std::string&& val) { return data_block(std::move(val)); }
57  static data_block convert(const std::string& val) { return data_block(val); }
58  static void convert(const std::string& val, data_block& store) { store = val; }
59  static std::string interpret(const data_view& block) { return block.string(); }
60  static void interpret(const data_view& block, std::string& val) { val = interpret(block); }
61  static std::string type() { return "string"; }
62 };
63 } // namespace helics
64 
65 // This should be at the end since it depends on the definitions in here
66 #ifndef HELICS_CXX_STATIC_DEFINE
67 # include "ValueConverter_impl.hpp"
68 #endif
69 
70 namespace helics {
71 namespace detail {
73  HELICS_CXX_EXPORT data_type detectType3(const unsigned char* data);
74 
75  HELICS_CXX_EXPORT void convertFromBinary3(const unsigned char* data, double& val);
76  HELICS_CXX_EXPORT void convertFromBinary3(const unsigned char* data, std::int64_t& val);
77  HELICS_CXX_EXPORT void convertFromBinary3(const unsigned char* data, std::complex<double>& val);
78  HELICS_CXX_EXPORT void convertFromBinary3(const unsigned char* data, char* val);
79  HELICS_CXX_EXPORT void convertFromBinary3(const unsigned char* data, std::string& val);
80  HELICS_CXX_EXPORT void convertFromBinary3(const unsigned char* data, NamedPoint& val);
81 
82  HELICS_CXX_EXPORT void convertFromBinary3(const unsigned char* data, std::vector<double>& val);
83  HELICS_CXX_EXPORT void convertFromBinary3(const unsigned char* data, double* val);
84 
85  HELICS_CXX_EXPORT void convertFromBinary3(const unsigned char* data,
86  std::vector<std::complex<double>>& val);
87 
91  HELICS_CXX_EXPORT size_t getDataSize3(const unsigned char* data);
92 } // namespace detail
93 
95 template<class X>
97  public:
98  using baseType = X;
99 
101  static X interpret(const data_view& block)
102  {
103  X val;
104  if (detail::detectType3(reinterpret_cast<const unsigned char*>(block.data())) ==
105  helicsType<X>()) {
106  detail::convertFromBinary3(reinterpret_cast<const unsigned char*>(block.data()), val);
107  } else {
108  ValueConverter<X>::interpret(block, val);
109  }
110 
111  return val;
112  }
113 
115  static void interpret(const data_view& block, X& val)
116  {
117  if (detail::detectType3(reinterpret_cast<const unsigned char*>(block.data())) ==
118  helicsType<X>()) {
119  detail::convertFromBinary3(reinterpret_cast<const unsigned char*>(block.data()), val);
120  } else {
121  ValueConverter<X>::interpret(block, val);
122  }
123  }
124 
126  static std::string type() { return typeNameString<X>(); }
127 };
128 
129 } // namespace helics
data
@ data
print timing+data transmissions
Definition: loggingHelper.hpp:30
helicsTypes.hpp
helics::ValueConverter3::interpret
static void interpret(const data_view &block, X &val)
Definition: ValueConverter.hpp:115
helics::data_block
Definition: core-data.hpp:31
helics::ValueConverter::interpret
static X interpret(const data_view &block)
Definition: ValueConverter_impl.hpp:317
helics::ValueConverter
Definition: ValueConverter.hpp:26
helics::data_view
Definition: data_view.hpp:22
helics::ValueConverter3::interpret
static X interpret(const data_view &block)
Definition: ValueConverter.hpp:101
helics::ValueConverter3::type
static std::string type()
Definition: ValueConverter.hpp:126
helics::ValueConverter::convert
static data_block convert(const X &val)
Definition: ValueConverter_impl.hpp:291
helics::data_view::string
std::string string() const
Definition: data_view.hpp:113
loadJsonStr
Json::Value loadJsonStr(const std::string &jsonString)
Definition: JsonProcessingFunctions.cpp:50
helics::ValueConverter::type
static std::string type()
Definition: ValueConverter.hpp:48
helics::ValueConverter3
Definition: ValueConverter.hpp:96
helics::data_view::data
const char * data() const noexcept
Definition: data_view.hpp:105
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_complex
struct helics_complex helics_complex
ValueConverter.hpp