EventAnalysis  1.0.0
Configurable.h
Go to the documentation of this file.
1 /*
2  * Configurable.h
3  *
4  * Created on: 06 Feb 2017
5  * Author: Nicola Mori
6  */
7 
10 #ifndef CONFIGURABLE_H_
11 #define CONFIGURABLE_H_
12 
13 #include "core/ObjectMap.h"
14 
15 namespace EA {
16 
25 class Configurable {
26 public:
28  Configurable();
29 
38  template <typename T> bool SetParameter(const std::string &name, const T &value);
39 
50  bool SetParameter(const std::string &name, const char *value);
51 
56  std::vector<std::string> GetParameters();
57 
58 protected:
68  template <typename T> bool DefineParameter(const std::string &name, T &variable);
69 
71  friend class Configurable;
73  };
74 
75  Configurable(const std::shared_ptr<Representation> &impl) : _repr{impl} {}
76  std::shared_ptr<Representation> &GetRepresentation() { return _repr; }
77 
78 private:
79  std::shared_ptr<Representation> _repr;
80 };
81 
82 #include <typeinfo>
83 
84 template <typename T> bool Configurable::SetParameter(const std::string &name, const T &value) {
85  ObjPtr<T> parameter;
86  if (!((parameter = _repr->paramsMap.GetObject<T>(name)).GetRetrievalResult() == RetrievalResult::SUCCESS))
87  return false;
88 
89  *parameter = value;
90 
91  return true;
92 }
93 
94 template <typename T> bool Configurable::DefineParameter(const std::string &name, T &variable) {
95 
96  static_assert(!(std::is_array<T>::value), "Can't use array as parameter");
97  static_assert(!(std::is_same<T, const char *>::value), "Can't use const char * as parameter; use std::string");
98 
99  return (_repr->paramsMap.AddObject(name, observer_ptr<T>(&variable)) == InsertionResult::SUCCESS);
100 }
101 
102 } // namespace EA
103 
104 #endif /* CONFIGURABLE_H_ */
Configurable(const std::shared_ptr< Representation > &impl)
Definition: Configurable.h:75
Class describing a pointer to an object retrieved from an ObjectMap.
Definition: ObjPtr.h:31
bool SetParameter(const std::string &name, const T &value)
Set the value of a parameter.
Definition: Configurable.h:84
std::shared_ptr< Representation > _repr
Definition: Configurable.h:79
bool DefineParameter(const std::string &name, T &variable)
Set a parameter.
Definition: Configurable.h:94
A smart pointer not owning the wrapped object.
Definition: ObserverPtr.h:28
IncludeFileExc.h IncludeFileExc class declaration.
Definition: Algorithm.h:21
Definition: Configurable.h:70
A map between strings and objects of ObjectWrapper kind.
Definition: ObjectMap.h:32
Configurable()
Constructor.
Definition: Configurable.cpp:14
std::vector< std::string > GetParameters()
Get the names of the parameters.
Definition: Configurable.cpp:26
ObjectMap paramsMap
Definition: Configurable.h:72
Interface for a configurable class.
Definition: Configurable.h:25
std::shared_ptr< Representation > & GetRepresentation()
Definition: Configurable.h:76