DD4hep  1.30.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 <iostream>
31 #include <typeinfo>
32 
33 // Forward declarations
34 class TClass;
35 
37 namespace dd4hep {
38 
40  class GrammarRegistry;
41 
43 
56  class BasicGrammar {
57  friend class GrammarRegistry;
58  public:
59  typedef unsigned long long int key_type;
61  const std::string name;
63  const key_type hash_value = 0;
65  mutable TClass* root_class = 0;
67  mutable int root_data_type = -1;
69  mutable bool inited = false;
70 
74  const Cast* cast = 0;
76  void (*bind)(void* pointer) = 0;
78  void (*copy)(void* to, const void* from) = 0;
80  std::string (*str)(const BasicGrammar& gr, const void* ptr) = 0;
82  bool (*fromString)(const BasicGrammar& gr, void* ptr, const std::string& value) = 0;
84  int (*eval)(const BasicGrammar& gr, void* ptr, const std::string& val) = 0;
86  specialization_t() = default;
96  bool operator==(const specialization_t& copy) const;
98 
99  protected:
101  BasicGrammar(const std::string& typ);
102 
104  virtual ~BasicGrammar();
105 
107  void initialize() const;
108  int initialized_data_type() const;
109  TClass* initialized_clazz() const;
110 
111 
113  static void pre_note(const std::type_info& info, const BasicGrammar& (*fcn)(), specialization_t specs);
114 
115  public:
117  template <typename TYPE> static const BasicGrammar& instance();
119  static const BasicGrammar& get(const std::type_info& info);
121  static const BasicGrammar& get(unsigned long long int hash_code);
123  static void invalidConversion(const std::type_info& from, const std::type_info& to);
125  static void invalidConversion(const std::string& value, const std::type_info& to);
126 
128  key_type hash() const { return hash_value; }
130  const std::string& type_name() const { return name; }
132  int data_type() const {
133  if ( inited ) return root_data_type;
134  return initialized_data_type();
135  }
137  TClass* clazz() const {
138  if ( inited ) return root_class;
139  return initialized_clazz();
140  }
142  virtual void setCast(const Cast* cast) const;
144  virtual const Cast& cast() const;
146  virtual bool equals(const std::type_info& other_type) const = 0;
148  virtual const std::type_info& type() const = 0;
150  virtual size_t sizeOf() const = 0;
152  virtual void destruct(void* pointer) const = 0;
154  virtual std::string str(const void* ptr) const;
156  virtual bool fromString(void* ptr, const std::string& value) const;
158  virtual int evaluate(void* ptr, const std::string& value) const;
159  };
160 
162 
167  template <typename TYPE> class Grammar : public BasicGrammar {
168  private:
169 
170  friend class BasicGrammar;
172  virtual ~Grammar();
174  Grammar();
175 
176  public:
177  typedef TYPE type_t;
178 
180  virtual const std::type_info& type() const override;
183  virtual bool equals(const std::type_info& other_type) const override;
185  virtual size_t sizeOf() const override;
187  virtual void destruct(void* pointer) const override;
189  template <typename... Args> void construct(void* pointer, Args... args) const;
190  };
191 
193  template <typename TYPE> Grammar<TYPE>::Grammar()
194  : BasicGrammar(typeName(typeid(TYPE)))
195  {
196  }
197 
199  template <typename TYPE> Grammar<TYPE>::~Grammar() {
200  }
201 
203  template <typename TYPE> const std::type_info& Grammar<TYPE>::type() const {
204  return typeid(TYPE);
205  }
206 
208  template <typename TYPE> bool Grammar<TYPE>::equals(const std::type_info& other_type) const {
209  return other_type == typeid(TYPE);
210  }
211 
213  template <typename TYPE> size_t Grammar<TYPE>::sizeOf() const {
214  return sizeof(TYPE);
215  }
216 
218  template <typename TYPE> void Grammar<TYPE>::destruct(void* pointer) const {
219  TYPE* obj = (TYPE*)pointer;
220  obj->~TYPE();
221  }
222 
224  template <typename TYPE> template <typename... Args>
225  void Grammar<TYPE>::construct(void* pointer, Args... args) const {
226  new(pointer) TYPE(std::forward<Args>(args)...);
227  }
228 
230 
237  GrammarRegistry() = default;
239  template <typename T> static std::string str(const BasicGrammar&, const void*) { return ""; }
240  public:
242  static const GrammarRegistry& instance();
243 
244  template <typename T> static const GrammarRegistry& pre_note_specs(BasicGrammar::specialization_t specs) {
245  void (Grammar<T>::*destruct)(void*) const = &Grammar<T>::destruct;
246  if ( !destruct ) {
247  BasicGrammar::invalidConversion("Grammar",typeid(T));
248  }
249  BasicGrammar::pre_note(typeid(T), BasicGrammar::instance<T>, specs);
250  return instance();
251  }
252  template <typename T> static const GrammarRegistry& pre_note(int) {
254  spec.bind = detail::constructObject<T>;
255  spec.copy = detail::copyObject<T>;
256  spec.str = GrammarRegistry::str<T>;
257  return pre_note_specs<T>(spec);
258  }
259  template <typename T> static const GrammarRegistry& pre_note() {
260  return pre_note_specs<T>({});
261  }
262  };
263 }
264 #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:119
dd4hep::BasicGrammar::name
const std::string name
Instance type name.
Definition: Grammar.h:61
dd4hep::BasicGrammar::key_type
unsigned long long int key_type
Definition: Grammar.h:59
dd4hep::BasicGrammar::hash
key_type hash() const
Access the hash value for this grammar type.
Definition: Grammar.h:128
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:278
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:259
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:95
dd4hep::Grammar::equals
virtual bool equals(const std::type_info &other_type) const override
Access to the type information.
Definition: Grammar.h:208
dd4hep::BasicGrammar::hash_value
const key_type hash_value
Instance hash code.
Definition: Grammar.h:63
dd4hep::BasicGrammar::initialized_clazz
TClass * initialized_clazz() const
Access the ROOT class for complex objects.
Definition: Grammar.cpp:224
dd4hep::BasicGrammar::BasicGrammar
BasicGrammar(const std::string &typ)
Default constructor.
Definition: Grammar.cpp:102
dd4hep::BasicGrammar::specialization_t::copy
void(* copy)(void *to, const void *from)=0
Opaque copy constructor.
Definition: Grammar.h:78
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::Grammar::~Grammar
virtual ~Grammar()
Default destructor.
Definition: Grammar.h:199
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::Grammar::construct
void construct(void *pointer, Args... args) const
Bind opaque address to object.
Definition: Grammar.h:225
dd4hep::BasicGrammar::data_type
int data_type() const
Access ROOT data type for fundamentals.
Definition: Grammar.h:132
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:130
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:82
dd4hep::Grammar::sizeOf
virtual size_t sizeOf() const override
Access the object size (sizeof operator)
Definition: Grammar.h:213
dd4hep::BasicGrammar::root_class
TClass * root_class
Cached TClass reference for speed improvements.
Definition: Grammar.h:65
dd4hep::GrammarRegistry::str
static std::string str(const BasicGrammar &, const void *)
PropertyGrammar overload: Serialize a property to a string.
Definition: Grammar.h:239
dd4hep::BasicGrammar
Base class describing string evaluation to C++ objects using boost::spirit.
Definition: Grammar.h:56
dd4hep::GrammarRegistry
Grammar registry interface.
Definition: Grammar.h:235
dd4hep::detail::eval
Definition: RootDictionary.h:84
dd4hep::BasicGrammar::specialization_t::cast
const Cast * cast
Ponter to ABI Cast structure.
Definition: Grammar.h:74
dd4hep::BasicGrammar::get
static const BasicGrammar & get(const std::type_info &info)
Access grammar by type info.
Definition: Grammar.cpp:184
dd4hep::GrammarRegistry::pre_note
static const GrammarRegistry & pre_note(int)
Definition: Grammar.h:252
dd4hep::Grammar::type
virtual const std::type_info & type() const override
PropertyGrammar overload: Access to the type information.
Definition: Grammar.h:203
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:76
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:247
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:80
dd4hep::BasicGrammar::inited
bool inited
Initialization flag.
Definition: Grammar.h:69
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:218
dd4hep::Grammar::destruct
virtual void destruct(void *pointer) const override
Opaque object destructor.
Definition: Grammar.h:218
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:137
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:67
dd4hep::BasicGrammar::cast
virtual const Cast & cast() const
Access ABI object cast.
Definition: Grammar.cpp:253
dd4hep::BasicGrammar::initialize
void initialize() const
Second step initialization after the virtual table is fixed.
Definition: Grammar.cpp:198
dd4hep::BasicGrammar::specialization_t
Structure to be filled if automatic object parsing from string is supposed to be supported.
Definition: Grammar.h:72
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:238
dd4hep::BasicGrammar::~BasicGrammar
virtual ~BasicGrammar()
Default destructor.
Definition: Grammar.cpp:115
dd4hep::Grammar::Grammar
Grammar()
Standard constructor.
Definition: Grammar.h:193
dd4hep::Grammar::type_t
TYPE type_t
Definition: Grammar.h:177
dd4hep::GrammarRegistry::instance
static const GrammarRegistry & instance()
Registry instance singleton.
Definition: Grammar.cpp:455
dd4hep::GrammarRegistry::pre_note_specs
static const GrammarRegistry & pre_note_specs(BasicGrammar::specialization_t specs)
Definition: Grammar.h:244