ATLAS Offline Software
StoreGate/StoreGate/ReadDecorHandleKey.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/ReadDecorHandleKey.icc
6  * @author scott snyder <snyder@bnl.gov>
7  * @date Jan, 2018
8  * @brief Property holding a SG store/key/clid/attr name from which a
9  * ReadDecorHandle is made.
10  */
11 
12 
13 #include "StoreGate/DecorKeyHelpers.h"
14 
15 
16 namespace SG {
17 
18 
19 /**
20  * @brief Constructor.
21  * @param key The StoreGate key for the object.
22  * @param storeName Name to use for the store, if it's not encoded in sgkey.
23  *
24  * The provided key may actually start with the name of the store,
25  * separated by a "+": "MyStore+Obj". If no "+" is present
26  * the store named by @c storeName is used.
27  */
28 template <class T>
29 inline
30 ReadDecorHandleKey<T>::ReadDecorHandleKey (const std::string& key /*= ""*/,
31  const std::string& storeName /*= StoreID::storeName(StoreID::EVENT_STORE)*/)
32  : Base (ClassID_traits<topbase_t>::ID(), key, storeName),
33  m_contHandleKey (contKeyFromKey (key), storeName)
34 {
35 }
36 
37 
38 /**
39  * @brief Constructor with associated container.
40  * @param contKey ReadHandleKey of the associated container
41  * @param decorKey The decoration name.
42  * @param storeName Name to use for the store.
43  *
44  * The decoration @decorKey will be read from the container referenced
45  * by @contKey.
46  */
47 template <class T>
48 inline
49 ReadDecorHandleKey<T>::ReadDecorHandleKey (const VarHandleKey& contKey,
50  const std::string& decorKey /*= /""*/,
51  const std::string& storeName /*= StoreID::storeName(StoreID::EVENT_STORE*/)
52  : Base (ClassID_traits<topbase_t>::ID(), makeContDecorKey (contKey, decorKey), storeName),
53  m_contHandleKey (contKey.key(), storeName),
54  m_contKey(&contKey)
55 {
56 }
57 
58 
59 /**
60  * @brief auto-declaring Property Constructor.
61  * @param owner Owning component.
62  * @param name name of the Property
63  * @param key default StoreGate key for the object.
64  * @param doc Documentation string.
65  *
66  * will associate the named Property with this RHK via declareProperty
67  *
68  * The provided key may actually start with the name of the store,
69  * separated by a "+": "MyStore+Obj". If no "+" is present
70  * the store named by @c storeName is used.
71  */
72 template <class T>
73 template <class OWNER,
74  typename /*= typename std::enable_if<std::is_base_of<IProperty, OWNER>::value>::type*/>
75 inline
76 ReadDecorHandleKey<T>::ReadDecorHandleKey( OWNER* owner,
77  const std::string& name,
78  const std::string& key /*={}*/,
79  const std::string& doc /*=""*/)
80  : Base (ClassID_traits<topbase_t>::ID(), owner, name, key, doc),
81  m_contHandleKey (contKeyFromKey (key), StoreID::storeName(StoreID::EVENT_STORE) )
82 {
83 }
84 
85 
86 /**
87  * @brief auto-declaring Property Constructor.
88  * @param owner Owning component.
89  * @param name name of the Property
90  * @param key default StoreGate key for the object.
91  * @param doc Documentation string.
92  *
93  * will associate the named Property with this RHK via declareProperty
94  *
95  * The provided key may actually start with the name of the store,
96  * separated by a "+": "MyStore+Obj". If no "+" is present
97  * the store named by @c storeName is used.
98  */
99 template <class T>
100 template <class OWNER,
101  typename /*= typename std::enable_if<std::is_base_of<IProperty, OWNER>::value>::type*/>
102 inline
103 ReadDecorHandleKey<T>::ReadDecorHandleKey( OWNER* owner,
104  const std::string& name,
105  const VarHandleKey& contKey,
106  const std::string& decorKey /*= {}*/,
107  const std::string& doc /*= ""*/)
108  : Base (ClassID_traits<topbase_t>::ID(), owner, name, makeContDecorKey (contKey, decorKey), doc),
109  m_contHandleKey (contKey.key(), StoreID::storeName(StoreID::EVENT_STORE) ),
110  m_contKey(&contKey)
111 {
112 }
113 
114 
115 /**
116  * @brief Change the key of the object to which we're referring.
117  * @param sgkey The StoreGate key for the object.
118  *
119  * The provided key may actually start with the name of the store,
120  * separated by a "+": "MyStore+Obj". If no "+" is present,
121  * the store is not changed.
122  */
123 template <class T>
124 ReadDecorHandleKey<T>&
125 ReadDecorHandleKey<T>::operator= (const std::string& sgkey)
126 {
127  const std::string key = m_contKey ? makeContDecorKey(*m_contKey, sgkey) : sgkey;
128  m_contHandleKey = contKeyFromKey (key);
129  Base::operator= (key);
130  return *this;
131 }
132 
133 
134 /**
135  * @brief Change the key of the object to which we're referring.
136  * @param sgkey The StoreGate key for the object.
137  *
138  * The provided key may actually start with the name of the store,
139  * separated by a "+": "MyStore+Obj". If no "+" is present
140  * the store is not changed. A key name that starts with a "+"
141  * is interpreted as a hierarchical key name, not an empty store name.
142  *
143  * Returns failure the key string format is bad.
144  */
145 template <class T>
146 StatusCode ReadDecorHandleKey<T>::assign (const std::string& sgkey)
147 {
148  const std::string key = m_contKey ? makeContDecorKey(*m_contKey, sgkey) : sgkey;
149  if (m_contHandleKey.assign (contKeyFromKey (key)).isFailure())
150  return StatusCode::FAILURE;
151  return Base::assign (key);
152 }
153 
154 
155 /**
156  * @brief Return the class ID for the referenced object.
157  *
158  * Overridden here to return the CLID for @c T instead of @c topbase_t.
159  */
160 template <class T>
161 inline
162 CLID ReadDecorHandleKey<T>::clid() const
163 {
164  return ClassID_traits<T>::ID();
165 }
166 
167 
168 /**
169  * @brief If this object is used as a property, then this should be called
170  * during the initialize phase. It will fail if the requested
171  * StoreGate service cannot be found or if the key is blank.
172  *
173  * @param used If false, then this handle is not to be used.
174  * Instead of normal initialization, the key will be cleared.
175  */
176 template <class T>
177 StatusCode ReadDecorHandleKey<T>::initialize (bool used /*= true*/)
178 {
179  // If a parent container is used, its key may have changed
180  if (m_contKey){
181  m_contHandleKey = m_contKey->key();
182  std::string decorKey=decorKeyFromKey(this->key());
183  VarHandleKey::operator=(makeContDecorKey (m_contKey->key(), decorKey));
184  }
185 
186  if (m_contHandleKey.initialize (used).isFailure())
187  return StatusCode::FAILURE;
188  return Base::initialize (used);
189 }
190 
191 
192 /**
193  * @brief Python representation of Handle.
194  */
195 template <class T>
196 std::string ReadDecorHandleKey<T>::pythonRepr() const
197 {
198  std::string repr = Base::pythonRepr();
199  // With a parent container our representation becomes the decoration name only.
200  if (m_contKey) {
201  removeContFromDecorKey(*m_contKey, repr);
202  }
203  return repr;
204 }
205 
206 
207 /**
208  * @brief Return the handle key for the container.
209  */
210 template <class T>
211 const ReadHandleKey<T>& ReadDecorHandleKey<T>::contHandleKey() const
212 {
213  return m_contHandleKey;
214 }
215 
216 
217 } // namespace SG