helics  3.0.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/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*/) { return 16; }
29  constexpr size_t getBinaryLength(std::int64_t /*val*/) { return 16; }
30  constexpr size_t getBinaryLength(std::complex<double> /*val*/) { return 24; }
31  inline size_t getBinaryLength(std::string_view val) { return val.size() + 8; }
32  inline size_t getBinaryLength(const std::vector<double>& val)
33  {
34  return val.size() * sizeof(double) + 8;
35  }
36  inline size_t getBinaryLength(const double* /*val*/, size_t size)
37  {
38  return size * sizeof(double) + 8;
39  }
40 
41  inline size_t getBinaryLength(const NamedPoint& np) { return np.name.size() + 16; }
42 
43  inline size_t getBinaryLength(const std::vector<std::complex<double>>& cv)
44  {
45  return cv.size() * sizeof(double) * 2 + 8;
46  }
47  inline size_t getBinaryLength(const std::complex<double>* /*unused*/, size_t size)
48  {
49  return size * sizeof(double) * 2 + 8;
50  }
51 
52  HELICS_CXX_EXPORT size_t convertToBinary(std::byte* data, double val);
53 
54  HELICS_CXX_EXPORT size_t convertToBinary(std::byte* data, std::int64_t val);
55 
56  HELICS_CXX_EXPORT size_t convertToBinary(std::byte* data, std::complex<double> val);
57 
58  HELICS_CXX_EXPORT size_t convertToBinary(std::byte* data, std::string_view val);
59 
60  HELICS_CXX_EXPORT size_t convertToBinary(std::byte* data, const NamedPoint& val);
61 
62  HELICS_CXX_EXPORT size_t convertToBinary(std::byte* data, const std::vector<double>& val);
63 
64  HELICS_CXX_EXPORT size_t convertToBinary(std::byte* data, const double* val, size_t size);
65 
66  HELICS_CXX_EXPORT size_t convertToBinary(std::byte* data,
67  const std::vector<std::complex<double>>& val);
68 
69  HELICS_CXX_EXPORT size_t convertToBinary(std::byte* data,
70  const std::complex<double>* val,
71  size_t size);
72 
74  HELICS_CXX_EXPORT DataType detectType(const std::byte* data);
75 
76  HELICS_CXX_EXPORT void convertFromBinary(const std::byte* data, double& val);
77  HELICS_CXX_EXPORT void convertFromBinary(const std::byte* data, std::int64_t& val);
78  HELICS_CXX_EXPORT void convertFromBinary(const std::byte* data, std::complex<double>& val);
79  HELICS_CXX_EXPORT void convertFromBinary(const std::byte* data, char* val);
80  HELICS_CXX_EXPORT void convertFromBinary(const std::byte* data, std::string& val);
81  HELICS_CXX_EXPORT void convertFromBinary(const std::byte* data, std::string_view& val);
82  HELICS_CXX_EXPORT void convertFromBinary(const std::byte* data, NamedPoint& val);
83 
84  HELICS_CXX_EXPORT void convertFromBinary(const std::byte* data, std::vector<double>& val);
85  HELICS_CXX_EXPORT void convertFromBinary(const std::byte* data, double* val);
86 
87  HELICS_CXX_EXPORT void convertFromBinary(const std::byte* data,
88  std::vector<std::complex<double>>& val);
89 
93  HELICS_CXX_EXPORT size_t getDataSize(const std::byte* data);
94 } // namespace detail
95 
97 template<class X>
99  public:
100  using baseType = X;
102  static SmallBuffer convert(const X& val)
103  {
104  auto dv = SmallBuffer();
105  convert(val, dv);
106  return dv;
107  }
108 
110  static void convert(const X& val, SmallBuffer& store)
111  {
112  store.resize(detail::getBinaryLength(val));
113  detail::convertToBinary(store.data(), val);
114  }
115 
117  static void convert(const X* vals, size_t size, SmallBuffer& store)
118  {
119  store.resize(detail::getBinaryLength(vals, size));
120  detail::convertToBinary(store.data(), vals, size);
121  }
122 
124  static SmallBuffer convert(const X* vals, size_t size)
125  {
126  auto dv = SmallBuffer();
127  convert(vals, size, dv);
128  return dv;
129  }
130 
132  static X interpret(const data_view& block)
133  {
134  X val;
135  detail::convertFromBinary(block.bytes(), val);
136  return val;
137  }
138 
140  static void interpret(const data_view& block, X& val)
141  {
142  detail::convertFromBinary(block.bytes(), val);
143  }
144 
146  static std::string type() { return typeNameString<X>(); }
147 };
148 
149 template<>
150 class ValueConverter<std::vector<std::string>> {
151  public:
152  using baseType = std::vector<std::string>;
154  static SmallBuffer convert(const std::vector<std::string>& val)
155  {
156  auto dv = SmallBuffer();
157  convert(val, dv);
158  return dv;
159  }
160 
162  static void convert(const std::vector<std::string>& val, SmallBuffer& store);
163 
165  static void interpret(const data_view& block, std::vector<std::string>& val);
166 
168  static std::vector<std::string> interpret(const data_view& block)
169  {
170  std::vector<std::string> val;
171  interpret(block, val);
172  return val;
173  }
174 
176  static std::string type() { return "string_vector"; }
177 };
178 } // namespace helics
helicsTypes.hpp
helics::SmallBuffer
Definition: SmallBuffer.hpp:24
helics::ValueConverter::interpret
static X interpret(const data_view &block)
Definition: ValueConverter.hpp:132
helics::ValueConverter
Definition: ValueConverter.hpp:98
helics::HELICS_UNKNOWN
@ HELICS_UNKNOWN
unknown state
Definition: CoreTypes.hpp:29
helics::ValueConverter::convert
static void convert(const X *vals, size_t size, SmallBuffer &store)
Definition: ValueConverter.hpp:117
helics::data_view
Definition: data_view.hpp:22
helics::ValueConverter::convert
static void convert(const X &val, SmallBuffer &store)
Definition: ValueConverter.hpp:110
helics::DataType
DataType
Definition: helicsTypes.hpp:272
helics::ValueConverter< std::vector< std::string > >::interpret
static std::vector< std::string > interpret(const data_view &block)
Definition: ValueConverter.hpp:168
helics::ValueConverter::convert
static SmallBuffer convert(const X &val)
Definition: ValueConverter.hpp:102
helics::ValueConverter::type
static std::string type()
Definition: ValueConverter.hpp:146
helics::ValueConverter::interpret
static void interpret(const data_view &block, X &val)
Definition: ValueConverter.hpp:140
helics::data_view::bytes
const std::byte * bytes() const noexcept
Definition: data_view.hpp:109
helics
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
helics::SmallBuffer::data
std::byte * data() const
Definition: SmallBuffer.hpp:144
helics::ValueConverter< std::vector< std::string > >::type
static std::string type()
Definition: ValueConverter.hpp:176
ValueConverter.hpp
helics::ValueConverter::convert
static SmallBuffer convert(const X *vals, size_t size)
Definition: ValueConverter.hpp:124
helics::ValueConverter< std::vector< std::string > >::convert
static SmallBuffer convert(const std::vector< std::string > &val)
Definition: ValueConverter.hpp:154