ATLAS Offline Software
StoreGate/StoreGate/WriteDecorHandleKey.icc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2002-2024 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 
17 namespace SG {
18 
19 
20 namespace detail {
21 
22 
23 /**
24  * @brief Optionally register read dependency of a @c WriteDecorHandleKey.
25  */
26 void 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  */
42 template <class T>
43 WriteDecorHandleKey<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  */
60 template <class T>
61 WriteDecorHandleKey<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  */
84 template <class T>
85 template <class OWNER,
86  typename /*= typename std::enable_if<std::is_base_of<IProperty, OWNER>::value>::type*/>
87 inline
88 WriteDecorHandleKey<T>::WriteDecorHandleKey( OWNER* owner,
89  const std::string& name,
90  const std::string& key /*={}*/,
91  const std::string& doc /*=""*/)
92  : Base (owner, name, key, doc),
93  m_contHandleKey (contKeyFromKey (key), StoreID::storeName(StoreID::EVENT_STORE) )
94 {
95 }
96 
97 
98 /**
99  * @brief auto-declaring Property Constructor.
100  * @param owner Owning component.
101  * @param name name of the Property
102  * @param contKey WriteHandleKey of the associated container
103  * @param decorKey name The decoration name.
104  * @param doc Documentation string.
105  *
106  * Will associate the named Property with this WDHK via declareProperty
107  * The container part of the decoration key will be taken from @c contKey,
108  * while @c decorKey gives the name of the decoration itself.
109  * If @c decorKey is blank, then the overall key will be blank.
110  */
111 template <class T>
112 template <class OWNER,
113  typename /*= typename std::enable_if<std::is_base_of<IProperty, OWNER>::value>::type*/>
114 inline
115 WriteDecorHandleKey<T>::WriteDecorHandleKey( OWNER* owner,
116  const std::string& name,
117  const VarHandleKey& contKey,
118  const std::string& decorKey /*={}*/,
119  const std::string& doc /*=""*/)
120  : Base (owner, name, makeContDecorKey (contKey, decorKey), doc),
121  m_contHandleKey (contKey.key(), StoreID::storeName(StoreID::EVENT_STORE)),
122  m_contKey(&contKey)
123 {
124 }
125 
126 
127 
128 
129 /**
130  * @brief Change the key of the object to which we're referring.
131  * @param sgkey The StoreGate key for the object.
132  *
133  * The provided key may actually start with the name of the store,
134  * separated by a "+": "MyStore+Obj". If no "+" is present,
135  * the store is not changed.
136  */
137 template <class T>
138 WriteDecorHandleKey<T>&
139 WriteDecorHandleKey<T>::operator= (const std::string& sgkey)
140 {
141  const std::string key = m_contKey ? makeContDecorKey(*m_contKey, sgkey) : sgkey;
142  m_contHandleKey = contKeyFromKey (key);
143  Base::operator= (key);
144  return *this;
145 }
146 
147 
148 /**
149  * @brief Change the key of the object to which we're referring.
150  * @param sgkey The StoreGate key for the object.
151  *
152  * The provided key may actually start with the name of the store,
153  * separated by a "+": "MyStore+Obj". If no "+" is present
154  * the store is not changed. A key name that starts with a "+"
155  * is interpreted as a hierarchical key name, not an empty store name.
156  *
157  * Returns failure the key string format is bad.
158  */
159 template <class T>
160 StatusCode WriteDecorHandleKey<T>::assign (const std::string& sgkey)
161 {
162  const std::string key = m_contKey ? makeContDecorKey(*m_contKey, sgkey) : sgkey;
163  if (m_contHandleKey.assign (contKeyFromKey (key)).isFailure())
164  return StatusCode::FAILURE;
165  return Base::assign (key);
166 }
167 
168 
169 /**
170  * @brief If this object is used as a property, then this should be called
171  * during the initialize phase. It will fail if the requested
172  * StoreGate service cannot be found or if the key is blank.
173  *
174  * @param used If false, then this handle is not to be used.
175  * Instead of normal initialization, the key will be cleared.
176  */
177 template <class T>
178 StatusCode WriteDecorHandleKey<T>::initialize (bool used /*= true*/)
179 {
180  // If a parent container is used, its key may have changed
181  if (m_contKey){
182  m_contHandleKey = m_contKey->key();
183  std::string decorKey=decorKeyFromKey(this->key());
184  VarHandleKey::operator=(makeContDecorKey (m_contKey->key(), decorKey));
185  }
186  if (!renounced()) {
187  detail::registerWriteDecorHandleKey (this->owner(), m_contHandleKey.fullKey());
188  }
189  if (m_contHandleKey.initialize (used).isFailure())
190  return StatusCode::FAILURE;
191  return Base::initialize (used);
192 }
193 
194 
195 /**
196  * @brief If this object is used as a property, then this should be called
197  * during the initialize phase. This variant will allow the key
198  * to be blank.
199  * @param Flag to select this variant. Call like
200  *@code
201  * ATH_CHECK( key.initialize (SG::AllowEmpty) );
202  @endcode
203 */
204 template <class T>
205 StatusCode WriteDecorHandleKey<T>::initialize (AllowEmptyEnum)
206 {
207  if (this->key().empty()) {
208  return StatusCode::SUCCESS;
209  }
210  return this->initialize (true);
211 }
212 
213 
214 /**
215  * @brief Python representation of Handle.
216  */
217 template <class T>
218 std::string WriteDecorHandleKey<T>::pythonRepr() const
219 {
220  std::string repr = Base::pythonRepr();
221  // With a parent container our representation becomes the decoration name only.
222  if (m_contKey) {
223  removeContFromDecorKey(*m_contKey, repr);
224  }
225  return repr;
226 }
227 
228 
229 /**
230  * @brief Return the handle key for the container.
231  */
232 template <class T>
233 const ReadHandleKey<T>& WriteDecorHandleKey<T>::contHandleKey() const
234 {
235  return m_contHandleKey;
236 }
237 
238 
239 /**
240  * @brief Return the handle key for the container.
241  */
242 template <class T>
243 ReadHandleKey<T>& WriteDecorHandleKey<T>::contHandleKey_nc()
244 {
245  return m_contHandleKey;
246 }
247 
248 
249 /**
250  * @brief Declare that this item does not participate in scheduling.
251  *
252  * If a WriteDataHandleKey is renounced, then we skip creating the
253  * output alias. Further, we skip the check that the element provided
254  * is a member of the declared container. A WriteDataHandle initialized
255  * from a renounced key will effectively behave like a simple Decorator.
256  */
257 template <class T>
258 inline
259 void WriteDecorHandleKey<T>::renounce()
260 {
261  m_renounced = true;
262 }
263 
264 
265 /**
266  * @brief Return the renounced flag.
267  */
268 template <class T>
269 inline
270 bool WriteDecorHandleKey<T>::renounced() const
271 {
272  return m_renounced;
273 }
274 
275 
276 } // namespace SG
277