netxsimdg
Loading...
Searching...
No Matches
Configured.hpp
Go to the documentation of this file.
1
8#ifndef CONFIGURED_HPP
9#define CONFIGURED_HPP
10
11#include "include/ConfigMap.hpp"
14
15#include <boost/program_options.hpp>
16#include <map>
17#include <string>
18#include <typeinfo>
19
20namespace Nextsim {
21
24public:
25 virtual ~ConfiguredBase() = default;
27 virtual void configure() = 0;
29 virtual ConfigMap getConfiguration() const = 0;
30};
31
36template <typename C> class Configured : public ConfiguredBase {
37public:
38 typedef ConfigurationHelp::HelpMap HelpMap;
39 using ConfigType = ConfigurationHelp::ConfigType;
40
41 Configured() = default;
42 virtual ~Configured() = default;
43
45 virtual void configure() = 0;
46
48 // FIXME remove default implementation
49 virtual ConfigMap getConfiguration() const { return ConfigMap(); }
50
61 template <typename T> static void tryConfigure(T& ref);
62
74 template <typename T> static void tryConfigure(T* ptr);
75
88 template <typename T> static ConfigMap tryGetConfiguration(T& ref);
89
102 template <typename T> static ConfigMap tryGetConfiguration(T* ptr);
103
112 template <typename T>
113 static inline T getConfiguration(const std::string& name, const T& defaultValue)
114 {
115 boost::program_options::options_description opt;
116 addOption(name, defaultValue, opt);
117 return retrieveValue<T>(name, opt);
118 }
119
125 static HelpMap& getHelpText(HelpMap& map, bool getAll);
126
133 static HelpMap& getHelpRecursive(HelpMap& map, bool getAll);
134
136 static void clearConfigurationMap() { singleOptions.clear(); }
137
139 static const std::map<int, std::string> keyMap;
140
141protected:
149 template <typename T> void addOption(const std::string& name, const T& defaultValue)
150 {
151 addOption(name, defaultValue, singleOptions[name]);
152 }
153
159 template <typename T> T retrieveValue(const std::string& name)
160 {
161 return retrieveValue<T>(name, singleOptions.at(name));
162 }
163
164private:
165 template <typename T>
166 static void addOption(const std::string& name, const T& defaultValue,
167 boost::program_options::options_description& opt)
168 {
169 opt.add_options()(
170 name.c_str(), boost::program_options::value<T>()->default_value(defaultValue), "");
171 }
172
173 template <typename T>
174 static T retrieveValue(
175 const std::string& name, boost::program_options::options_description& opt)
176 {
177 return Configurator::parse(opt)[name].as<T>();
178 }
179
180 static std::map<std::string, boost::program_options::options_description> singleOptions;
181};
182
183template <typename C>
184std::map<std::string, boost::program_options::options_description> Configured<C>::singleOptions;
185
195template <typename C> template <typename T> void Configured<C>::tryConfigure(T& ref)
196{
197 try {
198 dynamic_cast<ConfiguredBase&>(ref).configure();
199 } catch (const std::bad_cast& bc) {
200 // Do nothing. If the reference is not a derived class of Configured, ignore it.
201 }
202}
203
214template <typename C> template <typename T> void Configured<C>::tryConfigure(T* ptr)
215{
216 ConfiguredBase* cfg = dynamic_cast<ConfiguredBase*>(ptr);
217 if (cfg)
218 cfg->configure();
219}
220
233template <typename C> template <typename T> ConfigMap Configured<C>::tryGetConfiguration(T& ref)
234{
235 try {
236 return dynamic_cast<ConfiguredBase&>(ref).getConfiguration();
237 } catch (const std::bad_cast& bc) {
238 return ConfigMap();
239 }
240}
241
254template <typename C> template <typename T> ConfigMap Configured<C>::tryGetConfiguration(T* ptr)
255{
256 ConfiguredBase* cfg = dynamic_cast<ConfiguredBase*>(ptr);
257 if (cfg)
258 return cfg->getConfiguration();
259 else
260 return ConfigMap();
261}
271template <typename T> void tryConfigure(T& t) { Configured<int>::tryConfigure(t); }
272
284template <typename T> void tryConfigure(T* p_t) { Configured<int>::tryConfigure(p_t); }
285
286}
287
288#endif /* CONFIGURED_HPP */
void tryConfigure(T &t)
Template function for conditionally configuring references.
static boost::program_options::variables_map parse(const boost::program_options::options_description &opt)
Parses all configuration sources.
A base class necessary for the tryConfigure() functions to work.
virtual ConfigMap getConfiguration() const =0
Returns the current configuration of the object.
virtual void configure()=0
The configuration function.
static void tryConfigure(T *ptr)
Template function for conditionally configuring classes via a pointer.
static void tryConfigure(T &ref)
Template function for conditionally configuring references.
virtual ConfigMap getConfiguration() const
Returns the current configuration of the object.
static void clearConfigurationMap()
Clear the configuration map. Usually used only in test suites.
static ConfigMap tryGetConfiguration(T *ptr)
Template function for conditionally retrieving class configuration via a pointer.
virtual void configure()=0
The configuration function.
static HelpMap & getHelpText(HelpMap &map, bool getAll)
Gets the text to be printed as the help text for this configuration.
static const std::map< int, std::string > keyMap
A per-class static map to provide compile-time checking of configuration keys.
static HelpMap & getHelpRecursive(HelpMap &map, bool getAll)
Gets the configuration help text for the current class as well as any classes used herein.
void addOption(const std::string &name, const T &defaultValue)
Adds an option to the per-class option map.
static T getConfiguration(const std::string &name, const T &defaultValue)
Gets the value of the configuration with a given name from the default Configurator.
T retrieveValue(const std::string &name)
Retrieves a configured value of a single option.
static ConfigMap tryGetConfiguration(T &ref)
Template function for conditionally retrieving class configuration via a reference.