1 // Dear emacs, this is -*- c++ -*-
3 // Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
5 #ifndef XAODROOTACCESS_TSTORE_ICC
6 #define XAODROOTACCESS_TSTORE_ICC
8 #include "AthContainers/normalizedTypeinfoName.h"
14 #include "AthContainers/normalizedTypeinfoName.h"
17 #include "xAODRootAccess/tools/TDestructorRegistry.h"
18 #include "xAODRootAccess/tools/TCDVHolderT.h"
22 template< typename T >
23 ::Bool_t TStore::contains( const std::string& key ) const {
26 return contains( key, typeid( T ) );
29 template< typename T >
30 ::Bool_t TStore::isConst( const std::string& key ) const {
33 return isConst( key, typeid( T ) );
36 template< typename T >
37 StatusCode TStore::retrieve( const T*& obj, const std::string& key ) const {
39 // Try to find the object:
40 const void* ptr = getConstObject( key, typeid( T ) );
42 const std::string typeName = SG::normalizedTypeinfoName( typeid( T ) );
43 ::Warning( "xAOD::TStore::retrieve",
44 "Couldn't (const) retrieve \"%s/%s\"",
45 typeName.c_str(), key.c_str() );
46 return StatusCode::RECOVERABLE;
49 // If we were successful:
50 obj = reinterpret_cast< const T* >( ptr );
51 return StatusCode::SUCCESS;
54 template< typename T >
55 StatusCode TStore::retrieve( T*& obj, const std::string& key ) const {
57 // Try to find the object:
58 void* ptr = getObject( key, typeid( T ) );
60 const std::string typeName = SG::normalizedTypeinfoName( typeid( T ) );
61 ::Warning( "xAOD::TStore::retrieve",
62 "Couldn't (non-const) retrieve \"%s/%s\"",
63 typeName.c_str(), key.c_str() );
64 return StatusCode::RECOVERABLE;
67 // If we were successful:
68 obj = reinterpret_cast< T* >( ptr );
69 return StatusCode::SUCCESS;
72 template< typename T >
73 StatusCode TStore::record_impl( T* obj, const std::string& key,
74 ::Bool_t isOwner, ::Bool_t isConst ) {
76 // Check if it's possible to record the object with a dictionary:
77 StatusCode result = record( obj, key,
78 SG::normalizedTypeinfoName( typeid( T ) ),
81 // If it's a success or a failure, let's stop here. Only go on for a
83 if( ! result.isRecoverable() ) {
87 // Apparently we don't have a dictionary for this type. Let's fall back
88 // to a slightly dumber implementation then...
90 // Make sure that we'll be able to destruct this type later on:
91 TDestructorRegistry::instance().add< T >();
93 // Leave it to the non-template function to record the object:
94 return record( obj, key, typeid( T ), isOwner, isConst );
97 template< typename T >
98 StatusCode TStore::record( T* obj, const std::string& key ) {
100 return record_impl( obj, key, /*isOwner*/kTRUE, /*isConst*/kFALSE );
104 template< typename T >
105 StatusCode TStore::record( std::unique_ptr< T > obj,
106 const std::string& key ) {
108 StatusCode result = record_impl( obj.get(), key,
109 /*isOwner*/kTRUE, /*isConst*/kFALSE );
111 // In case of success, give up ownership of the object:
112 if( result.isSuccess() ) {
118 template< typename T >
119 StatusCode TStore::record( const T* obj, const std::string& key ) {
121 // We hold object as non-const pointers, but check on retrieval:
122 T* nc_obj ATLAS_THREAD_SAFE = const_cast<T*>(obj);
123 return record_impl( nc_obj, key, /*isOwner*/kTRUE, /*isConst*/kTRUE );
126 template< typename T >
127 StatusCode TStore::record( std::unique_ptr< const T > obj,
128 const std::string& key ) {
130 // We hold object as non-const pointers, but check on retrieval:
131 T* nc_obj ATLAS_THREAD_SAFE = const_cast<T*>(obj.get());
132 StatusCode result = record_impl( nc_obj, key,
133 /*isOwner*/kTRUE, /*isConst*/kTRUE );
135 // In case of success, give up ownership of the object:
136 if( result.isSuccess() ) {
143 StatusCode TStore::record( ConstDataVector< T >* obj, const std::string& key,
144 const std::type_info& ti,
145 ::Bool_t /*isOwner*/, ::Bool_t /*isConst*/ ) {
147 // We always store non-const pointers but check on retrieval:
148 THolder* hldr = new TCDVHolderT< T >( obj, ti );
149 return record( hldr, key );
152 template< typename T >
153 void TStore::keys( std::vector< std::string >& vkeys ) const {
154 getNames( SG::normalizedTypeinfoName( typeid( T ) ), vkeys );
160 #endif // XAODROOTACCESS_TSTORE_ICC