ATLAS Offline Software
Loading...
Searching...
No Matches
StoreGate/StoreGate/WriteDecorHandleKey.icc
Go to the documentation of this file.
1/*
2 * Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration.
3 */
4/**
5 * @file StoreGate/WriteDecorHandleKey.icc
6 * @author scott snyder <snyder@bnl.gov>
7 * @date Mar, 2017
8 * @brief Property holding a SG store/key/clid/attr name from which a
9 * WriteDecorHandle is made.
10 */
11
12
13#include "StoreGate/DecorKeyHelpers.h"
14#include "GaudiKernel/IDataHandleHolder.h"
15
16
17namespace SG {
18
19
20namespace detail {
21
22
23/**
24 * @brief Optionally register read dependency of a @c WriteDecorHandleKey.
25 */
26void registerWriteDecorHandleKey (IDataHandleHolder* owner,
27 const DataObjID& contHandleKey);
28
29
30} // namespace detail
31
32
33/**
34 * @brief Constructor.
35 * @param key The StoreGate key for the object.
36 * @param storeName Name to use for the store, if it's not encoded in sgkey.
37 *
38 * The provided key may actually start with the name of the store,
39 * separated by a "+": "MyStore+Obj". If no "+" is present
40 * the store named by @c storeName is used.
41 */
42template <class T>
43WriteDecorHandleKey<T>::WriteDecorHandleKey (const std::string& key /*= ""*/,
44 const std::string& storeName /*= "StoreGateSvc"*/) :
45 Base (key, storeName),
46 m_contHandleKey (contKeyFromKey (key), storeName)
47{
48}
49
50
51/**
52 * @brief Constructor with associated container.
53 * @param contKey WriteHandleKey of the associated container
54 * @param decorKey The decoration name.
55 * @param storeName Name to use for the store.
56 *
57 * The decoration @decorKey will be applied on the container referenced
58 * by @contKey.
59 */
60template <class T>
61WriteDecorHandleKey<T>::WriteDecorHandleKey (const VarHandleKey& contKey,
62 const std::string& decorKey /*= ""*/,
63 const std::string& storeName /*= "StoreGateSvc"*/) :
64 Base (makeContDecorKey (contKey, decorKey), storeName),
65 m_contHandleKey (contKey.key(), storeName),
66 m_contKey(&contKey)
67{
68}
69
70
71/**
72 * @brief auto-declaring Property Constructor.
73 * @param owner Owning component.
74 * @param name name of the Property
75 * @param key default StoreGate key for the object.
76 * @param doc Documentation string.
77 *
78 * will associate the named Property with this key via declareProperty
79 *
80 * The provided key may actually start with the name of the store,
81 * separated by a "+": "MyStore+Obj". If no "+" is present
82 * the store named by @c storeName is used.
83 */
84template <class T>
85template <std::derived_from<IProperty> OWNER>
86inline
87WriteDecorHandleKey<T>::WriteDecorHandleKey( OWNER* owner,
88 const std::string& name,
89 const std::string& key /*={}*/,
90 const std::string& doc /*=""*/)
91 : Base (owner, name, key, doc),
92 m_contHandleKey (contKeyFromKey (key), StoreID::storeName(StoreID::EVENT_STORE) )
93{
94}
95
96
97/**
98 * @brief auto-declaring Property Constructor.
99 * @param owner Owning component.
100 * @param name name of the Property
101 * @param contKey WriteHandleKey of the associated container
102 * @param decorKey name The decoration name.
103 * @param doc Documentation string.
104 *
105 * Will associate the named Property with this WDHK via declareProperty
106 * The container part of the decoration key will be taken from @c contKey,
107 * while @c decorKey gives the name of the decoration itself.
108 * If @c decorKey is blank, then the overall key will be blank.
109 */
110template <class T>
111template <std::derived_from<IProperty> OWNER>
112inline
113WriteDecorHandleKey<T>::WriteDecorHandleKey( OWNER* owner,
114 const std::string& name,
115 const VarHandleKey& contKey,
116 const std::string& decorKey /*={}*/,
117 const std::string& doc /*=""*/)
118 : Base (owner, name, makeContDecorKey (contKey, decorKey), doc),
119 m_contHandleKey (contKey.key(), StoreID::storeName(StoreID::EVENT_STORE)),
120 m_contKey(&contKey)
121{
122}
123
124
125
126
127/**
128 * @brief Change the key of the object to which we're referring.
129 * @param sgkey The StoreGate key for the object.
130 *
131 * The provided key may actually start with the name of the store,
132 * separated by a "+": "MyStore+Obj". If no "+" is present,
133 * the store is not changed.
134 */
135template <class T>
136WriteDecorHandleKey<T>&
137WriteDecorHandleKey<T>::operator= (const std::string& sgkey)
138{
139 const std::string key = m_contKey ? makeContDecorKey(*m_contKey, sgkey) : sgkey;
140 m_contHandleKey = contKeyFromKey (key);
141 Base::operator= (key);
142 return *this;
143}
144
145
146/**
147 * @brief Change the key of the object to which we're referring.
148 * @param sgkey The StoreGate key for the object.
149 *
150 * The provided key may actually start with the name of the store,
151 * separated by a "+": "MyStore+Obj". If no "+" is present
152 * the store is not changed. A key name that starts with a "+"
153 * is interpreted as a hierarchical key name, not an empty store name.
154 *
155 * Returns failure if the key string format is bad.
156 */
157template <class T>
158StatusCode WriteDecorHandleKey<T>::assign (const std::string& sgkey)
159{
160 const std::string key = m_contKey ? makeContDecorKey(*m_contKey, sgkey) : sgkey;
161 if (m_contHandleKey.assign (contKeyFromKey (key)).isFailure())
162 return StatusCode::FAILURE;
163 return Base::assign (key);
164}
165
166
167/**
168 * @brief If this object is used as a property, then this should be called
169 * during the initialize phase. It will fail if the requested
170 * StoreGate service cannot be found or if the key is blank.
171 *
172 * @param used If false, then this handle is not to be used.
173 * Instead of normal initialization, the key will be cleared.
174 */
175template <class T>
176StatusCode WriteDecorHandleKey<T>::initialize (bool used /*= true*/)
177{
178 // If a parent container is used, its key may have changed
179 if (m_contKey){
180 m_contHandleKey = m_contKey->key();
181 const std::string decorKey = decorKeyFromKey(this->key(), this->key());
182 VarHandleKey::operator=(makeContDecorKey (m_contKey->key(), decorKey));
183 }
184 if (!renounced()) {
185 detail::registerWriteDecorHandleKey (this->owner(), m_contHandleKey.fullKey());
186 }
187 if (m_contHandleKey.initialize (used).isFailure())
188 return StatusCode::FAILURE;
189 return Base::initialize (used);
190}
191
192
193/**
194 * @brief If this object is used as a property, then this should be called
195 * during the initialize phase. This variant will allow the key
196 * to be blank.
197 * @param Flag to select this variant. Call like
198 *@code
199 * ATH_CHECK( key.initialize (SG::AllowEmpty) );
200 @endcode
201*/
202template <class T>
203StatusCode WriteDecorHandleKey<T>::initialize (AllowEmptyEnum)
204{
205 if (this->key().empty()) {
206 return StatusCode::SUCCESS;
207 }
208 return this->initialize (true);
209}
210
211
212/**
213 * @brief Python representation of Handle.
214 */
215template <class T>
216std::string WriteDecorHandleKey<T>::pythonRepr() const
217{
218 std::string repr = Base::pythonRepr();
219 // With a parent container our representation becomes the decoration name only.
220 if (m_contKey) {
221 removeContFromDecorKey(*m_contKey, repr);
222 }
223 return repr;
224}
225
226
227/**
228 * @brief Return the handle key for the container.
229 */
230template <class T>
231const ReadHandleKey<T>& WriteDecorHandleKey<T>::contHandleKey() const
232{
233 return m_contHandleKey;
234}
235
236
237/**
238 * @brief Return the handle key for the container.
239 */
240template <class T>
241ReadHandleKey<T>& WriteDecorHandleKey<T>::contHandleKey_nc()
242{
243 return m_contHandleKey;
244}
245
246
247/**
248 * @brief Declare that this item does not participate in scheduling.
249 *
250 * If a WriteDataHandleKey is renounced, then we skip creating the
251 * output alias. Further, we skip the check that the element provided
252 * is a member of the declared container. A WriteDataHandle initialized
253 * from a renounced key will effectively behave like a simple Decorator.
254 */
255template <class T>
256inline
257void WriteDecorHandleKey<T>::renounce()
258{
259 m_renounced = true;
260}
261
262
263/**
264 * @brief Return the renounced flag.
265 */
266template <class T>
267inline
268bool WriteDecorHandleKey<T>::renounced() const
269{
270 return m_renounced;
271}
272
273
274} // namespace SG
275