helics  2.8.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 <vector>
20 
22 static const int coreValidationIdentifier = 0x378424EC;
23 static const int brokerValidationIdentifier = 0xA3467D20;
24 
25 namespace helics {
26 class Core;
27 class Federate;
28 class Broker;
29 class ValueFederate;
30 class MessageFederate;
31 class Input;
32 class Publication;
33 class Endpoint;
34 class Filter;
35 
36 class FilterObject;
37 
39 enum class vtype : int { generic_fed, value_fed, message_fed, combination_fed, invalid_fed };
40 
42 class BrokerObject {
43  public:
44  std::shared_ptr<Broker> brokerptr;
45  int index{-2};
46  int valid{0};
47 };
48 
52 class CoreObject {
53  public:
54  std::shared_ptr<Core> coreptr;
55  std::vector<std::unique_ptr<FilterObject>> filters;
56  int index{0};
57  int valid{-2};
58  CoreObject() = default;
59  ~CoreObject();
60 };
61 
63 CoreObject* getCoreObject(helics_core core, helics_error* err) noexcept;
64 
65 class InputObject;
66 class PublicationObject;
67 class EndpointObject;
70  private:
71  std::vector<std::unique_ptr<Message>> messages;
72  std::vector<int> freeMessageSlots;
73 
74  public:
75  Message* addMessage(std::unique_ptr<Message>& mess);
76  Message* newMessage();
77  std::unique_ptr<Message> extractMessage(int index);
78  void freeMessage(int index);
79  void clear();
80 };
82 class FedObject {
83  public:
84  vtype type = vtype::invalid_fed;
85  int index{-2};
86  int valid{0};
87  std::shared_ptr<Federate> fedptr;
88  MessageHolder messages;
89  std::vector<std::unique_ptr<InputObject>> inputs;
90  std::vector<std::unique_ptr<PublicationObject>> pubs;
91  std::vector<std::unique_ptr<EndpointObject>> epts;
92  std::vector<std::unique_ptr<FilterObject>> filters;
93  FedObject() = default;
94  ~FedObject();
95 };
96 
99 
101 class InputObject {
102  public:
103  int valid{0};
104  std::shared_ptr<ValueFederate> fedptr;
105  Input* inputPtr{nullptr};
106 };
107 
110  public:
111  int valid{0};
112  std::shared_ptr<ValueFederate> fedptr;
113  Publication* pubPtr = nullptr;
114 };
117  public:
118  Endpoint* endPtr{nullptr};
119  FedObject* fed{nullptr};
120  std::shared_ptr<MessageFederate> fedptr;
121  int valid{0};
122 };
123 
126  public:
127  bool cloning{false};
128  bool custom{false};
129  int valid{0};
130  Filter* filtPtr{nullptr};
131  std::unique_ptr<Filter> uFilter;
132  std::shared_ptr<Federate> fedptr;
133  std::shared_ptr<Core> corePtr;
134 };
135 
137 class QueryObject {
138  public:
139  std::string target;
140  std::string query;
141  std::string response;
142  std::shared_ptr<Federate> activeFed;
143  bool activeAsync{false};
146  int valid{0};
147 };
148 
149 } // namespace helics
150 
152 #define HELICS_ERROR_CHECK(err, retval) \
153  do { \
154  if (((err) != nullptr) && ((err)->error_code != 0)) { \
155  return (retval); \
156  } \
157  } while (false)
158 
160 inline void assignError(helics_error* err, int errorCode, const char* string)
161 {
162  if (err != nullptr) {
163  err->error_code = errorCode;
164  err->message = string;
165  }
166 }
167 
168 extern const std::string emptyStr;
169 extern const std::string nullStringArgument;
170 #define AS_STRING(str) ((str) != nullptr) ? std::string(str) : emptyStr
171 
172 #define CHECK_NULL_STRING(str, retval) \
173  do { \
174  if ((str) == nullptr) { \
175  assignError(err, helics_error_invalid_argument, nullStringArgument.c_str()); \
176  return (retval); \
177  } \
178  } while (false)
179 
183 helics::Core* getCore(helics_core core, helics_error* err);
184 helics::Broker* getBroker(helics_broker broker, helics_error* err);
185 helics::Message* getMessageObj(helics_message_object message, helics_error* err);
187 helics_message_object createMessageObject(std::unique_ptr<helics::Message>& mess);
188 
189 std::shared_ptr<helics::Federate> getFedSharedPtr(helics_federate fed, helics_error* err);
190 std::shared_ptr<helics::ValueFederate> getValueFedSharedPtr(helics_federate fed, helics_error* err);
191 std::shared_ptr<helics::MessageFederate> getMessageFedSharedPtr(helics_federate fed, helics_error* err);
192 std::shared_ptr<helics::Core> getCoreSharedPtr(helics_core core, helics_error* err);
194 void helicsErrorHandler(helics_error* err) noexcept;
201 bool checkOutArgString(const char* outputString, int maxlen, helics_error* err);
202 
205  private:
206  guarded<std::deque<std::unique_ptr<helics::BrokerObject>>> brokers;
207  guarded<std::deque<std::unique_ptr<helics::CoreObject>>> cores;
208  guarded<std::deque<std::unique_ptr<helics::FedObject>>> feds;
209  gmlc::concurrency::TripWireDetector tripDetect;
210  guarded<std::deque<std::string>> errorStrings;
211  public:
212  MasterObjectHolder() noexcept;
214  helics::FedObject* findFed(const std::string& fedName);
216  int addBroker(std::unique_ptr<helics::BrokerObject> broker);
218  int addCore(std::unique_ptr<helics::CoreObject> core);
220  int addFed(std::unique_ptr<helics::FedObject> fed);
221  void clearBroker(int index);
222  void clearCore(int index);
223  void clearFed(int index);
224  void deleteAll();
225  void abortAll(int errorCode, const std::string& error);
228  const char* addErrorString(std::string newError);
229 };
230 
231 std::shared_ptr<MasterObjectHolder> getMasterHolder();
232 void clearAllObjects();
helics::MessageHolder
Definition: api_objects.h:69
helics::Input
Definition: Inputs.hpp:37
helics::CoreObject::filters
std::vector< std::unique_ptr< FilterObject > > filters
list of filters created directly through the core
Definition: api_objects.h:55
helics::EndpointObject
Definition: api_objects.h:116
helics::QueryObject::target
std::string target
the target of the query
Definition: api_objects.h:139
helics::ValueFederate
Definition: application_api/ValueFederate.hpp:25
helics_message_object
void * helics_message_object
Definition: api-data.h:76
helics::QueryObject
Definition: api_objects.h:137
helics_core
void * helics_core
Definition: api-data.h:46
helics::vtype
vtype
Definition: api_objects.h:39
helics::FilterObject::custom
bool custom
indicator that the filter is a custom filter and requires a callback
Definition: api_objects.h:128
helics::Endpoint
Definition: Endpoints.hpp:18
helics::QueryObject::asyncIndexCode
query_id_t asyncIndexCode
the index to use for the queryComplete call
Definition: api_objects.h:145
helics::InputObject
Definition: api_objects.h:101
helics::MessageFederate
Definition: application_api/MessageFederate.hpp:20
helics_sequencing_mode
helics_sequencing_mode
Definition: helics_enums.h:333
helics::Core
Definition: core/Core.hpp:42
helics::identifier_id_t< identifier_type, identifiers::query, invalid_id_value >
MasterObjectHolder::addBroker
int addBroker(std::unique_ptr< helics::BrokerObject > broker)
Definition: helicsExport.cpp:1093
helics::QueryObject::query
std::string query
the actual query itself
Definition: api_objects.h:140
helics::FedObject
Definition: api_objects.h:82
helics::BrokerObject
Definition: api_objects.h:42
fed
@ fed
special logging command for message coming from a fed
Definition: loggingHelper.hpp:32
MasterObjectHolder::addErrorString
const char * addErrorString(std::string newError)
Definition: helicsExport.cpp:1228
MasterObjectHolder
Definition: api_objects.h:204
helics::CoreObject
Definition: api_objects.h:52
helics_error
Definition: api-data.h:166
helics::Publication
Definition: Publications.hpp:23
helics::getFedObject
FedObject * getFedObject(helics_federate fed, helics_error *err) noexcept
Definition: FederateExport.cpp:27
helics
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
helics::getCoreObject
CoreObject * getCoreObject(helics_core core, helics_error *err) noexcept
Definition: helicsExport.cpp:204
error
@ error
only print errors
Definition: loggingHelper.hpp:22
helics::FilterObject::cloning
bool cloning
indicator that the filter is a cloning filter
Definition: api_objects.h:127
helics::FilterObject
Definition: api_objects.h:125
helics::QueryObject::activeFed
std::shared_ptr< Federate > activeFed
pointer to the fed with the active Query
Definition: api_objects.h:142
helics_error::error_code
int32_t error_code
Definition: api-data.h:167
helics::Federate
Definition: application_api/Federate.hpp:44
helics_broker
void * helics_broker
Definition: api-data.h:51
helics_error::message
const char * message
Definition: api-data.h:168
helics::Broker
Definition: core/Broker.hpp:18
MasterObjectHolder::addCore
int addCore(std::unique_ptr< helics::CoreObject > core)
Definition: helicsExport.cpp:1102
helics_sequencing_mode_fast
@ helics_sequencing_mode_fast
Definition: helics_enums.h:335
helics::QueryObject::response
std::string response
the response to the query
Definition: api_objects.h:141
helics::PublicationObject
Definition: api_objects.h:109
helics::getBrokerObject
BrokerObject * getBrokerObject(helics_broker broker, helics_error *err) noexcept
Definition: helicsExport.cpp:221
helics::QueryObject::mode
helics_sequencing_mode mode
the ordering mode used for the query
Definition: api_objects.h:144
helics::Message
Definition: core-data.hpp:146
helics_federate
void * helics_federate
Definition: api-data.h:56
MasterObjectHolder::addFed
int addFed(std::unique_ptr< helics::FedObject > fed)
Definition: helicsExport.cpp:1111