netxsimdg
Loading...
Searching...
No Matches
Configurator.hpp
Go to the documentation of this file.
1
8#ifndef CONFIGURATOR_HPP
9#define CONFIGURATOR_HPP
10
11#include "include/ConfigMap.hpp"
12
13#include <boost/program_options.hpp>
14#include <fstream>
15#include <istream>
16#include <memory>
17#include <sstream>
18#include <string>
19#include <vector>
20
21namespace Nextsim {
22
23class NoAdditionalConfiguration;
24
34public:
35 Configurator() = default;
36 virtual ~Configurator() = default;
37
44 inline static void addFile(const std::string& filename)
45 {
46 addStream(std::unique_ptr<std::istream>(new std::fstream(filename)));
47 }
48
58 template <typename C> static void addFiles(const C& container)
59 {
60 for (auto& filename : container)
61 addFile(filename);
62 }
68 inline static void addStream(std::unique_ptr<std::istream> pis)
69 {
70 sources.push_back(std::move(pis));
71 }
81 template <typename C> static void addStreams(const C& container)
82 {
83 for (auto& stream : container) {
84 addStream(stream);
85 }
86 }
87
92 static void addSStream(const std::stringstream& sstream);
93
97 inline static void clearStreams() { sources.clear(); }
98
102 inline static void clear()
103 {
104 clearStreams();
105 setCommandLine(0, nullptr);
106 }
118 inline static void setCommandLine(int argc, char* argv[])
119 {
120 m_argc = argc;
121 m_argv = argv;
122 }
123
129 static void getAdditionalConfiguration(const std::string& source);
130
144 static boost::program_options::variables_map parse(
145 const boost::program_options::options_description& opt);
146
147private:
148 static std::vector<std::unique_ptr<std::istream>> sources;
149
150 static int m_argc;
151 static char** m_argv;
152
153public:
155 public:
156 virtual ~AdditionalConfiguration() = default;
162 virtual std::stringstream read(const std::string& source) = 0;
163 };
172
173private:
174 static AdditionalConfiguration* p_addConf;
175};
176
179 std::stringstream read(const std::string& source) override { return std::stringstream(); }
180};
181
182} /* namespace Nextsim */
183
184#endif /* CONFIGURATOR_HPP */
virtual std::stringstream read(const std::string &source)=0
Reads the additional configuration from the provided source, which is interpreted in an implementatio...
A class to handle the sources of configuration, both files and the command line.
static void addStream(std::unique_ptr< std::istream > pis)
Adds a istream source of configuration data.
static void addFile(const std::string &filename)
static void addFiles(const C &container)
Adds several config files to the configuration sources.
static void setAdditionalConfiguration(AdditionalConfiguration *pAC)
Sets the source of any additional configuration data, such as a netCDF restart file.
static void clearStreams()
static void addSStream(const std::stringstream &sstream)
Adds a std::stringstream configuration stream, wrapping all the pointer mechanics.
static void getAdditionalConfiguration(const std::string &source)
Gets the additional configuration according to the supplied implementation.
static boost::program_options::variables_map parse(const boost::program_options::options_description &opt)
Parses all configuration sources.
static void addStreams(const C &container)
Adds several istream sources of configuration data.
static void setCommandLine(int argc, char *argv[])
Sets the command line data to be parsed.
A default implementation of Configurator::AdditionalConfiguration.