| helics 3.6.1

cpp98/Broker.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_BROKER_HPP_
8 #define HELICS_CPP98_BROKER_HPP_
9 #pragma once
10
11 #include "config.hpp"
12 #include "helics/helics.h"
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());
32 if (helicsBrokerIsConnected(broker) != HELICS_TRUE) {
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 }
48 Broker(const Broker& brk) { broker = helicsBrokerClone(brk.broker, hThrowOnError()); }
50 Broker& operator=(const Broker& brk)
51 {
52 broker = helicsBrokerClone(brk.broker, hThrowOnError());
53 return *this;
54 }
55 #ifdef HELICS_HAS_RVALUE_REFS
57 Broker(Broker&& brk) HELICS_NOTHROW: broker(brk.broker) { brk.broker = HELICS_NULL_POINTER; }
59 Broker& operator=(Broker&& brk) HELICS_NOTHROW
60 {
61 broker = brk.broker;
62 brk.broker = HELICS_NULL_POINTER;
63 return *this;
64 }
65 #endif
67 virtual ~Broker()
68 {
69 if (broker != HELICS_NULL_POINTER) {
70 helicsBrokerFree(broker);
71 }
72 }
74 operator HelicsBroker() { return broker; }
77 HelicsBroker baseObject() const { return broker; }
79 bool isConnected() const { return (helicsBrokerIsConnected(broker) != HELICS_FALSE); }
84 bool waitForDisconnect(int msToWait = -1)
85 {
86 return (helicsBrokerWaitForDisconnect(broker, msToWait, hThrowOnError()) != HELICS_FALSE);
87 }
90 void disconnect() { helicsBrokerDisconnect(broker, hThrowOnError()); }
92 const char* getIdentifier() const { return helicsBrokerGetIdentifier(broker); }
94 const char* getAddress() const { return helicsBrokerGetAddress(broker); }
95
104 void setGlobal(const std::string& valueName, const std::string& value)
105 {
106 helicsBrokerSetGlobal(broker, valueName.c_str(), value.c_str(), hThrowOnError());
107 }
112 void addAlias(const std::string& interfaceName, const std::string& alias)
113 {
114 helicsBrokerAddAlias(broker, interfaceName.c_str(), alias.c_str(), hThrowOnError());
115 }
119 void dataLink(const std::string& source, const std::string& target)
120 {
121 helicsBrokerDataLink(broker, source.c_str(), target.c_str(), hThrowOnError());
122 }
127 void addSourceFilterToEndpoint(const std::string& filter, const std::string& target)
128 {
129 helicsBrokerAddSourceFilterToEndpoint(broker,
130 filter.c_str(),
131 target.c_str(),
132 hThrowOnError());
133 }
138 void addDestinationFilterToEndpoint(const std::string& filter, const std::string& target)
139 {
140 helicsBrokerAddDestinationFilterToEndpoint(broker,
141 filter.c_str(),
142 target.c_str(),
143 hThrowOnError());
144 }
145
159 std::string query(const std::string& target,
160 const std::string& queryStr,
161 HelicsSequencingModes mode = HELICS_SEQUENCING_MODE_FAST) const
162 {
163 // returns HelicsQuery
164 HelicsQuery q = helicsCreateQuery(target.c_str(), queryStr.c_str());
165 if (mode != HELICS_SEQUENCING_MODE_FAST) {
166 helicsQuerySetOrdering(q, mode, HELICS_IGNORE_ERROR);
167 }
168 std::string result(helicsQueryBrokerExecute(q, broker, hThrowOnError()));
169 helicsQueryFree(q);
170 return result;
171 }
172
177 void sendCommand(const std::string& target, const std::string& command)
178 {
179 helicsBrokerSendCommand(broker, target.c_str(), command.c_str(), hThrowOnError());
180 }
181
182 void setTimeBarrier(HelicsTime barrierTime)
183 {
184 helicsBrokerSetTimeBarrier(broker, barrierTime, HELICS_IGNORE_ERROR);
185 }
186
187 void clearTimeBarrier() { helicsBrokerClearTimeBarrier(broker); }
188
189 void globalError(int errorCode, const std::string& errorString)
190 {
191 helicsBrokerGlobalError(broker, errorCode, errorString.c_str(), HELICS_IGNORE_ERROR);
192 }
193
194 protected:
195 HelicsBroker broker;
196 };
197
198 } // namespace helicscpp
199 #endif
HelicsTime
double HelicsTime
Definition: api-data.h:106
HelicsQuery
void * HelicsQuery
Definition: api-data.h:83
HELICS_FALSE
const HelicsBool HELICS_FALSE
Definition: api-data.h:121
HELICS_TRUE
const HelicsBool HELICS_TRUE
Definition: api-data.h:120
HelicsBroker
void * HelicsBroker
Definition: api-data.h:60
helicscpp::Broker
Definition: cpp98/Broker.hpp:19
helicscpp::Broker::getIdentifier
const char * getIdentifier() const
Definition: cpp98/Broker.hpp:92
helicscpp::Broker::addAlias
void addAlias(const std::string &interfaceName, const std::string &alias)
Definition: cpp98/Broker.hpp:112
helicscpp::Broker::Broker
Broker() HELICS_NOTHROW
Definition: cpp98/Broker.hpp:22
helicscpp::Broker::Broker
Broker(std::string type, std::string name, int argc, char **argv)
Definition: cpp98/Broker.hpp:42
helicscpp::Broker::baseObject
HelicsBroker baseObject() const
Definition: cpp98/Broker.hpp:77
helicscpp::Broker::Broker
Broker(const Broker &brk)
Definition: cpp98/Broker.hpp:48
helicscpp::Broker::addSourceFilterToEndpoint
void addSourceFilterToEndpoint(const std::string &filter, const std::string &target)
Definition: cpp98/Broker.hpp:127
helicscpp::Broker::broker
HelicsBroker broker
underlying broker information
Definition: cpp98/Broker.hpp:195
helicscpp::Broker::Broker
Broker(std::string type, std::string name, std::string initString)
Definition: cpp98/Broker.hpp:28
helicscpp::Broker::disconnect
void disconnect()
Definition: cpp98/Broker.hpp:90
helicscpp::Broker::dataLink
void dataLink(const std::string &source, const std::string &target)
Definition: cpp98/Broker.hpp:119
helicscpp::Broker::isConnected
bool isConnected() const
Definition: cpp98/Broker.hpp:79
helicscpp::Broker::~Broker
virtual ~Broker()
Definition: cpp98/Broker.hpp:67
helicscpp::Broker::waitForDisconnect
bool waitForDisconnect(int msToWait=-1)
Definition: cpp98/Broker.hpp:84
helicscpp::Broker::query
std::string query(const std::string &target, const std::string &queryStr, HelicsSequencingModes mode=HELICS_SEQUENCING_MODE_FAST) const
Definition: cpp98/Broker.hpp:159
helicscpp::Broker::operator=
Broker & operator=(const Broker &brk)
Definition: cpp98/Broker.hpp:50
helicscpp::Broker::getAddress
const char * getAddress() const
Definition: cpp98/Broker.hpp:94
helicscpp::Broker::sendCommand
void sendCommand(const std::string &target, const std::string &command)
Definition: cpp98/Broker.hpp:177
helicscpp::Broker::addDestinationFilterToEndpoint
void addDestinationFilterToEndpoint(const std::string &filter, const std::string &target)
Definition: cpp98/Broker.hpp:138
helicscpp::Broker::setGlobal
void setGlobal(const std::string &valueName, const std::string &value)
Definition: cpp98/Broker.hpp:104
helicscpp::hThrowOnError
Definition: helicsExceptions.hpp:38
helicsBrokerSetTimeBarrier
void helicsBrokerSetTimeBarrier(HelicsBroker broker, HelicsTime barrierTime, HelicsError *err)
Definition: helicsExport.cpp:682
helicsBrokerGetIdentifier
const char * helicsBrokerGetIdentifier(HelicsBroker broker)
Definition: helicsExport.cpp:875
helicsBrokerGetAddress
const char * helicsBrokerGetAddress(HelicsBroker broker)
Definition: helicsExport.cpp:896
helicsBrokerDataLink
void helicsBrokerDataLink(HelicsBroker broker, const char *source, const char *target, HelicsError *err)
Definition: helicsExport.cpp:563
helicsBrokerGlobalError
void helicsBrokerGlobalError(HelicsBroker broker, int errorCode, const char *errorString, HelicsError *err)
Definition: helicsExport.cpp:700
helicsBrokerWaitForDisconnect
HelicsBool helicsBrokerWaitForDisconnect(HelicsBroker broker, int msToWait, HelicsError *err)
Definition: helicsExport.cpp:969
helicsQuerySetOrdering
void helicsQuerySetOrdering(HelicsQuery query, int32_t mode, HelicsError *err)
Definition: helicsExport.cpp:1305
helicsBrokerSendCommand
void helicsBrokerSendCommand(HelicsBroker broker, const char *target, const char *command, HelicsError *err)
Definition: helicsExport.cpp:655
helicsBrokerClone
HelicsBroker helicsBrokerClone(HelicsBroker broker, HelicsError *err)
Definition: helicsExport.cpp:529
helicsCreateBrokerFromArgs
HelicsBroker helicsCreateBrokerFromArgs(const char *type, const char *name, int argc, const char *const *argv, HelicsError *err)
Definition: helicsExport.cpp:496
helicsQueryBrokerExecute
const char * helicsQueryBrokerExecute(HelicsQuery query, HelicsBroker broker, HelicsError *err)
Definition: helicsExport.cpp:1192
helicsQueryFree
void helicsQueryFree(HelicsQuery query)
Definition: helicsExport.cpp:1314
helicsBrokerAddDestinationFilterToEndpoint
void helicsBrokerAddDestinationFilterToEndpoint(HelicsBroker broker, const char *filter, const char *endpoint, HelicsError *err)
Definition: helicsExport.cpp:731
helicsCreateQuery
HelicsQuery helicsCreateQuery(const char *target, const char *query)
Definition: helicsExport.cpp:1128
helicsBrokerClearTimeBarrier
void helicsBrokerClearTimeBarrier(HelicsBroker broker)
Definition: helicsExport.cpp:691
helicsBrokerIsConnected
HelicsBool helicsBrokerIsConnected(HelicsBroker broker)
Definition: helicsExport.cpp:552
helicsCreateBroker
HelicsBroker helicsCreateBroker(const char *type, const char *name, const char *initString, HelicsError *err)
Definition: helicsExport.cpp:468
helicsBrokerDisconnect
void helicsBrokerDisconnect(HelicsBroker broker, HelicsError *err)
Definition: helicsExport.cpp:1005
helicsBrokerAddSourceFilterToEndpoint
void helicsBrokerAddSourceFilterToEndpoint(HelicsBroker broker, const char *filter, const char *endpoint, HelicsError *err)
Definition: helicsExport.cpp:718
helicsBrokerSetGlobal
void helicsBrokerSetGlobal(HelicsBroker broker, const char *valueName, const char *value, HelicsError *err)
Definition: helicsExport.cpp:591
helicsBrokerFree
void helicsBrokerFree(HelicsBroker broker)
Definition: helicsExport.cpp:1049
helicsBrokerAddAlias
void helicsBrokerAddAlias(HelicsBroker broker, const char *interfaceName, const char *alias, HelicsError *err)
Definition: helicsExport.cpp:631
HelicsSequencingModes
HelicsSequencingModes
Definition: helics_enums.h:427
HELICS_SEQUENCING_MODE_FAST
@ HELICS_SEQUENCING_MODE_FAST
Definition: helics_enums.h:429
helicscpp
Definition: cpp98/Broker.hpp:18