helics  3.5.2
application_api/CoreApp.hpp
1 /*
2 Copyright (c) 2017-2024,
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 = "true");
135  const std::string& getTag(std::string_view tag) const;
136 
144  void setGlobal(std::string_view valueName, std::string_view value = "true");
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
169  auto* operator->() const { return core.operator->(); }
170 #else
171  CoreApp* operator->() { return this; }
172  const CoreApp* operator->() const { return this; }
173 #endif
175  std::shared_ptr<Core> getCopyofCorePointer() const { return core; }
176 
178  void reset();
180  void globalError(int32_t errorCode, std::string_view errorString);
181 
182  private:
183  void processArgs(std::unique_ptr<helicsCLI11App>& app);
184  std::unique_ptr<helicsCLI11App> generateParser();
185 
186  std::shared_ptr<Core> core;
187  std::string name;
188 };
189 
191 class CoreKeeper {
192  public:
193  template<class... Args>
194  explicit CoreKeeper(Args&&... args): cr(std::forward<Args...>(args...))
195  {
196  }
197  CoreKeeper(CoreKeeper&& brkeep) = default;
198  CoreKeeper(const CoreKeeper& crkeep) = default;
199  CoreKeeper& operator=(CoreKeeper&& crkeep) = default;
200  CoreKeeper& operator=(const CoreKeeper& crkeep) = default;
202  bool isConnected() { return cr.isConnected(); }
207  {
208  if (cr.isConnected()) {
209  cr.waitForDisconnect();
210  }
211  }
212 
213  private:
214  CoreApp cr;
215 };
216 } // namespace helics
Definition: application_api/CoreApp.hpp:25
bool waitForDisconnect(std::chrono::milliseconds waitTime=std::chrono::milliseconds(0))
Definition: CoreApp.cpp:160
void forceTerminate()
Definition: CoreApp.cpp:150
std::shared_ptr< Core > getCopyofCorePointer() const
Definition: application_api/CoreApp.hpp:175
bool isConnected() const
Definition: CoreApp.cpp:135
CoreApp()=default
Definition: application_api/CoreApp.hpp:191
~CoreKeeper()
the destructor waits for the core to terminate
Definition: application_api/CoreApp.hpp:206
bool isConnected()
is the core connected
Definition: application_api/CoreApp.hpp:202
void forceTerminate()
Force terminate the core.
Definition: application_api/CoreApp.hpp:204
HelicsSequencingModes
Definition: helics_enums.h:425
@ HELICS_SEQUENCING_MODE_FAST
Definition: helics_enums.h:427
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
CoreType
Definition: CoreTypes.hpp:46