| helics 3.6.1

cpp98/ValueFederate.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_VALUE_FEDERATE_HPP_
8 #define HELICS_CPP98_VALUE_FEDERATE_HPP_
9 #pragma once
10
11 #include "Federate.hpp"
12 #include "Input.hpp"
13 #include "Publication.hpp"
14 #include "helics/helics.h"
15
16 #include <exception>
17 #include <sstream>
18 #include <string>
19 #include <utility>
20 #include <vector>
21
22 namespace helicscpp {
24 enum PubSubTypes {
25 STRING_TYPE = HELICS_DATA_TYPE_STRING,
26 DOUBLE_TYPE = HELICS_DATA_TYPE_DOUBLE,
27 INT_TYPE = HELICS_DATA_TYPE_INT,
28 COMPLEX_TYPE = HELICS_DATA_TYPE_COMPLEX,
29 VECTOR_TYPE = HELICS_DATA_TYPE_VECTOR,
30 TIME_TYPE = HELICS_DATA_TYPE_TIME,
31 BOOLEAN_TYPE = HELICS_DATA_TYPE_BOOLEAN,
32 RAW_TYPE = HELICS_DATA_TYPE_RAW
33 };
34
36 class ValueFederate: public virtual Federate {
37 private:
38 std::vector<HelicsInput> ipts;
39 std::vector<HelicsPublication> pubs;
40
41 public:
42 friend class helicscpp::FederateInfo;
48 explicit ValueFederate(const std::string& fedName, FederateInfo& fedInfo)
49 {
50 fed = helicsCreateValueFederate(fedName.c_str(), fedInfo.getInfo(), hThrowOnError());
51 if (fed == NULL) {
52 throw(HelicsException(HELICS_ERROR_REGISTRATION_FAILURE, "Fed==NULL"));
53 }
54 }
59 explicit ValueFederate(const std::string& configString)
60 {
61 fed = helicsCreateValueFederateFromConfig(configString.c_str(), hThrowOnError());
62 if (fed == NULL) {
63 throw(HelicsException(HELICS_ERROR_REGISTRATION_FAILURE, "Fed==NULL"));
64 }
65 }
67 ValueFederate(const ValueFederate& vfed): Federate(vfed), ipts(vfed.ipts), pubs(vfed.pubs) {}
69 ValueFederate& operator=(const ValueFederate& fedObj)
70 {
71 Federate::operator=(fedObj);
72 ipts = fedObj.ipts;
73 pubs = fedObj.pubs;
74 if (fed == NULL) {
75 throw(HelicsException(HELICS_ERROR_REGISTRATION_FAILURE, "Fed==NULL move constructor"));
76 }
77 return *this;
78 }
79 #ifdef HELICS_HAS_RVALUE_REFS
81 ValueFederate(ValueFederate&& fedObj) HELICS_NOTHROW:
82 Federate(),
83 ipts(std::move(fedObj.ipts)),
84 pubs(std::move(fedObj.pubs))
85 {
86 Federate::operator=(std::move(fedObj));
87 }
89 ValueFederate& operator=(ValueFederate&& fedObj) HELICS_NOTHROW
90 {
91 ipts = std::move(fedObj.ipts);
92 pubs = std::move(fedObj.pubs);
93 Federate::operator=(std::move(fedObj));
94 return *this;
95 }
96 #endif
98 ValueFederate() HELICS_NOTHROW {}
99
109 Publication registerPublication(const std::string& name,
110 const std::string& type,
111 const std::string& units = "")
112 {
113 HelicsPublication pub = helicsFederateRegisterTypePublication(
114 fed, name.c_str(), type.c_str(), units.c_str(), hThrowOnError());
115 pubs.push_back(pub);
116 return Publication(pub);
117 }
118
126 Publication registerPublication(const std::string& name,
127 HelicsDataTypes type,
128 const std::string& units = "")
129 {
130 HelicsPublication pub = helicsFederateRegisterPublication(
131 fed, name.c_str(), type, units.c_str(), hThrowOnError());
132 pubs.push_back(pub);
133 return Publication(pub);
134 }
135
143 Publication registerGlobalPublication(const std::string& name,
144 const std::string& type,
145 const std::string& units = "")
146 {
147 HelicsPublication pub = helicsFederateRegisterGlobalTypePublication(
148 fed, name.c_str(), type.c_str(), units.c_str(), hThrowOnError());
149 pubs.push_back(pub);
150 return Publication(pub);
151 }
152
160 Publication registerGlobalPublication(const std::string& key,
161 HelicsDataTypes type,
162 const std::string& units = "")
163 {
164 HelicsPublication pub = helicsFederateRegisterGlobalPublication(
165 fed, key.c_str(), type, units.c_str(), hThrowOnError());
166 pubs.push_back(pub);
167 return Publication(pub);
168 }
169
179 Publication registerIndexedPublication(const std::string& key,
180 int index1,
181 HelicsDataTypes type,
182 const std::string& units = "")
183 {
184 std::string indexed_name = key + '_' + toStr(index1);
185 return registerGlobalPublication(indexed_name, type, units);
186 }
187
198 Publication registerIndexedPublication(const std::string& key,
199 int index1,
200 int index2,
201 HelicsDataTypes type,
202 const std::string& units = std::string())
203 {
204 std::string indexed_name = key + '_' + toStr(index1) + '_' + toStr(index2);
205 return registerGlobalPublication(indexed_name, type, units);
206 }
207
217 Publication registerPublicationIndexed(const std::string& key,
218 int index1,
219 HelicsDataTypes type,
220 const std::string& units = "")
221 {
222 return registerIndexedPublication(key, index1, type, units);
223 }
224
235 Publication registerPublicationIndexed(const std::string& key,
236 int index1,
237 int index2,
238 HelicsDataTypes type,
239 const std::string& units = std::string())
240 {
241 return registerIndexedPublication(key, index1, index2, type, units);
242 }
247 void registerFromPublicationJSON(const std::string& json)
248 {
249 helicsFederateRegisterFromPublicationJSON(fed, json.c_str(), hThrowOnError());
250 }
252 Publication getPublication(const std::string& name)
253 {
254 return Publication(helicsFederateGetPublication(fed, name.c_str(), hThrowOnError()));
255 }
259 Publication getPublication(int index)
260 {
261 return Publication(helicsFederateGetPublicationByIndex(fed, index, hThrowOnError()));
262 }
263
265 Input registerSubscription(const std::string& name, const std::string& units = std::string())
266 {
267 HelicsInput sub =
268 helicsFederateRegisterSubscription(fed, name.c_str(), units.c_str(), hThrowOnError());
269 ipts.push_back(sub);
270 return Input(sub);
271 }
272
279 Input registerIndexedSubscription(const std::string& name,
280 int index1,
281 const std::string& units = "")
282 {
283 std::string indexed_name = name + '_' + toStr(index1);
284 return registerSubscription(indexed_name, units);
285 }
286
294 Input registerIndexedSubscription(const std::string& name,
295 int index1,
296 int index2,
297 const std::string& units = "")
298 {
299 std::string indexed_name = name + '_' + toStr(index1) + '_' + toStr(index2);
300 return registerSubscription(indexed_name, units);
301 }
302
309 Input registerSubscriptionIndexed(const std::string& name,
310 int index1,
311 const std::string& units = "")
312 {
313 return registerIndexedSubscription(name, index1, units);
314 }
315
323 Input registerSubscriptionIndexed(const std::string& name,
324 int index1,
325 int index2,
326 const std::string& units = "")
327 {
328 return registerIndexedSubscription(name, index1, index2, units);
329 }
330
338 Input registerInput(const std::string& name,
339 const std::string& type,
340 const std::string& units = "")
341 {
342 HelicsInput ipt = helicsFederateRegisterTypeInput(
343 fed, name.c_str(), type.c_str(), units.c_str(), hThrowOnError());
344 ipts.push_back(ipt);
345 return Input(ipt);
346 }
347
355 Input
356 registerInput(const std::string& name, HelicsDataTypes type, const std::string& units = "")
357 {
358 HelicsInput ipt = helicsFederateRegisterPublication(
359 fed, name.c_str(), type, units.c_str(), hThrowOnError());
360 pubs.push_back(ipt);
361 return Input(ipt);
362 }
363
371 Input registerGlobalInput(const std::string& name,
372 const std::string& type,
373 const std::string& units = "")
374 {
375 HelicsInput ipt = helicsFederateRegisterGlobalTypeInput(
376 fed, name.c_str(), type.c_str(), units.c_str(), hThrowOnError());
377 ipts.push_back(ipt);
378 return Input(ipt);
379 }
380
388 Input registerGlobalInput(const std::string& key,
389 HelicsDataTypes type,
390 const std::string& units = "")
391 {
392 HelicsInput inp = helicsFederateRegisterGlobalInput(
393 fed, key.c_str(), type, units.c_str(), hThrowOnError());
394 ipts.push_back(inp);
395 return Input(inp);
396 }
397
407 Input registerIndexedInput(const std::string& key,
408 int index1,
409 HelicsDataTypes type,
410 const std::string& units = "")
411 {
412 std::string indexed_name = key + '_' + toStr(index1);
413 return registerGlobalInput(indexed_name, type, units);
414 }
415
426 Input registerIndexedInput(const std::string& key,
427 int index1,
428 int index2,
429 HelicsDataTypes type,
430 const std::string& units = std::string())
431 {
432 std::string indexed_name = key + '_' + toStr(index1) + '_' + toStr(index2);
433 return registerGlobalInput(indexed_name, type, units);
434 }
435
437 Input getInput(const std::string& name)
438 {
439 return Input(helicsFederateGetInput(fed, name.c_str(), hThrowOnError()));
440 }
441
443 Input getInputByTarget(const std::string& target)
444 {
445 return Input(helicsFederateGetInputByTarget(fed, target.c_str(), hThrowOnError()));
446 }
447
449 Input getInput(int index)
450 {
451 return Input(helicsFederateGetInputByIndex(fed, index, hThrowOnError()));
452 }
454 int getInputCount() const { return helicsFederateGetInputCount(fed); }
456 int getPublicationCount() const { return helicsFederateGetPublicationCount(fed); }
457 // TODO(PT): use c api to implement this method... callbacks too?
459 std::vector<HelicsInput> queryUpdates() { return std::vector<HelicsInput>(); }
460
462 void clearUpdates() { helicsFederateClearUpdates(fed); }
464 void publishJSON(const std::string& json)
465 {
466 helicsFederatePublishJSON(fed, json.c_str(), hThrowOnError());
467 }
468
469 private:
470 // Utility function for converting numbers to string
471 template<typename T>
472 std::string toStr(T num)
473 {
474 std::ostringstream ss;
475 ss << num;
476 return ss.str();
477 }
478 };
479 } // namespace helicscpp
480 #endif
helicsFederateRegisterPublication
HelicsPublication helicsFederateRegisterPublication(HelicsFederate fed, const char *key, HelicsDataTypes type, const char *units, HelicsError *err)
Definition: ValueFederateExport.cpp:168
helicsFederateRegisterTypePublication
HelicsPublication helicsFederateRegisterTypePublication(HelicsFederate fed, const char *key, const char *type, const char *units, HelicsError *err)
Definition: ValueFederateExport.cpp:148
helicsFederateRegisterFromPublicationJSON
void helicsFederateRegisterFromPublicationJSON(HelicsFederate fed, const char *json, HelicsError *err)
Definition: ValueFederateExport.cpp:346
helicsFederateGetInput
HelicsInput helicsFederateGetInput(HelicsFederate fed, const char *key, HelicsError *err)
Definition: ValueFederateExport.cpp:430
helicsFederateRegisterGlobalTypeInput
HelicsPublication helicsFederateRegisterGlobalTypeInput(HelicsFederate fed, const char *key, const char *type, const char *units, HelicsError *err)
Definition: ValueFederateExport.cpp:296
helicsFederateGetPublicationCount
int helicsFederateGetPublicationCount(HelicsFederate fed)
Definition: ValueFederateExport.cpp:1670
helicsFederateRegisterGlobalPublication
HelicsPublication helicsFederateRegisterGlobalPublication(HelicsFederate fed, const char *key, HelicsDataTypes type, const char *units, HelicsError *err)
Definition: ValueFederateExport.cpp:218
helicsFederateGetPublicationByIndex
HelicsPublication helicsFederateGetPublicationByIndex(HelicsFederate fed, int index, HelicsError *err)
Definition: ValueFederateExport.cpp:405
helicsFederateRegisterGlobalTypePublication
HelicsPublication helicsFederateRegisterGlobalTypePublication(HelicsFederate fed, const char *key, const char *type, const char *units, HelicsError *err)
Definition: ValueFederateExport.cpp:198
helicsFederateClearUpdates
void helicsFederateClearUpdates(HelicsFederate fed)
Definition: ValueFederateExport.cpp:505
helicsFederateRegisterGlobalInput
HelicsPublication helicsFederateRegisterGlobalInput(HelicsFederate fed, const char *key, HelicsDataTypes type, const char *units, HelicsError *err)
Definition: ValueFederateExport.cpp:316
helicsFederatePublishJSON
void helicsFederatePublishJSON(HelicsFederate fed, const char *json, HelicsError *err)
Definition: ValueFederateExport.cpp:363
helicsFederateGetPublication
HelicsPublication helicsFederateGetPublication(HelicsFederate fed, const char *key, HelicsError *err)
Definition: ValueFederateExport.cpp:383
helicsFederateGetInputByIndex
HelicsInput helicsFederateGetInputByIndex(HelicsFederate fed, int index, HelicsError *err)
Definition: ValueFederateExport.cpp:453
helicsFederateRegisterTypeInput
HelicsInput helicsFederateRegisterTypeInput(HelicsFederate fed, const char *key, const char *type, const char *units, HelicsError *err)
Definition: ValueFederateExport.cpp:246
helicsFederateRegisterSubscription
HelicsInput helicsFederateRegisterSubscription(HelicsFederate fed, const char *key, const char *units, HelicsError *err)
Definition: ValueFederateExport.cpp:127
helicsFederateGetInputByTarget
HelicsInput helicsFederateGetInputByTarget(HelicsFederate fed, const char *target, HelicsError *err)
Definition: ValueFederateExport.cpp:482
helicsFederateGetInputCount
int helicsFederateGetInputCount(HelicsFederate fed)
Definition: ValueFederateExport.cpp:1680
HelicsInput
void * HelicsInput
Definition: api-data.h:26
HelicsPublication
void * HelicsPublication
Definition: api-data.h:31
helicscpp::FederateInfo
Definition: cpp98/Federate.hpp:28
helicscpp::FederateInfo::getInfo
HelicsFederateInfo getInfo()
Definition: cpp98/Federate.hpp:163
helicscpp::Federate
Definition: cpp98/Federate.hpp:229
helicscpp::Federate::fed
HelicsFederate fed
underlying HelicsFederate object
Definition: cpp98/Federate.hpp:904
helicscpp::Federate::operator=
Federate & operator=(const Federate &fedObj)
Copy assignment operator.
Definition: cpp98/Federate.hpp:239
helicscpp::Federate::Federate
Federate() HELICS_NOTHROW
Default constructor.
Definition: cpp98/Federate.hpp:232
helicscpp::HelicsException
Definition: helicsExceptions.hpp:19
helicscpp::Input
Definition: Input.hpp:21
helicscpp::Publication
Definition: Publication.hpp:20
helicscpp::ValueFederate
Definition: cpp98/ValueFederate.hpp:36
helicscpp::ValueFederate::registerIndexedInput
Input registerIndexedInput(const std::string &key, int index1, int index2, HelicsDataTypes type, const std::string &units=std::string())
Definition: cpp98/ValueFederate.hpp:426
helicscpp::ValueFederate::registerSubscriptionIndexed
Input registerSubscriptionIndexed(const std::string &name, int index1, int index2, const std::string &units="")
Definition: cpp98/ValueFederate.hpp:323
helicscpp::ValueFederate::publishJSON
void publishJSON(const std::string &json)
Definition: cpp98/ValueFederate.hpp:464
helicscpp::ValueFederate::getInput
Input getInput(const std::string &name)
Definition: cpp98/ValueFederate.hpp:437
helicscpp::ValueFederate::ValueFederate
ValueFederate(const ValueFederate &vfed)
Definition: cpp98/ValueFederate.hpp:67
helicscpp::ValueFederate::registerIndexedSubscription
Input registerIndexedSubscription(const std::string &name, int index1, const std::string &units="")
Definition: cpp98/ValueFederate.hpp:279
helicscpp::ValueFederate::registerFromPublicationJSON
void registerFromPublicationJSON(const std::string &json)
Definition: cpp98/ValueFederate.hpp:247
helicscpp::ValueFederate::ValueFederate
ValueFederate(const std::string &fedName, FederateInfo &fedInfo)
Definition: cpp98/ValueFederate.hpp:48
helicscpp::ValueFederate::registerSubscriptionIndexed
Input registerSubscriptionIndexed(const std::string &name, int index1, const std::string &units="")
Definition: cpp98/ValueFederate.hpp:309
helicscpp::ValueFederate::getInput
Input getInput(int index)
Definition: cpp98/ValueFederate.hpp:449
helicscpp::ValueFederate::registerGlobalPublication
Publication registerGlobalPublication(const std::string &key, HelicsDataTypes type, const std::string &units="")
Definition: cpp98/ValueFederate.hpp:160
helicscpp::ValueFederate::registerIndexedPublication
Publication registerIndexedPublication(const std::string &key, int index1, HelicsDataTypes type, const std::string &units="")
Definition: cpp98/ValueFederate.hpp:179
helicscpp::ValueFederate::registerInput
Input registerInput(const std::string &name, HelicsDataTypes type, const std::string &units="")
Definition: cpp98/ValueFederate.hpp:356
helicscpp::ValueFederate::registerPublicationIndexed
Publication registerPublicationIndexed(const std::string &key, int index1, int index2, HelicsDataTypes type, const std::string &units=std::string())
Definition: cpp98/ValueFederate.hpp:235
helicscpp::ValueFederate::clearUpdates
void clearUpdates()
Definition: cpp98/ValueFederate.hpp:462
helicscpp::ValueFederate::registerGlobalPublication
Publication registerGlobalPublication(const std::string &name, const std::string &type, const std::string &units="")
Definition: cpp98/ValueFederate.hpp:143
helicscpp::ValueFederate::ValueFederate
ValueFederate(const std::string &configString)
Definition: cpp98/ValueFederate.hpp:59
helicscpp::ValueFederate::registerSubscription
Input registerSubscription(const std::string &name, const std::string &units=std::string())
Definition: cpp98/ValueFederate.hpp:265
helicscpp::ValueFederate::registerInput
Input registerInput(const std::string &name, const std::string &type, const std::string &units="")
Definition: cpp98/ValueFederate.hpp:338
helicscpp::ValueFederate::registerPublication
Publication registerPublication(const std::string &name, HelicsDataTypes type, const std::string &units="")
Definition: cpp98/ValueFederate.hpp:126
helicscpp::ValueFederate::getInputByTarget
Input getInputByTarget(const std::string &target)
Definition: cpp98/ValueFederate.hpp:443
helicscpp::ValueFederate::registerGlobalInput
Input registerGlobalInput(const std::string &key, HelicsDataTypes type, const std::string &units="")
Definition: cpp98/ValueFederate.hpp:388
helicscpp::ValueFederate::registerPublicationIndexed
Publication registerPublicationIndexed(const std::string &key, int index1, HelicsDataTypes type, const std::string &units="")
Definition: cpp98/ValueFederate.hpp:217
helicscpp::ValueFederate::registerIndexedInput
Input registerIndexedInput(const std::string &key, int index1, HelicsDataTypes type, const std::string &units="")
Definition: cpp98/ValueFederate.hpp:407
helicscpp::ValueFederate::getPublication
Publication getPublication(int index)
Definition: cpp98/ValueFederate.hpp:259
helicscpp::ValueFederate::registerPublication
Publication registerPublication(const std::string &name, const std::string &type, const std::string &units="")
Definition: cpp98/ValueFederate.hpp:109
helicscpp::ValueFederate::operator=
ValueFederate & operator=(const ValueFederate &fedObj)
Definition: cpp98/ValueFederate.hpp:69
helicscpp::ValueFederate::ValueFederate
ValueFederate() HELICS_NOTHROW
Definition: cpp98/ValueFederate.hpp:98
helicscpp::ValueFederate::getInputCount
int getInputCount() const
Definition: cpp98/ValueFederate.hpp:454
helicscpp::ValueFederate::getPublicationCount
int getPublicationCount() const
Definition: cpp98/ValueFederate.hpp:456
helicscpp::ValueFederate::registerIndexedSubscription
Input registerIndexedSubscription(const std::string &name, int index1, int index2, const std::string &units="")
Definition: cpp98/ValueFederate.hpp:294
helicscpp::ValueFederate::getPublication
Publication getPublication(const std::string &name)
Definition: cpp98/ValueFederate.hpp:252
helicscpp::ValueFederate::registerGlobalInput
Input registerGlobalInput(const std::string &name, const std::string &type, const std::string &units="")
Definition: cpp98/ValueFederate.hpp:371
helicscpp::ValueFederate::registerIndexedPublication
Publication registerIndexedPublication(const std::string &key, int index1, int index2, HelicsDataTypes type, const std::string &units=std::string())
Definition: cpp98/ValueFederate.hpp:198
helicscpp::ValueFederate::queryUpdates
std::vector< HelicsInput > queryUpdates()
Definition: cpp98/ValueFederate.hpp:459
helicscpp::hThrowOnError
Definition: helicsExceptions.hpp:38
helicsCreateValueFederateFromConfig
HelicsFederate helicsCreateValueFederateFromConfig(const char *configFile, HelicsError *err)
Definition: FederateExport.cpp:499
helicsCreateValueFederate
HelicsFederate helicsCreateValueFederate(const char *fedName, HelicsFederateInfo fedInfo, HelicsError *err)
Definition: FederateExport.cpp:476
HELICS_ERROR_REGISTRATION_FAILURE
@ HELICS_ERROR_REGISTRATION_FAILURE
Definition: helics_enums.h:262
HelicsDataTypes
HelicsDataTypes
Definition: helics_enums.h:67
HELICS_DATA_TYPE_INT
@ HELICS_DATA_TYPE_INT
Definition: helics_enums.h:74
HELICS_DATA_TYPE_DOUBLE
@ HELICS_DATA_TYPE_DOUBLE
Definition: helics_enums.h:72
HELICS_DATA_TYPE_STRING
@ HELICS_DATA_TYPE_STRING
Definition: helics_enums.h:70
HELICS_DATA_TYPE_TIME
@ HELICS_DATA_TYPE_TIME
Definition: helics_enums.h:86
HELICS_DATA_TYPE_RAW
@ HELICS_DATA_TYPE_RAW
Definition: helics_enums.h:90
HELICS_DATA_TYPE_COMPLEX
@ HELICS_DATA_TYPE_COMPLEX
Definition: helics_enums.h:76
HELICS_DATA_TYPE_VECTOR
@ HELICS_DATA_TYPE_VECTOR
Definition: helics_enums.h:78
HELICS_DATA_TYPE_BOOLEAN
@ HELICS_DATA_TYPE_BOOLEAN
Definition: helics_enums.h:84
helicscpp
Definition: cpp98/Broker.hpp:18
helicscpp::PubSubTypes
PubSubTypes
Definition: cpp98/ValueFederate.hpp:24