DD4hep  1.31.0
Detector Description Toolkit for High Energy Physics
Grammar.h
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 // NOTE:
15 //
16 // This is an internal include file. It should only be included to
17 // instantiate code. Otherwise the BasicGrammar include file should be
18 // sufficient for all practical purposes.
19 //
20 //==========================================================================
21 #ifndef DD4HEP_GRAMMAR_H
22 #define DD4HEP_GRAMMAR_H
23 
24 // Framework include files
25 #include <DD4hep/config.h>
26 #include <DD4hep/Primitives.h>
27 
28 // C/C++ include files
29 #include <string>
30 #include <typeinfo>
31 
32 // Forward declarations
33 class TClass;
34 
36 namespace dd4hep {
37 
39  class GrammarRegistry;
40 
42 
55  class BasicGrammar {
56  friend class GrammarRegistry;
57  public:
58  typedef unsigned long long int key_type;
60  const std::string name;
62  const key_type hash_value = 0;
64  mutable TClass* root_class = 0;
66  mutable int root_data_type = -1;
68  mutable bool inited = false;
69 
73  const Cast* cast = 0;
75  void (*bind)(void* pointer) = 0;
77  void (*copy)(void* to, const void* from) = 0;
79  std::string (*str)(const BasicGrammar& gr, const void* ptr) = 0;
81  bool (*fromString)(const BasicGrammar& gr, void* ptr, const std::string& value) = 0;
83  int (*eval)(const BasicGrammar& gr, void* ptr, const std::string& val) = 0;
85  specialization_t() = default;
95  bool operator==(const specialization_t& copy) const;
97 
98  protected:
100  BasicGrammar(const std::string& typ);
101 
103  virtual ~BasicGrammar();
104 
106  void initialize() const;
107  int initialized_data_type() const;
108  TClass* initialized_clazz() const;
109 
110 
112  static void pre_note(const std::type_info& info, const BasicGrammar& (*fcn)(), specialization_t specs);
113 
114  public:
116  template <typename TYPE> static const BasicGrammar& instance();
118  static const BasicGrammar& get(const std::type_info& info);
120  static const BasicGrammar& get(unsigned long long int hash_code);
122  static void invalidConversion(const std::type_info& from, const std::type_info& to);
124  static void invalidConversion(const std::string& value, const std::type_info& to);
125 
127  key_type hash() const { return hash_value; }
129  const std::string& type_name() const { return name; }
131  int data_type() const {
132  if ( inited ) return root_data_type;
133  return initialized_data_type();
134  }
136  TClass* clazz() const {
137  if ( inited ) return root_class;
138  return initialized_clazz();
139  }
141  virtual void setCast(const Cast* cast) const;
143  virtual const Cast& cast() const;
145  virtual bool equals(const std::type_info& other_type) const = 0;
147  virtual const std::type_info& type() const = 0;
149  virtual size_t sizeOf() const = 0;
151  virtual void destruct(void* pointer) const = 0;
153  virtual std::string str(const void* ptr) const;
155  virtual bool fromString(void* ptr, const std::string& value) const;
157  virtual int evaluate(void* ptr, const std::string& value) const;
158  };
159 
161 
166  template <typename TYPE> class Grammar : public BasicGrammar {
167  private:
168 
169  friend class BasicGrammar;
171  virtual ~Grammar();
173  Grammar();
174 
175  public:
176  typedef TYPE type_t;
177 
179  virtual const std::type_info& type() const override;
182  virtual bool equals(const std::type_info& other_type) const override;
184  virtual size_t sizeOf() const override;
186  virtual void destruct(void* pointer) const override;
188  template <typename... Args> void construct(void* pointer, Args... args) const;
189  };
190 
192  template <typename TYPE> Grammar<TYPE>::Grammar()
193  : BasicGrammar(typeName(typeid(TYPE)))
194  {
195  }
196 
198  template <typename TYPE> Grammar<TYPE>::~Grammar() {
199  }
200 
202  template <typename TYPE> const std::type_info& Grammar<TYPE>::type() const {
203  return typeid(TYPE);
204  }
205 
207  template <typename TYPE> bool Grammar<TYPE>::equals(const std::type_info& other_type) const {
208  return other_type == typeid(TYPE);
209  }
210 
212  template <typename TYPE> size_t Grammar<TYPE>::sizeOf() const {
213  return sizeof(TYPE);
214  }
215 
217  template <typename TYPE> void Grammar<TYPE>::destruct(void* pointer) const {
218  TYPE* obj = (TYPE*)pointer;
219  obj->~TYPE();
220  }
221 
223  template <typename TYPE> template <typename... Args>
224  void Grammar<TYPE>::construct(void* pointer, Args... args) const {
225  new(pointer) TYPE(std::forward<Args>(args)...);
226  }
227 
229 
236  GrammarRegistry() = default;
238  template <typename T> static std::string str(const BasicGrammar&, const void*) { return ""; }
239  public:
241  static const GrammarRegistry& instance();
242 
243  template <typename T> static const GrammarRegistry& pre_note_specs(BasicGrammar::specialization_t specs) {
244  void (Grammar<T>::*destruct)(void*) const = &Grammar<T>::destruct;
245  if ( !destruct ) {
246  BasicGrammar::invalidConversion("Grammar",typeid(T));
247  }
248  BasicGrammar::pre_note(typeid(T), BasicGrammar::instance<T>, specs);
249  return instance();
250  }
251  template <typename T> static const GrammarRegistry& pre_note(int) {
253  spec.bind = detail::constructObject<T>;
254  spec.copy = detail::copyObject<T>;
255  spec.str = GrammarRegistry::str<T>;
256  return pre_note_specs<T>(spec);
257  }
258  template <typename T> static const GrammarRegistry& pre_note() {
259  return pre_note_specs<T>({});
260  }
261  };
262 }
263 #endif // DD4HEP_GRAMMAR_H
dd4hep::BasicGrammar::pre_note
static void pre_note(const std::type_info &info, const BasicGrammar &(*fcn)(), specialization_t specs)
Instance factory.
Definition: Grammar.cpp:118
dd4hep::BasicGrammar::name
const std::string name
Instance type name.
Definition: Grammar.h:60
dd4hep::BasicGrammar::key_type
unsigned long long int key_type
Definition: Grammar.h:58
dd4hep::BasicGrammar::hash
key_type hash() const
Access the hash value for this grammar type.
Definition: Grammar.h:127
dd4hep::BasicGrammar::evaluate
virtual int evaluate(void *ptr, const std::string &value) const
Evaluate string value if possible before calling boost::spirit.
Definition: Grammar.cpp:277
dd4hep::BasicGrammar::sizeOf
virtual size_t sizeOf() const =0
Access the object size (sizeof operator)
dd4hep::BasicGrammar::equals
virtual bool equals(const std::type_info &other_type) const =0
Access to the type information.
dd4hep::GrammarRegistry::pre_note
static const GrammarRegistry & pre_note()
Definition: Grammar.h:258
config.h
dd4hep::info
std::size_t info(const std::string &src, const std::string &msg)
Definition: RootDictionary.h:65
dd4hep::BasicGrammar::specialization_t::operator==
bool operator==(const specialization_t &copy) const
Equality operator.
Definition: Grammar.cpp:94
dd4hep::Grammar::equals
virtual bool equals(const std::type_info &other_type) const override
Access to the type information.
Definition: Grammar.h:207
dd4hep::BasicGrammar::hash_value
const key_type hash_value
Instance hash code.
Definition: Grammar.h:62
dd4hep::BasicGrammar::initialized_clazz
TClass * initialized_clazz() const
Access the ROOT class for complex objects.
Definition: Grammar.cpp:223
dd4hep::BasicGrammar::BasicGrammar
BasicGrammar(const std::string &typ)
Default constructor.
Definition: Grammar.cpp:101
dd4hep::BasicGrammar::specialization_t::copy
void(* copy)(void *to, const void *from)=0
Opaque copy constructor.
Definition: Grammar.h:77
dd4hep::Grammar
Concrete type dependent grammar definition.
Definition: Grammar.h:166
dd4hep::BasicGrammar::str
virtual std::string str(const void *ptr) const
Serialize an opaque value to a string.
Definition: Grammar.cpp:260
dd4hep::Grammar::~Grammar
virtual ~Grammar()
Default destructor.
Definition: Grammar.h:198
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:268
dd4hep::Grammar::construct
void construct(void *pointer, Args... args) const
Bind opaque address to object.
Definition: Grammar.h:224
dd4hep::BasicGrammar::data_type
int data_type() const
Access ROOT data type for fundamentals.
Definition: Grammar.h:131
dd4hep::BasicGrammar::specialization_t::specialization_t
specialization_t(const specialization_t &copy)=default
Copy constructor.
dd4hep::BasicGrammar::specialization_t::specialization_t
specialization_t(specialization_t &&copy)=default
Move constructor.
dd4hep::BasicGrammar::type_name
const std::string & type_name() const
Access to the type information name.
Definition: Grammar.h:129
dd4hep::BasicGrammar::instance
static const BasicGrammar & instance()
Instance factory.
Definition: GrammarParsed.h:274
dd4hep::BasicGrammar::specialization_t::operator=
specialization_t & operator=(const specialization_t &copy)=default
Copy assignment.
dd4hep::BasicGrammar::specialization_t::fromString
bool(* fromString)(const BasicGrammar &gr, void *ptr, const std::string &value)=0
PropertyGrammar overload: Retrieve value from string.
Definition: Grammar.h:81
dd4hep::Grammar::sizeOf
virtual size_t sizeOf() const override
Access the object size (sizeof operator)
Definition: Grammar.h:212
dd4hep::BasicGrammar::root_class
TClass * root_class
Cached TClass reference for speed improvements.
Definition: Grammar.h:64
dd4hep::GrammarRegistry::str
static std::string str(const BasicGrammar &, const void *)
PropertyGrammar overload: Serialize a property to a string.
Definition: Grammar.h:238
dd4hep::BasicGrammar
Base class describing string evaluation to C++ objects using boost::spirit.
Definition: Grammar.h:55
dd4hep::GrammarRegistry
Grammar registry interface.
Definition: Grammar.h:234
dd4hep::detail::eval
Definition: RootDictionary.h:84
dd4hep::BasicGrammar::specialization_t::cast
const Cast * cast
Ponter to ABI Cast structure.
Definition: Grammar.h:73
dd4hep::BasicGrammar::get
static const BasicGrammar & get(const std::type_info &info)
Access grammar by type info.
Definition: Grammar.cpp:183
dd4hep::GrammarRegistry::pre_note
static const GrammarRegistry & pre_note(int)
Definition: Grammar.h:251
dd4hep::Grammar::type
virtual const std::type_info & type() const override
PropertyGrammar overload: Access to the type information.
Definition: Grammar.h:202
dd4hep::GrammarRegistry::GrammarRegistry
GrammarRegistry()=default
Default constructor.
dd4hep::BasicGrammar::specialization_t::bind
void(* bind)(void *pointer)=0
Bind opaque address to object.
Definition: Grammar.h:75
dd4hep::BasicGrammar::specialization_t::operator=
specialization_t & operator=(specialization_t &&copy)=default
Move assignment.
dd4hep::BasicGrammar::setCast
virtual void setCast(const Cast *cast) const
Set cast structure.
Definition: Grammar.cpp:246
dd4hep::BasicGrammar::specialization_t::str
std::string(* str)(const BasicGrammar &gr, const void *ptr)=0
PropertyGrammar overload: Serialize a property to a string.
Definition: Grammar.h:79
dd4hep::BasicGrammar::inited
bool inited
Initialization flag.
Definition: Grammar.h:68
dd4hep::BasicGrammar::specialization
struct dd4hep::BasicGrammar::specialization_t specialization
dd4hep::BasicGrammar::specialization_t::specialization_t
specialization_t()=default
Default constructor.
dd4hep::BasicGrammar::initialized_data_type
int initialized_data_type() const
Access ROOT data type for fundamentals.
Definition: Grammar.cpp:217
dd4hep::Grammar::destruct
virtual void destruct(void *pointer) const override
Opaque object destructor.
Definition: Grammar.h:217
dd4hep::BasicGrammar::type
virtual const std::type_info & type() const =0
Access to the type information.
dd4hep::BasicGrammar::clazz
TClass * clazz() const
Access the ROOT class for complex objects.
Definition: Grammar.h:136
Primitives.h
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::BasicGrammar::root_data_type
int root_data_type
Cached TDataType information for fundamental types.
Definition: Grammar.h:66
dd4hep::BasicGrammar::cast
virtual const Cast & cast() const
Access ABI object cast.
Definition: Grammar.cpp:252
dd4hep::BasicGrammar::initialize
void initialize() const
Second step initialization after the virtual table is fixed.
Definition: Grammar.cpp:197
dd4hep::BasicGrammar::specialization_t
Structure to be filled if automatic object parsing from string is supposed to be supported.
Definition: Grammar.h:71
dd4hep::BasicGrammar::destruct
virtual void destruct(void *pointer) const =0
Opaque object destructor.
dd4hep::BasicGrammar::invalidConversion
static void invalidConversion(const std::type_info &from, const std::type_info &to)
Error callback on invalid conversion.
Definition: Grammar.cpp:237
dd4hep::BasicGrammar::~BasicGrammar
virtual ~BasicGrammar()
Default destructor.
Definition: Grammar.cpp:114
dd4hep::Grammar::Grammar
Grammar()
Standard constructor.
Definition: Grammar.h:192
dd4hep::Grammar::type_t
TYPE type_t
Definition: Grammar.h:176
dd4hep::GrammarRegistry::instance
static const GrammarRegistry & instance()
Registry instance singleton.
Definition: Grammar.cpp:454
dd4hep::GrammarRegistry::pre_note_specs
static const GrammarRegistry & pre_note_specs(BasicGrammar::specialization_t specs)
Definition: Grammar.h:243