DataObjIDProperty.cpp
Go to the documentation of this file.
1 
2 // stl includes
3 #include <sstream>
4 #include <map>
5 #include <boost/tokenizer.hpp>
6 #include <boost/algorithm/string.hpp>
7 
8 // StoreGate includes
10 
11 using namespace Gaudi;
12 using namespace Gaudi::Parsers;
13 using namespace Gaudi::Utils;
14 
15 namespace Gaudi {
16  namespace Parsers {
17 
19  parse(DataObjID& v, const std::string& s) {
20  // default values
22  std::string prop;
23  sc = Gaudi::Parsers::parse(prop, s);
24 
25  if (sc.isSuccess()) {
26  //split the string in 1 or 2 words:
27  typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
28  boost::char_separator<char> sep("(), ");
29  tokenizer tokens(prop, sep);
30  int nToks(distance(tokens.begin(), tokens.end()));
31  auto it = tokens.begin();
32 
33  if (nToks == 1) {
34  // Gaudi style /path/to/object
35  std::string k = *it;
36  boost::erase_all(k,"'");
37  v = DataObjID( k );
38 
39  } else if (nToks == 2) {
40  // ATLAS style (clid, 'key') or ('ClassName', 'key')
41  CLID c(0);
42  std::string cn(*it);
43  DataObjID id;
44 
45  try {
46  c = std::stoi(*it);
47  } catch (const std::invalid_argument& /*e*/) {
48  // not a number
49  boost::erase_all(cn,"'");
50  c = 0;
51  }
52 
53  ++it;
54  std::string k = *it;
55  boost::erase_all(k,"'");
56  ++it;
57 
58  if ( c != 0) {
59  v = DataObjID(c,k);
60  } else {
61  v = DataObjID(cn,k);
62  }
63  } else {
64  std::cerr << "Unable to instantiate a DataObjID from a Property " << s
65  << " :: Format is bad" << std::endl;
67  return sc;
68  }
69 
70  }
71  return sc;
72  }
73 
74 //---------------------------------------------------------------------------
75 
78  // default values
80  std::string prop;
81  sc = Gaudi::Parsers::parse(prop, s);
82 
83  if (sc.isSuccess()) {
84 
85  // Reset Collection
86  v.clear();
87 
88  bool isGaudi(true);
89 
90  // Gaudi style [ '/path/to/data', '/other/data' ] or
91  // ATLAS style [ ('ClassName', 'key') , (ClassID, 'key2') ]
92  if (s.find("(") != std::string::npos) {
93  isGaudi = false;
94  }
95 
96  // split the string in 1 or 2 words:
97  typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
98  boost::char_separator<char> sep("[](), ");
99  tokenizer tokens(prop, sep);
100  auto it = tokens.begin();
101 
102  CLID c(0);
103  std::string cn;
104 
105  while (it != tokens.end()) {
106 
107  if (isGaudi) {
108 
109  std::string k = *it;
110  boost::erase_all(k, "'");
111  v.emplace( DataObjID( k ) );
112  ++it;
113 
114  } else {
115 
116  try {
117  c = std::stoi(*it);
118  } catch (const std::invalid_argument& /*e*/) {
119  // class name, not ClassID
120  cn = *it;
121  boost::erase_all(cn,"'");
122  c = 0;
123  }
124 
125  ++it;
126  std::string k = *it;
127  boost::erase_all(k,"'");
128  ++it;
129 
130  if ( c != 0) {
131  v.emplace( DataObjID(c,k) );
132  } else {
133  v.emplace( DataObjID(cn,k) );
134  }
135  }
136  }
137  }
138  return sc;
139  }
140 
141 
142 
143  } //> ns Parsers
144 
145  namespace Utils {
146 
147  std::ostream&
149  if (v.clid() == 0) {
150  o << "'" << v.key() << "'";
151  } else {
152  o << "(" << v.clid() << ",'" << v.key() << "')";
153  }
154  return o;
155  }
156 
157  std::ostream&
159  o << "[";
160  for (auto &i : v) {
161  toStream(i,o);
162  o << ",";
163  }
164  o << "]";
165  return o;
166  }
167 
168  } //> ns Utils
169 
170 } //> ns Gaudi
171 
172 //---------------------------------------------------------------------------
173 
175  DataObjID& ref )
176  : PropertyWithHandlers( name, typeid( DataObjID ) ),
177  m_pValue( &ref )
178 {}
179 
180 //---------------------------------------------------------------------------
181 
183 {}
184 
185 //---------------------------------------------------------------------------
186 
189 {
190  if (!Gaudi::Parsers::parse(*m_pValue, s).isSuccess()) {
191  return StatusCode::FAILURE;
192  }
193  return useUpdateHandler()
196 }
197 
198 //---------------------------------------------------------------------------
199 
200 bool
202 {
203  m_pValue->operator=(value);
204  return useUpdateHandler();
205 }
206 
207 //---------------------------------------------------------------------------
208 
211 {
212  useReadHandler();
215  return o.str();
216 }
217 
218 //---------------------------------------------------------------------------
219 void
221 {
222  useReadHandler();
223  out << this->toString();
224 }
225 
226 
227 //---------------------------------------------------------------------------
228 //---------------------------------------------------------------------------
229 
231  DataObjIDColl& ref )
232  : PropertyWithHandlers( name, typeid( DataObjIDColl ) ),
233  m_pValue( &ref )
234 {}
235 
236 //---------------------------------------------------------------------------
237 
239 {}
240 
241 //---------------------------------------------------------------------------
242 
245 {
246  if (!Gaudi::Parsers::parse(*m_pValue, s).isSuccess()) {
247  return StatusCode::FAILURE;
248  }
249  return useUpdateHandler()
252 }
253 
254 //---------------------------------------------------------------------------
255 
256 bool
258 {
259  m_pValue->operator=(value);
260  return useUpdateHandler();
261 }
262 
263 //---------------------------------------------------------------------------
264 
267 {
268  useReadHandler();
271  return o.str();
272 }
273 
274 //---------------------------------------------------------------------------
275 void
277 {
278  useReadHandler();
279  out << this->toString();
280 }
std::ostream & toStream(ITERATOR first, ITERATOR last, std::ostream &s, const std::string &open, const std::string &close, const std::string &delim)
the helper function to print the sequence
Definition: ToStream.h:315
DataObjIDProperty(const std::string &name, DataObjID &ref)
void toStream(std::ostream &out) const override
value -> stream
const std::string name() const
property name
Definition: Property.h:40
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:74
T endl(T...args)
std::string toString() const override
value -> string
bool setValue(const DataObjID &value)
StatusCode parse(GaudiUtils::HashMap< K, V > &result, const std::string &input)
Basic parser for the types of HashMap used in DODBasicMapper.
std::string toString() const override
value -> string
StatusCode fromString(const std::string &s) override
string -> value
Helper class to simplify the migration old properties deriving directly from PropertyBase.
Definition: Property.h:751
STL class.
DataObjID * m_pValue
Pointer to the real property.
CLID clid() const
Definition: DataObjID.h:54
bool setValue(const DataObjIDColl &value)
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
const std::string & key() const
Definition: DataObjID.h:49
StatusCode fromString(const std::string &s) override
string -> value
unsigned int CLID
Class ID definition.
Definition: ClassID.h:8
T clear(T...args)
const DataObjID & value() const
T find(T...args)
void useReadHandler() const
use the call-back function at reading, if available
Definition: Property.h:777
bool useUpdateHandler() override
use the call-back function at update, if available
Definition: Property.h:780
T emplace(T...args)
string s
Definition: gaudirun.py:245
DataObjIDColl * m_pValue
Pointer to the real property.
const DataObjIDColl & value() const
void toStream(std::ostream &out) const override
value -> stream
DataObjIDCollProperty(const std::string &name, DataObjIDColl &ref)
T stoi(T...args)
Helper functions to set/get the application return code.
Definition: __init__.py:1