helics  3.3.0
api_objects.h
1 /*
2 Copyright (c) 2017-2022,
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 Input;
35 class Publication;
36 class Endpoint;
37 class Filter;
38 class Translator;
39 
40 class FilterObject;
41 class TranslatorObject;
42 class SmallBuffer;
43 
45 enum class FederateType : int { GENERIC, VALUE, MESSAGE, COMBINATION, INVALID };
46 
48 class BrokerObject {
49  public:
50  std::shared_ptr<Broker> brokerptr;
51  int index{-2};
52  int valid{0};
53 };
54 
58 class CoreObject {
59  public:
60  std::shared_ptr<Core> coreptr;
61  std::vector<std::unique_ptr<FilterObject>> filters;
62  std::vector<std::unique_ptr<TranslatorObject>> translators;
63  int index{0};
64  int valid{-2};
65  CoreObject() = default;
66  ~CoreObject();
67 };
68 
70 CoreObject* getCoreObject(HelicsCore core, HelicsError* err) noexcept;
71 
72 class InputObject;
73 class PublicationObject;
74 class EndpointObject;
77  private:
78  std::vector<std::unique_ptr<Message>> messages;
79  std::vector<int> freeMessageSlots;
80 
81  public:
82  Message* addMessage(std::unique_ptr<Message>& mess);
83  Message* newMessage();
84  std::unique_ptr<Message> extractMessage(int index);
85  void freeMessage(int index);
86  void clear();
87 };
89 class FedObject {
90  public:
91  FederateType type = FederateType::INVALID;
92  int index{-2};
93  int valid{0};
94  std::shared_ptr<Federate> fedptr;
95  MessageHolder messages;
96  std::vector<std::unique_ptr<InputObject>> inputs;
97  std::vector<std::unique_ptr<PublicationObject>> pubs;
98  std::vector<std::unique_ptr<EndpointObject>> epts;
99  std::vector<std::unique_ptr<FilterObject>> filters;
100  std::vector<std::unique_ptr<TranslatorObject>> translators;
101  std::pair<std::string, std::string> commandBuffer;
102  FedObject() = default;
103  ~FedObject();
104 };
105 
107 FedObject* getFedObject(HelicsFederate fed, HelicsError* err) noexcept;
108 
110 class InputObject {
111  public:
112  int valid{0};
113  std::shared_ptr<ValueFederate> fedptr;
114  Input* inputPtr{nullptr};
115 };
116 
119  public:
120  int valid{0};
121  std::shared_ptr<ValueFederate> fedptr;
122  Publication* pubPtr = nullptr;
123 };
126  public:
127  Endpoint* endPtr{nullptr};
128  FedObject* fed{nullptr};
129  std::shared_ptr<MessageFederate> fedptr;
130  int valid{0};
131 };
132 
135  public:
136  bool cloning{false};
137  bool custom{false};
138  int valid{0};
139  Filter* filtPtr{nullptr};
140  std::unique_ptr<Filter> uFilter;
141  std::shared_ptr<Federate> fedptr;
142  std::shared_ptr<Core> corePtr;
143 };
144 
147  public:
148  bool custom{false};
149  int valid{0};
150  Translator* transPtr{nullptr};
151  std::unique_ptr<Translator> mTrans;
152  std::shared_ptr<Federate> fedptr;
153  std::shared_ptr<Core> corePtr;
154 };
155 
157 class QueryObject {
158  public:
159  std::string target;
160  std::string query;
161  std::string response;
162  std::shared_ptr<Federate> activeFed;
163  bool activeAsync{false};
166  int valid{0};
167 };
168 
169 } // namespace helics
170 
172 #define HELICS_ERROR_CHECK(err, retval) \
173  do { \
174  if (((err) != nullptr) && ((err)->error_code != 0)) { \
175  return (retval); \
176  } \
177  } while (false)
178 
180 inline void assignError(HelicsError* err, int error_code, const char* string)
181 {
182  if (err != nullptr) {
183  err->error_code = error_code;
184  err->message = string;
185  }
186 }
187 
188 extern const std::string gHelicsEmptyStr;
189 constexpr char gHelicsNullStringArgument[] = "The supplied string argument is null and therefore invalid";
190 #define AS_STRING(str) ((str) != nullptr) ? std::string(str) : gHelicsEmptyStr
191 
192 #define AS_STRING_VIEW(str) ((str) != nullptr) ? std::string_view(str) : std::string_view(gHelicsEmptyStr)
193 
194 #define CHECK_NULL_STRING(str, retval) \
195  do { \
196  if ((str) == nullptr) { \
197  assignError(err, HELICS_ERROR_INVALID_ARGUMENT, gHelicsNullStringArgument); \
198  return (retval); \
199  } \
200  } while (false)
201 
202 helics::Federate* getFed(HelicsFederate fed, HelicsError* err);
203 helics::ValueFederate* getValueFed(HelicsFederate fed, HelicsError* err);
204 helics::MessageFederate* getMessageFed(HelicsFederate fed, HelicsError* err);
205 helics::Core* getCore(HelicsCore core, HelicsError* err);
206 helics::Broker* getBroker(HelicsBroker broker, HelicsError* err);
207 helics::Message* getMessageObj(HelicsMessage message, HelicsError* err);
208 
209 std::unique_ptr<helics::Message> getMessageUniquePtr(HelicsMessage message, HelicsError* err);
211 HelicsMessage createAPIMessage(std::unique_ptr<helics::Message>& mess);
212 
213 HelicsDataBuffer createAPIDataBuffer(helics::SmallBuffer& buff);
214 
215 std::shared_ptr<helics::Federate> getFedSharedPtr(HelicsFederate fed, HelicsError* err);
216 std::shared_ptr<helics::ValueFederate> getValueFedSharedPtr(HelicsFederate fed, HelicsError* err);
217 std::shared_ptr<helics::MessageFederate> getMessageFedSharedPtr(HelicsFederate fed, HelicsError* err);
218 std::shared_ptr<helics::Core> getCoreSharedPtr(HelicsCore core, HelicsError* err);
220 void helicsErrorHandler(HelicsError* err) noexcept;
227 bool checkOutputArgString(const char* outputString, int maxlen, HelicsError* err);
228 
231  private:
232  guarded<std::deque<std::unique_ptr<helics::BrokerObject>>> brokers;
233  guarded<std::deque<std::unique_ptr<helics::CoreObject>>> cores;
234  guarded<std::deque<std::unique_ptr<helics::FedObject>>> feds;
235  gmlc::concurrency::TripWireDetector tripDetect;
236  guarded<std::deque<std::string>> errorStrings;
237  public:
238  MasterObjectHolder() noexcept;
240  helics::FedObject* findFed(std::string_view fedName);
242  helics::FedObject* findFed(std::string_view fedName, int validationCode);
244  int addBroker(std::unique_ptr<helics::BrokerObject> broker);
246  int addCore(std::unique_ptr<helics::CoreObject> core);
248  int addFed(std::unique_ptr<helics::FedObject> fed);
250  bool removeFed(std::string_view name, int validationCode);
251  void clearBroker(int index);
252  void clearCore(int index);
253  void clearFed(int index);
254  void deleteAll();
255  void abortAll(int errorCode, std::string_view error);
258  const char* addErrorString(std::string_view newError);
259 };
260 
261 std::shared_ptr<MasterObjectHolder> getMasterHolder();
262 void clearAllObjects();
helics::MessageHolder
Definition: api_objects.h:76
helics::Input
Definition: Inputs.hpp:38
helics::CoreObject::filters
std::vector< std::unique_ptr< FilterObject > > filters
list of filters created directly through the core
Definition: api_objects.h:61
helics::EndpointObject
Definition: api_objects.h:125
helics::QueryObject::target
std::string target
the target of the query
Definition: api_objects.h:159
helics::getFedObject
FedObject * getFedObject(HelicsFederate fed, HelicsError *err) noexcept
Definition: FederateExport.cpp:29
helics::IdentifierId< IdentifierType, Identifiers::QUERY, invalid_id_value >
helics::ValueFederate
Definition: application_api/ValueFederate.hpp:26
helics::SmallBuffer
Definition: SmallBuffer.hpp:24
HelicsError::message
const char * message
Definition: api-data.h:171
helics::QueryObject::asyncIndexCode
QueryId asyncIndexCode
the index to use for the queryComplete call
Definition: api_objects.h:165
HelicsFederate
void * HelicsFederate
Definition: api-data.h:65
helics::QueryObject
Definition: api_objects.h:157
helics::FilterObject::custom
bool custom
indicator that the filter is a custom filter and requires a callback
Definition: api_objects.h:137
helics::Endpoint
Definition: Endpoints.hpp:21
helics::InputObject
Definition: api_objects.h:110
helics::MessageFederate
Definition: application_api/MessageFederate.hpp:22
helics::TranslatorObject::custom
bool custom
indicator that the translator is a custom translator and requires callbacks
Definition: api_objects.h:148
helics::TranslatorObject
Definition: api_objects.h:146
helics::Core
Definition: core/Core.hpp:41
helics::getBrokerObject
BrokerObject * getBrokerObject(HelicsBroker broker, HelicsError *err) noexcept
Definition: helicsExport.cpp:316
HelicsBroker
void * HelicsBroker
Definition: api-data.h:60
MasterObjectHolder::addBroker
int addBroker(std::unique_ptr< helics::BrokerObject > broker)
Definition: helicsExport.cpp:1229
helics::QueryObject::query
std::string query
the actual query itself
Definition: api_objects.h:160
helics::FedObject
Definition: api_objects.h:89
helics::BrokerObject
Definition: api_objects.h:48
helics::getCoreObject
CoreObject * getCoreObject(HelicsCore core, HelicsError *err) noexcept
Definition: helicsExport.cpp:299
MasterObjectHolder::addErrorString
const char * addErrorString(std::string_view newError)
Definition: helicsExport.cpp:1395
HelicsSequencingModes
HelicsSequencingModes
Definition: helics_enums.h:395
MasterObjectHolder
Definition: api_objects.h:230
helics::CoreObject
Definition: api_objects.h:58
HelicsMessage
void * HelicsMessage
Definition: api-data.h:94
helics::Publication
Definition: Publications.hpp:25
HelicsError
Definition: api-data.h:169
helics
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
MasterObjectHolder::removeFed
bool removeFed(std::string_view name, int validationCode)
Definition: helicsExport.cpp:1283
helics::QueryObject::mode
HelicsSequencingModes mode
the ordering mode used for the query
Definition: api_objects.h:164
helics::FilterObject::cloning
bool cloning
indicator that the filter is a cloning filter
Definition: api_objects.h:136
HELICS_SEQUENCING_MODE_FAST
@ HELICS_SEQUENCING_MODE_FAST
Definition: helics_enums.h:397
HelicsCore
void * HelicsCore
Definition: api-data.h:54
helics::FilterObject
Definition: api_objects.h:134
helics::QueryObject::activeFed
std::shared_ptr< Federate > activeFed
pointer to the fed with the active Query
Definition: api_objects.h:162
helics::Federate
Definition: application_api/Federate.hpp:48
helics::CoreObject::translators
std::vector< std::unique_ptr< TranslatorObject > > translators
list of filters created directly through the core
Definition: api_objects.h:62
helics::Broker
Definition: core/Broker.hpp:18
MasterObjectHolder::addCore
int addCore(std::unique_ptr< helics::CoreObject > core)
Definition: helicsExport.cpp:1238
helics::QueryObject::response
std::string response
the response to the query
Definition: api_objects.h:161
helics::PublicationObject
Definition: api_objects.h:118
helics::FederateType
FederateType
Definition: api_objects.h:45
HelicsError::error_code
int32_t error_code
Definition: api-data.h:170
HelicsDataBuffer
void * HelicsDataBuffer
Definition: api-data.h:82
helics::Message
Definition: core-data.hpp:29
MasterObjectHolder::addFed
int addFed(std::unique_ptr< helics::FedObject > fed)
Definition: helicsExport.cpp:1247