netxsimdg
Loading...
Searching...
No Matches
ConfiguredModule.cpp
Go to the documentation of this file.
1
9
11#include "include/Module.hpp"
12
13#include <boost/program_options.hpp>
14#include <stdexcept>
15
16namespace Nextsim {
17
18const std::string ConfiguredModule::MODULE_PREFIX = "Modules";
19
20ConfiguredModule::map ConfiguredModule::configuredModules;
21
23{
24 // A default string that can never be a valid C++ class name
25 std::string defaultStr = "+++DEFAULT+++";
26 // Construct a new options map
27 boost::program_options::options_description opt;
28
29 for (auto entry : configuredModules) {
30 std::string module = entry.first;
31 opt.add_options()(addPrefix(module).c_str(),
32 boost::program_options::value<std::string>()->default_value(defaultStr),
33 ("Load an implementation of " + module).c_str());
34 }
35
36 boost::program_options::variables_map vm = Configurator::parse(opt);
37
38 for (auto entry : configuredModules) {
39 std::string implString = vm[addPrefix(entry.first)].as<std::string>();
40 // Only do anything if the retrieved option is not the default value
41 if (implString != defaultStr) {
42 entry.second.first(implString);
43 }
44 }
45}
46
47std::string ConfiguredModule::addPrefix(const std::string& moduleName)
48{
49 return MODULE_PREFIX + "." + moduleName;
50}
51
53{
54 map newMap(ls);
55 configuredModules.merge(newMap);
56}
57
58void ConfiguredModule::configureModule(const std::string& mod, const fn& setter, const ofn& getter)
59{
60 configuredModules[mod] = { setter, getter };
61}
62
63std::string ConfiguredModule::getModuleConfiguration(const std::string& module)
64{
65 return configuredModules[module].second();
66}
67
69{
70 ConfigMap iiMap;
71 for (auto entry : configuredModules) {
72 iiMap[addPrefix(entry.first)] = entry.second.second();
73 }
74 return iiMap;
75}
76} /* namespace Nextsim */
static boost::program_options::variables_map parse(const boost::program_options::options_description &opt)
Parses all configuration sources.
static std::string addPrefix(const std::string &moduleName)
Transforms a module name into a configuration option name.
static void parseConfigurator()
Parse the configuration for all of the modules defined in ModuleLoader.
static void setConfiguredModules(const map &ls)
static void configureModule(const std::string &mod, const fn &setter, const ofn &getter)
static std::string getModuleConfiguration(const std::string &interface)
static ConfigMap getAllModuleConfigurations()
static const std::string MODULE_PREFIX
The configuration options section name for modules.