ATLAS Offline Software
Loading...
Searching...
No Matches
ReadMetaHandle.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 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 StoreGateSvc* ms = m_hkey.getStore();
94 MetaContBase* cb{nullptr};
95 if(ms->retrieve(cb, m_hkey.key()).isFailure()) {
96 msg << MSG::ERROR
97 << "can't retrieve " << m_hkey.fullKey()
98 << " via base class" << endmsg;
99 throw std::runtime_error("ReadCondHandle: ptr to CondCont<T> is zero");
100 }
101 else {
102 m_cont = dynamic_cast< MetaCont<T>* > (cb);
103 if(!m_cont) {
104 msg << MSG::ERROR
105 << "can't dcast MetaContBase to " << m_hkey.fullKey()
106 << endmsg;
107 throw std::runtime_error("ReadMetaHandle: ptr to MetaCont<T> is zero");
108 }
109 }
110 }
111 }
112
113 //------------------------------------------------------------------------
114
115 template <typename T>
116 bool
118 {
119
120 if (m_ent) return true;
121
122 // Initialize sid from dbKey found at ReadMetaHandleKey initialize
123// m_sid = m_hkey.dbKey(); ???? Do I need this ????
124
125 if ( ATH_UNLIKELY(! m_cont->find(m_sid, m_ent)) ) {
126 std::ostringstream ost;
127 m_cont->list(ost);
128 MsgStream msg(Athena::getMessageSvc(), "ReadMetaHandle");
129 msg << MSG::ERROR
130 << "ReadMetaHandle:: could not find current SourceID "
131 << m_sid << " for key " << m_hkey.objKey() << "\n"
132 << ost.str() << endmsg;
133 m_ent = nullptr;
134 return false;
135 }
136
137 return true;
138 }
139
140 //------------------------------------------------------------------------
141
142 template <typename T>
143 const T*
145 {
146
147 if (m_ent == 0) {
148 if (!initMetaHandle()) {
149 return nullptr;
150 }
151 }
152
153 return m_ent;
154 }
155
156 //------------------------------------------------------------------------
157
158 template <typename T>
159 const T*
161 {
162 if (sid == m_sid) {
163 return retrieve();
164 }
165
166 pointer_type obj(0);
167 if (! (m_cont->find(sid, obj) ) ) {
168 std::ostringstream ost;
169 m_cont->list(ost);
170 MsgStream msg(Athena::getMessageSvc(), "ReadMetaHandle");
171 msg << MSG::ERROR
172 << "ReadMetaHandle::retrieve() could not find SourceID "
173 << sid << " for key " << m_hkey.objKey() << "\n"
174 << ost.str()
175 << endmsg;
176 return nullptr;
177 }
178
179 return obj;
180 }
181
182 //---------------------------------------------------------------------------
183
184 template <typename T>
185 bool
187
188 return initMetaHandle();
189 }
190
191}
192
193#endif
194
#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()
The Athena Transient Store API.
singleton-like access to IMessageSvc via open function and helper
IMessageSvc * getMessageSvc(bool quiet=false)
=============================================================================
Forward declaration.
MsgStream & msg
Definition testRead.cxx:32