| helics 3.6.1

ValueConverter.hpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2017-2025,
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/SmallBuffer.hpp"
16 #include "data_view.hpp"
17 #include "helicsTypes.hpp"
18 #include "helics_cxx_export.h"
19
20 #include <cstddef>
21 #include <string>
22 #include <utility>
23 #include <vector>
24
25 namespace helics {
26
27 namespace detail {
28 constexpr size_t getBinaryLength(double /*val*/)
29 {
30 return 16;
31 }
32 constexpr size_t getBinaryLength(std::int64_t /*val*/)
33 {
34 return 16;
35 }
36 constexpr size_t getBinaryLength(std::complex<double> /*val*/)
37 {
38 return 24;
39 }
40 inline size_t getBinaryLength(std::string_view val)
41 {
42 return val.size() + 8;
43 }
44 inline size_t getBinaryLength(const std::vector<double>& val)
45 {
46 return val.size() * sizeof(double) + 8;
47 }
48 inline size_t getBinaryLength(const double* /*val*/, size_t size)
49 {
50 return size * sizeof(double) + 8;
51 }
52
53 inline size_t getBinaryLength(const NamedPoint& np)
54 {
55 return np.name.size() + 16;
56 }
57
58 inline size_t getBinaryLength(const std::vector<std::complex<double>>& cv)
59 {
60 return cv.size() * sizeof(double) * 2 + 8;
61 }
62 inline size_t getBinaryLength(const std::complex<double>* /*unused*/, size_t size)
63 {
64 return size * sizeof(double) * 2 + 8;
65 }
66
67 HELICS_CXX_EXPORT size_t convertToBinary(std::byte* data, double val);
68
69 HELICS_CXX_EXPORT size_t convertToBinary(std::byte* data, std::int64_t val);
70
71 HELICS_CXX_EXPORT size_t convertToBinary(std::byte* data, std::complex<double> val);
72
73 HELICS_CXX_EXPORT size_t convertToBinary(std::byte* data, std::string_view val);
74
75 HELICS_CXX_EXPORT size_t convertToBinary(std::byte* data, const NamedPoint& val);
76
77 HELICS_CXX_EXPORT size_t convertToBinary(std::byte* data, const std::vector<double>& val);
78
79 HELICS_CXX_EXPORT size_t convertToBinary(std::byte* data, const double* val, size_t size);
80
81 HELICS_CXX_EXPORT size_t convertToBinary(std::byte* data,
82 const std::vector<std::complex<double>>& val);
83
84 HELICS_CXX_EXPORT size_t convertToBinary(std::byte* data,
85 const std::complex<double>* val,
86 size_t size);
87
89 HELICS_CXX_EXPORT DataType detectType(const std::byte* data);
90
91 HELICS_CXX_EXPORT void convertFromBinary(const std::byte* data, double& val);
92 HELICS_CXX_EXPORT void convertFromBinary(const std::byte* data, std::int64_t& val);
93 HELICS_CXX_EXPORT void convertFromBinary(const std::byte* data, std::complex<double>& val);
94 HELICS_CXX_EXPORT void convertFromBinary(const std::byte* data, char* val);
95 HELICS_CXX_EXPORT void convertFromBinary(const std::byte* data, std::string& val);
96 HELICS_CXX_EXPORT void convertFromBinary(const std::byte* data, std::string_view& val);
97 HELICS_CXX_EXPORT void convertFromBinary(const std::byte* data, NamedPoint& val);
98
99 HELICS_CXX_EXPORT void convertFromBinary(const std::byte* data, std::vector<double>& val);
100 HELICS_CXX_EXPORT void convertFromBinary(const std::byte* data, double* val);
101
102 HELICS_CXX_EXPORT void convertFromBinary(const std::byte* data,
103 std::vector<std::complex<double>>& val);
104
108 HELICS_CXX_EXPORT size_t getDataSize(const std::byte* data);
109 } // namespace detail
110
112 template<class X>
113 class ValueConverter {
114 public:
115 using baseType = X;
117 static SmallBuffer convert(const X& val)
118 {
119 auto dv = SmallBuffer();
120 convert(val, dv);
121 return dv;
122 }
123
125 static void convert(const X& val, SmallBuffer& store)
126 {
127 store.resize(detail::getBinaryLength(val));
128 detail::convertToBinary(store.data(), val);
129 }
130
132 static void convert(const X* vals, size_t size, SmallBuffer& store)
133 {
134 store.resize(detail::getBinaryLength(vals, size));
135 detail::convertToBinary(store.data(), vals, size);
136 }
137
139 static SmallBuffer convert(const X* vals, size_t size)
140 {
141 auto dv = SmallBuffer();
142 convert(vals, size, dv);
143 return dv;
144 }
145
147 static X interpret(const data_view& block)
148 {
149 X val;
150 detail::convertFromBinary(block.bytes(), val);
151 return val;
152 }
153
155 static void interpret(const data_view& block, X& val)
156 {
157 detail::convertFromBinary(block.bytes(), val);
158 }
159
161 static std::string type() { return typeNameString<X>(); }
162 };
163
164 template<>
165 class ValueConverter<std::vector<std::string>> {
166 public:
167 using baseType = std::vector<std::string>;
169 static SmallBuffer convert(const std::vector<std::string>& val)
170 {
171 auto dv = SmallBuffer();
172 convert(val, dv);
173 return dv;
174 }
175
177 static void convert(const std::vector<std::string>& val, SmallBuffer& store);
178
180 static void interpret(const data_view& block, std::vector<std::string>& val);
181
183 static std::vector<std::string> interpret(const data_view& block)
184 {
185 std::vector<std::string> val;
186 interpret(block, val);
187 return val;
188 }
189
191 static std::string type() { return "string_vector"; }
192 };
193 } // namespace helics
helics::SmallBuffer
Definition: SmallBuffer.hpp:25
helics::SmallBuffer::data
std::byte * data() const
Definition: SmallBuffer.hpp:160
helics::ValueConverter< std::vector< std::string > >::type
static std::string type()
Definition: ValueConverter.hpp:191
helics::ValueConverter< std::vector< std::string > >::convert
static SmallBuffer convert(const std::vector< std::string > &val)
Definition: ValueConverter.hpp:169
helics::ValueConverter< std::vector< std::string > >::interpret
static std::vector< std::string > interpret(const data_view &block)
Definition: ValueConverter.hpp:183
helics::ValueConverter
Definition: ValueConverter.hpp:113
helics::ValueConverter::interpret
static void interpret(const data_view &block, X &val)
Definition: ValueConverter.hpp:155
helics::ValueConverter::convert
static SmallBuffer convert(const X &val)
Definition: ValueConverter.hpp:117
helics::ValueConverter::convert
static void convert(const X *vals, size_t size, SmallBuffer &store)
Definition: ValueConverter.hpp:132
helics::ValueConverter::convert
static SmallBuffer convert(const X *vals, size_t size)
Definition: ValueConverter.hpp:139
helics::ValueConverter::convert
static void convert(const X &val, SmallBuffer &store)
Definition: ValueConverter.hpp:125
helics::ValueConverter::type
static std::string type()
Definition: ValueConverter.hpp:161
helics::ValueConverter::interpret
static X interpret(const data_view &block)
Definition: ValueConverter.hpp:147
helics::data_view
Definition: data_view.hpp:22
helics::data_view::bytes
const std::byte * bytes() const noexcept
Definition: data_view.hpp:109
helicsTypes.hpp
helics
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
helics::DataType
DataType
Definition: helicsTypes.hpp:273