ATLAS Offline Software
Loading...
Searching...
No Matches
ReadMetaHandle.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
5#ifndef STOREGATE_READMETAHANDLE_H
6#define STOREGATE_READMETAHANDLE_H
7
11
15
16#include "GaudiKernel/DataHandle.h"
17#include "GaudiKernel/DataObjID.h"
18#include "GaudiKernel/EventContext.h"
19#include "GaudiKernel/ThreadLocalContext.h"
20
21#include <string>
22#include <stdexcept>
23
24namespace SG {
25
26 template <typename T>
28
29 public:
30 typedef T* pointer_type; // FIXME: better handling of
31 typedef const T* const_pointer_type; // qualified T type ?
32 typedef T& reference_type;
33 typedef const T& const_reference_type;
34
35 public:
38 , const EventContext& ctx);
39
40 ReadMetaHandle(SG::ReadMetaHandleKey<T>&& key) = delete; // Not allowed from a temporary.
42 const EventContext& ctx) = delete; // Not allowed from a temporary.
43
45
48
51
52 bool isValid();
53
54 private:
55
56 bool initMetaHandle();
57
59 const MetaCont<T>* m_cont {nullptr};
60 T* m_ent {nullptr};
61
63 };
64
65
66 //------------------------------------------------------------------------
67
68 template <typename T>
70 : ReadMetaHandle(key, Gaudi::Hive::currentContext())
71 {
72 }
73
74 //------------------------------------------------------------------------
75
76 template <typename T>
78 , const EventContext& ctx)
79 : m_sid( Atlas::getExtendedEventContext(ctx).proxy()->sourceID() )
80 , m_cont( key.getContainer() )
81 , m_hkey(key)
82 {
83 MsgStream msg(Athena::getMessageSvc(), "ReadMetaHandle");
84 if (ATH_UNLIKELY(!m_hkey.isInit())) {
85 msg << MSG::ERROR
86 << "ReadMetaHandleKey " << key.objKey() << " was not initialized"
87 << endmsg;
88 throw std::runtime_error("ReadMetaHandle: ReadMetaHandleKey was not initialized");
89 }
90
91 if (ATH_UNLIKELY(m_cont == 0)) {
92 // Try to retrieve it
93 MetaContBase* cb{nullptr};
94 if(m_hkey.getStore()->retrieve(cb, m_hkey.key()).isFailure()) {
95 msg << MSG::ERROR
96 << "can't retrieve " << m_hkey.fullKey()
97 << " via base class" << endmsg;
98 throw std::runtime_error("ReadCondHandle: ptr to CondCont<T> is zero");
99 }
100 else {
101 m_cont = dynamic_cast< MetaCont<T>* > (cb);
102 if(!m_cont) {
103 msg << MSG::ERROR
104 << "can't dcast MetaContBase to " << m_hkey.fullKey()
105 << endmsg;
106 throw std::runtime_error("ReadMetaHandle: ptr to MetaCont<T> is zero");
107 }
108 }
109 }
110 }
111
112 //------------------------------------------------------------------------
113
114 template <typename T>
115 bool
117 {
118
119 if (m_ent) return true;
120
121 // Initialize sid from dbKey found at ReadMetaHandleKey initialize
122// m_sid = m_hkey.dbKey(); ???? Do I need this ????
123
124 if ( ATH_UNLIKELY(! m_cont->find(m_sid, m_ent)) ) {
125 std::ostringstream ost;
126 m_cont->list(ost);
127 MsgStream msg(Athena::getMessageSvc(), "ReadMetaHandle");
128 msg << MSG::ERROR
129 << "ReadMetaHandle:: could not find current SourceID "
130 << m_sid << " for key " << m_hkey.objKey() << "\n"
131 << ost.str() << endmsg;
132 m_ent = nullptr;
133 return false;
134 }
135
136 return true;
137 }
138
139 //------------------------------------------------------------------------
140
141 template <typename T>
142 const T*
144 {
145
146 if (m_ent == 0) {
147 if (!initMetaHandle()) {
148 return nullptr;
149 }
150 }
151
152 return m_ent;
153 }
154
155 //------------------------------------------------------------------------
156
157 template <typename T>
158 const T*
160 {
161 if (sid == m_sid) {
162 return retrieve();
163 }
164
165 pointer_type obj(0);
166 if (! (m_cont->find(sid, obj) ) ) {
167 std::ostringstream ost;
168 m_cont->list(ost);
169 MsgStream msg(Athena::getMessageSvc(), "ReadMetaHandle");
170 msg << MSG::ERROR
171 << "ReadMetaHandle::retrieve() could not find SourceID "
172 << sid << " for key " << m_hkey.objKey() << "\n"
173 << ost.str()
174 << endmsg;
175 return nullptr;
176 }
177
178 return obj;
179 }
180
181 //---------------------------------------------------------------------------
182
183 template <typename T>
184 bool
186
187 return initMetaHandle();
188 }
189
190}
191
192#endif
193
#define endmsg
#define ATH_UNLIKELY(x)
Handle class for reading from StoreGate.
ReadMetaHandle(const SG::ReadMetaHandleKey< T > &key)
const_pointer_type operator*()
const SG::ReadMetaHandleKey< T > & m_hkey
ReadMetaHandle(SG::ReadMetaHandleKey< T > &&key)=delete
const T & const_reference_type
const T * const_pointer_type
ReadMetaHandle(SG::ReadMetaHandleKey< T > &&key, const EventContext &ctx)=delete
const MetaCont< T > * m_cont
const_pointer_type operator->()
const_pointer_type retrieve()
singleton-like access to IMessageSvc via open function and helper
IMessageSvc * getMessageSvc(bool quiet=false)
=============================================================================
Forward declaration.
MsgStream & msg
Definition testRead.cxx:32