helics  3.5.2
api_objects.h
1 /*
2 Copyright (c) 2017-2024,
3 Battelle Memorial Institute; Lawrence Livermore National Security, LLC; Alliance for Sustainable Energy, LLC. See the top-level NOTICE for
4 additional details. All rights reserved.
5 SPDX-License-Identifier: BSD-3-Clause
6 */
7 #pragma once
8 
9 #include "../../application_api/helicsTypes.hpp"
10 #include "../../common/GuardedTypes.hpp"
11 #include "../../core/core-data.hpp"
12 #include "../api-data.h"
13 #include "gmlc/concurrency/TripWire.hpp"
14 
15 #include <deque>
16 #include <exception>
17 #include <memory>
18 #include <mutex>
19 #include <string>
20 #include <string_view>
21 #include <utility>
22 #include <vector>
23 
25 static constexpr int gCoreValidationIdentifier = 0x3784'24EC;
26 static constexpr int gBrokerValidationIdentifier = 0xA346'7D20;
27 
28 namespace helics {
29 class Core;
30 class Federate;
31 class Broker;
32 class ValueFederate;
33 class MessageFederate;
34 class CallbackFederate;
35 class Input;
36 class Publication;
37 class Endpoint;
38 class Filter;
39 class Translator;
40 
41 class FilterObject;
42 class TranslatorObject;
43 class SmallBuffer;
44 
46 enum class FederateType : int { GENERIC, VALUE, MESSAGE, COMBINATION, CALLBACK, INVALID };
47 
49 class BrokerObject {
50  public:
51  std::shared_ptr<Broker> brokerptr;
52  int index{-2};
53  int valid{0};
54 };
55 
59 class CoreObject {
60  public:
61  std::shared_ptr<Core> coreptr;
62  std::vector<std::unique_ptr<FilterObject>> filters;
63  std::vector<std::unique_ptr<TranslatorObject>> translators;
64  int index{0};
65  int valid{-2};
66  CoreObject() = default;
67  ~CoreObject();
68 };
69 
71 CoreObject* getCoreObject(HelicsCore core, HelicsError* err) noexcept;
72 
73 class InputObject;
74 class PublicationObject;
75 class EndpointObject;
78  private:
79  std::vector<std::unique_ptr<Message>> messages;
80  std::vector<int> freeMessageSlots;
81 
82  public:
83  Message* addMessage(std::unique_ptr<Message>& mess);
84  Message* newMessage();
85  std::unique_ptr<Message> extractMessage(int index);
86  void freeMessage(int index);
87  void clear();
88 };
90 class FedObject {
91  public:
92  FederateType type = FederateType::INVALID;
93  int index{-2};
94  int valid{0};
95  std::shared_ptr<Federate> fedptr;
96  MessageHolder messages;
97  std::vector<std::unique_ptr<InputObject>> inputs;
98  std::vector<std::unique_ptr<PublicationObject>> pubs;
99  std::vector<std::unique_ptr<EndpointObject>> epts;
100  std::vector<std::unique_ptr<FilterObject>> filters;
101  std::vector<std::unique_ptr<TranslatorObject>> translators;
102  std::pair<std::string, std::string> commandBuffer;
103  FedObject() = default;
104  ~FedObject();
105 };
106 
108 FedObject* getFedObject(HelicsFederate fed, HelicsError* err) noexcept;
109 
111 class InputObject {
112  public:
113  int valid{0};
114  std::shared_ptr<ValueFederate> fedptr;
115  Input* inputPtr{nullptr};
116 };
117 
120  public:
121  int valid{0};
122  std::shared_ptr<ValueFederate> fedptr;
123  Publication* pubPtr = nullptr;
124 };
127  public:
128  Endpoint* endPtr{nullptr};
129  FedObject* fed{nullptr};
130  std::shared_ptr<MessageFederate> fedptr;
131  int valid{0};
132 };
133 
136  public:
137  bool cloning{false};
138  bool custom{false};
139  int valid{0};
140  Filter* filtPtr{nullptr};
141  std::unique_ptr<Filter> uFilter;
142  std::shared_ptr<Federate> fedptr;
143  std::shared_ptr<Core> corePtr;
144 };
145 
148  public:
149  bool custom{false};
150  int valid{0};
151  Translator* transPtr{nullptr};
152  std::unique_ptr<Translator> mTrans;
153  std::shared_ptr<Federate> fedptr;
154  std::shared_ptr<Core> corePtr;
155 };
156 
158 class QueryObject {
159  public:
160  std::string target;
161  std::string query;
162  std::string response;
163  std::shared_ptr<Federate> activeFed;
164  bool activeAsync{false};
167  int valid{0};
168 };
169 
170 } // namespace helics
171 
173 #define HELICS_ERROR_CHECK(err, retval) \
174  do { \
175  if (((err) != nullptr) && ((err)->error_code != 0)) { \
176  return (retval); \
177  } \
178  } while (false)
179 
181 inline void assignError(HelicsError* err, int error_code, const char* string)
182 {
183  if (err != nullptr) {
184  err->error_code = error_code;
185  err->message = string;
186  }
187 }
188 
189 extern const std::string gHelicsEmptyStr;
190 constexpr char gHelicsNullStringArgument[] = "The supplied string argument is null and therefore invalid";
191 #define AS_STRING(str) ((str) != nullptr) ? std::string(str) : gHelicsEmptyStr
192 
193 #define AS_STRING_VIEW(str) ((str) != nullptr) ? std::string_view(str) : std::string_view(gHelicsEmptyStr)
194 
195 #define CHECK_NULL_STRING(str, retval) \
196  do { \
197  if ((str) == nullptr) { \
198  assignError(err, HELICS_ERROR_INVALID_ARGUMENT, gHelicsNullStringArgument); \
199  return (retval); \
200  } \
201  } while (false)
202 
203 helics::Federate* getFed(HelicsFederate fed, HelicsError* err);
204 helics::ValueFederate* getValueFed(HelicsFederate fed, HelicsError* err);
205 helics::MessageFederate* getMessageFed(HelicsFederate fed, HelicsError* err);
206 helics::CallbackFederate* getCallbackFed(HelicsFederate fed, HelicsError* err);
207 helics::Core* getCore(HelicsCore core, HelicsError* err);
208 helics::Broker* getBroker(HelicsBroker broker, HelicsError* err);
209 helics::Message* getMessageObj(HelicsMessage message, HelicsError* err);
210 
211 std::unique_ptr<helics::Message> getMessageUniquePtr(HelicsMessage message, HelicsError* err);
213 HelicsMessage createAPIMessage(std::unique_ptr<helics::Message>& mess);
214 
216 HelicsDataBuffer createAPIDataBuffer(helics::SmallBuffer& buff);
218 helics::SmallBuffer* getBuffer(HelicsDataBuffer data);
219 
220 std::shared_ptr<helics::Federate> getFedSharedPtr(HelicsFederate fed, HelicsError* err);
221 std::shared_ptr<helics::ValueFederate> getValueFedSharedPtr(HelicsFederate fed, HelicsError* err);
222 std::shared_ptr<helics::MessageFederate> getMessageFedSharedPtr(HelicsFederate fed, HelicsError* err);
223 std::shared_ptr<helics::CallbackFederate> getCallbackFedSharedPtr(HelicsFederate fed, HelicsError* err);
224 std::shared_ptr<helics::Core> getCoreSharedPtr(HelicsCore core, HelicsError* err);
226 void helicsErrorHandler(HelicsError* err) noexcept;
233 bool checkOutputArgString(const char* outputString, int maxlen, HelicsError* err);
234 
237  private:
238  guarded<std::deque<std::unique_ptr<helics::BrokerObject>>> brokers;
239  guarded<std::deque<std::unique_ptr<helics::CoreObject>>> cores;
240  guarded<std::deque<std::unique_ptr<helics::FedObject>>> feds;
241  gmlc::concurrency::TripWireDetector tripDetect;
242  guarded<std::deque<std::string>> errorStrings;
243  public:
244  MasterObjectHolder() noexcept;
246  helics::FedObject* findFed(std::string_view fedName);
248  helics::FedObject* findFed(std::string_view fedName, int validationCode);
250  int addBroker(std::unique_ptr<helics::BrokerObject> broker);
252  int addCore(std::unique_ptr<helics::CoreObject> core);
254  int addFed(std::unique_ptr<helics::FedObject> fed);
256  bool removeFed(std::string_view name, int validationCode);
257  void clearBroker(int index);
258  void clearCore(int index);
259  void clearFed(int index);
260  void deleteAll();
261  void abortAll(int errorCode, std::string_view error);
264  const char* addErrorString(std::string_view newError);
265 };
266 
267 std::shared_ptr<MasterObjectHolder> getMasterHolder();
268 void clearAllObjects();
void * HelicsDataBuffer
Definition: api-data.h:82
void * HelicsCore
Definition: api-data.h:54
void * HelicsMessage
Definition: api-data.h:94
void * HelicsFederate
Definition: api-data.h:65
void * HelicsBroker
Definition: api-data.h:60
Definition: api_objects.h:236
const char * addErrorString(std::string_view newError)
Definition: helicsExport.cpp:1503
bool removeFed(std::string_view name, int validationCode)
Definition: helicsExport.cpp:1391
int addBroker(std::unique_ptr< helics::BrokerObject > broker)
Definition: helicsExport.cpp:1337
int addFed(std::unique_ptr< helics::FedObject > fed)
Definition: helicsExport.cpp:1355
int addCore(std::unique_ptr< helics::CoreObject > core)
Definition: helicsExport.cpp:1346
Definition: api_objects.h:49
Definition: core/Broker.hpp:18
Definition: CallbackFederate.hpp:19
Definition: api_objects.h:59
std::vector< std::unique_ptr< FilterObject > > filters
list of filters created directly through the core
Definition: api_objects.h:62
std::vector< std::unique_ptr< TranslatorObject > > translators
list of filters created directly through the core
Definition: api_objects.h:63
Definition: core/Core.hpp:41
Definition: api_objects.h:126
Definition: Endpoints.hpp:21
Definition: api_objects.h:90
Definition: application_api/Federate.hpp:48
Definition: api_objects.h:135
bool custom
indicator that the filter is a custom filter and requires a callback
Definition: api_objects.h:138
bool cloning
indicator that the filter is a cloning filter
Definition: api_objects.h:137
Definition: api_objects.h:111
Definition: Inputs.hpp:38
Definition: application_api/MessageFederate.hpp:24
Definition: api_objects.h:77
Definition: core-data.hpp:29
Definition: api_objects.h:119
Definition: Publications.hpp:25
Definition: api_objects.h:158
HelicsSequencingModes mode
the ordering mode used for the query
Definition: api_objects.h:165
std::string query
the actual query itself
Definition: api_objects.h:161
std::string target
the target of the query
Definition: api_objects.h:160
QueryId asyncIndexCode
the index to use for the queryComplete call
Definition: api_objects.h:166
std::string response
the response to the query
Definition: api_objects.h:162
std::shared_ptr< Federate > activeFed
pointer to the fed with the active Query
Definition: api_objects.h:163
Definition: SmallBuffer.hpp:25
Definition: api_objects.h:147
bool custom
indicator that the translator is a custom translator and requires callbacks
Definition: api_objects.h:149
Definition: application_api/ValueFederate.hpp:28
HelicsSequencingModes
Definition: helics_enums.h:425
@ HELICS_SEQUENCING_MODE_FAST
Definition: helics_enums.h:427
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
BrokerObject * getBrokerObject(HelicsBroker broker, HelicsError *err) noexcept
Definition: helicsExport.cpp:316
FederateType
Definition: api_objects.h:46
FedObject * getFedObject(HelicsFederate fed, HelicsError *err) noexcept
Definition: FederateExport.cpp:29
CoreObject * getCoreObject(HelicsCore core, HelicsError *err) noexcept
Definition: helicsExport.cpp:299
Definition: api-data.h:171
const char * message
Definition: api-data.h:173
int32_t error_code
Definition: api-data.h:172