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
Handle.inl
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 // Framework include files
15 #include "DD4hep/Handle.h"
16 
18 namespace DD4hep {
19 
21  template <typename T> void Handle<T>::bad_assignment(const std::type_info& from, const std::type_info& to)
22  {
24  }
25 
27  template <typename T> void Handle<T>::assign(T* n, const std::string& nam, const std::string& tit) {
28  this->m_element = n;
29  if (!nam.empty())
30  n->SetName(nam.c_str());
31  if (!tit.empty())
32  n->SetTitle(tit.c_str());
33  }
34 
36  template <typename T> const char* Handle<T>::name() const {
37  return this->m_element ? this->m_element->GetName() : "";
38  }
39 
41  template <typename T> T* Handle<T>::access() const {
42  if ( this->m_element ) return this->m_element;
43  invalidHandleError(typeid(T));
44  return 0; // We have thrown an exception before - does not harm!
45  }
46 
47 } /* End namespace DD4hep */
48 
49 #if 0
50 #include <iostream>
51 
52  std::cout << "element:" << (void*)m_element << std::endl;\
53  std::cout << "type: " << (char*)(m_element ? typeName(typeid(*m_element)).c_str() : "---") << std::endl;\
54  std::cout << "type: " << (void*)(m_element ? &typeid(*m_element) : 0) << std::endl;\
55  std::cout << "target: " << typeName(typeid(X)) << std::endl;\
56  std::cout << "target: " << (void*)&typeid(X) << std::endl;\
57  std::cout << "cast: " << dynamic_cast<X*>((TObject*)m_element) << std::endl;\
58  std::cout << "cast: " << dynamic_cast<X*>(m_element) << std::endl;\
59 
60 #endif
61 
62 #define DD4HEP_INSTANTIATE_HANDLE(X) \
63  namespace DD4hep { \
64  template <> void Handle<X>::verifyObject() const { \
65  increment_object_validations(); \
66  if (m_element && dynamic_cast<X*>(m_element) == 0) { \
67  bad_assignment(typeid(*m_element), typeid(X)); \
68  } \
69  }} \
70  template class DD4hep::Handle<X>
71 
72 #define DD4HEP_INSTANTIATE_HANDLE_NAMED(X) \
73  namespace DD4hep { \
74  template <> const char* Handle<X>::name() const \
75  { return this->m_element ? this->m_element->name.c_str() : ""; } \
76  template <> void \
77  Handle<X>::assign(X* p, const std::string& n, const std::string& t){\
78  this->m_element = p; \
79  p->name = n; \
80  p->type = t; \
81  } \
82  template <> void Handle<X>::verifyObject() const { \
83  increment_object_validations(); \
84  if (m_element && dynamic_cast<X*>((NamedObject*)m_element) == 0) {\
85  bad_assignment(typeid(*m_element), typeid(X)); \
86  } \
87  }} \
88  template class DD4hep::Handle<X>
89 
90 #define DD4HEP_INSTANTIATE_HANDLE_UNNAMED(X) \
91  namespace DD4hep { \
92  template <> void \
93  Handle<X>::assign(X* n, const std::string&, const std::string&) \
94  { this->m_element = n;} \
95  template <> const char* Handle<X>::name() const { return ""; } \
96  } \
97  DD4HEP_INSTANTIATE_HANDLE(X)
const char * name() const
Access the object name (or "" if not supported by the object)
Definition: Handle.inl:36
std::string typeName(const std::type_info &type)
ABI information about type names.
Definition: Primitives.cpp:186
void invalidHandleAssignmentError(const std::type_info &from, const std::type_info &to)
Throw exception when handles are badly assigned.
Definition: Primitives.cpp:195
void assign(Implementation *n, const std::string &nam, const std::string &title)
Assign a new named object. Note: object references must be managed by the user.
Definition: Handle.inl:27
T * access() const
Checked object access. Throws invalid handle runtime exception.
Definition: Handle.inl:41
Class of the ROOT toolkit. See http://root.cern.ch/root/htmldoc/ClassIndex.html.
Definition: ROOTClasses.h:38
void invalidHandleError(const std::type_info &type)
Throw exception when handles are check for validity.
Definition: Primitives.cpp:190
static void bad_assignment(const std::type_info &from, const std::type_info &to)
Helper routine called when unrelated types are assigned.
Definition: Handle.inl:21