helics 3.7.0
Loading...
Searching...
No Matches
cpp98/ValueFederate.hpp
1/*
2Copyright (c) 2017-2026,
3Battelle Memorial Institute; Lawrence Livermore National Security, LLC; Alliance for Energy
4Innovation LLC. See the top-level NOTICE for additional details. All rights reserved.
5SPDX-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
22namespace helicscpp {
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
36class 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) {
53 }
54 }
59 explicit ValueFederate(const std::string& configString)
60 {
61 fed = helicsCreateValueFederateFromConfig(configString.c_str(), hThrowOnError());
62 if (fed == NULL) {
64 }
65 }
67 ValueFederate(const ValueFederate& vfed): Federate(vfed), ipts(vfed.ipts), pubs(vfed.pubs) {}
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 {
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 {
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 {
148 fed, name.c_str(), type.c_str(), units.c_str(), hThrowOnError());
149 pubs.push_back(pub);
150 return Publication(pub);
151 }
152
161 HelicsDataTypes type,
162 const std::string& units = "")
163 {
165 fed, key.c_str(), type, units.c_str(), hThrowOnError());
166 pubs.push_back(pub);
167 return Publication(pub);
168 }
169
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
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
218 int index1,
219 HelicsDataTypes type,
220 const std::string& units = "")
221 {
222 return registerIndexedPublication(key, index1, type, units);
223 }
224
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 {
250 }
252 HELICS_NODISCARD Publication getPublication(const std::string& name)
253 {
255 }
259 HELICS_NODISCARD Publication getPublication(int index)
260 {
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 {
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 {
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 {
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 {
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 HELICS_NODISCARD Input getInput(const std::string& name)
438 {
439 return Input(helicsFederateGetInput(fed, name.c_str(), hThrowOnError()));
440 }
441
443 HELICS_NODISCARD Input getInputByTarget(const std::string& target)
444 {
445 return Input(helicsFederateGetInputByTarget(fed, target.c_str(), hThrowOnError()));
446 }
447
449 HELICS_NODISCARD Input getInput(int index)
450 {
452 }
454 HELICS_NODISCARD int getInputCount() const { return helicsFederateGetInputCount(fed); }
456 HELICS_NODISCARD int getPublicationCount() const
457 {
459 }
460 // TODO(PT): use c api to implement this method... callbacks too?
462 std::vector<HelicsInput> queryUpdates() { return std::vector<HelicsInput>(); }
463
467 void publishJSON(const std::string& json)
468 {
470 }
471
472 private:
473 // Utility function for converting numbers to string
474 template<typename T>
475 std::string toStr(T num)
476 {
477 std::ostringstream ss;
478 ss << num;
479 return ss.str();
480 }
481};
482} // namespace helicscpp
483#endif
HelicsPublication helicsFederateRegisterPublication(HelicsFederate fed, const char *key, HelicsDataTypes type, const char *units, HelicsError *err)
Definition ValueFederateExport.cpp:168
HelicsPublication helicsFederateRegisterTypePublication(HelicsFederate fed, const char *key, const char *type, const char *units, HelicsError *err)
Definition ValueFederateExport.cpp:148
void helicsFederateRegisterFromPublicationJSON(HelicsFederate fed, const char *json, HelicsError *err)
Definition ValueFederateExport.cpp:346
HelicsInput helicsFederateGetInput(HelicsFederate fed, const char *key, HelicsError *err)
Definition ValueFederateExport.cpp:430
int helicsFederateGetPublicationCount(HelicsFederate fed)
Definition ValueFederateExport.cpp:1670
HelicsPublication helicsFederateRegisterGlobalPublication(HelicsFederate fed, const char *key, HelicsDataTypes type, const char *units, HelicsError *err)
Definition ValueFederateExport.cpp:218
HelicsInput helicsFederateRegisterGlobalTypeInput(HelicsFederate fed, const char *key, const char *type, const char *units, HelicsError *err)
Definition ValueFederateExport.cpp:296
HelicsPublication helicsFederateGetPublicationByIndex(HelicsFederate fed, int index, HelicsError *err)
Definition ValueFederateExport.cpp:405
HelicsInput helicsFederateRegisterGlobalInput(HelicsFederate fed, const char *key, HelicsDataTypes type, const char *units, HelicsError *err)
Definition ValueFederateExport.cpp:316
HelicsPublication helicsFederateRegisterGlobalTypePublication(HelicsFederate fed, const char *key, const char *type, const char *units, HelicsError *err)
Definition ValueFederateExport.cpp:198
void helicsFederateClearUpdates(HelicsFederate fed)
Definition ValueFederateExport.cpp:505
void helicsFederatePublishJSON(HelicsFederate fed, const char *json, HelicsError *err)
Definition ValueFederateExport.cpp:363
HelicsPublication helicsFederateGetPublication(HelicsFederate fed, const char *key, HelicsError *err)
Definition ValueFederateExport.cpp:383
HelicsInput helicsFederateGetInputByIndex(HelicsFederate fed, int index, HelicsError *err)
Definition ValueFederateExport.cpp:453
HelicsInput helicsFederateRegisterTypeInput(HelicsFederate fed, const char *key, const char *type, const char *units, HelicsError *err)
Definition ValueFederateExport.cpp:246
HelicsInput helicsFederateRegisterSubscription(HelicsFederate fed, const char *key, const char *units, HelicsError *err)
Definition ValueFederateExport.cpp:127
HelicsInput helicsFederateGetInputByTarget(HelicsFederate fed, const char *target, HelicsError *err)
Definition ValueFederateExport.cpp:482
int helicsFederateGetInputCount(HelicsFederate fed)
Definition ValueFederateExport.cpp:1680
void * HelicsInput
Definition api-data.h:26
void * HelicsPublication
Definition api-data.h:31
Definition cpp98/Federate.hpp:28
HELICS_NODISCARD HelicsFederateInfo getInfo()
Definition cpp98/Federate.hpp:163
Definition cpp98/Federate.hpp:229
HelicsFederate fed
underlying HelicsFederate object
Definition cpp98/Federate.hpp:913
Federate & operator=(const Federate &fedObj)
Copy assignment operator.
Definition cpp98/Federate.hpp:239
Federate() HELICS_NOTHROW
Default constructor.
Definition cpp98/Federate.hpp:232
Definition helicsExceptions.hpp:19
Definition Input.hpp:21
Definition Publication.hpp:20
Definition cpp98/ValueFederate.hpp:36
Input registerIndexedInput(const std::string &key, int index1, int index2, HelicsDataTypes type, const std::string &units=std::string())
Definition cpp98/ValueFederate.hpp:426
Input registerSubscriptionIndexed(const std::string &name, int index1, int index2, const std::string &units="")
Definition cpp98/ValueFederate.hpp:323
void publishJSON(const std::string &json)
Definition cpp98/ValueFederate.hpp:467
std::vector< HelicsInput > queryUpdates()
Definition cpp98/ValueFederate.hpp:462
ValueFederate & operator=(const ValueFederate &fedObj)
Definition cpp98/ValueFederate.hpp:69
ValueFederate(const ValueFederate &vfed)
Definition cpp98/ValueFederate.hpp:67
Input registerIndexedSubscription(const std::string &name, int index1, const std::string &units="")
Definition cpp98/ValueFederate.hpp:279
void registerFromPublicationJSON(const std::string &json)
Definition cpp98/ValueFederate.hpp:247
ValueFederate(const std::string &fedName, FederateInfo &fedInfo)
Definition cpp98/ValueFederate.hpp:48
Input registerSubscriptionIndexed(const std::string &name, int index1, const std::string &units="")
Definition cpp98/ValueFederate.hpp:309
Publication registerGlobalPublication(const std::string &key, HelicsDataTypes type, const std::string &units="")
Definition cpp98/ValueFederate.hpp:160
Publication registerIndexedPublication(const std::string &key, int index1, HelicsDataTypes type, const std::string &units="")
Definition cpp98/ValueFederate.hpp:179
Input registerInput(const std::string &name, HelicsDataTypes type, const std::string &units="")
Definition cpp98/ValueFederate.hpp:356
Publication registerPublicationIndexed(const std::string &key, int index1, int index2, HelicsDataTypes type, const std::string &units=std::string())
Definition cpp98/ValueFederate.hpp:235
void clearUpdates()
Definition cpp98/ValueFederate.hpp:465
Publication registerGlobalPublication(const std::string &name, const std::string &type, const std::string &units="")
Definition cpp98/ValueFederate.hpp:143
ValueFederate(const std::string &configString)
Definition cpp98/ValueFederate.hpp:59
HELICS_NODISCARD int getPublicationCount() const
Definition cpp98/ValueFederate.hpp:456
Input registerSubscription(const std::string &name, const std::string &units=std::string())
Definition cpp98/ValueFederate.hpp:265
HELICS_NODISCARD Input getInput(const std::string &name)
Definition cpp98/ValueFederate.hpp:437
Input registerInput(const std::string &name, const std::string &type, const std::string &units="")
Definition cpp98/ValueFederate.hpp:338
Publication registerPublication(const std::string &name, HelicsDataTypes type, const std::string &units="")
Definition cpp98/ValueFederate.hpp:126
HELICS_NODISCARD Input getInput(int index)
Definition cpp98/ValueFederate.hpp:449
Input registerGlobalInput(const std::string &key, HelicsDataTypes type, const std::string &units="")
Definition cpp98/ValueFederate.hpp:388
Publication registerPublicationIndexed(const std::string &key, int index1, HelicsDataTypes type, const std::string &units="")
Definition cpp98/ValueFederate.hpp:217
Input registerIndexedInput(const std::string &key, int index1, HelicsDataTypes type, const std::string &units="")
Definition cpp98/ValueFederate.hpp:407
HELICS_NODISCARD Publication getPublication(const std::string &name)
Definition cpp98/ValueFederate.hpp:252
Publication registerPublication(const std::string &name, const std::string &type, const std::string &units="")
Definition cpp98/ValueFederate.hpp:109
HELICS_NODISCARD Publication getPublication(int index)
Definition cpp98/ValueFederate.hpp:259
HELICS_NODISCARD int getInputCount() const
Definition cpp98/ValueFederate.hpp:454
ValueFederate() HELICS_NOTHROW
Definition cpp98/ValueFederate.hpp:98
Input registerIndexedSubscription(const std::string &name, int index1, int index2, const std::string &units="")
Definition cpp98/ValueFederate.hpp:294
Input registerGlobalInput(const std::string &name, const std::string &type, const std::string &units="")
Definition cpp98/ValueFederate.hpp:371
HELICS_NODISCARD Input getInputByTarget(const std::string &target)
Definition cpp98/ValueFederate.hpp:443
Publication registerIndexedPublication(const std::string &key, int index1, int index2, HelicsDataTypes type, const std::string &units=std::string())
Definition cpp98/ValueFederate.hpp:198
Definition helicsExceptions.hpp:38
@ HELICS_ERROR_REGISTRATION_FAILURE
Definition helics_enums.h:262
HelicsDataTypes
Definition helics_enums.h:67
@ HELICS_DATA_TYPE_INT
Definition helics_enums.h:74
@ HELICS_DATA_TYPE_DOUBLE
Definition helics_enums.h:72
@ HELICS_DATA_TYPE_STRING
Definition helics_enums.h:70
@ HELICS_DATA_TYPE_TIME
Definition helics_enums.h:86
@ HELICS_DATA_TYPE_RAW
Definition helics_enums.h:90
@ HELICS_DATA_TYPE_COMPLEX
Definition helics_enums.h:76
@ HELICS_DATA_TYPE_VECTOR
Definition helics_enums.h:78
@ HELICS_DATA_TYPE_BOOLEAN
Definition helics_enums.h:84
Definition cpp98/Broker.hpp:18
PubSubTypes
Definition cpp98/ValueFederate.hpp:24