ATLAS Offline Software
StoreGate/StoreGate/VarHandleKeyArray.icc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2018 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 
6 #include "CxxUtils/checker_macros.h"
7 
8 
9 namespace 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;
46  if (!b.assign(s)) {
47  sc = StatusCode::FAILURE;
48  } else {
49  // Skip blank keys
50  if (b.key() != "") {
51  this->push_back(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 }