| helics 3.6.1

helicsApp.hpp
1 /*
2 Copyright (c) 2017-2025,
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 "../application_api/CombinationFederate.hpp"
10
11 #include <fstream>
12 #include <memory>
13 #include <string>
14 #include <string_view>
15 #include <vector>
16
17 namespace CLI {
18 class App;
19 } // namespace CLI
20
21 namespace helics::fileops {
22 class JsonBuffer;
23 }
24 namespace helics::apps {
25
26 class AppTextParser;
27
32 class HELICS_CXX_EXPORT App {
33 public:
35 App() = default;
40 App(std::string_view defaultAppName, std::vector<std::string> args);
46 App(std::string_view defaultAppName, int argc, char* argv[]);
52 App(std::string_view appName, const FederateInfo& fedInfo);
58 App(std::string_view appName, const std::shared_ptr<Core>& core, const FederateInfo& fedInfo);
64 App(std::string_view appName, CoreApp& core, const FederateInfo& fedInfo);
71 App(std::string_view appName, const std::string& configString);
72
74 App(App&& other_app) = default;
76 App(const App& other_app) = delete;
78 App& operator=(App&& app) = default;
81 App& operator=(const App& app) = delete;
82 virtual ~App();
83
90 void loadFile(const std::string& filename, bool enableFederateInterfaceRegistration = true);
97 virtual void initialize();
98 /*run the Player*/
99 virtual void run();
100
104 virtual void runTo(Time stopTime_input) = 0;
105
107 virtual void finalize();
108
110 bool isActive() const { return !deactivated; }
112 const CombinationFederate& accessUnderlyingFederate() const { return *fed; }
113
115 std::shared_ptr<CombinationFederate> getUnderlyingFederatePointer() { return fed; }
116
117 protected:
121 virtual void loadJsonFile(const std::string& jsonString,
122 bool enableFederateInterfaceRegistration);
128 void loadJsonFileConfiguration(const std::string& appName,
129 const std::string& jsonString,
130 bool enableFederateInterfaceRegistration);
132 virtual void loadTextFile(const std::string& textFile);
134 void loadInputFiles();
136 void loadConfigOptions(AppTextParser& aparser);
137
138 private:
139 void loadConfigOptions(const fileops::JsonBuffer& element);
141 std::unique_ptr<helicsCLI11App> generateParser();
143 void processArgs(std::unique_ptr<helicsCLI11App>& app,
144 FederateInfo& fedInfo,
145 std::string_view defaultAppName);
146
147 protected:
148 std::shared_ptr<CombinationFederate> fed;
149 Time stopTime = Time::maxVal();
150 std::string configFileName;
151 std::string inputFileName;
152 std::string outFileName;
153 bool useLocal{false};
154 bool fileLoaded{false};
155 bool deactivated{false};
156 bool quietMode{false};
157 bool helpMode{false};
158 std::vector<std::string> remArgs;
159 };
160
161 class AppTextParser {
162 public:
163 explicit AppTextParser(const std::string& filename);
165 std::vector<int> preParseFile(const std::vector<char>& klines);
166
167 const std::string& configString() const { return configStr; }
169 bool loadNextLine(std::string& line, int& lineNumber);
170 void reset();
171
172 private:
173 bool mLineComment{false};
174 std::ifstream filePtr;
175 std::string configStr;
176 std::string mFileName;
177 int currentLineNumber{0};
178 };
179 } // namespace helics::apps
helics::CombinationFederate
Definition: application_api/CombinationFederate.hpp:18
helics::FederateInfo
Definition: FederateInfo.hpp:28
helics::apps::AppTextParser
Definition: helicsApp.hpp:161
helics::apps::AppTextParser::preParseFile
std::vector< int > preParseFile(const std::vector< char > &klines)
Definition: helicsApp.cpp:151
helics::apps::AppTextParser::loadNextLine
bool loadNextLine(std::string &line, int &lineNumber)
Definition: helicsApp.cpp:199
helics::apps::App
Definition: helicsApp.hpp:32
helics::apps::App::inputFileName
std::string inputFileName
the name of the app input file
Definition: helicsApp.hpp:151
helics::apps::App::configFileName
std::string configFileName
name of the config file used for constructing the federate
Definition: helicsApp.hpp:150
helics::apps::App::operator=
App & operator=(App &&app)=default
helics::apps::App::fed
std::shared_ptr< CombinationFederate > fed
the federate created for the App
Definition: helicsApp.hpp:148
helics::apps::App::outFileName
std::string outFileName
the name of any output file
Definition: helicsApp.hpp:152
helics::apps::App::runTo
virtual void runTo(Time stopTime_input)=0
helics::apps::App::App
App(const App &other_app)=delete
helics::apps::App::App
App(App &&other_app)=default
helics::apps::App::isActive
bool isActive() const
Definition: helicsApp.hpp:110
helics::apps::App::App
App()=default
helics::apps::App::operator=
App & operator=(const App &app)=delete
helics::apps::App::accessUnderlyingFederate
const CombinationFederate & accessUnderlyingFederate() const
Definition: helicsApp.hpp:112
helics::apps::App::getUnderlyingFederatePointer
std::shared_ptr< CombinationFederate > getUnderlyingFederatePointer()
Definition: helicsApp.hpp:115
helics::fileops::JsonBuffer
Definition: JsonProcessingFunctions.hpp:25
helics::Time
TimeRepresentation< count_time< 9 > > Time
Definition: helicsTime.hpp:27