EventAnalysis  1.4.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 // EventAnalysis headers
14 #include "core/ObjectMap.h"
15 
16 // C/C++ headers
17 #include <functional>
18 
19 namespace EA {
20 
33 class Configurable {
34 public:
36  Configurable();
37 
47  template <typename T> bool SetParameter(const std::string &name, const T &value);
48 
59  bool SetParameter(const std::string &name, const char *value);
60 
68  std::vector<std::string> GetParameters();
69 
78  bool AddSubConfigurable(std::string name, EA::observer_ptr<Configurable> subConfigurable);
79 
80 protected:
93  template <typename T> bool DefineParameter(const std::string &name, T &variable, std::function<bool()> callBack = {});
94 
96  friend class Configurable;
98  std::unordered_map<std::string, std::function<bool()>> callBackMap;
99  std::vector<std::pair<std::string, observer_ptr<Configurable>>> subConfigurables;
100  };
101 
102  Configurable(const std::shared_ptr<Representation> &impl) : _repr{impl} {}
103  std::shared_ptr<Representation> &GetRepresentation() { return _repr; }
104 
105 private:
106  std::shared_ptr<Representation> _repr;
107 };
108 
109 #include <typeinfo>
110 
111 template <typename T> bool Configurable::SetParameter(const std::string &name, const T &value) {
112  ObjPtr<T> parameter;
113  auto firstPointPos = name.find('.');
114  if (firstPointPos == std::string::npos) {
115  // The parameter name does not contain a point, so it is referred to this object
116  if ((parameter = _repr->paramsMap.GetObject<T>(name)).GetRetrievalResult() == RetrievalResult::SUCCESS) {
117  *parameter = value;
118  auto callBackIter = _repr->callBackMap.find(name);
119  if (callBackIter != _repr->callBackMap.end()) {
120  return (callBackIter->second)();
121  } else {
122  return true;
123  }
124  }
125  } else {
126  // The parameter name contains a point, so it is referred to the sub-configurable specified by the first part of the
127  // name
128  std::string subConfName = name.substr(0, firstPointPos);
129  auto subConfIter =
130  std::find_if(_repr->subConfigurables.begin(), _repr->subConfigurables.end(),
131  [&subConfName](const std::pair<std::string, observer_ptr<Configurable>> &subConfEntry) {
132  if (subConfName == subConfEntry.first) {
133  return true;
134  }
135  return false;
136  });
137  if (subConfIter != _repr->subConfigurables.end()) {
138  return subConfIter->second->SetParameter(name.substr(firstPointPos + 1), value);
139  }
140  }
141 
142  return false;
143 }
144 
145 template <typename T>
146 bool Configurable::DefineParameter(const std::string &name, T &variable, std::function<bool()> callBack) {
147 
148  static_assert(!(std::is_array<T>::value), "Can't use array as parameter");
149  static_assert(!(std::is_same<T, const char *>::value), "Can't use const char * as parameter; use std::string");
150 
151  if (_repr->paramsMap.AddObject(name, observer_ptr<T>(&variable)) == InsertionResult::SUCCESS) {
152  if (callBack) {
153  _repr->callBackMap.emplace(name, callBack);
154  }
155  return true;
156  }
157 
158  return false;
159 }
160 
161 } // namespace EA
162 
163 #endif /* CONFIGURABLE_H_ */
Configurable(const std::shared_ptr< Representation > &impl)
Definition: Configurable.h:102
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 and then calls a callback function.
Definition: Configurable.h:111
std::shared_ptr< Representation > _repr
Definition: Configurable.h:106
std::vector< std::pair< std::string, observer_ptr< Configurable > > > subConfigurables
Definition: Configurable.h:99
bool DefineParameter(const std::string &name, T &variable, std::function< bool()> callBack={})
Set a parameter.
Definition: Configurable.h:146
A smart pointer not owning the wrapped object.
Definition: ObserverPtr.h:28
std::unordered_map< std::string, std::function< bool()> > callBackMap
Definition: Configurable.h:98
IncludeFileExc.h IncludeFileExc class declaration.
Definition: Algorithm.h:21
Definition: Configurable.h:95
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:20
ObjectMap paramsMap
Definition: Configurable.h:97
bool AddSubConfigurable(std::string name, EA::observer_ptr< Configurable > subConfigurable)
Adds a sub-configurable.
Definition: Configurable.cpp:33
Interface for a configurable class.
Definition: Configurable.h:33
std::shared_ptr< Representation > & GetRepresentation()
Definition: Configurable.h:103