MarlinMT  0.1.0
Component.h
Go to the documentation of this file.
1 #pragma once
2 
3 // -- std headers
4 #include <typeindex>
5 
6 // -- marlinmt headers
7 #include <marlinmt/Logging.h>
8 #include <marlinmt/Parameter.h>
9 
10 namespace marlinmt {
11 
12  class Application ;
13 
22  class Component : public Configurable {
23  public:
24  friend class Application ;
26 
27  public:
29  Component() = delete ;
31  Component(const Component &) = delete ;
32  Component &operator=(const Component &) = delete ;
33 
35  virtual ~Component() = default ;
36 
38  Component( const std::string &type ) ;
39 
43  const std::string &type() const ;
44 
48  const std::string &name() const ;
49 
55  void setName( const std::string &n ) ;
56 
60  const std::string &description() const ;
61 
67  void setDescription( const std::string &desc ) ;
68 
72  virtual void initialize() { /*nop*/ }
73 
77  const Application &application() const ;
78 
83 
92  template <class T>
93  inline Logging::StreamType log() const {
94  return _logger->log<T>() ;
95  }
96 
98  Logging::StreamType debug() const ;
99 
101  Logging::StreamType message() const ;
102 
104  Logging::StreamType warning() const ;
105 
107  Logging::StreamType error() const ;
108 
114  void setVerbosity( const std::string &level ) ;
115 
119  const std::string &verbosity() const ;
120 
124  bool isInitialized() const ;
125 
132  void setup( Application *app ) ;
133 
137  void printParameters() const ;
138 
142  template <class T>
143  void printParameters() const ;
144 
152  void setParameters( const ConfigSection &section, bool throwIfNotFound = false ) ;
153 
161  void getParameters( ConfigSection &section, const std::set<std::string> &exclude = {} ) const ;
162 
163  protected:
165  std::string _type {} ;
167  std::string _name {} ;
169  std::string _description {"No description"} ;
173  LoggerPtr _logger {nullptr} ;
175  StringParameter _verbosity { *this, "Verbosity", "The component verbosity level", "MESSAGE" } ;
176  };
177 
178  //--------------------------------------------------------------------------
179  //--------------------------------------------------------------------------
180 
181  template <class T>
183  log<T>() << name() << " (" << type() << ") parameters:" << std::endl ;
184  for( auto iter : _parameters ) {
185  auto paramStr = iter.second->isSet() ? iter.second->str() : ( iter.second->hasDefault() ? iter.second->defaultStr() :"[undefined]" ) ;
186  log<T>() << iter.first << " (" << iter.second->typeStr() << "): " << paramStr << std::endl ;
187  }
188  }
189 
190 }
Logging::StreamType error() const
Shortcut for log<ERROR>()
Definition: Component.cc:65
std::string _type
The component type.
Definition: Component.h:165
Configurable class Interface for configuring components in the framework.
Definition: Parameter.h:312
Logging::StreamType debug() const
Shortcut for log<DEBUG>()
Definition: Component.cc:47
const std::string & name() const
Get the component name.
Definition: Component.cc:23
Application * _application
The application in which the component has been registered.
Definition: Component.h:171
void setup(Application *app)
Setup the component.
Definition: Component.cc:101
void setName(const std::string &n)
Set the component name.
Definition: Component.cc:29
Component & operator=(const Component &)=delete
std::string _name
The component name.
Definition: Component.h:167
void setParameters(const ConfigSection &section, bool throwIfNotFound=false)
Set the parameters from the configuration section.
Definition: Component.cc:124
LoggerPtr _logger
The logger instance.
Definition: Component.h:173
Logging::StreamType message() const
Shortcut for log<MESSAGE>()
Definition: Component.cc:53
Component()=delete
No default constructor.
const std::string & type() const
Get the component name.
Definition: Component.cc:17
Logger::element_type::stream_type StreamType
Definition: Logging.h:74
std::string _description
The component description.
Definition: Component.h:169
void setDescription(const std::string &desc)
Set the component description.
Definition: Component.cc:41
const std::string & description() const
Get the component description.
Definition: Component.cc:35
Application class Base application interface for running a Marlin application.
Definition: Application.h:25
Component class.
Definition: Component.h:22
Logging::StreamType log() const
Log a message with specific log level.
Definition: Component.h:93
void setVerbosity(const std::string &level)
Set the verbosity level.
Definition: Component.cc:71
const Application & application() const
Get the application in which the component is registered.
Definition: Component.cc:83
bool isInitialized() const
Whether the component has been initialized.
Definition: Component.cc:112
StringParameter _verbosity
The verbosity level of the logger (parameter)
Definition: Component.h:175
virtual ~Component()=default
Default destructor.
std::shared_ptr< streamlog::logstreamT< mutex_type > > Logger
Definition: Logging.h:73
const std::string & verbosity() const
Get the verbosity level.
Definition: Component.cc:77
Logging::Logger LoggerPtr
Definition: Component.h:25
virtual void initialize()
Initialize the component.
Definition: Component.h:72
ConfigSection class Holds a set of parameters and subsection.
Definition: Configuration.h:20
void getParameters(ConfigSection &section, const std::set< std::string > &exclude={}) const
Get the parameters from configurable object and populate the config section with. ...
Definition: Component.cc:139
void printParameters() const
Print the component parameters.
Definition: Component.cc:118
ParameterMap _parameters
The parameter map.
Definition: Parameter.h:416
Logging::StreamType warning() const
Shortcut for log<WARNING>()
Definition: Component.cc:59