ABLATE Source Documentation  0.12.34
runEnvironment.hpp
1 #ifndef ABLATELIBRARY_RUNENVIRONMENT_HPP
2 #define ABLATELIBRARY_RUNENVIRONMENT_HPP
3 #include <filesystem>
4 #include <functional>
5 #include <memory>
6 #include <parameters/mapParameters.hpp>
7 #include <regex>
8 #include <string>
9 #include "parameters/parameters.hpp"
10 
11 namespace ablate::environment {
13  private:
15  inline static int DefaultGlobalArgc = 0;
16 
18  inline static char** DefaultGlobalArgs = nullptr;
19 
21  inline static int* GlobalArgc = &DefaultGlobalArgc;
22 
24  inline static char*** GlobalArgs = &DefaultGlobalArgs;
25 
27  std::filesystem::path outputDirectory;
28 
30  const std::string title;
31 
32  // default empty runEnvironment
33  explicit RunEnvironment();
34 
36  static const inline std::regex OutputDirectoryVariable = std::regex("\\$OutputDirectory");
37 
41  struct FinalizeFunction {
42  std::string name;
43  std::function<void()> function;
44  };
45 
49  inline static std::vector<FinalizeFunction> finalizeFunctions;
50 
51  public:
52  explicit RunEnvironment(const parameters::Parameters&, const std::filesystem::path& inputPath = {});
53  ~RunEnvironment() = default;
54 
55  // force RunEnvironment to be a singleton
56  RunEnvironment(RunEnvironment& other) = delete;
57  void operator=(const RunEnvironment&) = delete;
58 
59  // create an empty run env
60  static void Setup();
61 
62  // static access methods
63  static void Setup(const parameters::Parameters&, const std::filesystem::path& inputPath = {});
64  inline static const RunEnvironment& Get() {
65  if (!runEnvironment) {
66  runEnvironment.reset(new RunEnvironment());
67  }
68  return *runEnvironment;
69  }
70 
75  [[nodiscard]] inline const std::filesystem::path& GetOutputDirectory() const { return outputDirectory; }
76 
84  void ExpandVariables(std::string& value) const;
85 
89  static void Initialize(int* argc, char*** args);
90 
94  static void RegisterCleanUpFunction(const std::string& name, std::function<void()>);
95 
100  static void Finalize();
101 
102  static inline int* GetArgCount() { return GlobalArgc; }
103 
104  static inline char*** GetArgs() { return GlobalArgs; }
105 
109  static std::string_view GetVersion();
110 
115  public:
120  inline Parameters& OutputDirectory(const std::filesystem::path& path) {
121  Insert("directory", path.string());
122  return *this;
123  }
124 
129  inline Parameters& TagDirectory(bool tag) {
130  Insert("tagDirectory", tag);
131  return *this;
132  }
137  inline Parameters& Title(std::string simulationTitle) {
138  Insert("title", simulationTitle);
139  return *this;
140  }
141  };
142 
143  private:
144  inline static std::unique_ptr<RunEnvironment> runEnvironment = std::unique_ptr<RunEnvironment>();
145 };
146 } // namespace ablate::environment
147 
148 #endif // ABLATELIBRARY_RUNENVIRONMENT_HPP
Definition: runEnvironment.hpp:114
Parameters & Title(std::string simulationTitle)
Definition: runEnvironment.hpp:137
Parameters & TagDirectory(bool tag)
Definition: runEnvironment.hpp:129
Parameters & OutputDirectory(const std::filesystem::path &path)
Definition: runEnvironment.hpp:120
Definition: runEnvironment.hpp:12
static std::string_view GetVersion()
Definition: runEnvironment.cpp:88
static void RegisterCleanUpFunction(const std::string &name, std::function< void()>)
Definition: runEnvironment.cpp:68
const std::filesystem::path & GetOutputDirectory() const
Definition: runEnvironment.hpp:75
void ExpandVariables(std::string &value) const
Definition: runEnvironment.cpp:86
static void Initialize(int *argc, char ***args)
Definition: runEnvironment.cpp:63
static void Finalize()
Definition: runEnvironment.cpp:77
Definition: mapParameters.hpp:12
void Insert(std::string key, T value)
Definition: mapParameters.hpp:69
Definition: parameters.hpp:15