helics  3.3.0
DataBuffer.hpp
1 /*
2 Copyright (c) 2017-2022,
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 #ifndef HELICS_CPP98_DATABUFFER_HPP_
8 #define HELICS_CPP98_DATABUFFER_HPP_
9 
10 #include "helics/helics.h"
11 #include "helicsExceptions.hpp"
12 
13 #include <complex>
14 #include <string>
15 #include <vector>
16 
17 namespace helicscpp {
18 class DataBuffer {
19  public:
20  DataBuffer() HELICS_NOTHROW: buff(helicsCreateDataBuffer(0)) {}
21  explicit DataBuffer(int capacity): buff(helicsCreateDataBuffer(capacity)) {}
22 
23  void toBytes(double val) { helicsDoubleToBytes(val, buff); }
24  void toBytes(int64_t val) { helicsIntegerToBytes(val, buff); }
25  void toBytes(const std::string& val) { helicsStringToBytes(val.c_str(), buff); }
26  void toBytes(const std::vector<double>& val)
27  {
28  helicsVectorToBytes(val.data(), static_cast<int>(val.size()), buff);
29  }
30  void toBytes(const std::complex<double> val)
31  {
32  helicsComplexToBytes(val.real(), val.imag(), buff);
33  }
34  void toBytes(const double* vals, int size) { helicsVectorToBytes(vals, size, buff); }
35  void toBytes(const std::string& name, double val)
36  {
37  helicsNamedPointToBytes(name.c_str(), val, buff);
38  }
39  void toBytes(bool val) { helicsBooleanToBytes(val ? HELICS_TRUE : HELICS_FALSE, buff); }
40  void toBytes(char val) { helicsCharToBytes(val, buff); }
41 
43  int size() { return helicsDataBufferSize(buff); }
44 
46  int capacity() { return helicsDataBufferCapacity(buff); }
47 
49  int stringSize() { return helicsDataBufferStringSize(buff); }
50 
52  std::string toString()
53  {
54  int size = stringSize();
55  std::string result;
56 
57  result.resize(static_cast<size_t>(size) + 1);
58  // this function results in a null terminated string
59  helicsDataBufferToString(buff, &result[0], size + 1, &size);
60  if (!(result.empty()) && (result[static_cast<size_t>(size) - 1] == '\0')) {
61  result.resize(static_cast<size_t>(size) - 1);
62  } else {
63  result.resize(size);
64  }
65  return result;
66  }
67 
69  void toString(std::string& str)
70  {
71  int size = stringSize();
72  str.resize(static_cast<size_t>(size) + 1);
73  // this function results in a null terminated string
74  helicsDataBufferToString(buff, &str[0], size + 1, &size);
75  if (!(str.empty()) && (str[static_cast<size_t>(size) - 1] == '\0')) {
76  str.resize(static_cast<size_t>(size) - 1);
77  } else {
78  str.resize(size);
79  }
80  }
81 
83  void toNamedPoint(std::string& name, double* val)
84  {
85  int size = helicsDataBufferStringSize(buff);
86 
87  name.resize(static_cast<size_t>(size) + 1);
88  // this function results in a null terminated string
89  helicsDataBufferToNamedPoint(buff, &name[0], size + 1, &size, val);
90  name.resize(size);
91  }
93  int64_t toInt() { return helicsDataBufferToInteger(buff); }
95  bool toBoolean()
96  {
97  HelicsBool val = helicsDataBufferToBoolean(buff);
98  return (val == HELICS_TRUE);
99  }
101  double toDouble() { return helicsDataBufferToDouble(buff); }
103  std::complex<double> toComplex()
104  {
105  HelicsComplex hc = helicsDataBufferToComplexObject(buff);
106  std::complex<double> result(hc.real, hc.imag);
107  return result;
108  }
113  int toVector(double* data, int maxlen)
114  {
115  helicsDataBufferToVector(buff, data, maxlen, &maxlen);
116  // maxlen contains the actual length now
117  return maxlen;
118  }
120  void toVector(std::vector<double>& data)
121  {
122  int actualSize = helicsDataBufferVectorSize(buff);
123  data.resize(actualSize);
124  helicsDataBufferToVector(buff, data.data(), actualSize, HELICS_NULL_POINTER);
125  }
126 
131  int toComplexVector(double* data, int maxlen)
132  {
133  helicsDataBufferToComplexVector(buff, data, maxlen, &maxlen);
134  // maxlen contains the actual length now
135  return maxlen;
136  }
137 
138  private:
139  HelicsDataBuffer buff;
140 };
141 } // namespace helicscpp
142 #endif
HELICS_FALSE
const HelicsBool HELICS_FALSE
Definition: api-data.h:115
HELICS_TRUE
const HelicsBool HELICS_TRUE
Definition: api-data.h:114
helicscpp::DataBuffer::toComplex
std::complex< double > toComplex()
Definition: DataBuffer.hpp:103
helicscpp::DataBuffer::toString
std::string toString()
Definition: DataBuffer.hpp:52
helicscpp::DataBuffer::size
int size()
Definition: DataBuffer.hpp:43
helicscpp::DataBuffer::toInt
int64_t toInt()
Definition: DataBuffer.hpp:93
HelicsComplex
Definition: api-data.h:158
helicscpp::DataBuffer::toVector
int toVector(double *data, int maxlen)
Definition: DataBuffer.hpp:113
helicscpp::DataBuffer::capacity
int capacity()
Definition: DataBuffer.hpp:46
helicscpp::DataBuffer::toNamedPoint
void toNamedPoint(std::string &name, double *val)
Definition: DataBuffer.hpp:83
helicscpp::DataBuffer::toString
void toString(std::string &str)
Definition: DataBuffer.hpp:69
helicscpp::DataBuffer::toComplexVector
int toComplexVector(double *data, int maxlen)
Definition: DataBuffer.hpp:131
helicscpp::DataBuffer::stringSize
int stringSize()
Definition: DataBuffer.hpp:49
helicscpp::DataBuffer::toDouble
double toDouble()
Definition: DataBuffer.hpp:101
helicscpp::DataBuffer::toVector
void toVector(std::vector< double > &data)
Definition: DataBuffer.hpp:120
helicscpp::DataBuffer
Definition: DataBuffer.hpp:18
helicscpp::DataBuffer::toBoolean
bool toBoolean()
Definition: DataBuffer.hpp:95
HelicsDataBuffer
void * HelicsDataBuffer
Definition: api-data.h:82
helicscpp
Definition: cpp98/Broker.hpp:18
HelicsBool
int HelicsBool
Definition: api-data.h:112