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.cpp
Go to the documentation of this file.
1 // $Id$
2 //==========================================================================
3 // AIDA Detector description implementation for LCD
4 //--------------------------------------------------------------------------
5 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
6 // All rights reserved.
7 //
8 // For the licensing terms see $DD4hepINSTALL/LICENSE.
9 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
10 //
11 // Author : M.Frank
12 //
13 //==========================================================================
14 
15 // Framework include files
16 #include "DD4hep/Printout.h"
17 #include "DD4hep/Primitives.h"
18 #include "DD4hep/BasicGrammar.h"
20 
21 // C/C++ include files
22 #include <stdexcept>
23 #include <cstring>
24 
25 using namespace std;
26 using namespace DD4hep;
27 
29 PropertyGrammar::PropertyGrammar(const BasicGrammar& g) : m_grammar(g) {
30 }
31 
34 }
35 
37 void PropertyGrammar::invalidConversion(const std::type_info& from, const std::type_info& to) {
39 }
40 
42 void PropertyGrammar::invalidConversion(const std::string& value, const std::type_info& to) {
44 }
45 
47 const std::type_info& PropertyGrammar::type() const {
48  return m_grammar.type();
49 }
50 
52 std::string PropertyGrammar::str(const void* ptr) const {
53  return m_grammar.str(ptr);
54 }
55 
57 bool PropertyGrammar::fromString(void* ptr, const std::string& value) const {
58  return m_grammar.fromString(ptr,value);
59 }
60 
62 string Property::type(const Property& property) {
63  return type(property.grammar().type());
64 }
65 
67 string Property::type(const type_info& typ) {
68  return typeName(typ);
69 }
70 
72 string Property::type() const {
73  return Property::type(grammar().type());
74 }
75 
77  if (m_hdl)
78  return *m_hdl;
79  throw runtime_error("Attempt to access property grammar from invalid object.");
80 }
81 
83 string Property::str() const {
84  if (m_hdl && m_par ) {
85  return m_hdl->str(m_par);
86  }
87  throw runtime_error("Attempt to access property grammar from invalid object.");
88 }
89 
91 Property& Property::str(const std::string& input) {
92  if (m_hdl && m_par ) {
93  m_hdl->fromString(m_par,input);
94  return *this;
95  }
96  throw runtime_error("Attempt to access property grammar from invalid object.");
97 }
98 
100 //Property& Property::operator=(const string& val) {
101 // this->set<string>(val);
102 // return *this;
103 //}
104 
106 Property& Property::operator=(const char* val) {
107  if (val) {
108  this->set < string > (val);
109  return *this;
110  }
111  throw runtime_error("Attempt to set invalid string to property!");
112 }
113 
116 }
117 
120  m_properties.clear();
121 }
122 
125  m_properties = copy.m_properties;
126 }
127 
129 bool PropertyManager::exists(const std::string& name) const {
130  Properties::const_iterator i = m_properties.find(name);
131  return i != m_properties.end();
132 }
133 
135 void PropertyManager::verifyNonExistence(const string& name) const {
136  Properties::const_iterator i = m_properties.find(name);
137  if (i == m_properties.end())
138  return;
139  throw runtime_error("The property:" + name + " already exists for this component.");
140 }
141 
143 PropertyManager::Properties::const_iterator PropertyManager::verifyExistence(const string& name) const {
144  Properties::const_iterator i = m_properties.find(name);
145  if (i != m_properties.end())
146  return i;
147  throw runtime_error("PropertyManager: Unknown property:" + name);
148 }
149 
151 PropertyManager::Properties::iterator PropertyManager::verifyExistence(const string& name) {
152  Properties::iterator i = m_properties.find(name);
153  if (i != m_properties.end())
154  return i;
155  throw runtime_error("PropertyManager: Unknown property:" + name);
156 }
157 
159 Property& PropertyManager::property(const string& name) {
160  return (*verifyExistence(name)).second;
161 }
162 
164 const Property& PropertyManager::property(const string& name) const {
165  return (*verifyExistence(name)).second;
166 }
167 
169 Property& PropertyManager::operator[](const string& name) {
170  return (*verifyExistence(name)).second;
171 }
172 
174 const Property& PropertyManager::operator[](const string& name) const {
175  return (*verifyExistence(name)).second;
176 }
177 
179 void PropertyManager::add(const string& name, const Property& prop) {
180  verifyNonExistence(name);
181  m_properties.insert(make_pair(name, prop));
182 }
183 
185 void PropertyManager::set(const string& component_name, PropertyConfigurator& cfg) {
186  for (Properties::iterator i = m_properties.begin(); i != m_properties.end(); ++i) {
187  Property& p = (*i).second;
188  cfg.set(p.grammar(), component_name, (*i).first, p.ptr());
189  }
190 }
191 
193 void PropertyManager::dump() const {
194  for (Properties::const_iterator i = m_properties.begin(); i != m_properties.end(); ++i) {
195  const Property& p = (*i).second;
196  printout(ALWAYS, "PropertyManager", "Property %s = %s", (*i).first.c_str(), p.str().c_str());
197  }
198 }
199 
202 }
203 
206 }
207 
209 bool PropertyConfigurable::hasProperty(const string& nam) const {
210  return m_properties.exists(nam);
211 }
212 
215  return properties()[nam];
216 }
217 
218 namespace DD4hep {
219  namespace Parsers {
220  int parse(Property& result, const std::string& input) {
221  result.str(input);
222  return 1;
223  }
224  }
225  namespace Utils {
226  std::ostream& toStream(const Property& result, std::ostream& os) {
227  return os << result.str();
228  }
229  }
230 }
231 
232 #include <vector>
233 #include <list>
234 #include <set>
235 #include <map>
236 
237 #include "DD4hep/BasicGrammar_inl.h"
240 
241 namespace DD4hep {
242 
243  //DD4HEP_DEFINE_PROPERTY_TYPE(Property);
244  template Property Property::value() const;
245  template void Property::value(Property& value) const;
246  template void Property::set(const Property& value);
247  template void Property::make(Property& value);
248 
249 
250  #if defined(DD4HEP_HAVE_ALL_PARSERS)
254 #endif // DD4HEP_HAVE_ALL_PARSERS
255 
261 
262  // STL objects
264 
265  typedef map<string, int> map_string_int;
266  DD4HEP_DEFINE_PROPERTY_TYPE(map_string_int);
267 
268  typedef map<string, string> map_string_string;
269  DD4HEP_DEFINE_PROPERTY_TYPE(map_string_string);
270 }
271 
272 #ifndef DD4HEP_PARSERS_NO_ROOT
273 #include "Math/Point3D.h"
274 #include "Math/Vector3D.h"
275 #include "Math/Vector4D.h"
276 
277 namespace DD4hep {
278  // ROOT::Math Object instances
279  DD4HEP_DEFINE_PROPERTY_TYPE(ROOT::Math::XYZPoint);
280  DD4HEP_DEFINE_PROPERTY_TYPE(ROOT::Math::XYZVector);
281  DD4HEP_DEFINE_PROPERTY_TYPE(ROOT::Math::PxPyPzEVector);
282 }
283 #endif
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.
virtual const std::type_info & type() const =0
Access to the type information.
void dump() const
Dump string values.
PropertyConfigurable()
Standard constructor.
virtual std::string str(const void *ptr) const =0
Serialize an opaque value to a string.
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.
const BasicGrammar & m_grammar
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 typeName(const std::type_info &type)
ABI information about type names.
Definition: Primitives.cpp:186
std::string str() const
Conversion to string value.
void copy(Alignment from, Alignment to)
Copy alignment object from source object.
static void invalidConversion(const std::type_info &from, const std::type_info &to)
Error callback on invalid conversion.
const Property & property(const std::string &name) const
Access property by name (CONST)
#define DD4HEP_DEFINE_PARSER_GRAMMAR_TYPE(x)
map< string, string > map_string_string
void set(const std::string &component_name, PropertyConfigurator &setup)
Bulk set of all properties.
Properties m_properties
Property array/map.
DD4HEP_DEFINE_PROPERTY_TYPE(map_string_int)
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.
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)
Property & operator=(const Property &p)=default
Assignment operator.
void verifyNonExistence(const std::string &name) const
Verify that this property does not exist (throw exception if the name was found)
virtual bool fromString(void *ptr, const std::string &value) const =0
Set value from serialized string. On successful data conversion TRUE is returned. ...
const PropertyGrammar & grammar() const
Access grammar object.
static const double g
Definition: DD4hepUnits.h:162
TYPE value() const
Retrieve value.
Heler class to configure properties.
bool exists(const std::string &name) const
Check for existence.
DD4HEP_DEFINE_PROPERTY_CONT(bool)
std::ostream & toStream(const Property &result, std::ostream &os)
int parse(Property &result, const std::string &input)
Property & property(const std::string &name)
Access single property.
PropertyManager m_properties
Property pool.
virtual ~PropertyConfigurable()
Default destructor.
virtual ~PropertyGrammar()
Default destructor.
map< string, int > map_string_int
void * m_par
Pointer to the data location.
void * ptr() const
Access void data pointer.
PropertyManager()
Default constructor.
int printout(PrintLevel severity, const char *src, const char *fmt,...)
Calls the display action with a given severity level.
Definition: Printout.cpp:111
virtual std::string str(const void *ptr) const
Serialize an opaque value to a string.
void set(const TYPE &value)
Set value of this property.
Manager to ease the handling of groups of properties.
PropertyManager & properties()
Access to the properties of the object.
DD4HEP_DEFINE_PROPERTY_U_CONT(int)