ATLAS Offline Software
Loading...
Searching...
No Matches
StoreGate/StoreGate/VarHandleKeyArray.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
6#include "CxxUtils/checker_macros.h"
7
8
9namespace SG {
10 /**
11 * @brief forward the initialization to the member VarHandleKeys
12 * @param used If false, then this handle is not to be used.
13 * Instead of normal initialization, the key will be cleared.
14 */
15 template <class Base>
16 inline
17 StatusCode VarHandleKeyArrayCommon<Base>::initialize (bool used /*= true*/) {
18 StatusCode sc(StatusCode::SUCCESS);
19 if (used) {
20 IDataHandleHolder* const owner = this->owner();
21 for (Base& b : *this) {
22 if (owner != nullptr) {
23 b.setOwner(owner);
24 }
25 if ( b.initialize().isFailure() ) {
26 sc = StatusCode::FAILURE;
27 }
28 }
29 }
30 else {
31 this->clear();
32 }
33 return sc;
34 }
35
36 //
37 // Set the VarHandleKey from a string
38 //
39 template <class Base>
40 inline
41 StatusCode VarHandleKeyArrayCommon<Base>::assign(const std::vector<std::string>& vs) {
42 StatusCode sc(StatusCode::SUCCESS);
43 this->clear();
44 for (auto & s : vs) {
45 Base b = this->keyFromString (s);
46 if (!b.assign(s)) {
47 sc = StatusCode::FAILURE;
48 } else {
49 // Skip blank keys
50 if (b.key() != "") {
51 std::vector<Base>::emplace_back(std::move(b));
52 }
53 }
54 }
55 return sc;
56 }
57
58 //
59 // string representation of VarHandleKeyArray
60 //
61 template <class Base>
62 inline
63 std::string VarHandleKeyArrayCommon<Base>::toString() const {
64 std::ostringstream ost;
65 typename std::vector<Base>::const_iterator itr;
66 itr = this->begin();
67 size_t sz = this->size();
68 for ( size_t i=0; i < sz; ++i, ++itr) {
69 ost << "'" << itr->objKey() << "'";
70 if (i != sz-1) {
71 ost << ",";
72 }
73 }
74 return ost.str();
75 }
76
77 //
78 // create array of all base VarHandleKeys in the Array
79 //
80 template <class Base>
81 inline
82 std::vector<SG::VarHandleKey*> VarHandleKeyArrayCommon<Base>::keys() const {
83 std::vector<SG::VarHandleKey*> keys;
84 for (const SG::VarHandleKey& k : *this) {
85 // FIXME: This is a rule violation, but we can't really fix it without
86 // changing the IDataHandleHolder base class from Gaudi.
87 SG::VarHandleKey* k_nc ATLAS_THREAD_SAFE = const_cast<SG::VarHandleKey*>(&k);
88 keys.push_back (k_nc);
89 }
90 return keys;
91 }
92
93 template <class Base>
94 inline
95 void VarHandleKeyArrayCommon<Base>::declare(IDataHandleHolder* owner) {
96 if ( renounced() ) {
97 return;
98 }
99 for (auto k: keys() ) {
100 owner->declare ( *k );
101 k->setOwner( owner );
102 }
103 }
104
105
106 /**
107 * Create a HandleKey from a string.
108 * @param s String from which to create the HandleKey.
109 *
110 * This is for decoration handle keys. If this array was initialized
111 * with a container key, then that will be used to for the handle keys.
112 */
113 template <class Base>
114 template <class T /*= Base*/>
115 requires T::isDecorHandleKey
116 inline
117 Base VarHandleKeyArrayCommon<Base>::keyFromString (const std::string& s) const
118 {
119 if (m_contKey)
120 return Base (*m_contKey, s);
121 return Base (s);
122 }
123
124
125 /**
126 * Create a HandleKey from a string.
127 * @param s String from which to create the HandleKey.
128 *
129 * This is for non-decoration handle keys.
130 */
131 template <class Base>
132 template <class T /*= Base*/>
133 requires (!T::isDecorHandleKey)
134 inline
135 Base VarHandleKeyArrayCommon<Base>::keyFromString (const std::string& s) const
136 {
137 return Base (s);
138 }
139}