DD4hep - The AIDA detector description toolkit for high energy physics experiments
DD4hep  Rev:Unversioneddirectory
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ComponentProperties.h
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation for LCD
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 
14 #ifndef DD4HEP_DDG4_COMPONENTPROPERTIES_H
15 #define DD4HEP_DDG4_COMPONENTPROPERTIES_H
16 
17 // Framework include files
18 
19 // C/C++ include files
20 #include <algorithm>
21 #include <stdexcept>
22 #include <typeinfo>
23 #include <string>
24 #include <map>
25 
27 namespace DD4hep {
28 
29  class Property;
30  class BasicGrammar;
31  class PropertyGrammar;
32 
34 
40  protected:
41  virtual ~PropertyConfigurator();
42  public:
43  virtual void set(const PropertyGrammar& setter, const std::string&, const std::string&, void* ptr) const = 0;
44  };
45 
46 
48 
54  protected:
55  friend class Property;
57  public:
61  virtual ~PropertyGrammar();
63  static void invalidConversion(const std::type_info& from, const std::type_info& to);
65  static void invalidConversion(const std::string& value, const std::type_info& to);
67  virtual const std::type_info& type() const;
69  virtual std::string str(const void* ptr) const;
71  virtual bool fromString(void* ptr, const std::string& value) const;
72  };
73 
75 
86  class Property {
87  protected:
89  void* m_par = 0;
91  const PropertyGrammar* m_hdl = 0;
92 
94  template <typename TYPE> void make(TYPE& value);
95  public:
97  Property() = default;
99  Property(const Property& p) = default;
101  template <typename TYPE> Property(TYPE& val) : m_par(0), m_hdl(0) {
102  make(val);
103  }
105  static std::string type(const Property& proptery);
107  static std::string type(const std::type_info& proptery);
109  void* ptr() const { return m_par; }
111  std::string type() const;
113  const PropertyGrammar& grammar() const;
115  std::string str() const;
117  Property& str(const std::string& input);
119  Property& operator=(const Property& p) = default;
121  Property& operator=(const char* val);
123  //Property& operator=(const std::string& val);
125  template <typename TYPE> Property& operator=(const TYPE& val);
127  template <typename TYPE> TYPE value() const;
129  template <typename TYPE> void value(TYPE& value) const;
131  template <typename TYPE> void set(const TYPE& value);
132  };
133 
135 
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 PropertyGrammar& 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 
173  public:
175  typedef std::map<std::string, Property> Properties;
176  protected:
179 
181  void verifyNonExistence(const std::string& name) const;
183  Properties::iterator verifyExistence(const std::string& name);
185  Properties::const_iterator verifyExistence(const std::string& name) const;
186 
187  public:
189  PropertyManager();
191  virtual ~PropertyManager();
193  bool exists(const std::string& name) const;
195  const Property& property(const std::string& name) const;
197  Property& property(const std::string& name);
199  Property& operator[](const std::string& name);
201  const Property& operator[](const std::string& name) const;
203  void add(const std::string& name, const Property& property);
205  template <typename T> void add(const std::string& name, T& value) {
206  add(name, Property(value));
207  }
209  void set(const std::string& component_name, PropertyConfigurator& setup);
211  template <typename FUNCTOR> void for_each(FUNCTOR& func) {
212  std::for_each(m_properties.begin(), m_properties.end(), func);
213  }
215  void adopt(const PropertyManager& copy);
217  void dump() const;
218  };
219 
221 
226  protected:
229 
230  public:
234  virtual ~PropertyConfigurable();
237  return m_properties;
238  }
240  bool hasProperty(const std::string& name) const;
242  Property& property(const std::string& name);
244  template <typename T> void declareProperty(const std::string& nam, T& val);
246  template <typename T> void declareProperty(const char* nam, T& val);
247  };
248 
250  template <typename T>
251  void PropertyConfigurable::declareProperty(const std::string& nam, T& val) {
252  m_properties.add(nam, val);
253  }
254 
256  template <typename T>
257  void PropertyConfigurable::declareProperty(const char* nam, T& val) {
258  m_properties.add(nam, val);
259  }
260 
261 } // End namespace DD4hep
262 #endif // DD4HEP_DDG4_COMPONENTPROPERTIES_H
void make(TYPE &value)
Setup property.
void add(const std::string &name, const Property &property)
Add a new property.
virtual const std::type_info & type() const
Access to the type information.
Property & operator[](const std::string &name)
Access property by name.
The property class to assign options to actions.
virtual ~PropertyManager()
Default destructor.
void value(TYPE &val) const
Retrieve value from stack with data conversion (large values e.g. vectors etc.)
const PropertyGrammar & grammar() const
Access grammar object.
void dump() const
Dump string values.
PropertyConfigurable()
Standard constructor.
virtual bool fromString(void *ptr, const std::string &value) const
Set value from serialized string. On successful data conversion TRUE is returned. ...
std::string type() const
Property type name.
Class describing the grammar representation of a given data type.
Property object as base class for all objects supporting properties.
Property(TYPE &val)
User constructor.
const BasicGrammar & m_grammar
std::string str() const
Conversion to string value.
void adopt(const PropertyManager &copy)
Export properties of another instance.
Base class describing string evaluation to C++ objects using boost::spirit.
Definition: BasicGrammar.h:37
std::string str() const
Conversion to string value.
Concrete template instantiation of a combined property value pair.
void copy(Alignment from, Alignment to)
Copy alignment object from source object.
PropertyValue()
Default constructor.
const Property & property(const std::string &name) const
Access property by name (CONST)
void set(const std::string &component_name, PropertyConfigurator &setup)
Bulk set of all properties.
Properties m_properties
Property array/map.
void declareProperty(const std::string &nam, T &val)
Declare property.
virtual void set(const PropertyGrammar &setter, const std::string &, const std::string &, void *ptr) const =0
bool hasProperty(const std::string &name) const
Check property for existence.
static void invalidConversion(const std::type_info &from, const std::type_info &to)
Error callback on invalid conversion.
void set(const T &val)
Set value of this property with data conversion.
const PropertyGrammar * m_hdl
Reference to the grammar of this property (extended type description)
Properties::iterator verifyExistence(const std::string &name)
Verify that this property exists (throw exception if the name was not found)
PropertyGrammar(const BasicGrammar &g)
Default constructor.
Property & operator=(const Property &p)=default
Assignment operator.
void for_each(FUNCTOR &func)
Apply functor on properties.
void verifyNonExistence(const std::string &name) const
Verify that this property does not exist (throw exception if the name was found)
PropertyValue & operator=(const PropertyValue &c)=default
Assignment operator.
const PropertyGrammar & grammar() const
Access grammar object.
static const double g
Definition: DD4hepUnits.h:162
T value() const
Retrieve value with data conversion.
TYPE value() const
Retrieve value.
Heler class to configure properties.
bool exists(const std::string &name) const
Check for existence.
bool operator==(const TYPE &val) const
Equality operator.
Property & property(const std::string &name)
Access single property.
std::map< std::string, Property > Properties
Property array definition.
PropertyManager m_properties
Property pool.
virtual ~PropertyConfigurable()
Default destructor.
virtual ~PropertyGrammar()
Default destructor.
void * m_par
Pointer to the data location.
void * ptr() const
Access void data pointer.
PropertyManager()
Default constructor.
virtual std::string str(const void *ptr) const
Serialize an opaque value to a string.
PropertyValue & operator=(const TYPE &val)
Assignment operator.
void set(const TYPE &value)
Set value of this property.
Property()=default
Default constructor.
Manager to ease the handling of groups of properties.
void add(const std::string &name, T &value)
Add a new property.
PropertyManager & properties()
Access to the properties of the object.