| helics 3.6.1

Input.hpp
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 #ifndef HELICS_CPP98_INPUT_HPP_
8 #define HELICS_CPP98_INPUT_HPP_
9 #pragma once
10
11 #include "DataBuffer.hpp"
12 #include "helics/helics.h"
13 #include "helicsExceptions.hpp"
14
15 #include <complex>
16 #include <string>
17 #include <vector>
18
19 namespace helicscpp {
21 class Input {
22 public:
24 explicit Input(HelicsInput hsub) HELICS_NOTHROW: inp(hsub) {}
26 Input() HELICS_NOTHROW: inp(HELICS_NULL_POINTER) {}
28 Input(const Input& inputs) HELICS_NOTHROW: inp(inputs.inp) {}
30 Input& operator=(const Input& input)
31 {
32 inp = input.inp;
33 return *this;
34 }
36 operator HelicsInput() const { return inp; }
38 HelicsInput baseObject() const { return inp; }
40 bool isValid() const { return (helicsInputIsValid(inp) == HELICS_TRUE); }
42 void addTarget(const std::string& target)
43 {
44 helicsInputAddTarget(inp, target.c_str(), HELICS_IGNORE_ERROR);
45 }
48 void setDefault(const char* data, int len)
49 {
50 helicsInputSetDefaultBytes(inp, data, len, HELICS_IGNORE_ERROR);
51 }
53 void setDefault(const std::string& str)
54 {
55 helicsInputSetDefaultString(inp, str.c_str(), HELICS_IGNORE_ERROR);
56 }
58 void setDefault(int64_t val) { helicsInputSetDefaultInteger(inp, val, HELICS_IGNORE_ERROR); }
60 void setDefault(bool val)
61 {
62 helicsInputSetDefaultBoolean(inp, val ? HELICS_TRUE : HELICS_FALSE, HELICS_IGNORE_ERROR);
63 }
65 void setDefault(double val) { helicsInputSetDefaultDouble(inp, val, HELICS_IGNORE_ERROR); }
67 void setDefault(const std::complex<double>& cmplx)
68 {
69 helicsInputSetDefaultComplex(inp, cmplx.real(), cmplx.imag(), HELICS_IGNORE_ERROR);
70 }
71
73 void setDefault(const std::vector<double>& data)
74 {
75 helicsInputSetDefaultVector(inp,
76 data.data(),
77 static_cast<int>(data.size()),
78 HELICS_IGNORE_ERROR);
79 }
80
81 #if defined(__GNUC__)
82 # pragma GCC diagnostic push
83 # pragma GCC diagnostic ignored "-Wstrict-aliasing"
84 // std::complex is explicitly allowed to alias like this in the standard
85 #endif
87 void setDefault(const std::vector<std::complex<double> >& data)
88 {
89 helicsInputSetDefaultComplexVector(inp,
90 reinterpret_cast<const double*>(data.data()),
91 static_cast<int>(data.size()),
92 HELICS_IGNORE_ERROR);
93 }
94 #if defined(__GNUC__)
95 # pragma GCC diagnostic pop
96 #endif
99 int getBytes(std::vector<char>& data)
100 {
101 int size = helicsInputGetByteCount(inp);
102 data.resize(size);
103 helicsInputGetBytes(
104 inp, data.data(), static_cast<int>(data.size()), &size, HELICS_IGNORE_ERROR);
105 return size;
106 }
108 int getByteCount() { return helicsInputGetByteCount(inp); }
109
111 int getStringSize()
112 {
113 //-1 is for the null character which needs to be counted in C but not in a C++ string
114 return helicsInputGetStringSize(inp) - 1;
115 }
116
118 std::string getString()
119 {
120 int size = helicsInputGetStringSize(inp);
121 std::string result;
122
123 result.resize(static_cast<size_t>(size) + 1);
124 // this function results in a null terminated string
125 helicsInputGetString(inp, &result[0], size + 1, &size, HELICS_IGNORE_ERROR);
126 if (!(result.empty()) && (result[static_cast<size_t>(size) - 1] == '\0')) {
127 result.resize(static_cast<size_t>(size) - 1);
128 } else {
129 result.resize(size);
130 }
131 return result;
132 }
133
135 void getString(std::string& str)
136 {
137 int size = helicsInputGetStringSize(inp);
138 str.resize(static_cast<size_t>(size) + 1);
139 // this function results in a null terminated string
140 helicsInputGetString(inp, &str[0], size + 1, &size, HELICS_IGNORE_ERROR);
141 if (!(str.empty()) && (str[static_cast<size_t>(size) - 1] == '\0')) {
142 str.resize(static_cast<size_t>(size) - 1);
143 } else {
144 str.resize(size);
145 }
146 }
147
149 void getNamedPoint(std::string& name, double* val)
150 {
151 int size = helicsInputGetStringSize(inp);
152
153 name.resize(static_cast<size_t>(size) + 1);
154 // this function results in a null terminated string
155 helicsInputGetNamedPoint(inp, &name[0], size + 1, &size, val, HELICS_IGNORE_ERROR);
156 name.resize(size);
157 }
159 int64_t getInteger() { return helicsInputGetInteger(inp, HELICS_IGNORE_ERROR); }
161 bool getBoolean()
162 {
163 HelicsBool val = helicsInputGetBoolean(inp, HELICS_IGNORE_ERROR);
164 return (val == HELICS_TRUE);
165 }
167 double getDouble() { return helicsInputGetDouble(inp, HELICS_IGNORE_ERROR); }
169 std::complex<double> getComplex()
170 {
171 HelicsComplex hc = helicsInputGetComplexObject(inp, HELICS_IGNORE_ERROR);
172 std::complex<double> result(hc.real, hc.imag);
173 return result;
174 }
179 int getVector(double* data, int maxlen)
180 {
181 helicsInputGetVector(inp, data, maxlen, &maxlen, hThrowOnError());
182 // maxlen contains the actual length now
183 return maxlen;
184 }
186 void getVector(std::vector<double>& data)
187 {
188 int actualSize = helicsInputGetVectorSize(inp);
189 data.resize(actualSize);
190 helicsInputGetVector(inp, data.data(), actualSize, HELICS_NULL_POINTER, hThrowOnError());
191 }
192
197 int getComplexVector(double* data, int maxlen)
198 {
199 helicsInputGetComplexVector(inp, data, maxlen, &maxlen, hThrowOnError());
200 // maxlen contains the actual length now
201 return maxlen;
202 }
203 #if defined(__GNUC__)
204 # pragma GCC diagnostic push
205 # pragma GCC diagnostic ignored "-Wstrict-aliasing"
206 // std::complex is explicitly allowed to alias like this in the standard
207 #endif
209 void getComplexVector(std::vector<std::complex<double> >& data)
210 {
211 int actualSize = helicsInputGetVectorSize(inp);
212 data.resize(actualSize);
213 helicsInputGetComplexVector(inp,
214 reinterpret_cast<double*>(data.data()),
215 actualSize,
216 HELICS_NULL_POINTER,
217 hThrowOnError());
218 }
219 #if defined(__GNUC__)
220 # pragma GCC diagnostic pop
221 #endif
223 DataBuffer getDataBuffer()
224 {
225 return DataBuffer(helicsInputGetDataBuffer(inp, hThrowOnError()));
226 }
228 bool isUpdated() const { return (helicsInputIsUpdated(inp) > 0); }
229
231 HelicsTime getLastUpdateTime() const { return helicsInputLastUpdateTime(inp); }
232
234 void clearUpdate() { helicsInputClearUpdate(inp); }
235 // call helicsInputIsUpdated for each inp
236
239 const char* getName() const { return helicsInputGetName(inp); }
241 const char* getUnits() const { return helicsInputGetExtractionUnits(inp); }
243 const char* getInjectionUnits() const { return helicsInputGetInjectionUnits(inp); }
245 const char* getPublicationType() const { return helicsInputGetPublicationType(inp); }
247 const char* getType() const { return helicsInputGetType(inp); }
249 const char* getTarget() const { return helicsInputGetTarget(inp); }
251 const char* getInfo() const { return helicsInputGetInfo(inp); }
253 void setInfo(const std::string& info)
254 {
255 helicsInputSetInfo(inp, info.c_str(), HELICS_IGNORE_ERROR);
256 }
258 const char* getTag(const std::string& tagname) const
259 {
260 return helicsInputGetTag(inp, tagname.c_str());
261 }
263 void setTag(const std::string& tagname, const std::string& tagvalue)
264 {
265 helicsInputSetTag(inp, tagname.c_str(), tagvalue.c_str(), HELICS_IGNORE_ERROR);
266 }
267 void setOption(int32_t option, int32_t value = 1)
268 {
269 helicsInputSetOption(inp, option, value, HELICS_IGNORE_ERROR);
270 }
271 int32_t getOption(int32_t option) { return helicsInputGetOption(inp, option); }
272
273 private:
274 HelicsInput inp;
275 };
276
277 } // namespace helicscpp
278 #endif
helicsInputLastUpdateTime
HelicsTime helicsInputLastUpdateTime(HelicsInput ipt)
Definition: ValueFederateExport.cpp:1637
helicsInputIsUpdated
HelicsBool helicsInputIsUpdated(HelicsInput ipt)
Definition: ValueFederateExport.cpp:1626
helicsInputClearUpdate
void helicsInputClearUpdate(HelicsInput ipt)
Definition: ValueFederateExport.cpp:1654
HelicsInput
void * HelicsInput
Definition: api-data.h:26
HelicsTime
double HelicsTime
Definition: api-data.h:106
HelicsBool
int HelicsBool
Definition: api-data.h:118
HELICS_FALSE
const HelicsBool HELICS_FALSE
Definition: api-data.h:121
HELICS_TRUE
const HelicsBool HELICS_TRUE
Definition: api-data.h:120
helicscpp::DataBuffer
Definition: DataBuffer.hpp:18
helicscpp::Input
Definition: Input.hpp:21
helicscpp::Input::clearUpdate
void clearUpdate()
Definition: Input.hpp:234
helicscpp::Input::getVector
int getVector(double *data, int maxlen)
Definition: Input.hpp:179
helicscpp::Input::getDataBuffer
DataBuffer getDataBuffer()
Definition: Input.hpp:223
helicscpp::Input::getLastUpdateTime
HelicsTime getLastUpdateTime() const
Definition: Input.hpp:231
helicscpp::Input::getBoolean
bool getBoolean()
Definition: Input.hpp:161
helicscpp::Input::getInjectionUnits
const char * getInjectionUnits() const
Definition: Input.hpp:243
helicscpp::Input::getString
void getString(std::string &str)
Definition: Input.hpp:135
helicscpp::Input::isValid
bool isValid() const
Definition: Input.hpp:40
helicscpp::Input::getUnits
const char * getUnits() const
Definition: Input.hpp:241
helicscpp::Input::setDefault
void setDefault(const std::vector< double > &data)
Definition: Input.hpp:73
helicscpp::Input::setDefault
void setDefault(const std::string &str)
Definition: Input.hpp:53
helicscpp::Input::operator=
Input & operator=(const Input &input)
Definition: Input.hpp:30
helicscpp::Input::getBytes
int getBytes(std::vector< char > &data)
Definition: Input.hpp:99
helicscpp::Input::setInfo
void setInfo(const std::string &info)
Definition: Input.hpp:253
helicscpp::Input::setDefault
void setDefault(const char *data, int len)
Definition: Input.hpp:48
helicscpp::Input::setDefault
void setDefault(bool val)
Definition: Input.hpp:60
helicscpp::Input::setTag
void setTag(const std::string &tagname, const std::string &tagvalue)
Definition: Input.hpp:263
helicscpp::Input::getInfo
const char * getInfo() const
Definition: Input.hpp:251
helicscpp::Input::getInteger
int64_t getInteger()
Definition: Input.hpp:159
helicscpp::Input::Input
Input(const Input &inputs) HELICS_NOTHROW
Definition: Input.hpp:28
helicscpp::Input::getByteCount
int getByteCount()
Definition: Input.hpp:108
helicscpp::Input::getComplexVector
void getComplexVector(std::vector< std::complex< double > > &data)
Definition: Input.hpp:209
helicscpp::Input::getStringSize
int getStringSize()
Definition: Input.hpp:111
helicscpp::Input::getTarget
const char * getTarget() const
Definition: Input.hpp:249
helicscpp::Input::getNamedPoint
void getNamedPoint(std::string &name, double *val)
Definition: Input.hpp:149
helicscpp::Input::getComplex
std::complex< double > getComplex()
Definition: Input.hpp:169
helicscpp::Input::setDefault
void setDefault(const std::complex< double > &cmplx)
Definition: Input.hpp:67
helicscpp::Input::getComplexVector
int getComplexVector(double *data, int maxlen)
Definition: Input.hpp:197
helicscpp::Input::Input
Input() HELICS_NOTHROW
Definition: Input.hpp:26
helicscpp::Input::setDefault
void setDefault(const std::vector< std::complex< double > > &data)
Definition: Input.hpp:87
helicscpp::Input::Input
Input(HelicsInput hsub) HELICS_NOTHROW
Definition: Input.hpp:24
helicscpp::Input::getTag
const char * getTag(const std::string &tagname) const
Definition: Input.hpp:258
helicscpp::Input::isUpdated
bool isUpdated() const
Definition: Input.hpp:228
helicscpp::Input::addTarget
void addTarget(const std::string &target)
Definition: Input.hpp:42
helicscpp::Input::setDefault
void setDefault(int64_t val)
Definition: Input.hpp:58
helicscpp::Input::getDouble
double getDouble()
Definition: Input.hpp:167
helicscpp::Input::getName
const char * getName() const
Definition: Input.hpp:239
helicscpp::Input::getPublicationType
const char * getPublicationType() const
Definition: Input.hpp:245
helicscpp::Input::setDefault
void setDefault(double val)
Definition: Input.hpp:65
helicscpp::Input::getType
const char * getType() const
Definition: Input.hpp:247
helicscpp::Input::baseObject
HelicsInput baseObject() const
Definition: Input.hpp:38
helicscpp::Input::getString
std::string getString()
Definition: Input.hpp:118
helicscpp::Input::getVector
void getVector(std::vector< double > &data)
Definition: Input.hpp:186
helicscpp::hThrowOnError
Definition: helicsExceptions.hpp:38
helicsInputGetInfo
const char * helicsInputGetInfo(HelicsInput inp)
Definition: ValueFederateExport.cpp:1412
helicsInputGetType
const char * helicsInputGetType(HelicsInput ipt)
Definition: ValueFederateExport.cpp:1246
helicsInputSetInfo
void helicsInputSetInfo(HelicsInput inp, const char *info, HelicsError *err)
Definition: ValueFederateExport.cpp:1429
helicsInputGetTag
const char * helicsInputGetTag(HelicsInput inp, const char *tagname)
Definition: ValueFederateExport.cpp:1478
helicsInputSetOption
void helicsInputSetOption(HelicsInput inp, int option, int value, HelicsError *err)
Definition: ValueFederateExport.cpp:1560
helicsInputGetPublicationType
const char * helicsInputGetPublicationType(HelicsInput ipt)
Definition: ValueFederateExport.cpp:1264
helicsInputGetOption
int helicsInputGetOption(HelicsInput inp, int option)
Definition: ValueFederateExport.cpp:1544
helicsInputSetTag
void helicsInputSetTag(HelicsInput inp, const char *tagname, const char *tagvalue, HelicsError *err)
Definition: ValueFederateExport.cpp:1495
helicsInputGetExtractionUnits
const char * helicsInputGetExtractionUnits(HelicsInput ipt)
Definition: ValueFederateExport.cpp:1380
helicsInputGetInjectionUnits
const char * helicsInputGetInjectionUnits(HelicsInput ipt)
Definition: ValueFederateExport.cpp:1363
helicsInputGetTarget
const char * helicsInputGetTarget(HelicsInput ipt)
Definition: ValueFederateExport.cpp:1323
helicsInputGetName
const char * helicsInputGetName(HelicsInput ipt)
Definition: ValueFederateExport.cpp:1305
helicsInputSetDefaultBytes
void helicsInputSetDefaultBytes(HelicsInput ipt, const void *data, int inputDataLength, HelicsError *err)
Definition: ValueFederateExport.cpp:1092
helicsInputSetDefaultVector
void helicsInputSetDefaultVector(HelicsInput ipt, const double *vectorInput, int vectorLength, HelicsError *err)
Definition: ValueFederateExport.cpp:1185
helicsInputSetDefaultInteger
void helicsInputSetDefaultInteger(HelicsInput ipt, int64_t val, HelicsError *err)
Definition: ValueFederateExport.cpp:1128
helicsInputSetDefaultComplex
void helicsInputSetDefaultComplex(HelicsInput ipt, double real, double imag, HelicsError *err)
Definition: ValueFederateExport.cpp:1175
helicsInputSetDefaultComplexVector
void helicsInputSetDefaultComplexVector(HelicsInput ipt, const double *vectorInput, int vectorLength, HelicsError *err)
Definition: ValueFederateExport.cpp:1205
helicsInputSetDefaultDouble
void helicsInputSetDefaultDouble(HelicsInput ipt, double val, HelicsError *err)
Definition: ValueFederateExport.cpp:1146
helicsInputSetDefaultString
void helicsInputSetDefaultString(HelicsInput ipt, const char *defaultString, HelicsError *err)
Definition: ValueFederateExport.cpp:1112
helicsInputSetDefaultBoolean
void helicsInputSetDefaultBoolean(HelicsInput ipt, HelicsBool val, HelicsError *err)
Definition: ValueFederateExport.cpp:1137
helicsInputGetString
void helicsInputGetString(HelicsInput ipt, char *outputString, int maxStringLength, int *actualLength, HelicsError *err)
Definition: ValueFederateExport.cpp:813
helicsInputGetNamedPoint
void helicsInputGetNamedPoint(HelicsInput ipt, char *outputString, int maxStringLength, int *actualLength, double *val, HelicsError *err)
Definition: ValueFederateExport.cpp:1051
helicsInputGetStringSize
int helicsInputGetStringSize(HelicsInput ipt)
Definition: ValueFederateExport.cpp:981
helicsInputGetBytes
void helicsInputGetBytes(HelicsInput ipt, void *data, int maxDataLength, int *actualSize, HelicsError *err)
Definition: ValueFederateExport.cpp:779
helicsInputGetComplexVector
void helicsInputGetComplexVector(HelicsInput ipt, double data[], int maxLength, int *actualSize, HelicsError *err)
Definition: ValueFederateExport.cpp:1024
helicsInputGetBoolean
HelicsBool helicsInputGetBoolean(HelicsInput ipt, HelicsError *err)
Definition: ValueFederateExport.cpp:854
helicsInputGetDataBuffer
HelicsDataBuffer helicsInputGetDataBuffer(HelicsInput inp, HelicsError *err)
Definition: ValueFederateExport.cpp:768
helicsInputGetVectorSize
int helicsInputGetVectorSize(HelicsInput ipt)
Definition: ValueFederateExport.cpp:965
helicsInputGetInteger
int64_t helicsInputGetInteger(HelicsInput ipt, HelicsError *err)
Definition: ValueFederateExport.cpp:839
helicsInputGetByteCount
int helicsInputGetByteCount(HelicsInput ipt)
Definition: ValueFederateExport.cpp:749
helicsInputGetComplexObject
HelicsComplex helicsInputGetComplexObject(HelicsInput ipt, HelicsError *err)
Definition: ValueFederateExport.cpp:944
helicsInputGetVector
void helicsInputGetVector(HelicsInput ipt, double data[], int maxLength, int *actualSize, HelicsError *err)
Definition: ValueFederateExport.cpp:997
helicsInputGetDouble
double helicsInputGetDouble(HelicsInput ipt, HelicsError *err)
Definition: ValueFederateExport.cpp:872
helicsInputAddTarget
void helicsInputAddTarget(HelicsInput ipt, const char *target, HelicsError *err)
Definition: ValueFederateExport.cpp:739
helicsInputIsValid
HelicsBool helicsInputIsValid(HelicsInput ipt)
Definition: ValueFederateExport.cpp:730
helicscpp
Definition: cpp98/Broker.hpp:18
HelicsComplex
Definition: api-data.h:166