ATLAS Offline Software
Loading...
Searching...
No Matches
TStore.icc
Go to the documentation of this file.
1// Dear emacs, this is -*- c++ -*-
2//
3// Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
4//
5#ifndef XAODROOTACCESS_TSTORE_ICC
6#define XAODROOTACCESS_TSTORE_ICC
7
8#include "AthContainers/normalizedTypeinfoName.h"
9
10// ROOT include(s):
11#include <TError.h>
12
13// EDM include(s):
14#include "AthContainers/normalizedTypeinfoName.h"
15
16// Local include(s):
17#include "xAODRootAccess/tools/TDestructorRegistry.h"
18#include "xAODRootAccess/tools/TCDVHolderT.h"
19
20namespace xAOD {
21
22 template< typename T >
23 ::Bool_t TStore::contains( const std::string& key ) const {
24
25 // Delegate the call:
26 return contains( key, typeid( T ) );
27 }
28
29 template< typename T >
30 ::Bool_t TStore::isConst( const std::string& key ) const {
31
32 // Delegate the call:
33 return isConst( key, typeid( T ) );
34 }
35
36 template< typename T >
37 StatusCode TStore::retrieve( const T*& obj, const std::string& key ) const {
38
39 // Try to find the object:
40 const void* ptr = getConstObject( key, typeid( T ) );
41 if( ! ptr ) {
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;
47 }
48
49 // If we were successful:
50 obj = reinterpret_cast< const T* >( ptr );
51 return StatusCode::SUCCESS;
52 }
53
54 template< typename T >
55 StatusCode TStore::retrieve( T*& obj, const std::string& key ) const {
56
57 // Try to find the object:
58 void* ptr = getObject( key, typeid( T ) );
59 if( ! ptr ) {
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;
65 }
66
67 // If we were successful:
68 obj = reinterpret_cast< T* >( ptr );
69 return StatusCode::SUCCESS;
70 }
71
72 template< typename T >
73 StatusCode TStore::record_impl( T* obj, const std::string& key,
74 ::Bool_t isOwner, ::Bool_t isConst ) {
75
76 // Check if it's possible to record the object with a dictionary:
77 StatusCode result = record( obj, key,
78 SG::normalizedTypeinfoName( typeid( T ) ),
79 isOwner, isConst );
80
81 // If it's a success or a failure, let's stop here. Only go on for a
82 // recoverable error:
83 if( ! result.isRecoverable() ) {
84 return result;
85 }
86
87 // Apparently we don't have a dictionary for this type. Let's fall back
88 // to a slightly dumber implementation then...
89
90 // Make sure that we'll be able to destruct this type later on:
91 TDestructorRegistry::instance().add< T >();
92
93 // Leave it to the non-template function to record the object:
94 return record( obj, key, typeid( T ), isOwner, isConst );
95 }
96
97 template< typename T >
98 StatusCode TStore::record( T* obj, const std::string& key ) {
99
100 return record_impl( obj, key, /*isOwner*/kTRUE, /*isConst*/kFALSE );
101 }
102
103
104 template< typename T >
105 StatusCode TStore::record( std::unique_ptr< T > obj,
106 const std::string& key ) {
107
108 StatusCode result = record_impl( obj.get(), key,
109 /*isOwner*/kTRUE, /*isConst*/kFALSE );
110
111 // In case of success, give up ownership of the object:
112 if( result.isSuccess() ) {
113 (void)obj.release();
114 }
115 return result;
116 }
117
118 template< typename T >
119 StatusCode TStore::record( const T* obj, const std::string& key ) {
120
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 );
124 }
125
126 template< typename T >
127 StatusCode TStore::record( std::unique_ptr< const T > obj,
128 const std::string& key ) {
129
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 );
134
135 // In case of success, give up ownership of the object:
136 if( result.isSuccess() ) {
137 (void)obj.release();
138 }
139 return result;
140 }
141
142 template< class T >
143 StatusCode TStore::record( ConstDataVector< T >* obj, const std::string& key,
144 const std::type_info& ti,
145 ::Bool_t /*isOwner*/, ::Bool_t /*isConst*/ ) {
146
147 // Make sure that we'll be able to destruct this type later on:
148 TDestructorRegistry::instance().add< T >();
149
150 // We always store non-const pointers but check on retrieval:
151 THolder* hldr = new TCDVHolderT< T >( obj, ti );
152 return record( hldr, key );
153 }
154
155 template< typename T >
156 void TStore::keys( std::vector< std::string >& vkeys ) const {
157 getNames( SG::normalizedTypeinfoName( typeid( T ) ), vkeys );
158 }
159
160
161} // namespace xAOD
162
163#endif // XAODROOTACCESS_TSTORE_ICC