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
14
15#include "GaudiKernel/DataHandle.h"
16#include "GaudiKernel/DataObjID.h"
17#include "GaudiKernel/EventContext.h"
18#include "GaudiKernel/ThreadLocalContext.h"
19
20#include <string>
21#include <stdexcept>
22
23namespace SG {
24
25 template <typename T>
27
28 public:
29 typedef T* pointer_type; // FIXME: better handling of
30 typedef const T* const_pointer_type; // qualified T type ?
31 typedef T& reference_type;
32 typedef const T& const_reference_type;
33
34 public:
37 , const EventContext& ctx);
38
39 ReadMetaHandle(SG::ReadMetaHandleKey<T>&& key) = delete; // Not allowed from a temporary.
41 const EventContext& ctx) = delete; // Not allowed from a temporary.
42
44
47
50
51 bool isValid();
52
53 private:
54
55 bool initMetaHandle();
56
58 const MetaCont<T>* m_cont {nullptr};
59 T* m_ent {nullptr};
60
62 };
63
64
65 //------------------------------------------------------------------------
66
67 template <typename T>
69 : ReadMetaHandle(key, Gaudi::Hive::currentContext())
70 {
71 }
72
73 //------------------------------------------------------------------------
74
75 template <typename T>
77 , const EventContext& ctx)
78 : m_sid( Atlas::getExtendedEventContext(ctx).proxy()->sourceID() )
79 , m_cont( key.getContainer() )
80 , m_hkey(key)
81 {
82 MsgStream msg(Athena::getMessageSvc(), "ReadMetaHandle");
83 if (!m_hkey.isInit()) [[unlikely]] {
84 msg << MSG::ERROR
85 << "ReadMetaHandleKey " << key.objKey() << " was not initialized"
86 << endmsg;
87 throw std::runtime_error("ReadMetaHandle: ReadMetaHandleKey was not initialized");
88 }
89
90 if (m_cont == 0) [[unlikely]] {
91 // Try to retrieve it
92 MetaContBase* cb{nullptr};
93 if(m_hkey.getStore()->retrieve(cb, m_hkey.key()).isFailure()) {
94 msg << MSG::ERROR
95 << "can't retrieve " << m_hkey.fullKey()
96 << " via base class" << endmsg;
97 throw std::runtime_error("ReadCondHandle: ptr to CondCont<T> is zero");
98 }
99 else {
100 m_cont = dynamic_cast< MetaCont<T>* > (cb);
101 if(!m_cont) {
102 msg << MSG::ERROR
103 << "can't dcast MetaContBase to " << m_hkey.fullKey()
104 << endmsg;
105 throw std::runtime_error("ReadMetaHandle: ptr to MetaCont<T> is zero");
106 }
107 }
108 }
109 }
110
111 //------------------------------------------------------------------------
112
113 template <typename T>
114 bool
116 {
117
118 if (m_ent) return true;
119
120 // Initialize sid from dbKey found at ReadMetaHandleKey initialize
121// m_sid = m_hkey.dbKey(); ???? Do I need this ????
122
123 if (! m_cont->find(m_sid, m_ent)) [[unlikely]] {
124 std::ostringstream ost;
125 m_cont->list(ost);
126 MsgStream msg(Athena::getMessageSvc(), "ReadMetaHandle");
127 msg << MSG::ERROR
128 << "ReadMetaHandle:: could not find current SourceID "
129 << m_sid << " for key " << m_hkey.objKey() << "\n"
130 << ost.str() << endmsg;
131 m_ent = nullptr;
132 return false;
133 }
134
135 return true;
136 }
137
138 //------------------------------------------------------------------------
139
140 template <typename T>
141 const T*
143 {
144
145 if (m_ent == 0) {
146 if (!initMetaHandle()) {
147 return nullptr;
148 }
149 }
150
151 return m_ent;
152 }
153
154 //------------------------------------------------------------------------
155
156 template <typename T>
157 const T*
159 {
160 if (sid == m_sid) {
161 return retrieve();
162 }
163
164 pointer_type obj(0);
165 if (! (m_cont->find(sid, obj) ) ) {
166 std::ostringstream ost;
167 m_cont->list(ost);
168 MsgStream msg(Athena::getMessageSvc(), "ReadMetaHandle");
169 msg << MSG::ERROR
170 << "ReadMetaHandle::retrieve() could not find SourceID "
171 << sid << " for key " << m_hkey.objKey() << "\n"
172 << ost.str()
173 << endmsg;
174 return nullptr;
175 }
176
177 return obj;
178 }
179
180 //---------------------------------------------------------------------------
181
182 template <typename T>
183 bool
185
186 return initMetaHandle();
187 }
188
189}
190
191#endif
192
#define endmsg
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.
#define unlikely(x)
MsgStream & msg
Definition testRead.cxx:32