helics  3.0.1
api_objects.h
1 /*
2 Copyright (c) 2017-2021,
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 <memory>
17 #include <mutex>
18 #include <string>
19 #include <utility>
20 #include <vector>
21 
23 static const int gCoreValidationIdentifier = 0x378424EC;
24 static const int gBrokerValidationIdentifier = 0xA3467D20;
25 
26 namespace helics {
27 class Core;
28 class Federate;
29 class Broker;
30 class ValueFederate;
31 class MessageFederate;
32 class Input;
33 class Publication;
34 class Endpoint;
35 class Filter;
36 
37 class FilterObject;
38 
40 enum class FederateType : int { GENERIC, VALUE, MESSAGE, COMBINATION, INVALID };
41 
43 class BrokerObject {
44  public:
45  std::shared_ptr<Broker> brokerptr;
46  int index{-2};
47  int valid{0};
48 };
49 
53 class CoreObject {
54  public:
55  std::shared_ptr<Core> coreptr;
56  std::vector<std::unique_ptr<FilterObject>> filters;
57  int index{0};
58  int valid{-2};
59  CoreObject() = default;
60  ~CoreObject();
61 };
62 
64 CoreObject* getCoreObject(HelicsCore core, HelicsError* err) noexcept;
65 
66 class InputObject;
67 class PublicationObject;
68 class EndpointObject;
71  private:
72  std::vector<std::unique_ptr<Message>> messages;
73  std::vector<int> freeMessageSlots;
74 
75  public:
76  Message* addMessage(std::unique_ptr<Message>& mess);
77  Message* newMessage();
78  std::unique_ptr<Message> extractMessage(int index);
79  void freeMessage(int index);
80  void clear();
81 };
83 class FedObject {
84  public:
85  FederateType type = FederateType::INVALID;
86  int index{-2};
87  int valid{0};
88  std::shared_ptr<Federate> fedptr;
89  MessageHolder messages;
90  std::vector<std::unique_ptr<InputObject>> inputs;
91  std::vector<std::unique_ptr<PublicationObject>> pubs;
92  std::vector<std::unique_ptr<EndpointObject>> epts;
93  std::vector<std::unique_ptr<FilterObject>> filters;
94  std::pair<std::string, std::string> commandBuffer;
95  FedObject() = default;
96  ~FedObject();
97 };
98 
100 FedObject* getFedObject(HelicsFederate fed, HelicsError* err) noexcept;
101 
103 class InputObject {
104  public:
105  int valid{0};
106  std::shared_ptr<ValueFederate> fedptr;
107  Input* inputPtr{nullptr};
108 };
109 
112  public:
113  int valid{0};
114  std::shared_ptr<ValueFederate> fedptr;
115  Publication* pubPtr = nullptr;
116 };
119  public:
120  Endpoint* endPtr{nullptr};
121  FedObject* fed{nullptr};
122  std::shared_ptr<MessageFederate> fedptr;
123  int valid{0};
124 };
125 
128  public:
129  bool cloning{false};
130  bool custom{false};
131  int valid{0};
132  Filter* filtPtr{nullptr};
133  std::unique_ptr<Filter> uFilter;
134  std::shared_ptr<Federate> fedptr;
135  std::shared_ptr<Core> corePtr;
136 };
137 
139 class QueryObject {
140  public:
141  std::string target;
142  std::string query;
143  std::string response;
144  std::shared_ptr<Federate> activeFed;
145  bool activeAsync{false};
148  int valid{0};
149 };
150 
151 } // namespace helics
152 
154 #define HELICS_ERROR_CHECK(err, retval) \
155  do { \
156  if (((err) != nullptr) && ((err)->error_code != 0)) { \
157  return (retval); \
158  } \
159  } while (false)
160 
162 inline void assignError(HelicsError* err, int error_code, const char* string)
163 {
164  if (err != nullptr) {
165  err->error_code = error_code;
166  err->message = string;
167  }
168 }
169 
170 extern const std::string gEmptyStr;
171 extern const std::string gNullStringArgument;
172 #define AS_STRING(str) ((str) != nullptr) ? std::string(str) : gEmptyStr
173 
174 #define AS_STRING_VIEW(str) ((str) != nullptr) ? std::string_view(str) : std::string_view(gEmptyStr)
175 
176 #define CHECK_NULL_STRING(str, retval) \
177  do { \
178  if ((str) == nullptr) { \
179  assignError(err, HELICS_ERROR_INVALID_ARGUMENT, gNullStringArgument.c_str()); \
180  return (retval); \
181  } \
182  } while (false)
183 
184 helics::Federate* getFed(HelicsFederate fed, HelicsError* err);
185 helics::ValueFederate* getValueFed(HelicsFederate fed, HelicsError* err);
186 helics::MessageFederate* getMessageFed(HelicsFederate fed, HelicsError* err);
187 helics::Core* getCore(HelicsCore core, HelicsError* err);
188 helics::Broker* getBroker(HelicsBroker broker, HelicsError* err);
189 helics::Message* getMessageObj(HelicsMessage message, HelicsError* err);
191 HelicsMessage createAPIMessage(std::unique_ptr<helics::Message>& mess);
192 
193 std::shared_ptr<helics::Federate> getFedSharedPtr(HelicsFederate fed, HelicsError* err);
194 std::shared_ptr<helics::ValueFederate> getValueFedSharedPtr(HelicsFederate fed, HelicsError* err);
195 std::shared_ptr<helics::MessageFederate> getMessageFedSharedPtr(HelicsFederate fed, HelicsError* err);
196 std::shared_ptr<helics::Core> getCoreSharedPtr(HelicsCore core, HelicsError* err);
198 void helicsErrorHandler(HelicsError* err) noexcept;
205 bool checkOutputArgString(const char* outputString, int maxlen, HelicsError* err);
206 
209  private:
210  guarded<std::deque<std::unique_ptr<helics::BrokerObject>>> brokers;
211  guarded<std::deque<std::unique_ptr<helics::CoreObject>>> cores;
212  guarded<std::deque<std::unique_ptr<helics::FedObject>>> feds;
213  gmlc::concurrency::TripWireDetector tripDetect;
214  guarded<std::deque<std::string>> errorStrings;
215  public:
216  MasterObjectHolder() noexcept;
218  helics::FedObject* findFed(const std::string& fedName);
220  int addBroker(std::unique_ptr<helics::BrokerObject> broker);
222  int addCore(std::unique_ptr<helics::CoreObject> core);
224  int addFed(std::unique_ptr<helics::FedObject> fed);
225  void clearBroker(int index);
226  void clearCore(int index);
227  void clearFed(int index);
228  void deleteAll();
229  void abortAll(int errorCode, const std::string& error);
232  const char* addErrorString(std::string newError);
233 };
234 
235 std::shared_ptr<MasterObjectHolder> getMasterHolder();
236 void clearAllObjects();
helics::MessageHolder
Definition: api_objects.h:70
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:56
helics::EndpointObject
Definition: api_objects.h:118
helics::QueryObject::target
std::string target
the target of the query
Definition: api_objects.h:141
helics::getFedObject
FedObject * getFedObject(HelicsFederate fed, HelicsError *err) noexcept
Definition: FederateExport.cpp:27
helics::IdentifierId< IdentifierType, Identifiers::QUERY, invalid_id_value >
helics::ValueFederate
Definition: application_api/ValueFederate.hpp:25
HelicsError::message
const char * message
Definition: api-data.h:162
helics::QueryObject::asyncIndexCode
QueryId asyncIndexCode
the index to use for the queryComplete call
Definition: api_objects.h:147
HelicsFederate
void * HelicsFederate
Definition: api-data.h:60
helics::QueryObject
Definition: api_objects.h:139
helics::FilterObject::custom
bool custom
indicator that the filter is a custom filter and requires a callback
Definition: api_objects.h:130
helics::Endpoint
Definition: Endpoints.hpp:21
helics::InputObject
Definition: api_objects.h:103
helics::MessageFederate
Definition: application_api/MessageFederate.hpp:22
helics::Core
Definition: core/Core.hpp:42
helics::getBrokerObject
BrokerObject * getBrokerObject(HelicsBroker broker, HelicsError *err) noexcept
Definition: helicsExport.cpp:221
HelicsBroker
void * HelicsBroker
Definition: api-data.h:55
MasterObjectHolder::addBroker
int addBroker(std::unique_ptr< helics::BrokerObject > broker)
Definition: helicsExport.cpp:1130
helics::QueryObject::query
std::string query
the actual query itself
Definition: api_objects.h:142
helics::FedObject
Definition: api_objects.h:83
helics::BrokerObject
Definition: api_objects.h:43
helics::getCoreObject
CoreObject * getCoreObject(HelicsCore core, HelicsError *err) noexcept
Definition: helicsExport.cpp:204
MasterObjectHolder::addErrorString
const char * addErrorString(std::string newError)
Definition: helicsExport.cpp:1265
HelicsSequencingModes
HelicsSequencingModes
Definition: helics_enums.h:363
MasterObjectHolder
Definition: api_objects.h:208
helics::CoreObject
Definition: api_objects.h:53
HelicsMessage
void * HelicsMessage
Definition: api-data.h:84
helics::Publication
Definition: Publications.hpp:24
HelicsError
Definition: api-data.h:160
helics
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
helics::QueryObject::mode
HelicsSequencingModes mode
the ordering mode used for the query
Definition: api_objects.h:146
helics::FilterObject::cloning
bool cloning
indicator that the filter is a cloning filter
Definition: api_objects.h:129
HELICS_SEQUENCING_MODE_FAST
@ HELICS_SEQUENCING_MODE_FAST
Definition: helics_enums.h:365
HelicsCore
void * HelicsCore
Definition: api-data.h:49
helics::FilterObject
Definition: api_objects.h:127
helics::QueryObject::activeFed
std::shared_ptr< Federate > activeFed
pointer to the fed with the active Query
Definition: api_objects.h:144
helics::Federate
Definition: application_api/Federate.hpp:47
helics::Broker
Definition: core/Broker.hpp:18
MasterObjectHolder::addCore
int addCore(std::unique_ptr< helics::CoreObject > core)
Definition: helicsExport.cpp:1139
helics::QueryObject::response
std::string response
the response to the query
Definition: api_objects.h:143
helics::PublicationObject
Definition: api_objects.h:111
helics::FederateType
FederateType
Definition: api_objects.h:40
HelicsError::error_code
int32_t error_code
Definition: api-data.h:161
helics::Message
Definition: core-data.hpp:29
MasterObjectHolder::addFed
int addFed(std::unique_ptr< helics::FedObject > fed)
Definition: helicsExport.cpp:1148