DD4hep  1.31.0
Detector Description Toolkit for High Energy Physics
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ComponentProperties.h
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation
3 //--------------------------------------------------------------------------
4 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
5 // All rights reserved.
6 //
7 // For the licensing terms see $DD4hepINSTALL/LICENSE.
8 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
9 //
10 // Author : M.Frank
11 //
12 //==========================================================================
13 #ifndef DD4HEP_COMPONENTPROPERTIES_H
14 #define DD4HEP_COMPONENTPROPERTIES_H
15 
16 // Framework include files
17 #include <DD4hep/Grammar.h>
18 
19 // C/C++ include files
20 #include <algorithm>
21 #include <typeinfo>
22 #include <string>
23 #include <map>
24 
26 namespace dd4hep {
27 
28  class Property;
29  class BasicGrammar;
30 
32 
45  class Property {
46  protected:
48  void* m_par { nullptr };
50  const BasicGrammar* m_hdl { nullptr };
51 
52  public:
54  Property() = default;
56  Property(const Property& p) = default;
58  template <typename TYPE> Property(TYPE& val);
60  static std::string type(const Property& proptery);
62  static std::string type(const std::type_info& proptery);
64  void* ptr() const { return m_par; }
66  std::string type() const;
68  const BasicGrammar& grammar() const;
70  std::string str() const;
72  Property& str(const std::string& input);
74  const Property& str(const std::string& input) const;
76  Property& operator=(const Property& p) = default;
78  Property& operator=(const char* val);
80  //Property& operator=(const std::string& val);
82  template <typename TYPE> Property& operator=(const TYPE& val);
84  template <typename TYPE> TYPE value() const;
86  template <typename TYPE> void value(TYPE& value) const;
88  template <typename TYPE> void set(const TYPE& value);
89  };
90 
92  template <typename TYPE> Property::Property(TYPE& val)
93  : m_par(&val), m_hdl(0)
94  {
95  m_hdl = &BasicGrammar::get(typeid(TYPE));
96  }
97 
99  template <typename TYPE> void Property::set(const TYPE& val) {
100  const auto& grm = grammar();
101  if (grm.type() == typeid(TYPE))
102  *(TYPE*) m_par = val;
103  else if (!grm.fromString(m_par, BasicGrammar::instance< TYPE >().str(&val)))
104  BasicGrammar::invalidConversion(typeid(TYPE), grm.type());
105  }
106 
108  template <typename TYPE> Property& Property::operator=(const TYPE& val) {
109  this->set(val);
110  return *this;
111  }
112 
114  template <typename TYPE> void Property::value(TYPE& val) const {
115  const auto& grm = grammar();
116  if (grm.type() == typeid(TYPE))
117  val = *(TYPE*) m_par;
118  else if (!BasicGrammar::instance< TYPE >().fromString(&val, this->str()))
119  BasicGrammar::invalidConversion(grm.type(), typeid(TYPE));
120  }
121 
123  template <typename TYPE> TYPE Property::value() const {
124  TYPE temp;
125  this->value(temp);
126  return temp;
127  }
128 
130 
137  template <class TYPE> class PropertyValue : private Property {
138  public:
139  TYPE data {};
143  PropertyValue(const PropertyValue& c) = default;
145  PropertyValue& operator=(const PropertyValue& c) = default;
147  PropertyValue& operator=(const TYPE& val) { data = val; return *this; }
149  bool operator==(const TYPE& val) const { return val == data; }
151  const BasicGrammar& grammar() const { return this->Property::grammar(); }
153  std::string str() const { return this->Property::str(); }
155  template <typename T> T value() const { return this->Property::value<T>();}
157  template <typename T>
158  void value(TYPE& val) const { this->Property::value(val); }
160  template <typename T> void set(const T& val) { this->Property::set(val); }
161  };
162 
164 
172  public:
174  typedef std::map<std::string, Property> Properties;
175  protected:
178 
180  void verifyNonExistence(const std::string& name) const;
182  Properties::iterator verifyExistence(const std::string& name);
184  Properties::const_iterator verifyExistence(const std::string& name) const;
185 
186  public:
188  PropertyManager();
190  virtual ~PropertyManager();
192  size_t size() const;
194  bool exists(const std::string& name) const;
198  const Properties& properties() const { return m_properties; }
200  const Property& property(const std::string& name) const;
202  Property& property(const std::string& name);
204  Property& operator[](const std::string& name);
206  const Property& operator[](const std::string& name) const;
208  void add(const std::string& name, const Property& property);
210  template <typename T> void add(const std::string& name, T& value) {
211  add(name, Property(value));
212  }
214  template <typename FUNCTOR> void for_each(FUNCTOR& func) {
215  std::for_each(m_properties.begin(), m_properties.end(), func);
216  }
218  template <typename FUNCTOR> void for_each(const FUNCTOR& func) {
219  std::for_each(m_properties.begin(), m_properties.end(), func);
220  }
222  void adopt(const PropertyManager& copy);
224  void dump() const;
225  };
226 
228 
235  public:
237  virtual ~PropertyInterface() = default;
239  virtual PropertyManager& properties() = 0;
241  virtual const PropertyManager& properties() const = 0;
243  virtual bool hasProperty(const std::string& name) const = 0;
245  virtual Property& property(const std::string& name) = 0;
246  };
247 
249 
255  class PropertyConfigurable : virtual public PropertyInterface {
256  protected:
259 
260  public:
264  virtual ~PropertyConfigurable();
266  virtual PropertyManager& properties() override {
267  return m_properties;
268  }
270  virtual const PropertyManager& properties() const override {
271  return m_properties;
272  }
274  virtual bool hasProperty(const std::string& name) const override;
276  virtual Property& property(const std::string& name) override;
278  template <typename T> void declareProperty(const std::string& nam, T& val);
280  template <typename T> void declareProperty(const char* nam, T& val);
281  };
282 
284  template <typename T>
285  void PropertyConfigurable::declareProperty(const std::string& nam, T& val) {
286  m_properties.add(nam, val);
287  }
288 
290  template <typename T>
291  void PropertyConfigurable::declareProperty(const char* nam, T& val) {
292  m_properties.add(nam, val);
293  }
294 
295 } // End namespace dd4hep
296 #endif // DD4HEP_COMPONENTPROPERTIES_H
dd4hep::PropertyManager::verifyNonExistence
void verifyNonExistence(const std::string &name) const
Verify that this property does not exist (throw exception if the name was found)
Definition: ComponentProperties.cpp:108
dd4hep::Property::operator=
Property & operator=(const Property &p)=default
Assignment operator.
dd4hep::PropertyManager::for_each
void for_each(FUNCTOR &func)
Apply functor on properties.
Definition: ComponentProperties.h:214
dd4hep::PropertyConfigurable::declareProperty
void declareProperty(const std::string &nam, T &val)
Declare property.
Definition: ComponentProperties.h:285
dd4hep::PropertyValue::operator=
PropertyValue & operator=(const TYPE &val)
Assignment operator.
Definition: ComponentProperties.h:147
dd4hep::PropertyConfigurable::property
virtual Property & property(const std::string &name) override
Access single property.
Definition: ComponentProperties.cpp:180
dd4hep::Property
The property class to assign options to actions.
Definition: ComponentProperties.h:45
dd4hep::Property::type
std::string type() const
Property type name.
Definition: ComponentProperties.cpp:37
dd4hep::PropertyConfigurable::PropertyConfigurable
PropertyConfigurable()
Standard constructor.
Definition: ComponentProperties.cpp:167
dd4hep::PropertyValue::value
T value() const
Retrieve value with data conversion.
Definition: ComponentProperties.h:155
dd4hep::PropertyInterface::properties
virtual PropertyManager & properties()=0
Access to the properties of the object.
dd4hep::PropertyConfigurable
Property object as base class for all objects supporting properties.
Definition: ComponentProperties.h:255
dd4hep::Property::str
std::string str() const
Conversion to string value.
Definition: ComponentProperties.cpp:48
dd4hep::PropertyConfigurable::hasProperty
virtual bool hasProperty(const std::string &name) const override
Check property for existence.
Definition: ComponentProperties.cpp:175
dd4hep::PropertyValue::grammar
const BasicGrammar & grammar() const
Access grammar object.
Definition: ComponentProperties.h:151
dd4hep::PropertyManager::Properties
std::map< std::string, Property > Properties
Property array definition.
Definition: ComponentProperties.h:174
dd4hep::PropertyManager::exists
bool exists(const std::string &name) const
Check for existence.
Definition: ComponentProperties.cpp:102
dd4hep::PropertyValue
Concrete template instantiation of a combined property value pair.
Definition: ComponentProperties.h:137
dd4hep::PropertyManager::~PropertyManager
virtual ~PropertyManager()
Default destructor.
Definition: ComponentProperties.cpp:87
dd4hep::PropertyValue::set
void set(const T &val)
Set value of this property with data conversion.
Definition: ComponentProperties.h:160
dd4hep::PropertyManager::add
void add(const std::string &name, T &value)
Add a new property.
Definition: ComponentProperties.h:210
dd4hep::Property::Property
Property()=default
Default constructor.
dd4hep::PropertyValue::operator==
bool operator==(const TYPE &val) const
Equality operator.
Definition: ComponentProperties.h:149
dd4hep::Property::m_par
void * m_par
Pointer to the data location.
Definition: ComponentProperties.h:48
dd4hep::PropertyValue::operator=
PropertyValue & operator=(const PropertyValue &c)=default
Assignment operator.
dd4hep::BasicGrammar
Base class describing string evaluation to C++ objects using boost::spirit.
Definition: Grammar.h:55
dd4hep::PropertyManager::adopt
void adopt(const PropertyManager &copy)
Import properties of another instance.
Definition: ComponentProperties.cpp:97
dd4hep::PropertyManager::property
const Property & property(const std::string &name) const
Access property by name (CONST)
Definition: ComponentProperties.cpp:139
dd4hep::Property::ptr
void * ptr() const
Access void data pointer.
Definition: ComponentProperties.h:64
dd4hep::Property::m_hdl
const BasicGrammar * m_hdl
Reference to the grammar of this property (extended type description)
Definition: ComponentProperties.h:50
dd4hep::PropertyInterface
Property object as base class for all objects supporting properties.
Definition: ComponentProperties.h:234
dd4hep::PropertyManager::dump
void dump() const
Dump string values.
Definition: ComponentProperties.cpp:160
dd4hep::BasicGrammar::get
static const BasicGrammar & get(const std::type_info &info)
Access grammar by type info.
Definition: Grammar.cpp:183
dd4hep::PropertyConfigurable::properties
virtual const PropertyManager & properties() const override
Access to the properties of the object.
Definition: ComponentProperties.h:270
dd4hep::PropertyInterface::property
virtual Property & property(const std::string &name)=0
Access single property.
dd4hep::PropertyManager::for_each
void for_each(const FUNCTOR &func)
Apply functor on properties.
Definition: ComponentProperties.h:218
dd4hep::PropertyManager::operator[]
Property & operator[](const std::string &name)
Access property by name.
Definition: ComponentProperties.cpp:144
dd4hep::Property::Property
Property(const Property &p)=default
Copy constructor.
dd4hep::PropertyConfigurable::~PropertyConfigurable
virtual ~PropertyConfigurable()
Default destructor.
Definition: ComponentProperties.cpp:171
dd4hep::PropertyConfigurable::m_properties
PropertyManager m_properties
Property pool.
Definition: ComponentProperties.h:258
dd4hep::PropertyValue::str
std::string str() const
Conversion to string value.
Definition: ComponentProperties.h:153
dd4hep::PropertyValue::PropertyValue
PropertyValue()
Default constructor.
Definition: ComponentProperties.h:141
dd4hep::PropertyValue::value
void value(TYPE &val) const
Retrieve value from stack with data conversion (large values e.g. vectors etc.)
Definition: ComponentProperties.h:158
dd4hep::PropertyManager::add
void add(const std::string &name, const Property &property)
Add a new property.
Definition: ComponentProperties.cpp:154
dd4hep::Property::value
TYPE value() const
Retrieve value.
Definition: ComponentProperties.h:123
dd4hep::Property::set
void set(const TYPE &value)
Set value of this property.
Definition: ComponentProperties.h:99
dd4hep::PropertyConfigurable::properties
virtual PropertyManager & properties() override
Access to the properties of the object.
Definition: ComponentProperties.h:266
dd4hep::Property::grammar
const BasicGrammar & grammar() const
Access grammar object.
Definition: ComponentProperties.cpp:41
dd4hep::PropertyValue::data
TYPE data
Definition: ComponentProperties.h:139
dd4hep::PropertyManager
Manager to ease the handling of groups of properties.
Definition: ComponentProperties.h:171
dd4hep::PropertyManager::size
size_t size() const
Access total number of properties.
Definition: ComponentProperties.cpp:92
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::PropertyManager::verifyExistence
Properties::iterator verifyExistence(const std::string &name)
Verify that this property exists (throw exception if the name was not found)
Definition: ComponentProperties.cpp:126
dd4hep::PropertyInterface::properties
virtual const PropertyManager & properties() const =0
Access to the properties of the object.
dd4hep::PropertyManager::m_properties
Properties m_properties
Property array/map.
Definition: ComponentProperties.h:177
dd4hep::PropertyManager::properties
Properties & properties()
Access to the property container.
Definition: ComponentProperties.h:196
dd4hep::PropertyInterface::~PropertyInterface
virtual ~PropertyInterface()=default
Default destructor.
dd4hep::BasicGrammar::invalidConversion
static void invalidConversion(const std::type_info &from, const std::type_info &to)
Error callback on invalid conversion.
Definition: Grammar.cpp:237
dd4hep::detail::tools::copy
void copy(Alignment from, Alignment to)
Copy alignment object from source object.
Definition: AlignmentTools.cpp:43
dd4hep::PropertyValue::PropertyValue
PropertyValue(const PropertyValue &c)=default
Copy constructor.
dd4hep::PropertyManager::properties
const Properties & properties() const
Access to the property container.
Definition: ComponentProperties.h:198
dd4hep::PropertyManager::PropertyManager
PropertyManager()
Default constructor.
Definition: ComponentProperties.cpp:83
Grammar.h
dd4hep::PropertyInterface::hasProperty
virtual bool hasProperty(const std::string &name) const =0
Check property for existence.