ATLAS Offline Software
TCDVHolderT.icc
Go to the documentation of this file.
1 // Dear emacs, this is -*- c++ -*-
2 
3 /*
4  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
5 */
6 
7 #ifndef XAODROOTACCESS_TOOLS_TCDVHOLDERT_ICC
8 #define XAODROOTACCESS_TOOLS_TCDVHOLDERT_ICC
9 
10 // ROOT include(s):
11 #include <TClass.h>
12 
13 namespace xAOD {
14 
15  template< class T >
16  TCDVHolderT< T >::TCDVHolderT( ConstDataVector< T >* object,
17  const std::type_info& type,
18  ::Bool_t owner )
19  : THolder( static_cast< const void* >( object ), type, owner ),
20  m_cdvObject( object ),
21  m_holderHelper( object->asDataVector(),
22  ::TClass::GetClass( typeid( typename Object_t::base_data_vector ) ),
23  kFALSE ) {
24 
25  // In case there's no dictionary for the underlying DataVector type
26  // (which should only happen for some tests), let's give a type ID for the
27  // helper object.
28  if( ! m_holderHelper.getClass() ) {
29  m_holderHelper = THolder( object->asDataVector(),
30  typeid( typename Object_t::base_data_vector ), kFALSE );
31  }
32 
33  // Set our type to the underlying DataVector type
34  m_type = m_holderHelper.getClass();
35  m_typeInfo = m_holderHelper.getTypeInfo();
36  }
37 
38  template< class T >
39  void TCDVHolderT< T >::set( void* obj ) {
40 
41  // Set our own pointer:
42  m_cdvObject = reinterpret_cast< ConstDataVector< T >* >( obj );
43 
44  // Update the helper object:
45  void* nc_dv ATLAS_THREAD_SAFE = // we hold non-const pointers but check on retrieve
46  const_cast< void* >( static_cast< const void* >( m_cdvObject->asDataVector() ) );
47 
48  m_holderHelper.set( nc_dv );
49 
50  // Finally, update the base class:
51  THolder::set( obj );
52 
53  return;
54  }
55 
56  template< class T >
57  void* TCDVHolderT< T >::getAs( const std::type_info& tid,
58  ::Bool_t silent ) const {
59 
60  // Non-const retrieval of ConstDataVector is never allowed:
61  if( ! silent ) {
62  const std::string heldType =
63  SG::normalizedTypeinfoName( typeid( Object_t ) );
64  const std::string reqType =
65  SG::normalizedTypeinfoName( tid );
66  ::Warning( "xAOD::TCDVHolder::getAs",
67  "Trying to retrieve %s object with a non-const %s pointer",
68  heldType.c_str(), reqType.c_str() );
69  }
70 
71  return nullptr;
72  }
73 
74  template< class T >
75  const void* TCDVHolderT< T >::getAsConst( const std::type_info& tid,
76  ::Bool_t silent ) const {
77 
78  // Check if the helper holder can handle the request:
79  const void* result = m_holderHelper.getAsConst( tid, kTRUE );
80  if( result ) {
81  // If yes, then we're done:
82  return result;
83  }
84 
85  // In case there was no dictionary for the base class, check whether the
86  // user is just asking for the base type:
87  if( tid == typeid( typename Object_t::base_data_vector ) ) {
88  return m_cdvObject->asDataVector();
89  }
90  // or the original type:
91  else if( tid == typeid( Object_t ) ) {
92  return m_cdvObject;
93  }
94 
95  // Otherwise rely on the base class to do its thing:
96  return THolder::getAsConst( tid, silent );
97  }
98 
99 } // namespace xAOD
100 
101 #endif // XAODROOTACCESS_TOOLS_TCDVHOLDERT_ICC