helics  2.8.1
application_api/CoreApp.hpp
1 /*
2 Copyright (c) 2017-2021,
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/core-types.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(core_type ctype, std::vector<std::string> args);
38 
44  CoreApp(core_type ctype, const std::string& coreName, std::vector<std::string> args);
49  CoreApp(int argc, char* argv[]);
56  CoreApp(core_type ctype, const std::string& coreName, int argc, char* argv[]);
62  CoreApp(core_type ctype, int argc, char* argv[]);
66  explicit CoreApp(const std::string& argString);
71  CoreApp(core_type ctype, const std::string& argString);
77  CoreApp(core_type ctype, const std::string& coreName, const std::string& 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 dataLink(const std::string& source, const std::string& target);
97  void addSourceFilterToEndpoint(const std::string& filter, const std::string& endpoint);
99  void addDestinationFilterToEndpoint(const std::string& filter, const std::string& endpoint);
101  void makeConnections(const std::string& file);
103  const std::string& getIdentifier() const;
105  const std::string& getAddress() const;
106 
115  std::string query(const std::string& target,
116  const std::string& queryStr,
118 
125  void setGlobal(const std::string& valueName, const std::string& value);
126 
128  void setLogFile(const std::string& logFile);
130  void setLoggingLevel(int loglevel);
132  void setReadyToInit();
134  void haltInit();
135 #ifdef HELICS_CXX_STATIC_DEFINE
136 
138  auto* operator->() const { return core.operator->(); }
139 #else
140  CoreApp* operator->() { return this; }
141  const CoreApp* operator->() const { return this; }
142 #endif
143 
144  std::shared_ptr<Core> getCopyofCorePointer() const { return core; }
145 
147  void reset();
149  void globalError(int32_t errorCode, const std::string& errorString);
150 
151  private:
152  void processArgs(std::unique_ptr<helicsCLI11App>& app);
153  std::unique_ptr<helicsCLI11App> generateParser();
154 
155  std::shared_ptr<Core> core;
156  std::string name;
157 };
158 
160 class CoreKeeper {
161  public:
162  template<class... Args>
163  explicit CoreKeeper(Args&&... args): cr(std::forward<Args...>(args...))
164  {
165  }
166  CoreKeeper(CoreKeeper&& brkeep) = default;
167  CoreKeeper(const CoreKeeper& crkeep) = default;
168  CoreKeeper& operator=(CoreKeeper&& crkeep) = default;
169  CoreKeeper& operator=(const CoreKeeper& crkeep) = default;
171  bool isConnected() { return cr.isConnected(); }
176  {
177  if (cr.isConnected()) {
178  cr.waitForDisconnect();
179  }
180  }
181 
182  private:
183  CoreApp cr;
184 };
185 } // 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::setLogFile
void setLogFile(const std::string &logFile)
Definition: CoreApp.cpp:233
helics::CoreApp::dataLink
void dataLink(const std::string &source, const std::string &target)
Definition: CoreApp.cpp:168
helics::CoreApp::setGlobal
void setGlobal(const std::string &valueName, const std::string &value)
Definition: CoreApp.cpp:218
helics::core_type
core_type
Definition: core-types.hpp:37
helics::CoreApp::connect
bool connect()
Definition: CoreApp.cpp:140
helics::CoreApp::isOpenToNewFederates
bool isOpenToNewFederates() const
Definition: CoreApp.cpp:145
helics::CoreApp::reset
void reset()
Definition: CoreApp.cpp:253
helics::CoreApp::setLoggingLevel
void setLoggingLevel(int loglevel)
Definition: CoreApp.cpp:225
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:175
helics::CoreApp::getAddress
const std::string & getAddress() const
Definition: CoreApp.cpp:204
helics::CoreApp::addSourceFilterToEndpoint
void addSourceFilterToEndpoint(const std::string &filter, const std::string &endpoint)
Definition: CoreApp.cpp:175
helics_sequencing_mode
helics_sequencing_mode
Definition: helics_enums.h:333
helics::CoreKeeper::isConnected
bool isConnected()
is the core connected
Definition: application_api/CoreApp.hpp:171
helics::core_type::DEFAULT
@ DEFAULT
ZMQ if available or UDP.
helics::CoreApp
Definition: application_api/CoreApp.hpp:25
helics::CoreFactory::displayHelp
void displayHelp(core_type type)
Definition: CoreFactory.cpp:363
helics::CoreApp::globalError
void globalError(int32_t errorCode, const std::string &errorString)
Definition: CoreApp.cpp:259
helics::CoreApp::setReadyToInit
void setReadyToInit()
Definition: CoreApp.cpp:240
helics::CoreApp::getCopyofCorePointer
std::shared_ptr< Core > getCopyofCorePointer() const
Definition: application_api/CoreApp.hpp:144
helics::CoreApp::CoreApp
CoreApp()=default
helics::CoreFactory::findCore
std::shared_ptr< Core > findCore(const std::string &name)
Definition: CoreFactory.cpp:267
helics::CoreApp::addDestinationFilterToEndpoint
void addDestinationFilterToEndpoint(const std::string &filter, const std::string &endpoint)
Definition: CoreApp.cpp:182
helics::CoreKeeper::forceTerminate
void forceTerminate()
Force terminate the core.
Definition: application_api/CoreApp.hpp:173
helics::CoreApp::haltInit
void haltInit()
Definition: CoreApp.cpp:246
helics_flag_delay_init_entry
@ helics_flag_delay_init_entry
Definition: helics_enums.h:135
helics
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
helics::CoreApp::getIdentifier
const std::string & getIdentifier() const
Definition: CoreApp.cpp:197
helics::local_core_id
constexpr local_federate_id local_core_id(-259)
helics::CoreApp::makeConnections
void makeConnections(const std::string &file)
Definition: CoreApp.cpp:189
helics_sequencing_mode_fast
@ helics_sequencing_mode_fast
Definition: helics_enums.h:335
helics::CoreKeeper
Definition: application_api/CoreApp.hpp:160
helics::CoreApp::query
std::string query(const std::string &target, const std::string &queryStr, helics_sequencing_mode mode=helics_sequencing_mode_fast)
Definition: CoreApp.cpp:211