helics  2.8.1
cpp98/Broker.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_BROKER_HPP_
8 #define HELICS_CPP98_BROKER_HPP_
9 #pragma once
10 
11 #include "../shared_api_library/helics.h"
12 #include "config.hpp"
13 #include "helicsExceptions.hpp"
14 
15 #include <stdexcept>
16 #include <string>
17 
18 namespace helicscpp {
19 class Broker {
20  public:
22  Broker() HELICS_NOTHROW: broker(HELICS_NULL_POINTER) {}
28  Broker(std::string type, std::string name, std::string initString)
29  {
30  broker =
31  helicsCreateBroker(type.c_str(), name.c_str(), initString.c_str(), hThrowOnError());
33  throw(std::runtime_error("broker creation failed"));
34  }
35  }
42  Broker(std::string type, std::string name, int argc, char** argv)
43  {
44  broker =
45  helicsCreateBrokerFromArgs(type.c_str(), name.c_str(), argc, argv, hThrowOnError());
46  }
50  Broker& operator=(const Broker& brk)
51  {
53  return *this;
54  }
55 #ifdef HELICS_HAS_RVALUE_REFS
56 
57  Broker(Broker&& brk) HELICS_NOTHROW
58  {
59  broker = brk.broker;
60  brk.broker = HELICS_NULL_POINTER;
61  }
63  Broker& operator=(Broker&& brk) HELICS_NOTHROW
64  {
65  broker = brk.broker;
66  brk.broker = HELICS_NULL_POINTER;
67  return *this;
68  }
69 #endif
70 
71  virtual ~Broker()
72  {
73  if (broker != HELICS_NULL_POINTER) {
75  }
76  }
78  operator helics_broker() { return broker; }
81  helics_broker baseObject() const { return broker; }
83  bool isConnected() const { return (helicsBrokerIsConnected(broker) != helics_false); }
88  bool waitForDisconnect(int msToWait = -1)
89  {
91  }
96  const char* getIdentifier() const { return helicsBrokerGetIdentifier(broker); }
98  const char* getAddress() const { return helicsBrokerGetAddress(broker); }
99 
106  void setGlobal(const std::string& valueName, const std::string& value)
107  {
108  helicsBrokerSetGlobal(broker, valueName.c_str(), value.c_str(), hThrowOnError());
109  }
113  void dataLink(const std::string& source, const std::string& target)
114  {
115  helicsBrokerDataLink(broker, source.c_str(), target.c_str(), hThrowOnError());
116  }
121  void addSourceFilterToEndpoint(const std::string& filter, const std::string& target)
122  {
124  filter.c_str(),
125  target.c_str(),
126  hThrowOnError());
127  }
132  void addDestinationFilterToEndpoint(const std::string& filter, const std::string& target)
133  {
135  filter.c_str(),
136  target.c_str(),
137  hThrowOnError());
138  }
139 
153  std::string query(const std::string& target,
154  const std::string& queryStr,
156  {
157  // returns helics_query
158  helics_query q = helicsCreateQuery(target.c_str(), queryStr.c_str());
159  if (mode != helics_sequencing_mode_fast) {
160  helicsQuerySetOrdering(q, mode, HELICS_IGNORE_ERROR);
161  }
162  std::string result(helicsQueryBrokerExecute(q, broker, hThrowOnError()));
163  helicsQueryFree(q);
164  return result;
165  }
166 
167  void setTimeBarrier(helics_time barrierTime)
168  {
169  helicsBrokerSetTimeBarrier(broker, barrierTime, HELICS_IGNORE_ERROR);
170  }
171  void clearTimeBarrier() { helicsBrokerClearTimeBarrier(broker); }
172 
173  void globalError(int errorCode, const std::string& errorString)
174  {
175  helicsBrokerGlobalError(broker, errorCode, errorString.c_str(), HELICS_IGNORE_ERROR);
176  }
177 
178  protected:
180 };
181 
182 } // namespace helicscpp
183 #endif
helicscpp::Broker::waitForDisconnect
bool waitForDisconnect(int msToWait=-1)
Definition: cpp98/Broker.hpp:88
helicsQueryFree
void helicsQueryFree(helics_query query)
Definition: helicsExport.cpp:1063
helicsBrokerSetTimeBarrier
void helicsBrokerSetTimeBarrier(helics_broker broker, helics_time barrierTime, helics_error *err)
Definition: helicsExport.cpp:529
helicscpp::Broker::isConnected
bool isConnected() const
Definition: cpp98/Broker.hpp:83
helicscpp::Broker::operator=
Broker & operator=(const Broker &brk)
Definition: cpp98/Broker.hpp:50
helicsCreateBrokerFromArgs
helics_broker helicsCreateBrokerFromArgs(const char *type, const char *name, int argc, const char *const *argv, helics_error *err)
Definition: helicsExport.cpp:412
helicscpp::Broker
Definition: cpp98/Broker.hpp:19
helicscpp::Broker::addDestinationFilterToEndpoint
void addDestinationFilterToEndpoint(const std::string &filter, const std::string &target)
Definition: cpp98/Broker.hpp:132
helicsBrokerGlobalError
void helicsBrokerGlobalError(helics_broker broker, int errorCode, const char *errorString, helics_error *err)
Definition: helicsExport.cpp:547
helics_time
double helics_time
Definition: api-data.h:81
helics_query
void * helics_query
Definition: api-data.h:66
helics_false
const helics_bool helics_false
Definition: api-data.h:95
helicsBrokerDisconnect
void helicsBrokerDisconnect(helics_broker broker, helics_error *err)
Definition: helicsExport.cpp:783
helicscpp::Broker::dataLink
void dataLink(const std::string &source, const std::string &target)
Definition: cpp98/Broker.hpp:113
helics_true
const helics_bool helics_true
Definition: api-data.h:94
helicsBrokerGetIdentifier
const char * helicsBrokerGetIdentifier(helics_broker broker)
Definition: helicsExport.cpp:676
helicsBrokerAddSourceFilterToEndpoint
void helicsBrokerAddSourceFilterToEndpoint(helics_broker broker, const char *filter, const char *endpoint, helics_error *err)
Definition: helicsExport.cpp:565
helicscpp::Broker::baseObject
helics_broker baseObject() const
Definition: cpp98/Broker.hpp:81
helics_sequencing_mode
helics_sequencing_mode
Definition: helics_enums.h:333
helicsBrokerSetGlobal
void helicsBrokerSetGlobal(helics_broker broker, const char *valueName, const char *value, helics_error *err)
Definition: helicsExport.cpp:507
helicsBrokerFree
void helicsBrokerFree(helics_broker broker)
Definition: helicsExport.cpp:827
helicscpp::Broker::Broker
Broker(std::string type, std::string name, int argc, char **argv)
Definition: cpp98/Broker.hpp:42
helicscpp::Broker::getAddress
const char * getAddress() const
Definition: cpp98/Broker.hpp:98
helicsBrokerAddDestinationFilterToEndpoint
void helicsBrokerAddDestinationFilterToEndpoint(helics_broker broker, const char *filter, const char *endpoint, helics_error *err)
Definition: helicsExport.cpp:578
helicscpp::Broker::Broker
Broker(std::string type, std::string name, std::string initString)
Definition: cpp98/Broker.hpp:28
helicsQuerySetOrdering
void helicsQuerySetOrdering(helics_query query, int32_t mode, helics_error *err)
Definition: helicsExport.cpp:1054
helicscpp::Broker::broker
helics_broker broker
underlying broker information
Definition: cpp98/Broker.hpp:179
helicscpp::hThrowOnError
Definition: helicsExceptions.hpp:38
helicsBrokerIsConnected
helics_bool helicsBrokerIsConnected(helics_broker broker)
Definition: helicsExport.cpp:468
helicscpp::Broker::Broker
Broker() HELICS_NOTHROW
Definition: cpp98/Broker.hpp:22
helicscpp::Broker::addSourceFilterToEndpoint
void addSourceFilterToEndpoint(const std::string &filter, const std::string &target)
Definition: cpp98/Broker.hpp:121
helicsBrokerClearTimeBarrier
void helicsBrokerClearTimeBarrier(helics_broker broker)
Definition: helicsExport.cpp:538
helicsBrokerGetAddress
const char * helicsBrokerGetAddress(helics_broker broker)
Definition: helicsExport.cpp:697
helicscpp::Broker::query
std::string query(const std::string &target, const std::string &queryStr, helics_sequencing_mode mode=helics_sequencing_mode_fast) const
Definition: cpp98/Broker.hpp:153
helicscpp::Broker::Broker
Broker(const Broker &brk)
Definition: cpp98/Broker.hpp:48
helicscpp::Broker::getIdentifier
const char * getIdentifier() const
Definition: cpp98/Broker.hpp:96
helicscpp::Broker::~Broker
virtual ~Broker()
Definition: cpp98/Broker.hpp:71
helicsBrokerClone
helics_broker helicsBrokerClone(helics_broker broker, helics_error *err)
Definition: helicsExport.cpp:445
helics_broker
void * helics_broker
Definition: api-data.h:51
helicsBrokerDataLink
void helicsBrokerDataLink(helics_broker broker, const char *source, const char *target, helics_error *err)
Definition: helicsExport.cpp:479
helicscpp::Broker::setGlobal
void setGlobal(const std::string &valueName, const std::string &value)
Definition: cpp98/Broker.hpp:106
helics_sequencing_mode_fast
@ helics_sequencing_mode_fast
Definition: helics_enums.h:335
helicsQueryBrokerExecute
const char * helicsQueryBrokerExecute(helics_query query, helics_broker broker, helics_error *err)
Definition: helicsExport.cpp:959
helicsCreateBroker
helics_broker helicsCreateBroker(const char *type, const char *name, const char *initString, helics_error *err)
Definition: helicsExport.cpp:384
helicscpp::Broker::disconnect
void disconnect()
Definition: cpp98/Broker.hpp:94
helicscpp
Definition: cpp98/Broker.hpp:18
helicsCreateQuery
helics_query helicsCreateQuery(const char *target, const char *query)
Definition: helicsExport.cpp:906
helicsBrokerWaitForDisconnect
helics_bool helicsBrokerWaitForDisconnect(helics_broker broker, int msToWait, helics_error *err)
Definition: helicsExport.cpp:763