DD4hep  1.28.0
Detector Description Toolkit for High Energy Physics
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 <stdexcept>
22 #include <typeinfo>
23 #include <iostream>
24 #include <sstream>
25 #include <string>
26 #include <map>
27 
29 namespace dd4hep {
30 
31  class Property;
32  class BasicGrammar;
33 
35 
48  class Property {
49  protected:
51  void* m_par { nullptr };
53  const BasicGrammar* m_hdl { nullptr };
54 
55  public:
57  Property() = default;
59  Property(const Property& p) = default;
61  template <typename TYPE> Property(TYPE& val);
63  static std::string type(const Property& proptery);
65  static std::string type(const std::type_info& proptery);
67  void* ptr() const { return m_par; }
69  std::string type() const;
71  const BasicGrammar& grammar() const;
73  std::string str() const;
75  Property& str(const std::string& input);
77  const Property& str(const std::string& input) const;
79  Property& operator=(const Property& p) = default;
81  Property& operator=(const char* val);
83  //Property& operator=(const std::string& val);
85  template <typename TYPE> Property& operator=(const TYPE& val);
87  template <typename TYPE> TYPE value() const;
89  template <typename TYPE> void value(TYPE& value) const;
91  template <typename TYPE> void set(const TYPE& value);
92  };
93 
95  template <typename TYPE> Property::Property(TYPE& val)
96  : m_par(&val), m_hdl(0)
97  {
98  m_hdl = &BasicGrammar::get(typeid(TYPE));
99  }
100 
102  template <typename TYPE> void Property::set(const TYPE& val) {
103  const auto& grm = grammar();
104  if (grm.type() == typeid(TYPE))
105  *(TYPE*) m_par = val;
106  else if (!grm.fromString(m_par, BasicGrammar::instance< TYPE >().str(&val)))
107  BasicGrammar::invalidConversion(typeid(TYPE), grm.type());
108  }
109 
111  template <typename TYPE> Property& Property::operator=(const TYPE& val) {
112  this->set(val);
113  return *this;
114  }
115 
117  template <typename TYPE> void Property::value(TYPE& val) const {
118  const auto& grm = grammar();
119  if (grm.type() == typeid(TYPE))
120  val = *(TYPE*) m_par;
121  else if (!BasicGrammar::instance< TYPE >().fromString(&val, this->str()))
122  BasicGrammar::invalidConversion(grm.type(), typeid(TYPE));
123  }
124 
126  template <typename TYPE> TYPE Property::value() const {
127  TYPE temp;
128  this->value(temp);
129  return temp;
130  }
131 
133 
140  template <class TYPE> class PropertyValue : private Property {
141  public:
142  TYPE data {};
146  PropertyValue(const PropertyValue& c) = default;
148  PropertyValue& operator=(const PropertyValue& c) = default;
150  PropertyValue& operator=(const TYPE& val) { data = val; return *this; }
152  bool operator==(const TYPE& val) const { return val == data; }
154  const BasicGrammar& grammar() const { return this->Property::grammar(); }
156  std::string str() const { return this->Property::str(); }
158  template <typename T> T value() const { return this->Property::value<T>();}
160  template <typename T>
161  void value(TYPE& val) const { this->Property::value(val); }
163  template <typename T> void set(const T& val) { this->Property::set(val); }
164  };
165 
167 
175  public:
177  typedef std::map<std::string, Property> Properties;
178  protected:
181 
183  void verifyNonExistence(const std::string& name) const;
185  Properties::iterator verifyExistence(const std::string& name);
187  Properties::const_iterator verifyExistence(const std::string& name) const;
188 
189  public:
191  PropertyManager();
193  virtual ~PropertyManager();
195  size_t size() const;
197  bool exists(const std::string& name) const;
201  const Properties& properties() const { return m_properties; }
203  const Property& property(const std::string& name) const;
205  Property& property(const std::string& name);
207  Property& operator[](const std::string& name);
209  const Property& operator[](const std::string& name) const;
211  void add(const std::string& name, const Property& property);
213  template <typename T> void add(const std::string& name, T& value) {
214  add(name, Property(value));
215  }
217  template <typename FUNCTOR> void for_each(FUNCTOR& func) {
218  std::for_each(m_properties.begin(), m_properties.end(), func);
219  }
221  template <typename FUNCTOR> void for_each(const FUNCTOR& func) {
222  std::for_each(m_properties.begin(), m_properties.end(), func);
223  }
225  void adopt(const PropertyManager& copy);
227  void dump() const;
228  };
229 
231 
238  public:
240  virtual ~PropertyInterface() = default;
242  virtual PropertyManager& properties() = 0;
244  virtual const PropertyManager& properties() const = 0;
246  virtual bool hasProperty(const std::string& name) const = 0;
248  virtual Property& property(const std::string& name) = 0;
249  };
250 
252 
258  class PropertyConfigurable : virtual public PropertyInterface {
259  protected:
262 
263  public:
267  virtual ~PropertyConfigurable();
269  virtual PropertyManager& properties() override {
270  return m_properties;
271  }
273  virtual const PropertyManager& properties() const override {
274  return m_properties;
275  }
277  virtual bool hasProperty(const std::string& name) const override;
279  virtual Property& property(const std::string& name) override;
281  template <typename T> void declareProperty(const std::string& nam, T& val);
283  template <typename T> void declareProperty(const char* nam, T& val);
284  };
285 
287  template <typename T>
288  void PropertyConfigurable::declareProperty(const std::string& nam, T& val) {
289  m_properties.add(nam, val);
290  }
291 
293  template <typename T>
294  void PropertyConfigurable::declareProperty(const char* nam, T& val) {
295  m_properties.add(nam, val);
296  }
297 
298 } // End namespace dd4hep
299 #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:217
dd4hep::PropertyConfigurable::declareProperty
void declareProperty(const std::string &nam, T &val)
Declare property.
Definition: ComponentProperties.h:288
dd4hep::PropertyValue::operator=
PropertyValue & operator=(const TYPE &val)
Assignment operator.
Definition: ComponentProperties.h:150
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:48
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:158
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:258
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:154
dd4hep::PropertyManager::Properties
std::map< std::string, Property > Properties
Property array definition.
Definition: ComponentProperties.h:177
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:140
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:163
dd4hep::PropertyManager::add
void add(const std::string &name, T &value)
Add a new property.
Definition: ComponentProperties.h:213
dd4hep::Property::Property
Property()=default
Default constructor.
dd4hep::PropertyValue::operator==
bool operator==(const TYPE &val) const
Equality operator.
Definition: ComponentProperties.h:152
dd4hep::Property::m_par
void * m_par
Pointer to the data location.
Definition: ComponentProperties.h:51
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:56
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:67
dd4hep::Property::m_hdl
const BasicGrammar * m_hdl
Reference to the grammar of this property (extended type description)
Definition: ComponentProperties.h:53
dd4hep::PropertyInterface
Property object as base class for all objects supporting properties.
Definition: ComponentProperties.h:237
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:184
dd4hep::PropertyConfigurable::properties
virtual const PropertyManager & properties() const override
Access to the properties of the object.
Definition: ComponentProperties.h:273
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:221
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:261
dd4hep::PropertyValue::str
std::string str() const
Conversion to string value.
Definition: ComponentProperties.h:156
dd4hep::PropertyValue::PropertyValue
PropertyValue()
Default constructor.
Definition: ComponentProperties.h:144
dd4hep::PropertyValue::value
void value(TYPE &val) const
Retrieve value from stack with data conversion (large values e.g. vectors etc.)
Definition: ComponentProperties.h:161
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:126
dd4hep::Property::set
void set(const TYPE &value)
Set value of this property.
Definition: ComponentProperties.h:102
dd4hep::PropertyConfigurable::properties
virtual PropertyManager & properties() override
Access to the properties of the object.
Definition: ComponentProperties.h:269
dd4hep::Property::grammar
const BasicGrammar & grammar() const
Access grammar object.
Definition: ComponentProperties.cpp:41
dd4hep::PropertyValue::data
TYPE data
Definition: ComponentProperties.h:142
dd4hep::PropertyManager
Manager to ease the handling of groups of properties.
Definition: ComponentProperties.h:174
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:180
dd4hep::PropertyManager::properties
Properties & properties()
Access to the property container.
Definition: ComponentProperties.h:199
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:238
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:201
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.