ATLAS Offline Software
StoreGate/StoreGate/WriteDecorHandleKey.icc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2002-2023 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  */
108 template <class T>
109 template <class OWNER,
110  typename /*= typename std::enable_if<std::is_base_of<IProperty, OWNER>::value>::type*/>
111 inline
112 WriteDecorHandleKey<T>::WriteDecorHandleKey( OWNER* owner,
113  const std::string& name,
114  const VarHandleKey& contKey,
115  const std::string& decorKey /*={}*/,
116  const std::string& doc /*=""*/)
117  : Base (owner, name, makeContDecorKey (contKey, decorKey), doc),
118  m_contHandleKey (contKey.key(), StoreID::storeName(StoreID::EVENT_STORE)),
119  m_contKey(&contKey)
120 {
121 }
122 
123 
124 
125 
126 /**
127  * @brief Change the key of the object to which we're referring.
128  * @param sgkey The StoreGate key for the object.
129  *
130  * The provided key may actually start with the name of the store,
131  * separated by a "+": "MyStore+Obj". If no "+" is present,
132  * the store is not changed.
133  */
134 template <class T>
135 WriteDecorHandleKey<T>&
136 WriteDecorHandleKey<T>::operator= (const std::string& sgkey)
137 {
138  const std::string key = m_contKey ? makeContDecorKey(*m_contKey, sgkey) : sgkey;
139  m_contHandleKey = contKeyFromKey (key);
140  Base::operator= (key);
141  return *this;
142 }
143 
144 
145 /**
146  * @brief Change the key of the object to which we're referring.
147  * @param sgkey The StoreGate key for the object.
148  *
149  * The provided key may actually start with the name of the store,
150  * separated by a "+": "MyStore+Obj". If no "+" is present
151  * the store is not changed. A key name that starts with a "+"
152  * is interpreted as a hierarchical key name, not an empty store name.
153  *
154  * Returns failure the key string format is bad.
155  */
156 template <class T>
157 StatusCode WriteDecorHandleKey<T>::assign (const std::string& sgkey)
158 {
159  const std::string key = m_contKey ? makeContDecorKey(*m_contKey, sgkey) : sgkey;
160  if (m_contHandleKey.assign (contKeyFromKey (key)).isFailure())
161  return StatusCode::FAILURE;
162  return Base::assign (key);
163 }
164 
165 
166 /**
167  * @brief If this object is used as a property, then this should be called
168  * during the initialize phase. It will fail if the requested
169  * StoreGate service cannot be found or if the key is blank.
170  *
171  * @param used If false, then this handle is not to be used.
172  * Instead of normal initialization, the key will be cleared.
173  */
174 template <class T>
175 StatusCode WriteDecorHandleKey<T>::initialize (bool used /*= true*/)
176 {
177  // If a parent container is used, its key may have changed
178  if (m_contKey){
179  m_contHandleKey = m_contKey->key();
180  std::string decorKey=decorKeyFromKey(this->key());
181  VarHandleKey::operator=(makeContDecorKey (m_contKey->key(), decorKey));
182  }
183  detail::registerWriteDecorHandleKey (this->owner(), m_contHandleKey.fullKey());
184  if (m_contHandleKey.initialize (used).isFailure())
185  return StatusCode::FAILURE;
186  return Base::initialize (used);
187 }
188 
189 
190 /**
191  * @brief Python representation of Handle.
192  */
193 template <class T>
194 std::string WriteDecorHandleKey<T>::pythonRepr() const
195 {
196  std::string repr = Base::pythonRepr();
197  // With a parent container our representation becomes the decoration name only.
198  if (m_contKey) {
199  removeContFromDecorKey(*m_contKey, repr);
200  }
201  return repr;
202 }
203 
204 
205 /**
206  * @brief Return the handle key for the container.
207  */
208 template <class T>
209 const ReadHandleKey<T>& WriteDecorHandleKey<T>::contHandleKey() const
210 {
211  return m_contHandleKey;
212 }
213 
214 
215 /**
216  * @brief Return the handle key for the container.
217  */
218 template <class T>
219 ReadHandleKey<T>& WriteDecorHandleKey<T>::contHandleKey_nc()
220 {
221  return m_contHandleKey;
222 }
223 
224 
225 /**
226  * @brief Declare that this item does not participate in scheduling.
227  *
228  * If a WriteDataHandleKey is renounced, then we skip creating the
229  * output alias. Further, we skip the check that the element provided
230  * is a member of the declared container. A WriteDataHandle initialized
231  * from a renounced key will effectively behave like a simple Decorator.
232  */
233 template <class T>
234 inline
235 void WriteDecorHandleKey<T>::renounce()
236 {
237  m_renounced = true;
238 }
239 
240 
241 /**
242  * @brief Return the renounced flag.
243  */
244 template <class T>
245 inline
246 bool WriteDecorHandleKey<T>::renounced() const
247 {
248  return m_renounced;
249 }
250 
251 
252 } // namespace SG
253