helics  3.3.0
cpp98/ValueFederate.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_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 {
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& fi)
49  {
50  fed = helicsCreateValueFederate(fedName.c_str(), fi.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
80 
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
97 
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 
160  Publication registerGlobalPublication(const std::string& key,
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 
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  {
250  }
252  Publication getPublication(const std::string& name)
253  {
254  return Publication(helicsFederateGetPublication(fed, name.c_str(), hThrowOnError()));
255  }
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  Input getInput(const std::string& name)
438  {
439  return Input(helicsFederateGetInput(fed, name.c_str(), hThrowOnError()));
440  }
443  {
445  }
447  int getInputCount() const
448  {
450  }
453  {
455  }
456  // TODO(PT): use c api to implement this method... callbacks too?
458  std::vector<HelicsInput> queryUpdates()
459  {
460  return std::vector<HelicsInput>();
461  }
462 
465  {
467  }
469  void publishJSON(const std::string& json)
470  {
471  helicsFederatePublishJSON(fed, json.c_str(), hThrowOnError());
472  }
473 
474  private:
475  // Utility function for converting numbers to string
476  template<typename T>
477  std::string toStr(T num)
478  {
479  std::ostringstream ss;
480  ss << num;
481  return ss.str();
482  }
483 };
484 } // namespace helicscpp
485 #endif
helicscpp::ValueFederate::ValueFederate
ValueFederate(const std::string &configString)
Definition: cpp98/ValueFederate.hpp:59
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::registerSubscriptionIndexed
Input registerSubscriptionIndexed(const std::string &name, int index1, const std::string &units="")
Definition: cpp98/ValueFederate.hpp:309
helicscpp::ValueFederate::registerGlobalPublication
Publication registerGlobalPublication(const std::string &key, HelicsDataTypes type, const std::string &units="")
Definition: cpp98/ValueFederate.hpp:160
helicsCreateValueFederateFromConfig
HelicsFederate helicsCreateValueFederateFromConfig(const char *configFile, HelicsError *err)
Definition: FederateExport.cpp:447
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, HelicsDataTypes type, const std::string &units="")
Definition: cpp98/ValueFederate.hpp:356
helicscpp::FederateInfo::getInfo
HelicsFederateInfo getInfo()
Definition: cpp98/Federate.hpp:179
helicscpp::ValueFederate::registerGlobalInput
Input registerGlobalInput(const std::string &name, const std::string &type, const std::string &units="")
Definition: cpp98/ValueFederate.hpp:371
HELICS_DATA_TYPE_COMPLEX
@ HELICS_DATA_TYPE_COMPLEX
Definition: helics_enums.h:74
HelicsDataTypes
HelicsDataTypes
Definition: helics_enums.h:65
helicsFederateGetPublicationCount
int helicsFederateGetPublicationCount(HelicsFederate fed)
Definition: ValueFederateExport.cpp:1592
helicscpp::ValueFederate::ValueFederate
ValueFederate(const std::string &fedName, FederateInfo &fi)
Definition: cpp98/ValueFederate.hpp:48
helicscpp::ValueFederate::registerIndexedSubscription
Input registerIndexedSubscription(const std::string &name, int index1, int index2, const std::string &units="")
Definition: cpp98/ValueFederate.hpp:294
helicscpp::PubSubTypes
PubSubTypes
Definition: cpp98/ValueFederate.hpp:24
HELICS_ERROR_REGISTRATION_FAILURE
@ HELICS_ERROR_REGISTRATION_FAILURE
Definition: helics_enums.h:242
helicscpp::Federate
Definition: cpp98/Federate.hpp:248
helicscpp::ValueFederate::ValueFederate
ValueFederate() HELICS_NOTHROW
Definition: cpp98/ValueFederate.hpp:98
helicscpp::ValueFederate::registerInput
Input registerInput(const std::string &name, const std::string &type, const std::string &units="")
Definition: cpp98/ValueFederate.hpp:338
helicscpp::ValueFederate::registerIndexedSubscription
Input registerIndexedSubscription(const std::string &name, int index1, const std::string &units="")
Definition: cpp98/ValueFederate.hpp:279
HelicsInput
void * HelicsInput
Definition: api-data.h:26
helicscpp::ValueFederate
Definition: cpp98/ValueFederate.hpp:36
HELICS_DATA_TYPE_STRING
@ HELICS_DATA_TYPE_STRING
Definition: helics_enums.h:68
helicscpp::ValueFederate::registerPublication
Publication registerPublication(const std::string &name, HelicsDataTypes type, const std::string &units="")
Definition: cpp98/ValueFederate.hpp:126
helicscpp::FederateInfo
Definition: cpp98/Federate.hpp:28
helicsFederateGetInputCount
int helicsFederateGetInputCount(HelicsFederate fed)
Definition: ValueFederateExport.cpp:1602
helicsFederateGetPublication
HelicsPublication helicsFederateGetPublication(HelicsFederate fed, const char *key, HelicsError *err)
Definition: ValueFederateExport.cpp:333
helicscpp::HelicsException
Definition: helicsExceptions.hpp:19
helicscpp::ValueFederate::registerPublication
Publication registerPublication(const std::string &name, const std::string &type, const std::string &units="")
Definition: cpp98/ValueFederate.hpp:109
helicsFederateRegisterTypePublication
HelicsPublication helicsFederateRegisterTypePublication(HelicsFederate fed, const char *key, const char *type, const char *units, HelicsError *err)
Definition: ValueFederateExport.cpp:98
helicsFederateClearUpdates
void helicsFederateClearUpdates(HelicsFederate fed)
Definition: ValueFederateExport.cpp:466
helicscpp::ValueFederate::registerSubscriptionIndexed
Input registerSubscriptionIndexed(const std::string &name, int index1, int index2, const std::string &units="")
Definition: cpp98/ValueFederate.hpp:323
helicsFederatePublishJSON
void helicsFederatePublishJSON(HelicsFederate fed, const char *json, HelicsError *err)
Definition: ValueFederateExport.cpp:313
helicsFederateRegisterGlobalInput
HelicsPublication helicsFederateRegisterGlobalInput(HelicsFederate fed, const char *key, HelicsDataTypes type, const char *units, HelicsError *err)
Definition: ValueFederateExport.cpp:266
HELICS_DATA_TYPE_BOOLEAN
@ HELICS_DATA_TYPE_BOOLEAN
Definition: helics_enums.h:82
HELICS_DATA_TYPE_RAW
@ HELICS_DATA_TYPE_RAW
Definition: helics_enums.h:88
helicscpp::ValueFederate::queryUpdates
std::vector< HelicsInput > queryUpdates()
Definition: cpp98/ValueFederate.hpp:458
helicsFederateGetInput
HelicsInput helicsFederateGetInput(HelicsFederate fed, const char *key, HelicsError *err)
Definition: ValueFederateExport.cpp:387
helicsFederateRegisterGlobalPublication
HelicsPublication helicsFederateRegisterGlobalPublication(HelicsFederate fed, const char *key, HelicsDataTypes type, const char *units, HelicsError *err)
Definition: ValueFederateExport.cpp:168
HELICS_DATA_TYPE_TIME
@ HELICS_DATA_TYPE_TIME
Definition: helics_enums.h:84
helicscpp::ValueFederate::clearUpdates
void clearUpdates()
Definition: cpp98/ValueFederate.hpp:464
helicscpp::ValueFederate::getInputCount
int getInputCount() const
Definition: cpp98/ValueFederate.hpp:447
HELICS_DATA_TYPE_VECTOR
@ HELICS_DATA_TYPE_VECTOR
Definition: helics_enums.h:76
helicscpp::Federate::operator=
Federate & operator=(const Federate &fedObj)
Copy assignment operator.
Definition: cpp98/Federate.hpp:258
helicsFederateGetInputByIndex
HelicsInput helicsFederateGetInputByIndex(HelicsFederate fed, int index, HelicsError *err)
Definition: ValueFederateExport.cpp:413
helicscpp::Input
Definition: Input.hpp:20
helicsFederateRegisterPublication
HelicsPublication helicsFederateRegisterPublication(HelicsFederate fed, const char *key, HelicsDataTypes type, const char *units, HelicsError *err)
Definition: ValueFederateExport.cpp:118
helicscpp::hThrowOnError
Definition: helicsExceptions.hpp:38
helicscpp::ValueFederate::ValueFederate
ValueFederate(const ValueFederate &vfed)
Definition: cpp98/ValueFederate.hpp:67
helicsFederateRegisterSubscription
HelicsInput helicsFederateRegisterSubscription(HelicsFederate fed, const char *key, const char *units, HelicsError *err)
Definition: ValueFederateExport.cpp:77
helicscpp::ValueFederate::getInput
Input getInput(const std::string &name)
Definition: cpp98/ValueFederate.hpp:437
HELICS_DATA_TYPE_INT
@ HELICS_DATA_TYPE_INT
Definition: helics_enums.h:72
helicscpp::ValueFederate::getPublication
Publication getPublication(const std::string &name)
Definition: cpp98/ValueFederate.hpp:252
helicscpp::ValueFederate::registerFromPublicationJSON
void registerFromPublicationJSON(const std::string &json)
Definition: cpp98/ValueFederate.hpp:247
helicscpp::ValueFederate::getPublicationCount
int getPublicationCount() const
Definition: cpp98/ValueFederate.hpp:452
helicscpp::Federate::Federate
Federate() HELICS_NOTHROW
Default constructor.
Definition: cpp98/Federate.hpp:251
helicscpp::ValueFederate::registerGlobalInput
Input registerGlobalInput(const std::string &key, HelicsDataTypes type, const std::string &units="")
Definition: cpp98/ValueFederate.hpp:388
HELICS_DATA_TYPE_DOUBLE
@ HELICS_DATA_TYPE_DOUBLE
Definition: helics_enums.h:70
helicscpp::ValueFederate::registerPublicationIndexed
Publication registerPublicationIndexed(const std::string &key, int index1, HelicsDataTypes type, const std::string &units="")
Definition: cpp98/ValueFederate.hpp:217
helicsFederateRegisterGlobalTypePublication
HelicsPublication helicsFederateRegisterGlobalTypePublication(HelicsFederate fed, const char *key, const char *type, const char *units, HelicsError *err)
Definition: ValueFederateExport.cpp:148
HelicsPublication
void * HelicsPublication
Definition: api-data.h:31
helicsFederateRegisterTypeInput
HelicsInput helicsFederateRegisterTypeInput(HelicsFederate fed, const char *key, const char *type, const char *units, HelicsError *err)
Definition: ValueFederateExport.cpp:196
helicsFederateRegisterGlobalTypeInput
HelicsPublication helicsFederateRegisterGlobalTypeInput(HelicsFederate fed, const char *key, const char *type, const char *units, HelicsError *err)
Definition: ValueFederateExport.cpp:246
helicsFederateRegisterFromPublicationJSON
void helicsFederateRegisterFromPublicationJSON(HelicsFederate fed, const char *json, HelicsError *err)
Definition: ValueFederateExport.cpp:296
helicscpp::ValueFederate::getPublication
Publication getPublication(int index)
Definition: cpp98/ValueFederate.hpp:259
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::Federate::fed
HelicsFederate fed
underlying HelicsFederate object
Definition: cpp98/Federate.hpp:892
helicscpp::ValueFederate::operator=
ValueFederate & operator=(const ValueFederate &fedObj)
Definition: cpp98/ValueFederate.hpp:69
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::getSubscription
Input getSubscription(int index)
Definition: cpp98/ValueFederate.hpp:442
helicsCreateValueFederate
HelicsFederate helicsCreateValueFederate(const char *fedName, HelicsFederateInfo fi, HelicsError *err)
Definition: FederateExport.cpp:420
helicscpp::ValueFederate::registerGlobalPublication
Publication registerGlobalPublication(const std::string &name, const std::string &type, const std::string &units="")
Definition: cpp98/ValueFederate.hpp:143
helicsFederateGetPublicationByIndex
HelicsPublication helicsFederateGetPublicationByIndex(HelicsFederate fed, int index, HelicsError *err)
Definition: ValueFederateExport.cpp:358
helicscpp::ValueFederate::registerIndexedInput
Input registerIndexedInput(const std::string &key, int index1, HelicsDataTypes type, const std::string &units="")
Definition: cpp98/ValueFederate.hpp:407
helicscpp::ValueFederate::registerIndexedPublication
Publication registerIndexedPublication(const std::string &key, int index1, HelicsDataTypes type, const std::string &units="")
Definition: cpp98/ValueFederate.hpp:179
helicscpp::ValueFederate::publishJSON
void publishJSON(const std::string &json)
Definition: cpp98/ValueFederate.hpp:469
helicscpp::Publication
Definition: Publication.hpp:19
helicscpp
Definition: cpp98/Broker.hpp:18