netxsimdg
Loading...
Searching...
No Matches
Configurator.cpp
Go to the documentation of this file.
1
9
10#include <iostream>
11
12namespace Nextsim {
13
14std::vector<std::unique_ptr<std::istream>> Configurator::sources;
15int Configurator::m_argc;
16char** Configurator::m_argv;
17static NoAdditionalConfiguration noAddConf;
18Configurator::AdditionalConfiguration* Configurator::p_addConf = &noAddConf;
19
20boost::program_options::variables_map Configurator::parse(
21 const boost::program_options::options_description& opt)
22{
23 boost::program_options::variables_map vm;
24
25 // Parse the command file for any overrides
26 int use_argc;
27 char** use_argv;
28 if (m_argc && m_argv) {
29 use_argc = m_argc;
30 use_argv = m_argv;
31 } else {
32 // Use a fake command line to ensure at least one parse happens in all cases
33 use_argc = 1;
34 char name[] = { 'n', 'e', 'x', 't', 's', 'i', 'm', 'd', 'g', '\0' };
35 char* fake_argv[] = { name, nullptr };
36 use_argv = fake_argv;
37 }
38 auto parsed = boost::program_options::command_line_parser(use_argc, use_argv)
39 .options(opt)
40 .style(boost::program_options::command_line_style::
41 unix_style) // | po::command_line_style::allow_long_disguise)
42 .allow_unregistered()
43 .run();
44 boost::program_options::store(parsed, vm);
45
46 // Parse the named streams for configuration
47 for (auto iter = sources.begin(); iter != sources.end(); ++iter) {
48 try {
49 boost::program_options::store(
50 boost::program_options::parse_config_file(**iter, opt, true), vm);
51 } catch (std::exception& e) {
52 // Echo the exception, but carry on
53 std::cerr << e.what() << std::endl;
54 }
55 // Once the stream has been parsed, clear all flags (especially EOF)
56 // and seek back to the start.
57 (*iter)->clear();
58 (*iter)->seekg(0, std::ios_base::beg);
59 }
60
61 return vm;
62}
63
64void Configurator::addSStream(const std::stringstream& sstream)
65{
66 addStream(std::move(std::unique_ptr<std::istream>(new std::stringstream(sstream.str()))));
67}
68
70
71void Configurator::getAdditionalConfiguration(const std::string& source)
72{
73 if (p_addConf) {
74 addSStream(p_addConf->read(source));
75 }
76}
77
78} /* namespace Nextsim */
virtual std::stringstream read(const std::string &source)=0
Reads the additional configuration from the provided source, which is interpreted in an implementatio...
static void addStream(std::unique_ptr< std::istream > pis)
Adds a istream source of configuration data.
static void setAdditionalConfiguration(AdditionalConfiguration *pAC)
Sets the source of any additional configuration data, such as a netCDF restart file.
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.