The Gaudi Framework  v29r0 (ff2e7097)
DataObjIDProperty.cpp
Go to the documentation of this file.
1 
2 // stl includes
3 #include <boost/algorithm/string.hpp>
4 #include <boost/tokenizer.hpp>
5 #include <map>
6 #include <sstream>
7 
8 // StoreGate includes
10 
11 using namespace Gaudi;
12 using namespace Gaudi::Parsers;
13 using namespace Gaudi::Utils;
14 
15 namespace Gaudi
16 {
17  namespace Parsers
18  {
19 
21  {
22  // default values
24  std::string prop;
25  sc = Gaudi::Parsers::parse( prop, s );
26 
27  if ( sc.isSuccess() ) {
28  // split the string in 1 or 2 words:
29  typedef boost::tokenizer<boost::char_separator<char>> tokenizer;
30  boost::char_separator<char> sep( "(), " );
31  tokenizer tokens( prop, sep );
32  int nToks( distance( tokens.begin(), tokens.end() ) );
33  auto it = tokens.begin();
34 
35  if ( nToks == 1 ) {
36  // Gaudi style /path/to/object
37  std::string k = *it;
38  boost::erase_all( k, "'" );
39  v = DataObjID( k );
40 
41  } else if ( nToks == 2 ) {
42  // ATLAS style (clid, 'key') or ('ClassName', 'key')
43  CLID c( 0 );
44  std::string cn( *it );
45  DataObjID id;
46 
47  try {
48  c = std::stoi( *it );
49  } catch ( const std::invalid_argument& /*e*/ ) {
50  // not a number
51  boost::erase_all( cn, "'" );
52  c = 0;
53  }
54 
55  ++it;
56  std::string k = *it;
57  boost::erase_all( k, "'" );
58  ++it;
59 
60  if ( c != 0 ) {
61  v = DataObjID( c, k );
62  } else {
63  v = DataObjID( cn, k );
64  }
65  } else {
66  std::cerr << "Unable to instantiate a DataObjID from a Property " << s << " :: Format is bad" << std::endl;
68  return sc;
69  }
70  }
71  return sc;
72  }
73 
74  //---------------------------------------------------------------------------
75 
77  {
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  } //> ns Parsers
142 
143  namespace Utils
144  {
145 
147  {
148  if ( v.clid() == 0 ) {
149  o << "'" << v.key() << "'";
150  } else {
151  o << "(" << v.clid() << ",'" << v.key() << "')";
152  }
153  return o;
154  }
155 
157  {
158  o << "[";
159  for ( auto& i : v ) {
160  toStream( i, o );
161  o << ",";
162  }
163  o << "]";
164  return o;
165  }
166 
167  } //> ns Utils
168 
169 } //> ns Gaudi
170 
171 //---------------------------------------------------------------------------
172 
174  : PropertyWithHandlers( name, typeid( DataObjID ) ), m_pValue( &ref )
175 {
176 }
177 
178 //---------------------------------------------------------------------------
179 
181 
182 //---------------------------------------------------------------------------
183 
185 {
186  if ( !Gaudi::Parsers::parse( *m_pValue, s ).isSuccess() ) {
187  return StatusCode::FAILURE;
188  }
190 }
191 
192 //---------------------------------------------------------------------------
193 
195 {
196  m_pValue->operator=( value );
197  return useUpdateHandler();
198 }
199 
200 //---------------------------------------------------------------------------
201 
203 {
204  useReadHandler();
207  return o.str();
208 }
209 
210 //---------------------------------------------------------------------------
212 {
213  useReadHandler();
214  out << this->toString();
215 }
216 
217 //---------------------------------------------------------------------------
218 //---------------------------------------------------------------------------
219 
221  : PropertyWithHandlers( name, typeid( DataObjIDColl ) ), m_pValue( &ref )
222 {
223 }
224 
225 //---------------------------------------------------------------------------
226 
228 
229 //---------------------------------------------------------------------------
230 
232 {
233  if ( !Gaudi::Parsers::parse( *m_pValue, s ).isSuccess() ) {
234  return StatusCode::FAILURE;
235  }
237 }
238 
239 //---------------------------------------------------------------------------
240 
242 {
243  m_pValue->operator=( value );
244  return useUpdateHandler();
245 }
246 
247 //---------------------------------------------------------------------------
248 
250 {
251  useReadHandler();
254  return o.str();
255 }
256 
257 //---------------------------------------------------------------------------
259 {
260  useReadHandler();
261  out << this->toString();
262 }
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:293
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:75
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:786
STL class.
std::ostream & toStream(const Type &, std::ostream &)
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:28
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:812
bool useUpdateHandler() override
use the call-back function at update, if available
Definition: Property.h:815
T emplace(T...args)
string s
Definition: gaudirun.py:253
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