File indexing completed on 2025-07-03 08:34:16
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #ifndef DD4HEP_COMPONENTPROPERTIES_H
0014 #define DD4HEP_COMPONENTPROPERTIES_H
0015
0016
0017 #include <DD4hep/Grammar.h>
0018
0019
0020 #include <algorithm>
0021 #include <typeinfo>
0022 #include <string>
0023 #include <map>
0024
0025
0026 namespace dd4hep {
0027
0028 class Property;
0029 class BasicGrammar;
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045 class Property {
0046 protected:
0047
0048 void* m_par { nullptr };
0049
0050 const BasicGrammar* m_hdl { nullptr };
0051
0052 public:
0053
0054 Property() = default;
0055
0056 Property(const Property& p) = default;
0057
0058 template <typename TYPE> Property(TYPE& val);
0059
0060 static std::string type(const Property& proptery);
0061
0062 static std::string type(const std::type_info& proptery);
0063
0064 void* ptr() const { return m_par; }
0065
0066 std::string type() const;
0067
0068 const BasicGrammar& grammar() const;
0069
0070 std::string str() const;
0071
0072 Property& str(const std::string& input);
0073
0074 const Property& str(const std::string& input) const;
0075
0076 Property& operator=(const Property& p) = default;
0077
0078 Property& operator=(const char* val);
0079
0080
0081
0082 template <typename TYPE> Property& operator=(const TYPE& val);
0083
0084 template <typename TYPE> TYPE value() const;
0085
0086 template <typename TYPE> void value(TYPE& value) const;
0087
0088 template <typename TYPE> void set(const TYPE& value);
0089 };
0090
0091
0092 template <typename TYPE> Property::Property(TYPE& val)
0093 : m_par(&val), m_hdl(0)
0094 {
0095 m_hdl = &BasicGrammar::get(typeid(TYPE));
0096 }
0097
0098
0099 template <typename TYPE> void Property::set(const TYPE& val) {
0100 const auto& grm = grammar();
0101 if (grm.type() == typeid(TYPE))
0102 *(TYPE*) m_par = val;
0103 else if (!grm.fromString(m_par, BasicGrammar::instance< TYPE >().str(&val)))
0104 BasicGrammar::invalidConversion(typeid(TYPE), grm.type());
0105 }
0106
0107
0108 template <typename TYPE> Property& Property::operator=(const TYPE& val) {
0109 this->set(val);
0110 return *this;
0111 }
0112
0113
0114 template <typename TYPE> void Property::value(TYPE& val) const {
0115 const auto& grm = grammar();
0116 if (grm.type() == typeid(TYPE))
0117 val = *(TYPE*) m_par;
0118 else if (!BasicGrammar::instance< TYPE >().fromString(&val, this->str()))
0119 BasicGrammar::invalidConversion(grm.type(), typeid(TYPE));
0120 }
0121
0122
0123 template <typename TYPE> TYPE Property::value() const {
0124 TYPE temp;
0125 this->value(temp);
0126 return temp;
0127 }
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137 template <class TYPE> class PropertyValue : private Property {
0138 public:
0139 TYPE data {};
0140
0141 PropertyValue() : Property(data) {}
0142
0143 PropertyValue(const PropertyValue& c) = default;
0144
0145 PropertyValue& operator=(const PropertyValue& c) = default;
0146
0147 PropertyValue& operator=(const TYPE& val) { data = val; return *this; }
0148
0149 bool operator==(const TYPE& val) const { return val == data; }
0150
0151 const BasicGrammar& grammar() const { return this->Property::grammar(); }
0152
0153 std::string str() const { return this->Property::str(); }
0154
0155 template <typename T> T value() const { return this->Property::value<T>();}
0156
0157 template <typename T>
0158 void value(TYPE& val) const { this->Property::value(val); }
0159
0160 template <typename T> void set(const T& val) { this->Property::set(val); }
0161 };
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171 class PropertyManager {
0172 public:
0173
0174 typedef std::map<std::string, Property> Properties;
0175 protected:
0176
0177 Properties m_properties;
0178
0179
0180 void verifyNonExistence(const std::string& name) const;
0181
0182 Properties::iterator verifyExistence(const std::string& name);
0183
0184 Properties::const_iterator verifyExistence(const std::string& name) const;
0185
0186 public:
0187
0188 PropertyManager();
0189
0190 virtual ~PropertyManager();
0191
0192 size_t size() const;
0193
0194 bool exists(const std::string& name) const;
0195
0196 Properties& properties() { return m_properties; }
0197
0198 const Properties& properties() const { return m_properties; }
0199
0200 const Property& property(const std::string& name) const;
0201
0202 Property& property(const std::string& name);
0203
0204 Property& operator[](const std::string& name);
0205
0206 const Property& operator[](const std::string& name) const;
0207
0208 void add(const std::string& name, const Property& property);
0209
0210 template <typename T> void add(const std::string& name, T& value) {
0211 add(name, Property(value));
0212 }
0213
0214 template <typename FUNCTOR> void for_each(FUNCTOR& func) {
0215 std::for_each(m_properties.begin(), m_properties.end(), func);
0216 }
0217
0218 template <typename FUNCTOR> void for_each(const FUNCTOR& func) {
0219 std::for_each(m_properties.begin(), m_properties.end(), func);
0220 }
0221
0222 void adopt(const PropertyManager& copy);
0223
0224 void dump() const;
0225 };
0226
0227
0228
0229
0230
0231
0232
0233
0234 class PropertyInterface {
0235 public:
0236
0237 virtual ~PropertyInterface() = default;
0238
0239 virtual PropertyManager& properties() = 0;
0240
0241 virtual const PropertyManager& properties() const = 0;
0242
0243 virtual bool hasProperty(const std::string& name) const = 0;
0244
0245 virtual Property& property(const std::string& name) = 0;
0246 };
0247
0248
0249
0250
0251
0252
0253
0254
0255 class PropertyConfigurable : virtual public PropertyInterface {
0256 protected:
0257
0258 PropertyManager m_properties;
0259
0260 public:
0261
0262 PropertyConfigurable();
0263
0264 virtual ~PropertyConfigurable();
0265
0266 virtual PropertyManager& properties() override {
0267 return m_properties;
0268 }
0269
0270 virtual const PropertyManager& properties() const override {
0271 return m_properties;
0272 }
0273
0274 virtual bool hasProperty(const std::string& name) const override;
0275
0276 virtual Property& property(const std::string& name) override;
0277
0278 template <typename T> void declareProperty(const std::string& nam, T& val);
0279
0280 template <typename T> void declareProperty(const char* nam, T& val);
0281 };
0282
0283
0284 template <typename T>
0285 void PropertyConfigurable::declareProperty(const std::string& nam, T& val) {
0286 m_properties.add(nam, val);
0287 }
0288
0289
0290 template <typename T>
0291 void PropertyConfigurable::declareProperty(const char* nam, T& val) {
0292 m_properties.add(nam, val);
0293 }
0294
0295 }
0296 #endif