helics  2.8.1
cpp98/ValueFederate.hpp
1 /*
2 Copyright (c) 2017-2021,
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 "../shared_api_library/ValueFederate.h"
12 #include "Federate.hpp"
13 #include "Input.hpp"
14 #include "Publication.hpp"
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<helics_input> ipts;
39  std::vector<helics_publication> 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  helics_data_type 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  helics_data_type 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  helics_data_type 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  helics_data_type 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  helics_data_type 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  helics_data_type 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  helics_input 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, helics_data_type 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  helics_data_type 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  helics_data_type 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  helics_data_type 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  }
450  // TODO(PT): use c api to implement this method... callbacks too?
452  std::vector<helics_input> queryUpdates() { return std::vector<helics_input>(); }
453 
457  void publishJSON(const std::string& json)
458  {
459  helicsFederatePublishJSON(fed, json.c_str(), hThrowOnError());
460  }
461 
462  private:
463  // Utility function for converting numbers to string
464  template<typename T>
465  std::string toStr(T num)
466  {
467  std::ostringstream ss;
468  ss << num;
469  return ss.str();
470  }
471 };
472 } // namespace helicscpp
473 #endif
helicscpp::ValueFederate::ValueFederate
ValueFederate(const std::string &configString)
Definition: cpp98/ValueFederate.hpp:59
helics_data_type_raw
@ helics_data_type_raw
Definition: helics_enums.h:71
helicscpp::ValueFederate::registerSubscriptionIndexed
Input registerSubscriptionIndexed(const std::string &name, int index1, const std::string &units="")
Definition: cpp98/ValueFederate.hpp:309
helicscpp::ValueFederate::registerSubscription
Input registerSubscription(const std::string &name, const std::string &units=std::string())
Definition: cpp98/ValueFederate.hpp:265
helicscpp::ValueFederate::registerGlobalInput
Input registerGlobalInput(const std::string &key, helics_data_type type, const std::string &units="")
Definition: cpp98/ValueFederate.hpp:388
helicsFederateRegisterSubscription
helics_input helicsFederateRegisterSubscription(helics_federate fed, const char *key, const char *units, helics_error *err)
Definition: ValueFederateExport.cpp:77
helicsFederateGetPublication
helics_publication helicsFederateGetPublication(helics_federate fed, const char *key, helics_error *err)
Definition: ValueFederateExport.cpp:334
helicscpp::ValueFederate::registerPublication
Publication registerPublication(const std::string &name, helics_data_type type, const std::string &units="")
Definition: cpp98/ValueFederate.hpp:126
helicscpp::ValueFederate::registerGlobalInput
Input registerGlobalInput(const std::string &name, const std::string &type, const std::string &units="")
Definition: cpp98/ValueFederate.hpp:371
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
helicsFederateRegisterGlobalTypeInput
helics_publication helicsFederateRegisterGlobalTypeInput(helics_federate fed, const char *key, const char *type, const char *units, helics_error *err)
Definition: ValueFederateExport.cpp:248
helicsFederateGetInputCount
int helicsFederateGetInputCount(helics_federate fed)
Definition: ValueFederateExport.cpp:1450
helics_publication
void * helics_publication
Definition: api-data.h:31
helicsCreateValueFederate
helics_federate helicsCreateValueFederate(const char *fedName, helics_federate_info fi, helics_error *err)
Definition: FederateExport.cpp:382
helicscpp::ValueFederate::registerIndexedPublication
Publication registerIndexedPublication(const std::string &key, int index1, int index2, helics_data_type type, const std::string &units=std::string())
Definition: cpp98/ValueFederate.hpp:198
helicscpp::PubSubTypes
PubSubTypes
Definition: cpp98/ValueFederate.hpp:24
helicscpp::Federate
Definition: cpp98/Federate.hpp:186
helicsFederateGetPublicationByIndex
helics_publication helicsFederateGetPublicationByIndex(helics_federate fed, int index, helics_error *err)
Definition: ValueFederateExport.cpp:359
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
helicscpp::ValueFederate
Definition: cpp98/ValueFederate.hpp:36
helicscpp::ValueFederate::registerGlobalPublication
Publication registerGlobalPublication(const std::string &key, helics_data_type type, const std::string &units="")
Definition: cpp98/ValueFederate.hpp:160
helicscpp::FederateInfo
Definition: cpp98/Federate.hpp:29
helicsFederateRegisterGlobalTypePublication
helics_publication helicsFederateRegisterGlobalTypePublication(helics_federate fed, const char *key, const char *type, const char *units, helics_error *err)
Definition: ValueFederateExport.cpp:144
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
helicscpp::ValueFederate::registerSubscriptionIndexed
Input registerSubscriptionIndexed(const std::string &name, int index1, int index2, const std::string &units="")
Definition: cpp98/ValueFederate.hpp:323
helicsFederateRegisterGlobalPublication
helics_publication helicsFederateRegisterGlobalPublication(helics_federate fed, const char *key, helics_data_type type, const char *units, helics_error *err)
Definition: ValueFederateExport.cpp:167
helicscpp::FederateInfo::getInfo
helics_federate_info getInfo()
Definition: cpp98/Federate.hpp:156
helics_error_registration_failure
@ helics_error_registration_failure
Definition: helics_enums.h:208
helicsCreateValueFederateFromConfig
helics_federate helicsCreateValueFederateFromConfig(const char *configFile, helics_error *err)
Definition: FederateExport.cpp:409
helicsFederateRegisterTypePublication
helics_publication helicsFederateRegisterTypePublication(helics_federate fed, const char *key, const char *type, const char *units, helics_error *err)
Definition: ValueFederateExport.cpp:98
helicscpp::ValueFederate::registerIndexedPublication
Publication registerIndexedPublication(const std::string &key, int index1, helics_data_type type, const std::string &units="")
Definition: cpp98/ValueFederate.hpp:179
helicscpp::ValueFederate::registerIndexedInput
Input registerIndexedInput(const std::string &key, int index1, helics_data_type type, const std::string &units="")
Definition: cpp98/ValueFederate.hpp:407
helicscpp::ValueFederate::clearUpdates
void clearUpdates()
Definition: cpp98/ValueFederate.hpp:455
helicscpp::ValueFederate::getInputCount
int getInputCount() const
Definition: cpp98/ValueFederate.hpp:447
helicscpp::Federate::operator=
Federate & operator=(const Federate &fedObj)
Copy assignment operator.
Definition: cpp98/Federate.hpp:196
helicscpp::Input
Definition: Input.hpp:19
helicsFederateRegisterPublication
helics_publication helicsFederateRegisterPublication(helics_federate fed, const char *key, helics_data_type type, const char *units, helics_error *err)
Definition: ValueFederateExport.cpp:118
helicscpp::hThrowOnError
Definition: helicsExceptions.hpp:38
helicscpp::ValueFederate::ValueFederate
ValueFederate(const ValueFederate &vfed)
Definition: cpp98/ValueFederate.hpp:67
helics_data_type_double
@ helics_data_type_double
Definition: helics_enums.h:55
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:57
helicscpp::Federate::fed
helics_federate fed
underlying helics_federate object
Definition: cpp98/Federate.hpp:662
helicscpp::ValueFederate::getPublication
Publication getPublication(const std::string &name)
Definition: cpp98/ValueFederate.hpp:252
helics_data_type
helics_data_type
Definition: helics_enums.h:51
helicscpp::ValueFederate::registerFromPublicationJSON
void registerFromPublicationJSON(const std::string &json)
Definition: cpp98/ValueFederate.hpp:247
helicscpp::ValueFederate::getPublicationCount
int getPublicationCount() const
Definition: cpp98/ValueFederate.hpp:449
helicscpp::Federate::Federate
Federate() HELICS_NOTHROW
Default constructor.
Definition: cpp98/Federate.hpp:189
helics_data_type_string
@ helics_data_type_string
Definition: helics_enums.h:53
helics_data_type_vector
@ helics_data_type_vector
Definition: helics_enums.h:61
helicsFederateGetPublicationCount
int helicsFederateGetPublicationCount(helics_federate fed)
Definition: ValueFederateExport.cpp:1440
helicsFederateRegisterGlobalInput
helics_publication helicsFederateRegisterGlobalInput(helics_federate fed, const char *key, helics_data_type type, const char *units, helics_error *err)
Definition: ValueFederateExport.cpp:268
helics_data_type_complex
@ helics_data_type_complex
Definition: helics_enums.h:59
helics_data_type_time
@ helics_data_type_time
Definition: helics_enums.h:69
helicscpp::ValueFederate::registerPublicationIndexed
Publication registerPublicationIndexed(const std::string &key, int index1, helics_data_type type, const std::string &units="")
Definition: cpp98/ValueFederate.hpp:217
helicscpp::ValueFederate::getPublication
Publication getPublication(int index)
Definition: cpp98/ValueFederate.hpp:259
helicsFederateClearUpdates
void helicsFederateClearUpdates(helics_federate fed)
Definition: ValueFederateExport.cpp:467
helicscpp::ValueFederate::operator=
ValueFederate & operator=(const ValueFederate &fedObj)
Definition: cpp98/ValueFederate.hpp:69
helicscpp::ValueFederate::getSubscription
Input getSubscription(int index)
Definition: cpp98/ValueFederate.hpp:442
helicscpp::ValueFederate::registerIndexedInput
Input registerIndexedInput(const std::string &key, int index1, int index2, helics_data_type type, const std::string &units=std::string())
Definition: cpp98/ValueFederate.hpp:426
helicsFederateRegisterTypeInput
helics_input helicsFederateRegisterTypeInput(helics_federate fed, const char *key, const char *type, const char *units, helics_error *err)
Definition: ValueFederateExport.cpp:199
helicsFederatePublishJSON
void helicsFederatePublishJSON(helics_federate fed, const char *json, helics_error *err)
Definition: ValueFederateExport.cpp:314
helicsFederateRegisterFromPublicationJSON
void helicsFederateRegisterFromPublicationJSON(helics_federate fed, const char *json, helics_error *err)
Definition: ValueFederateExport.cpp:297
helics_input
void * helics_input
Definition: api-data.h:26
helicscpp::ValueFederate::registerGlobalPublication
Publication registerGlobalPublication(const std::string &name, const std::string &type, const std::string &units="")
Definition: cpp98/ValueFederate.hpp:143
helicscpp::ValueFederate::registerPublicationIndexed
Publication registerPublicationIndexed(const std::string &key, int index1, int index2, helics_data_type type, const std::string &units=std::string())
Definition: cpp98/ValueFederate.hpp:235
helicscpp::ValueFederate::registerInput
Input registerInput(const std::string &name, helics_data_type type, const std::string &units="")
Definition: cpp98/ValueFederate.hpp:356
helics_data_type_boolean
@ helics_data_type_boolean
Definition: helics_enums.h:67
helicscpp::ValueFederate::queryUpdates
std::vector< helics_input > queryUpdates()
Definition: cpp98/ValueFederate.hpp:452
helicsFederateGetInput
helics_input helicsFederateGetInput(helics_federate fed, const char *key, helics_error *err)
Definition: ValueFederateExport.cpp:388
helicsFederateGetInputByIndex
helics_input helicsFederateGetInputByIndex(helics_federate fed, int index, helics_error *err)
Definition: ValueFederateExport.cpp:414
helicscpp::ValueFederate::publishJSON
void publishJSON(const std::string &json)
Definition: cpp98/ValueFederate.hpp:457
helicscpp::Publication
Definition: Publication.hpp:19
helicscpp
Definition: cpp98/Broker.hpp:18