helics  3.5.2
DataBuffer.hpp
1 /*
2 Copyright (c) 2017-2024,
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)) {}
23  explicit DataBuffer(HelicsDataBuffer buffer): buff(buffer) {}
24  DataBuffer(void* buffer, int32_t datasize, int32_t capacity):
25  buff(helicsWrapDataInBuffer(buffer, datasize, capacity))
26  {
27  }
29  ~DataBuffer() { helicsDataBufferFree(buff); }
30  void fill(double val) { helicsDataBufferFillFromDouble(buff, val); }
31  void fill(int64_t val) { helicsDataBufferFillFromInteger(buff, val); }
32  void fill(const std::string& val) { helicsDataBufferFillFromString(buff, val.c_str()); }
33  void fill(const char* val) { helicsDataBufferFillFromString(buff, val); }
34  void fill(const std::vector<double>& val)
35  {
36  helicsDataBufferFillFromVector(buff, val.data(), static_cast<int>(val.size()));
37  }
38  void fill(const std::complex<double> val)
39  {
40  helicsDataBufferFillFromComplex(buff, val.real(), val.imag());
41  }
42  void fill(const double* vals, int size) { helicsDataBufferFillFromVector(buff, vals, size); }
43  void fill(const std::string& name, double val)
44  {
45  helicsDataBufferFillFromNamedPoint(buff, name.c_str(), val);
46  }
47  void fill(bool val) { helicsDataBufferFillFromBoolean(buff, val ? HELICS_TRUE : HELICS_FALSE); }
48  void fill(char val) { helicsDataBufferFillFromChar(buff, val); }
50  DataBuffer clone() { return DataBuffer(helicsDataBufferClone(buff)); }
52  int size() { return helicsDataBufferSize(buff); }
53 
55  int capacity() { return helicsDataBufferCapacity(buff); }
57  void* data() { return helicsDataBufferData(buff); }
59  bool reserve(int32_t newCapacity)
60  {
61  return helicsDataBufferReserve(buff, newCapacity) == HELICS_TRUE;
62  }
64  int stringSize() { return helicsDataBufferStringSize(buff); }
66  int vectorSize() { return helicsDataBufferVectorSize(buff); }
68  int type() const { return helicsDataBufferType(buff); }
70  bool isValid() const { return (helicsDataBufferIsValid(buff) == HELICS_TRUE); }
72  std::string toString()
73  {
74  int size = stringSize();
75  std::string result;
76 
77  result.resize(static_cast<size_t>(size) + 1);
78  // this function results in a null terminated string
79  helicsDataBufferToString(buff, &result[0], size + 1, &size);
80  if (!(result.empty()) && (result[static_cast<size_t>(size) - 1] == '\0')) {
81  result.resize(static_cast<size_t>(size) - 1);
82  } else {
83  result.resize(size);
84  }
85  return result;
86  }
87 
89  void toString(std::string& str)
90  {
91  int size = stringSize();
92  str.resize(static_cast<size_t>(size) + 1);
93  // this function results in a null terminated string
94  helicsDataBufferToString(buff, &str[0], size + 1, &size);
95  if (!(str.empty()) && (str[static_cast<size_t>(size) - 1] == '\0')) {
96  str.resize(static_cast<size_t>(size) - 1);
97  } else {
98  str.resize(size);
99  }
100  }
101 
103  void toNamedPoint(std::string& name, double* val)
104  {
105  int size = helicsDataBufferStringSize(buff);
106 
107  name.resize(static_cast<size_t>(size) + 1);
108  // this function results in a null terminated string
109  helicsDataBufferToNamedPoint(buff, &name[0], size + 1, &size, val);
110  name.resize(size);
111  }
113  int64_t toInt() { return helicsDataBufferToInteger(buff); }
115  bool toBoolean()
116  {
117  HelicsBool val = helicsDataBufferToBoolean(buff);
118  return (val == HELICS_TRUE);
119  }
121  double toDouble() { return helicsDataBufferToDouble(buff); }
123  std::complex<double> toComplex()
124  {
125  HelicsComplex hc = helicsDataBufferToComplexObject(buff);
126  std::complex<double> result(hc.real, hc.imag);
127  return result;
128  }
133  int toVector(double* data, int maxlen)
134  {
135  helicsDataBufferToVector(buff, data, maxlen, &maxlen);
136  // maxlen contains the actual length now
137  return maxlen;
138  }
140  void toVector(std::vector<double>& data)
141  {
142  int actualSize = helicsDataBufferVectorSize(buff);
143  data.resize(actualSize);
144  helicsDataBufferToVector(buff, data.data(), actualSize, HELICS_NULL_POINTER);
145  }
146 
151  int toComplexVector(double* data, int maxlen)
152  {
153  helicsDataBufferToComplexVector(buff, data, maxlen, &maxlen);
154  // maxlen contains the actual length now
155  return maxlen;
156  }
160  bool convertToType(int newDataType)
161  {
162  return (helicsDataBufferConvertToType(buff, newDataType) == HELICS_TRUE);
163  }
166 
167  private:
168  HelicsDataBuffer buff;
169 };
170 } // namespace helicscpp
171 #endif
void * HelicsDataBuffer
Definition: api-data.h:82
int HelicsBool
Definition: api-data.h:112
const HelicsBool HELICS_FALSE
Definition: api-data.h:115
const HelicsBool HELICS_TRUE
Definition: api-data.h:114
Definition: DataBuffer.hpp:18
void toNamedPoint(std::string &name, double *val)
Definition: DataBuffer.hpp:103
int64_t toInt()
Definition: DataBuffer.hpp:113
int toComplexVector(double *data, int maxlen)
Definition: DataBuffer.hpp:151
void toVector(std::vector< double > &data)
Definition: DataBuffer.hpp:140
DataBuffer(HelicsDataBuffer buffer)
Definition: DataBuffer.hpp:23
double toDouble()
Definition: DataBuffer.hpp:121
std::complex< double > toComplex()
Definition: DataBuffer.hpp:123
bool toBoolean()
Definition: DataBuffer.hpp:115
int stringSize()
Definition: DataBuffer.hpp:64
int capacity()
Definition: DataBuffer.hpp:55
DataBuffer clone()
Definition: DataBuffer.hpp:50
int vectorSize()
Definition: DataBuffer.hpp:66
int size()
Definition: DataBuffer.hpp:52
int type() const
Definition: DataBuffer.hpp:68
void toString(std::string &str)
Definition: DataBuffer.hpp:89
~DataBuffer()
Definition: DataBuffer.hpp:29
void * data()
Definition: DataBuffer.hpp:57
int toVector(double *data, int maxlen)
Definition: DataBuffer.hpp:133
HelicsDataBuffer getHelicsDataBuffer()
Definition: DataBuffer.hpp:165
std::string toString()
Definition: DataBuffer.hpp:72
bool convertToType(int newDataType)
Definition: DataBuffer.hpp:160
bool reserve(int32_t newCapacity)
Definition: DataBuffer.hpp:59
bool isValid() const
Definition: DataBuffer.hpp:70
Definition: cpp98/Broker.hpp:18
Definition: api-data.h:160