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