helics  3.3.0
application_api/CoreApp.hpp
1 /*
2 Copyright (c) 2017-2022,
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 #pragma once
8 
9 #include "../core/CoreTypes.hpp"
10 #include "helics_cxx_export.h"
11 
12 #include <chrono>
13 #include <memory>
14 #include <string>
15 #include <utility>
16 #include <vector>
17 
18 namespace helics {
19 class Core;
20 class helicsCLI11App;
21 
25 class HELICS_CXX_EXPORT CoreApp {
26  public:
28  CoreApp() = default;
32  explicit CoreApp(std::vector<std::string> args);
37  CoreApp(CoreType ctype, std::vector<std::string> args);
38 
44  CoreApp(CoreType ctype, std::string_view coreName, std::vector<std::string> args);
49  CoreApp(int argc, char* argv[]);
56  CoreApp(CoreType ctype, std::string_view coreName, int argc, char* argv[]);
62  CoreApp(CoreType ctype, int argc, char* argv[]);
66  explicit CoreApp(std::string_view argString);
71  CoreApp(CoreType ctype, std::string_view argString);
77  CoreApp(CoreType ctype, std::string_view coreName, std::string_view argString);
78 
80  explicit CoreApp(std::shared_ptr<Core> cr);
82  bool isConnected() const;
83 
85  bool connect();
86 
89  bool isOpenToNewFederates() const;
91  void forceTerminate();
93  bool waitForDisconnect(std::chrono::milliseconds waitTime = std::chrono::milliseconds(0));
95  void linkEndpoints(std::string_view source, std::string_view target);
97  void dataLink(std::string_view source, std::string_view target);
99  void addSourceFilterToEndpoint(std::string_view filter, std::string_view endpoint);
101  void addDestinationFilterToEndpoint(std::string_view filter, std::string_view endpoint);
103  void addAlias(std::string_view interfaceName, std::string_view alias);
105  void makeConnections(const std::string& file);
107  const std::string& getIdentifier() const;
109  const std::string& getAddress() const;
110 
119  std::string query(std::string_view target,
120  std::string_view queryStr,
122 
128  void setTag(std::string_view tag, std::string_view value);
135  const std::string& getTag(std::string_view tag) const;
136 
144  void setGlobal(std::string_view valueName, std::string_view value);
145 
154  void sendCommand(std::string_view target,
155  std::string_view commandStr,
157 
159  void setLogFile(std::string_view logFile);
161  void setLoggingLevel(int loglevel);
163  void setReadyToInit();
165  void haltInit();
166 #ifdef HELICS_CXX_STATIC_DEFINE
167 
169  auto* operator->() const
170  {
171  return core.operator->();
172  }
173 #else
174  CoreApp* operator->()
175  {
176  return this;
177  }
178  const CoreApp* operator->() const
179  {
180  return this;
181  }
182 #endif
183 
184  std::shared_ptr<Core> getCopyofCorePointer() const
185  {
186  return core;
187  }
188 
190  void reset();
192  void globalError(int32_t errorCode, std::string_view errorString);
193 
194  private:
195  void processArgs(std::unique_ptr<helicsCLI11App>& app);
196  std::unique_ptr<helicsCLI11App> generateParser();
197 
198  std::shared_ptr<Core> core;
199  std::string name;
200 };
201 
203 class CoreKeeper {
204  public:
205  template<class... Args>
206  explicit CoreKeeper(Args&&... args): cr(std::forward<Args...>(args...))
207  {
208  }
209  CoreKeeper(CoreKeeper&& brkeep) = default;
210  CoreKeeper(const CoreKeeper& crkeep) = default;
211  CoreKeeper& operator=(CoreKeeper&& crkeep) = default;
212  CoreKeeper& operator=(const CoreKeeper& crkeep) = default;
214  bool isConnected() { return cr.isConnected(); }
219  {
220  if (cr.isConnected()) {
221  cr.waitForDisconnect();
222  }
223  }
224 
225  private:
226  CoreApp cr;
227 };
228 } // namespace helics
helics::CoreApp::forceTerminate
void forceTerminate()
Definition: CoreApp.cpp:150
helics::CoreApp::waitForDisconnect
bool waitForDisconnect(std::chrono::milliseconds waitTime=std::chrono::milliseconds(0))
Definition: CoreApp.cpp:160
helics::CoreApp::setGlobal
void setGlobal(std::string_view valueName, std::string_view value)
Definition: CoreApp.cpp:248
helics::CoreFactory::findCore
std::shared_ptr< Core > findCore(std::string_view name)
Definition: CoreFactory.cpp:304
helics::CoreApp::linkEndpoints
void linkEndpoints(std::string_view source, std::string_view target)
Definition: CoreApp.cpp:168
helics::CoreApp::connect
bool connect()
Definition: CoreApp.cpp:140
helics::CoreType
CoreType
Definition: CoreTypes.hpp:36
helics::CoreApp::isOpenToNewFederates
bool isOpenToNewFederates() const
Definition: CoreApp.cpp:145
helics::CoreApp::reset
void reset()
Definition: CoreApp.cpp:292
helics::CoreApp::setLoggingLevel
void setLoggingLevel(int loglevel)
Definition: CoreApp.cpp:264
helics::CoreApp::getTag
const std::string & getTag(std::string_view tag) const
Definition: CoreApp.cpp:240
helics::CoreApp::dataLink
void dataLink(std::string_view source, std::string_view target)
Definition: CoreApp.cpp:175
helics::CoreApp::isConnected
bool isConnected() const
Definition: CoreApp.cpp:135
helics::CoreKeeper::~CoreKeeper
~CoreKeeper()
the destructor waits for the core to terminate
Definition: application_api/CoreApp.hpp:218
helics::CoreApp::globalError
void globalError(int32_t errorCode, std::string_view errorString)
Definition: CoreApp.cpp:298
helics::CoreType::DEFAULT
@ DEFAULT
ZMQ if available or UDP.
HELICS_FLAG_DELAY_INIT_ENTRY
@ HELICS_FLAG_DELAY_INIT_ENTRY
Definition: helics_enums.h:150
helics::CoreApp::addSourceFilterToEndpoint
void addSourceFilterToEndpoint(std::string_view filter, std::string_view endpoint)
Definition: CoreApp.cpp:182
helics::CoreApp::getAddress
const std::string & getAddress() const
Definition: CoreApp.cpp:220
helics::CoreApp::query
std::string query(std::string_view target, std::string_view queryStr, HelicsSequencingModes mode=HELICS_SEQUENCING_MODE_FAST)
Definition: CoreApp.cpp:227
helics::CoreKeeper::isConnected
bool isConnected()
is the core connected
Definition: application_api/CoreApp.hpp:214
helics::CoreApp
Definition: application_api/CoreApp.hpp:25
helics::CoreApp::setLogFile
void setLogFile(std::string_view logFile)
Definition: CoreApp.cpp:272
helics::CoreApp::setReadyToInit
void setReadyToInit()
Definition: CoreApp.cpp:279
helics::CoreFactory::displayHelp
void displayHelp(CoreType type)
Definition: CoreFactory.cpp:407
helics::CoreApp::addAlias
void addAlias(std::string_view interfaceName, std::string_view alias)
Definition: CoreApp.cpp:196
helics::CoreApp::getCopyofCorePointer
std::shared_ptr< Core > getCopyofCorePointer() const
Definition: application_api/CoreApp.hpp:184
helics::CoreApp::CoreApp
CoreApp()=default
HelicsSequencingModes
HelicsSequencingModes
Definition: helics_enums.h:395
helics::CoreKeeper::forceTerminate
void forceTerminate()
Force terminate the core.
Definition: application_api/CoreApp.hpp:216
helics::CoreApp::haltInit
void haltInit()
Definition: CoreApp.cpp:285
helics
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
helics::CoreApp::sendCommand
void sendCommand(std::string_view target, std::string_view commandStr, HelicsSequencingModes mode=HELICS_SEQUENCING_MODE_FAST)
Definition: CoreApp.cpp:255
helics::CoreApp::getIdentifier
const std::string & getIdentifier() const
Definition: CoreApp.cpp:214
HELICS_SEQUENCING_MODE_FAST
@ HELICS_SEQUENCING_MODE_FAST
Definition: helics_enums.h:397
helics::CoreApp::makeConnections
void makeConnections(const std::string &file)
Definition: CoreApp.cpp:203
helics::gLocalCoreId
constexpr LocalFederateId gLocalCoreId(-259)
helics::CoreApp::addDestinationFilterToEndpoint
void addDestinationFilterToEndpoint(std::string_view filter, std::string_view endpoint)
Definition: CoreApp.cpp:189
helics::CoreApp::setTag
void setTag(std::string_view tag, std::string_view value)
Definition: CoreApp.cpp:233
helics::CoreKeeper
Definition: application_api/CoreApp.hpp:203
helics::generateJsonErrorResponse
std::string generateJsonErrorResponse(JsonErrorCodes code, const std::string &message)
Definition: JsonGeneration.hpp:36