All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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  v.emplace( DataObjID( *it ) );
109  ++it;
110 
111  } else {
112 
113  try {
114  c = std::stoi(*it);
115  } catch (const std::invalid_argument& /*e*/) {
116  // class name, not ClassID
117  cn = *it;
118  boost::erase_all(cn,"'");
119  c = 0;
120  }
121 
122  ++it;
123  std::string k = *it;
124  boost::erase_all(k,"'");
125  ++it;
126 
127  if ( c != 0) {
128  v.emplace( DataObjID(c,k) );
129  } else {
130  v.emplace( DataObjID(cn,k) );
131  }
132  }
133  }
134  }
135  return sc;
136  }
137 
138 
139 
140  } //> ns Parsers
141 
142  namespace Utils {
143 
144  std::ostream&
146  if (v.clid() == 0) {
147  o << "'" << v.key() << "'";
148  } else {
149  o << "(" << v.clid() << ",'" << v.key() << "')";
150  }
151  return o;
152  }
153 
154  std::ostream&
156  o << "[";
157  for (auto &i : v) {
158  toStream(i,o);
159  o << ",";
160  }
161  o << "]";
162  return o;
163  }
164 
165  } //> ns Utils
166 
167 } //> ns Gaudi
168 
169 //---------------------------------------------------------------------------
170 
172  DataObjID& ref )
173  : PropertyWithHandlers( name, typeid( DataObjID ) ),
174  m_pValue( &ref )
175 {}
176 
177 //---------------------------------------------------------------------------
178 
180 {}
181 
182 //---------------------------------------------------------------------------
183 
186 {
187  if (!Gaudi::Parsers::parse(*m_pValue, s).isSuccess()) {
188  return StatusCode::FAILURE;
189  }
190  return useUpdateHandler()
193 }
194 
195 //---------------------------------------------------------------------------
196 
197 bool
199 {
200  m_pValue->operator=(value);
201  return useUpdateHandler();
202 }
203 
204 //---------------------------------------------------------------------------
205 
208 {
209  useReadHandler();
212  return o.str();
213 }
214 
215 //---------------------------------------------------------------------------
216 void
218 {
219  useReadHandler();
220  out << this->toString();
221 }
222 
223 
224 //---------------------------------------------------------------------------
225 //---------------------------------------------------------------------------
226 
228  DataObjIDColl& ref )
229  : PropertyWithHandlers( name, typeid( DataObjIDColl ) ),
230  m_pValue( &ref )
231 {}
232 
233 //---------------------------------------------------------------------------
234 
236 {}
237 
238 //---------------------------------------------------------------------------
239 
242 {
243  if (!Gaudi::Parsers::parse(*m_pValue, s).isSuccess()) {
244  return StatusCode::FAILURE;
245  }
246  return useUpdateHandler()
249 }
250 
251 //---------------------------------------------------------------------------
252 
253 bool
255 {
256  m_pValue->operator=(value);
257  return useUpdateHandler();
258 }
259 
260 //---------------------------------------------------------------------------
261 
264 {
265  useReadHandler();
268  return o.str();
269 }
270 
271 //---------------------------------------------------------------------------
272 void
274 {
275  useReadHandler();
276  out << this->toString();
277 }
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:736
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
bool useUpdateHandler() override
use the call-back function at update, if available
Definition: Property.h:765
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
void useReadHandler() const
use the call-back function at reading, if available
Definition: Property.h:762
T find(T...args)
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