| helics 3.6.1

cpp98/MessageFederate.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
8 #ifndef HELICS_CPP98_MESSAGE_FEDERATE_HPP_
9 #define HELICS_CPP98_MESSAGE_FEDERATE_HPP_
10 #pragma once
11
12 #include "Endpoint.hpp"
13 #include "Federate.hpp"
14 #include "helics/helics.h"
15
16 #include <string>
17 #include <vector>
18
19 namespace helicscpp {
22 class MessageFederate: public virtual Federate {
23 public:
29 MessageFederate(const std::string& fedName, FederateInfo& fedInfo)
30 {
31 fed = helicsCreateMessageFederate(fedName.c_str(), fedInfo.getInfo(), hThrowOnError());
32 }
33
37 explicit MessageFederate(const std::string& configString)
38 {
39 fed = helicsCreateMessageFederateFromConfig(configString.c_str(), hThrowOnError());
40 }
41
43 MessageFederate() HELICS_NOTHROW {}
44
53 Endpoint registerEndpoint(const std::string& name, const std::string& type = std::string())
54 {
55 HelicsEndpoint ep =
56 helicsFederateRegisterEndpoint(fed, name.c_str(), type.c_str(), hThrowOnError());
57 local_endpoints.push_back(ep);
58 return Endpoint(ep);
59 }
60
66 Endpoint registerGlobalEndpoint(const std::string& name,
67 const std::string& type = std::string())
68 {
69 HelicsEndpoint ep =
70 helicsFederateRegisterGlobalEndpoint(fed, name.c_str(), type.c_str(), hThrowOnError());
71 local_endpoints.push_back(ep);
72 return Endpoint(ep);
73 }
74
78 Endpoint getEndpoint(const std::string& name)
79 {
80 return Endpoint(helicsFederateGetEndpoint(fed, name.c_str(), hThrowOnError()));
81 }
85 Endpoint getEndpoint(int index)
86 {
87 return Endpoint(helicsFederateGetEndpointByIndex(fed, index, hThrowOnError()));
88 }
89
91 bool hasMessage() const
92 {
93 // returns int, 1 = true, 0 = false
94 return (helicsFederateHasMessage(fed) > 0);
95 }
96
98 int pendingMessageCount() const { return helicsFederatePendingMessageCount(fed); }
99
101 Message getMessage() { return Message(helicsFederateGetMessage(fed)); }
102
104 Message createMessage() { return Message(helicsFederateCreateMessage(fed, hThrowOnError())); }
106 int getEndpointCount() const { return helicsFederateGetEndpointCount(fed); }
107
108 private:
109 std::vector<HelicsEndpoint> local_endpoints;
110 };
111 // this code needs the definition of federate before it can de defined
112 inline Message::Message(const Federate& fed):
113 mo(helicsFederateCreateMessage(fed.getObject(), hThrowOnError()))
114 {
115 }
116
117 inline Message& Message::newMessageObject(const Federate& fed)
118 {
119 HelicsMessage newmo = helicsFederateCreateMessage(fed.getObject(), hThrowOnError());
120 if (mo != HELICS_NULL_POINTER) {
121 helicsMessageFree(mo);
122 }
123 mo = newmo;
124 return *this;
125 }
126 } // namespace helicscpp
127 #endif
helicsFederateRegisterGlobalEndpoint
HelicsEndpoint helicsFederateRegisterGlobalEndpoint(HelicsFederate fed, const char *name, const char *type, HelicsError *err)
Definition: MessageFederateExport.cpp:118
helicsFederateRegisterEndpoint
HelicsEndpoint helicsFederateRegisterEndpoint(HelicsFederate fed, const char *name, const char *type, HelicsError *err)
Definition: MessageFederateExport.cpp:76
helicsFederateGetMessage
HelicsMessage helicsFederateGetMessage(HelicsFederate fed)
Definition: MessageFederateExport.cpp:569
helicsFederateHasMessage
HelicsBool helicsFederateHasMessage(HelicsFederate fed)
Definition: MessageFederateExport.cpp:436
helicsFederateGetEndpointCount
int helicsFederateGetEndpointCount(HelicsFederate fed)
Definition: MessageFederateExport.cpp:679
helicsFederateGetEndpoint
HelicsEndpoint helicsFederateGetEndpoint(HelicsFederate fed, const char *name, HelicsError *err)
Definition: MessageFederateExport.cpp:161
helicsFederateCreateMessage
HelicsMessage helicsFederateCreateMessage(HelicsFederate fed, HelicsError *err)
Definition: MessageFederateExport.cpp:587
helicsFederatePendingMessageCount
int helicsFederatePendingMessageCount(HelicsFederate fed)
Definition: MessageFederateExport.cpp:454
helicsFederateGetEndpointByIndex
HelicsEndpoint helicsFederateGetEndpointByIndex(HelicsFederate fed, int index, HelicsError *err)
Definition: MessageFederateExport.cpp:184
HelicsEndpoint
void * HelicsEndpoint
Definition: api-data.h:37
HelicsMessage
void * HelicsMessage
Definition: api-data.h:100
helicscpp::Endpoint
Definition: Endpoint.hpp:209
helicscpp::FederateInfo
Definition: cpp98/Federate.hpp:28
helicscpp::FederateInfo::getInfo
HelicsFederateInfo getInfo()
Definition: cpp98/Federate.hpp:163
helicscpp::Federate
Definition: cpp98/Federate.hpp:229
helicscpp::Federate::fed
HelicsFederate fed
underlying HelicsFederate object
Definition: cpp98/Federate.hpp:904
helicscpp::Federate::getObject
HelicsFederate getObject() const
Definition: cpp98/Federate.hpp:901
helicscpp::MessageFederate
Definition: cpp98/MessageFederate.hpp:22
helicscpp::MessageFederate::registerEndpoint
Endpoint registerEndpoint(const std::string &name, const std::string &type=std::string())
Definition: cpp98/MessageFederate.hpp:53
helicscpp::MessageFederate::MessageFederate
MessageFederate(const std::string &configString)
Definition: cpp98/MessageFederate.hpp:37
helicscpp::MessageFederate::getEndpointCount
int getEndpointCount() const
Definition: cpp98/MessageFederate.hpp:106
helicscpp::MessageFederate::pendingMessageCount
int pendingMessageCount() const
Definition: cpp98/MessageFederate.hpp:98
helicscpp::MessageFederate::hasMessage
bool hasMessage() const
Definition: cpp98/MessageFederate.hpp:91
helicscpp::MessageFederate::getMessage
Message getMessage()
Definition: cpp98/MessageFederate.hpp:101
helicscpp::MessageFederate::getEndpoint
Endpoint getEndpoint(const std::string &name)
Definition: cpp98/MessageFederate.hpp:78
helicscpp::MessageFederate::getEndpoint
Endpoint getEndpoint(int index)
Definition: cpp98/MessageFederate.hpp:85
helicscpp::MessageFederate::registerGlobalEndpoint
Endpoint registerGlobalEndpoint(const std::string &name, const std::string &type=std::string())
Definition: cpp98/MessageFederate.hpp:66
helicscpp::MessageFederate::createMessage
Message createMessage()
Definition: cpp98/MessageFederate.hpp:104
helicscpp::MessageFederate::MessageFederate
MessageFederate(const std::string &fedName, FederateInfo &fedInfo)
Definition: cpp98/MessageFederate.hpp:29
helicscpp::MessageFederate::MessageFederate
MessageFederate() HELICS_NOTHROW
Definition: cpp98/MessageFederate.hpp:43
helicscpp::Message
Definition: Endpoint.hpp:22
helicscpp::Message::Message
Message() HELICS_NOTHROW
Definition: Endpoint.hpp:25
helicscpp::Message::newMessageObject
Message & newMessageObject(const Federate &fed)
Definition: cpp98/MessageFederate.hpp:117
helicscpp::hThrowOnError
Definition: helicsExceptions.hpp:38
helicsMessageFree
void helicsMessageFree(HelicsMessage message)
Definition: MessageFederateExport.cpp:1252
helicsCreateMessageFederate
HelicsFederate helicsCreateMessageFederate(const char *fedName, HelicsFederateInfo fedInfo, HelicsError *err)
Definition: FederateExport.cpp:515
helicsCreateMessageFederateFromConfig
HelicsFederate helicsCreateMessageFederateFromConfig(const char *configFile, HelicsError *err)
Definition: FederateExport.cpp:537
helicscpp
Definition: cpp98/Broker.hpp:18