MarlinMT  0.1.0
Configuration.h
Go to the documentation of this file.
1 #pragma once
2 
3 // -- std headers
4 #include <string>
5 #include <map>
6 #include <vector>
7 
8 // -- marlinmt headers
9 #include <marlinmt/Utils.h>
10 #include <marlinmt/Exceptions.h>
11 
12 namespace marlinmt {
13 
14  class Configurable ;
15 
20  class ConfigSection {
21  public:
22  using ConfigSectionMap = std::map<std::string, ConfigSection> ;
23  using ParameterMap = std::map<std::string, std::string> ;
24  using Metadata = std::map<std::string, std::string> ;
25 
26  public:
27  // default copy and move
28  ConfigSection( const ConfigSection& ) = default ;
29  ConfigSection& operator=( const ConfigSection& ) = default ;
30  ConfigSection( ConfigSection&& ) = default ;
31  ConfigSection& operator=( ConfigSection&& ) = default ;
32 
38  ConfigSection( const std::string &name ) ;
39 
43  const std::string &name() const ;
44 
48  bool empty() const ;
49 
52 
58  ConfigSection &section( const std::string &n ) ;
59 
65  const ConfigSection &section( const std::string &n ) const ;
66 
72  ConfigSection &addSection( const std::string &n ) ;
73 
79  bool hasSection( const std::string &n ) const ;
80 
84  std::vector<std::string> subsectionNames() const ;
86 
89 
96  template <typename T>
97  ConfigSection &setParameter( const std::string &n, const T &val ) {
98  _parameters.insert_or_assign( n, details::convert<T>::to_string(val) ) ;
99  return *this ;
100  }
101 
108  template <typename T>
109  inline T parameter( const std::string &n ) const {
110  auto iter = _parameters.find( n ) ;
111  if( _parameters.end() == iter ) {
112  MARLINMT_THROW( "No parameter '" + n + "' in section '" + name() + "'" ) ;
113  }
114  return details::convert<T>::from_string( iter->second ) ;
115  }
116 
124  template <typename T>
125  inline T parameter( const std::string &n, const T &defVal ) const {
126  auto iter = _parameters.find( n ) ;
127  if( _parameters.end() == iter ) {
128  return defVal ;
129  }
130  return details::convert<T>::from_string( iter->second ) ;
131  }
132 
138  bool hasParameter( const std::string &n ) const ;
139 
143  std::vector<std::string> parameterNames() const ;
144 
148  const Metadata &metadata() const ;
149 
153  Metadata &metadata() ;
155 
161  void loadParameters( Configurable &cfg ) const ;
162 
166  const ParameterMap &parameters() const ;
167 
168  private:
176  const std::string _name ;
177  };
178 
179  //--------------------------------------------------------------------------
180  //--------------------------------------------------------------------------
181 
188  public:
190  using ConstantsMap = std::map<std::string, std::string> ;
191 
192  public:
193  // Default constructors, destructors, copy, move
194  Configuration() = default ;
195  ~Configuration() = default ;
196  Configuration( const Configuration & ) = default ;
197  Configuration( Configuration && ) = default ;
198  Configuration& operator=( const Configuration & ) = default ;
199  Configuration& operator=( Configuration && ) = default ;
200 
203 
209  template <typename T>
210  T constantAs( const std::string &cn ) const {
211  auto iter = _constants.find( cn ) ;
212  if( iter == _constants.end() ) {
213  MARLINMT_THROW( "No constant '" + cn + "' in configuration" ) ;
214  }
215  return details::convert<T>::from_string( iter->second ) ;
216  }
217 
225  template <typename T>
226  T constantAs( const std::string &cn, const T &defVal ) const {
227  auto iter = _constants.find( cn ) ;
228  if( iter == _constants.end() ) {
229  return defVal ;
230  }
231  return details::convert<T>::from_string( iter->second ) ;
232  }
233 
241  template <typename T>
242  Configuration &addConstant( const std::string &cn, const T &val ) {
243  auto iter = _constants.find( cn ) ;
244  if( iter != _constants.end() ) {
245  MARLINMT_THROW( "Constant '" + cn + "' already present in configuration" ) ;
246  }
247  _constants.emplace( cn, details::convert<T>::to_string( val ) ) ;
248  return *this ;
249  }
250 
254  bool hasConstant( const std::string &cn ) const ;
255 
259  const ConstantsMap &constants() const ;
261 
264 
270  ConfigSection &createSection( const std::string &sn ) ;
271 
277  ConfigSection &section( const std::string &sn ) ;
278 
284  const ConfigSection &section( const std::string &sn ) const ;
285 
289  std::vector<std::string> sections() const ;
290 
296  bool hasSection( const std::string &n ) const ;
298 
301 
308  void replaceConstants( std::string& str ) const ;
310 
311  private:
313  ConfigSectionMap _sections {} ;
315  ConstantsMap _constants {} ;
316  };
317 
319  std::ostream &operator<<( std::ostream &stream, const Configuration &cfg ) ;
320 
321  //--------------------------------------------------------------------------
322  //--------------------------------------------------------------------------
323 
328  class ConfigReader {
329  public:
331  virtual ~ConfigReader() = default ;
332 
340  virtual void init( const std::string &desc ) = 0 ;
341 
347  virtual void read( Configuration &cfg ) = 0 ;
348  };
349 
350  //--------------------------------------------------------------------------
351  //--------------------------------------------------------------------------
352 
357  class ConfigWriter {
358  public:
360  virtual ~ConfigWriter() = default ;
361 
369  virtual void init( const std::string &desc ) = 0 ;
370 
376  virtual void write( const Configuration &cfg ) = 0 ;
377  };
378 
379  //--------------------------------------------------------------------------
380  //--------------------------------------------------------------------------
381 
386  class ConfigHelper {
387  // Static API only
388  ConfigHelper() = delete ;
389  ~ConfigHelper() = delete ;
390 
391  public:
401  static std::pair<std::string, std::string> splitPluginInput( const std::string &str ) ;
402 
409  static void readConfig( const std::string &str, Configuration &cfg ) ;
410 
417  static void writeConfig( const std::string &str, Configuration &cfg ) ;
418  };
419 
420 }
const Metadata & metadata() const
Get the section metadata.
ConfigSection(const ConfigSection &)=default
ConfigSection & section(const std::string &n)
Get a subsection by name.
T parameter(const std::string &n) const
Get a parameter value as type T.
Configurable class Interface for configuring components in the framework.
Definition: Parameter.h:312
ConfigSection::ConfigSectionMap ConfigSectionMap
const std::string & name() const
Get the section name.
std::vector< std::string > parameterNames() const
Get the list of parameter names.
std::ostream & operator<<(std::ostream &stream, const Configuration &cfg)
Stream operator.
std::map< std::string, std::string > ParameterMap
Definition: Configuration.h:23
bool hasSection(const std::string &n) const
Whether the subsection exists.
std::map< std::string, std::string > ConstantsMap
std::map< std::string, ConfigSection > ConfigSectionMap
Definition: Configuration.h:22
Metadata _metadata
The section metdata map.
T constantAs(const std::string &cn) const
Get a constant value as type T.
std::map< std::string, std::string > Metadata
Definition: Configuration.h:24
Configuration & addConstant(const std::string &cn, const T &val)
Add a constant.
T constantAs(const std::string &cn, const T &defVal) const
Get a constant value as type T.
ConfigReader base class Interface for reading configuration.
const std::string _name
The section name.
bool empty() const
Whether the section is empty (no parameter, no subsection)
bool hasParameter(const std::string &n) const
Whether the parameter exists.
#define MARLINMT_THROW(message)
Definition: Exceptions.h:8
static T from_string(const std::string &str)
Definition: Utils.h:378
ConfigSection & addSection(const std::string &n)
Create a new subsection.
T parameter(const std::string &n, const T &defVal) const
Get a parameter value as type T.
Configuration class.
ParameterMap _parameters
The parameter map.
ConfigWriter base class Interface for writing configuration.
std::vector< std::string > subsectionNames() const
Get the list of subsection names.
ConfigSectionMap _subsections
The subsection map.
ConfigSection & setParameter(const std::string &n, const T &val)
Set a parameter value.
Definition: Configuration.h:97
ConfigSection & operator=(const ConfigSection &)=default
const ParameterMap & parameters() const
Get the raw parameter storage.
ConfigSection class Holds a set of parameters and subsection.
Definition: Configuration.h:20
ConfigHelper class A simple class with helper methods for configuration.
void loadParameters(Configurable &cfg) const
Load the parameters of the current section in the configurable object.