File indexing completed on 2025-07-05 08:52:14
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021 #ifndef DD4HEP_GRAMMAR_H
0022 #define DD4HEP_GRAMMAR_H
0023
0024
0025 #include <DD4hep/config.h>
0026 #include <DD4hep/Primitives.h>
0027
0028
0029 #include <string>
0030 #include <typeinfo>
0031
0032
0033 class TClass;
0034
0035
0036 namespace dd4hep {
0037
0038
0039 class GrammarRegistry;
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055 class BasicGrammar {
0056 friend class GrammarRegistry;
0057 public:
0058 typedef unsigned long long int key_type;
0059
0060 const std::string name;
0061
0062 const key_type hash_value = 0;
0063
0064 mutable TClass* root_class = 0;
0065
0066 mutable int root_data_type = -1;
0067
0068 mutable bool inited = false;
0069
0070
0071 struct specialization_t {
0072
0073 const Cast* cast = 0;
0074
0075 void (*bind)(void* pointer) = 0;
0076
0077 void (*copy)(void* to, const void* from) = 0;
0078
0079 std::string (*str)(const BasicGrammar& gr, const void* ptr) = 0;
0080
0081 bool (*fromString)(const BasicGrammar& gr, void* ptr, const std::string& value) = 0;
0082
0083 int (*eval)(const BasicGrammar& gr, void* ptr, const std::string& val) = 0;
0084
0085 specialization_t() = default;
0086
0087 specialization_t(specialization_t&& copy) = default;
0088
0089 specialization_t(const specialization_t& copy) = default;
0090
0091 specialization_t& operator=(specialization_t&& copy) = default;
0092
0093 specialization_t& operator=(const specialization_t& copy) = default;
0094
0095 bool operator==(const specialization_t& copy) const;
0096 } specialization;
0097
0098 protected:
0099
0100 BasicGrammar(const std::string& typ);
0101
0102
0103 virtual ~BasicGrammar();
0104
0105
0106 void initialize() const;
0107 int initialized_data_type() const;
0108 TClass* initialized_clazz() const;
0109
0110
0111
0112 static void pre_note(const std::type_info& info, const BasicGrammar& (*fcn)(), specialization_t specs);
0113
0114 public:
0115
0116 template <typename TYPE> static const BasicGrammar& instance();
0117
0118 static const BasicGrammar& get(const std::type_info& info);
0119
0120 static const BasicGrammar& get(unsigned long long int hash_code);
0121
0122 static void invalidConversion(const std::type_info& from, const std::type_info& to);
0123
0124 static void invalidConversion(const std::string& value, const std::type_info& to);
0125
0126
0127 key_type hash() const { return hash_value; }
0128
0129 const std::string& type_name() const { return name; }
0130
0131 int data_type() const {
0132 if ( inited ) return root_data_type;
0133 return initialized_data_type();
0134 }
0135
0136 TClass* clazz() const {
0137 if ( inited ) return root_class;
0138 return initialized_clazz();
0139 }
0140
0141 virtual void setCast(const Cast* cast) const;
0142
0143 virtual const Cast& cast() const;
0144
0145 virtual bool equals(const std::type_info& other_type) const = 0;
0146
0147 virtual const std::type_info& type() const = 0;
0148
0149 virtual size_t sizeOf() const = 0;
0150
0151 virtual void destruct(void* pointer) const = 0;
0152
0153 virtual std::string str(const void* ptr) const;
0154
0155 virtual bool fromString(void* ptr, const std::string& value) const;
0156
0157 virtual int evaluate(void* ptr, const std::string& value) const;
0158 };
0159
0160
0161
0162
0163
0164
0165
0166 template <typename TYPE> class Grammar : public BasicGrammar {
0167 private:
0168
0169 friend class BasicGrammar;
0170
0171 virtual ~Grammar();
0172
0173 Grammar();
0174
0175 public:
0176 typedef TYPE type_t;
0177
0178
0179
0180 virtual const std::type_info& type() const override;
0181
0182 virtual bool equals(const std::type_info& other_type) const override;
0183
0184 virtual size_t sizeOf() const override;
0185
0186 virtual void destruct(void* pointer) const override;
0187
0188 template <typename... Args> void construct(void* pointer, Args... args) const;
0189 };
0190
0191
0192 template <typename TYPE> Grammar<TYPE>::Grammar()
0193 : BasicGrammar(typeName(typeid(TYPE)))
0194 {
0195 }
0196
0197
0198 template <typename TYPE> Grammar<TYPE>::~Grammar() {
0199 }
0200
0201
0202 template <typename TYPE> const std::type_info& Grammar<TYPE>::type() const {
0203 return typeid(TYPE);
0204 }
0205
0206
0207 template <typename TYPE> bool Grammar<TYPE>::equals(const std::type_info& other_type) const {
0208 return other_type == typeid(TYPE);
0209 }
0210
0211
0212 template <typename TYPE> size_t Grammar<TYPE>::sizeOf() const {
0213 return sizeof(TYPE);
0214 }
0215
0216
0217 template <typename TYPE> void Grammar<TYPE>::destruct(void* pointer) const {
0218 TYPE* obj = (TYPE*)pointer;
0219 obj->~TYPE();
0220 }
0221
0222
0223 template <typename TYPE> template <typename... Args>
0224 void Grammar<TYPE>::construct(void* pointer, Args... args) const {
0225 new(pointer) TYPE(std::forward<Args>(args)...);
0226 }
0227
0228
0229
0230
0231
0232
0233
0234 class GrammarRegistry {
0235
0236 GrammarRegistry() = default;
0237
0238 template <typename T> static std::string str(const BasicGrammar&, const void*) { return ""; }
0239 public:
0240
0241 static const GrammarRegistry& instance();
0242
0243 template <typename T> static const GrammarRegistry& pre_note_specs(BasicGrammar::specialization_t specs) {
0244 void (Grammar<T>::*destruct)(void*) const = &Grammar<T>::destruct;
0245 if ( !destruct ) {
0246 BasicGrammar::invalidConversion("Grammar",typeid(T));
0247 }
0248 BasicGrammar::pre_note(typeid(T), BasicGrammar::instance<T>, specs);
0249 return instance();
0250 }
0251 template <typename T> static const GrammarRegistry& pre_note(int) {
0252 BasicGrammar::specialization_t spec;
0253 spec.bind = detail::constructObject<T>;
0254 spec.copy = detail::copyObject<T>;
0255 spec.str = GrammarRegistry::str<T>;
0256 return pre_note_specs<T>(spec);
0257 }
0258 template <typename T> static const GrammarRegistry& pre_note() {
0259 return pre_note_specs<T>({});
0260 }
0261 };
0262 }
0263 #endif