DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
ComponentProperties.cpp
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 
14 // Framework include files
15 #include <DD4hep/Printout.h>
16 #include <DD4hep/Primitives.h>
17 #include <Parsers/Parsers.h>
19 
20 // C/C++ include files
21 #include <stdexcept>
22 #include <cstring>
23 
24 using namespace dd4hep;
25 
27 std::string Property::type(const Property& property) {
28  return type(property.grammar().type());
29 }
30 
32 std::string Property::type(const std::type_info& typ) {
33  return typeName(typ);
34 }
35 
37 std::string Property::type() const {
38  return Property::type(grammar().type());
39 }
40 
42  if ( m_hdl )
43  return *m_hdl;
44  throw std::runtime_error("Attempt to access property grammar from invalid object.");
45 }
46 
48 std::string Property::str() const {
49  if ( m_hdl && m_par ) {
50  return m_hdl->str(m_par);
51  }
52  throw std::runtime_error("Attempt to access property grammar from invalid object.");
53 }
54 
56 const Property& Property::str(const std::string& input) const {
57  if ( m_hdl && m_par ) {
58  m_hdl->fromString(m_par,input);
59  return *this;
60  }
61  throw std::runtime_error("Attempt to access property grammar from invalid object.");
62 }
63 
65 Property& Property::str(const std::string& input) {
66  if ( m_hdl && m_par ) {
67  m_hdl->fromString(m_par,input);
68  return *this;
69  }
70  throw std::runtime_error("Attempt to access property grammar from invalid object.");
71 }
72 
74 Property& Property::operator=(const char* val) {
75  if ( val ) {
76  this->set < std::string > (val);
77  return *this;
78  }
79  throw std::runtime_error("Attempt to set invalid string to property!");
80 }
81 
84 }
85 
88  m_properties.clear();
89 }
90 
92 size_t PropertyManager::size() const {
93  return m_properties.size();
94 }
95 
98  m_properties = copy.m_properties;
99 }
100 
102 bool PropertyManager::exists(const std::string& name) const {
103  Properties::const_iterator i = m_properties.find(name);
104  return i != m_properties.end();
105 }
106 
108 void PropertyManager::verifyNonExistence(const std::string& name) const {
109  Properties::const_iterator i = m_properties.find(name);
110  if (i == m_properties.end())
111  return;
112  throw std::runtime_error("The property:" + name + " already exists for this component.");
113 }
114 
116 PropertyManager::Properties::const_iterator
117 PropertyManager::verifyExistence(const std::string& name) const {
118  Properties::const_iterator i = m_properties.find(name);
119  if (i != m_properties.end())
120  return i;
121  throw std::runtime_error("PropertyManager: Unknown property:" + name);
122 }
123 
125 PropertyManager::Properties::iterator
126 PropertyManager::verifyExistence(const std::string& name) {
127  Properties::iterator i = m_properties.find(name);
128  if (i != m_properties.end())
129  return i;
130  throw std::runtime_error("PropertyManager: Unknown property:" + name);
131 }
132 
134 Property& PropertyManager::property(const std::string& name) {
135  return (*verifyExistence(name)).second;
136 }
137 
139 const Property& PropertyManager::property(const std::string& name) const {
140  return (*verifyExistence(name)).second;
141 }
142 
144 Property& PropertyManager::operator[](const std::string& name) {
145  return (*verifyExistence(name)).second;
146 }
147 
149 const Property& PropertyManager::operator[](const std::string& name) const {
150  return (*verifyExistence(name)).second;
151 }
152 
154 void PropertyManager::add(const std::string& name, const Property& prop) {
155  verifyNonExistence(name);
156  m_properties.emplace(name, prop);
157 }
158 
160 void PropertyManager::dump() const {
161  for (const auto& i : m_properties )
162  printout(ALWAYS, "PropertyManager", "Property %s = %s",
163  i.first.c_str(), i.second.str().c_str());
164 }
165 
168 }
169 
172 }
173 
175 bool PropertyConfigurable::hasProperty(const std::string& nam) const {
176  return m_properties.exists(nam);
177 }
178 
180 Property& PropertyConfigurable::property(const std::string& nam) {
181  return properties()[nam];
182 }
183 
184 #include <DD4hep/GrammarParsed.h>
185 namespace dd4hep {
186  namespace Parsers {
187  template <> int parse(Property& result, const std::string& input) {
188  result.str(input);
189  return 1;
190  }
191  template <> std::ostream& toStream(const Property& result, std::ostream& os) {
192  return os << result.str();
193  }
194  }
195  // Ensure the grammars are registered
196  template class Grammar<Property>;
197  static auto s_registry = GrammarRegistry::pre_note<Property>(1);
198 }
199 
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::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::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::Grammar
Concrete type dependent grammar definition.
Definition: Grammar.h:167
dd4hep::BasicGrammar::str
virtual std::string str(const void *ptr) const
Serialize an opaque value to a string.
Definition: Grammar.cpp:261
dd4hep::BasicGrammar::fromString
virtual bool fromString(void *ptr, const std::string &value) const
Set value from serialized string. On successful data conversion TRUE is returned.
Definition: Grammar.cpp:269
dd4hep::PropertyManager::exists
bool exists(const std::string &name) const
Check for existence.
Definition: ComponentProperties.cpp:102
dd4hep::PropertyManager::~PropertyManager
virtual ~PropertyManager()
Default destructor.
Definition: ComponentProperties.cpp:87
GrammarParsed.h
dd4hep::Property::m_par
void * m_par
Pointer to the data location.
Definition: ComponentProperties.h:51
dd4hep::BasicGrammar
Base class describing string evaluation to C++ objects using boost::spirit.
Definition: Grammar.h:56
ComponentProperties.h
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::m_hdl
const BasicGrammar * m_hdl
Reference to the grammar of this property (extended type description)
Definition: ComponentProperties.h:53
dd4hep::PropertyManager::dump
void dump() const
Dump string values.
Definition: ComponentProperties.cpp:160
dd4hep::Parsers::toStream
std::ostream & toStream(const Property &result, std::ostream &os)
Definition: ComponentProperties.cpp:191
dd4hep::Parsers::parse
int parse(Property &result, const std::string &input)
Definition: ComponentProperties.cpp:187
dd4hep::PropertyManager::operator[]
Property & operator[](const std::string &name)
Access property by name.
Definition: ComponentProperties.cpp:144
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::PropertyManager::add
void add(const std::string &name, const Property &property)
Add a new property.
Definition: ComponentProperties.cpp:154
dd4hep::BasicGrammar::type
virtual const std::type_info & type() const =0
Access to the type information.
Primitives.h
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::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::PropertyManager::m_properties
Properties m_properties
Property array/map.
Definition: ComponentProperties.h:180
dd4hep::detail::tools::copy
void copy(Alignment from, Alignment to)
Copy alignment object from source object.
Definition: AlignmentTools.cpp:43
dd4hep::PropertyManager::PropertyManager
PropertyManager()
Default constructor.
Definition: ComponentProperties.cpp:83
Printout.h