DD4hep - The AIDA detector description toolkit for high energy physics experiments
DD4hep  Rev:Unversioneddirectory
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OpaqueData_inl.h
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation for LCD
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 OpaqueData include file should be
18 // sufficient for all practical purposes.
19 //
20 //==========================================================================
21 #ifndef DD4HEP_OPAQUEDATA_INL_H
22 #define DD4HEP_OPAQUEDATA_INL_H
23 
24 // Framework include files
25 #include "DD4hep/Primitives.h"
26 #include "DD4hep/OpaqueData.h"
27 #include "DD4hep/BasicGrammar.h"
28 
30 namespace DD4hep {
31 
32  namespace {
33  template <typename T> static void opaqueCopyObject(void* t, const void* s) {
34  new(t) T(*(const T*)s);
35  }
36  template <typename T> static void opaqueDestructObject(void* p) {
37  T* t = (T*)p;
38  t->~T();
39  }
40  }
41 
43  template <typename T> T& OpaqueData::get() {
44  if (!grammar || (grammar->type() != typeid(T))) { throw std::bad_cast(); }
45  return *(T*)pointer;
46  }
47 
49  template <typename T> const T& OpaqueData::get() const {
50  if (!grammar || (grammar->type() != typeid(T))) { throw std::bad_cast(); }
51  return *(T*)pointer;
52  }
53 
55  template <typename T> T& OpaqueDataBlock::bind() {
56  this->bind(&BasicGrammar::instance<T>(),opaqueCopyObject<T>,opaqueDestructObject<T>);
57  return *(new(this->pointer) T());
58  }
59 
61  template <typename T> T& OpaqueDataBlock::bind(const std::string& value) {
62  T& ret = this->bind<T>();
63  if ( !value.empty() && !this->fromString(value) ) {
64  throw std::runtime_error("OpaqueDataBlock::set> Failed to bind type "+
65  typeName(typeid(T))+" to condition data block.");
66  }
67  return ret;
68  }
69 
71  template <typename T> T& OpaqueDataBlock::set(const std::string& value) {
72  T& ret = this->bind<T>();
73  if ( !value.empty() && !this->fromString(value) ) {
74  throw std::runtime_error("OpaqueDataBlock::set> Failed to bind type "+
75  typeName(typeid(T))+" to condition data block.");
76  }
77  return ret;
78  }
79 
80 } /* End namespace DD4hep */
81 #endif /* DD4HEP_OPAQUEDATA_INL_H */
virtual const std::type_info & type() const =0
Access to the type information.
T & set(const std::string &value)
Bind grammar and assign value.
std::string typeName(const std::type_info &type)
ABI information about type names.
Definition: Primitives.cpp:186
T & get()
Generic getter. Specify the exact type, not a polymorph type.
TGeoShape * s
Definition: Volumes.cpp:294
bool fromString(const std::string &rep)
Create data block from string representation.
Definition: OpaqueData.cpp:50
void * pointer
Pointer to object data.
Definition: OpaqueData.h:54
const BasicGrammar * grammar
Data type.
Definition: OpaqueData.h:50
T & bind()
Bind data value.