ATLAS Offline Software
Loading...
Searching...
No Matches
MetaObject.cxx File Reference
#include <SampleHandler/MetaObject.h>
#include <cstdlib>
#include <memory>
#include <sstream>
#include <TList.h>
#include <TNamed.h>
#include <TBuffer.h>
#include <RootCoreUtils/Assert.h>
#include <RootCoreUtils/ThrowMsg.h>
#include <SampleHandler/MetaData.h>

Go to the source code of this file.

Macros

#define TYPENAME(X)
#define DEFAULT_CONVERT(FROM, TO)

Functions

 ClassImp (SH::MetaObject) namespace SH

Macro Definition Documentation

◆ DEFAULT_CONVERT

#define DEFAULT_CONVERT ( FROM,
TO )
Value:
template<> struct Convert<FROM,TO> { \
static void convert (const FROM& from, TO& to, const std::string& /*field*/) { \
to = from; } };

◆ TYPENAME

#define TYPENAME ( X)
Value:
template<> struct TypeName<X> { \
static std::string name () {return #X;}; };

Function Documentation

◆ ClassImp()

ClassImp ( SH::MetaObject )
Author
Nils Krumnack

trim leading/trailing spaces from the given string

Guarantee
strong
Failures
out of memory II

Definition at line 28 of file MetaObject.cxx.

31{
32 namespace
33 {
34 template<class T> struct TypeName
35 {
36 static std::string name () {return typeid (T).name();}
37 };
38#define TYPENAME(X) \
39 template<> struct TypeName<X> { \
40 static std::string name () {return #X;}; };
41 TYPENAME (std::string)
42 TYPENAME (double)
43 TYPENAME (int)
44 TYPENAME (bool)
45#undef TYPENAME
46
47
48 template<class From,class To> struct Convert
49 {
50 static void convert (const std::string& /*from*/, To& /*to*/, const std::string& field)
51 {
52 if (field.empty())
53 return;
54 RCU_THROW_MSG ("no conversion defined from type " + TypeName<From>::name + " to type " + TypeName<To>::name + " for field " + field);
55 }
56 };
57
58 // the conversion string => type is a bit problematic, as it is
59 // expected to produce an exception if the conversion cannot be
60 // performed. this requires several lines of code. in order to avoid
61 // duplication, this has been made into an additional preprocessor
62 // macro. while a template function would have been nicer, this
63 // would not allow to add the target typename to the error message,
64 // which is why this approach has been chosen.
65 template<class To> struct Convert<std::string,To>
66 {
67 static void convert (const std::string& from, To& to, const std::string& field)
68 {
69 std::stringstream ss (from);
70 To retval;
71 ss >> retval;
72 if (!(ss.fail() || ss.rdbuf()->in_avail() > 0))
73 {
74 std::swap (retval, to);
75 return;
76 }
77 if (field.empty())
78 return;
79 RCU_THROW_MSG ("unable to convert string '" + from + "' to type " + TypeName<To>::name() + " for field " + field);
80 }
81 };
82
83 template<class Type> struct Convert<Type,Type>
84 {
85 static void convert (const Type& from, Type& to, const std::string& /*field*/)
86 {
87 to = from;
88 }
89 };
90
91 template<class From> struct Convert<From,std::string>
92 {
93 static void convert (const From& from, std::string& to, const std::string& /*field*/)
94 {
95 std::ostringstream ss;
96 ss << from;
97 to = ss.str();
98 }
99 };
100
101 template<> struct Convert<bool,std::string>
102 {
103 static void convert (const bool& from, std::string& to, const std::string& /*field*/)
104 {
105 if (from)
106 to = "true";
107 else
108 to = "false";
109 }
110 };
111
112 template<> struct Convert<std::string,bool>
113 {
114 static void convert (const std::string& from, bool& to, const std::string& field)
115 {
116 if (from == "true" || from == "True" || from == "TRUE" || from == "1")
117 {
118 to = true;
119 return;
120 }
121 if (from == "false" || from == "False" || from == "FALSE" || from == "0")
122 {
123 to = false;
124 return;
125 }
126 if (field.empty())
127 return;
128 RCU_THROW_MSG ("unable to convert string '" + from + "' to type bool for field " + field);
129 }
130 };
131
132#define DEFAULT_CONVERT(FROM,TO) \
133 template<> struct Convert<FROM,TO> { \
134 static void convert (const FROM& from, TO& to, const std::string& /*field*/) { \
135 to = from; } };
136 DEFAULT_CONVERT (std::string, std::string)
137 DEFAULT_CONVERT (bool, double)
138 DEFAULT_CONVERT (int, double)
139 DEFAULT_CONVERT (bool, int)
140 DEFAULT_CONVERT (double, int)
141 DEFAULT_CONVERT (int, bool)
142 DEFAULT_CONVERT (double, bool)
143#undef DEFAULT_CONVERT
144
145
146 template<class From,class To> bool
147 convertSingle (const TObject *from, To& to, const std::string& field)
148 {
149 const MetaData<From> *myfrom =
150 dynamic_cast<const MetaData<From>* >(from);
151 if (myfrom == 0)
152 return false;
153 Convert<From,To>::convert (myfrom->value, to, field);
154 return true;
155 }
156
157 template<class To> void
158 convert (const TObject *from, To& to, const std::string& field)
159 {
160 if (convertSingle<std::string> (from, to, field)) return;
161 if (convertSingle<double> (from, to, field)) return;
162 if (convertSingle<int> (from, to, field)) return;
163 if (convertSingle<bool> (from, to, field)) return;
164 if (!field.empty())
165 RCU_THROW_MSG ("unkown input type " + std::string (typeid(*from).name()) + " for field " + field);
166 }
167
173 std::string trim (const std::string& str)
174 {
175 size_t endpos = str.find_last_not_of(" \t");
176 size_t startpos = str.find_first_not_of(" \t");
177 if( std::string::npos == endpos ) return "";
178 if(str[startpos] == '"' || str[startpos] == '\'') startpos++;
179 if(str[endpos] == '"' || str[endpos] == '\'') endpos--;
180 return str.substr( startpos, endpos+1-startpos );
181 }
182
183
184 /*
195 public:
196 template<class T> T getT(const std::string& name, T def_val) const;
197
207 public:
208 template<class T> static T convertT(const TObject* meta, T defval);
209 */
210 }
211
212
213
214 std::string dbg (const MetaObject& /*obj*/, unsigned /*verbosity*/)
215 {
216 return "MetaObject";
217 }
218
219
220
221 void swap (MetaObject& a, MetaObject& b)
222 {
223 a.swap (b);
224 }
225
226
227
228 void MetaObject ::
229 testInvariant () const
230 {
231 RCU_INVARIANT (m_dataList != 0);
232 }
233
234
235
236 MetaObject ::
237 MetaObject ()
238 : m_dataList (new TList)
239 {
240 m_dataList->SetOwner(true);
241 RCU_NEW_INVARIANT (this);
242 }
243
244
245
246 MetaObject ::
247 MetaObject (const MetaObject& that)
248 : TCollection (), m_dataList (new TList)
249 {
250 m_dataList->SetOwner(true);
251 TIter iter (&that);
252 TObject *object = 0;
253 while ((object = iter.Next()))
254 {
255 m_dataList->Add (object->Clone());
256 };
257
258 RCU_NEW_INVARIANT (this);
259 }
260
261
262
263 MetaObject ::
264 ~MetaObject ()
265 {
267
268 delete m_dataList;
269 }
270
271
272
273 MetaObject& MetaObject ::
274 operator = (const MetaObject& that)
275 {
276 // no invariant used
277 MetaObject tmp (that);
278 tmp.swap (*this);
279 return *this;
280 }
281
282
283
284 void MetaObject ::
285 swap (MetaObject& that)
286 {
287 std::swap (m_dataList, that.m_dataList);
288 }
289
290
291
292 void MetaObject ::
293 remove (const std::string& name)
294 {
296
297 TObject *object = 0;
298 while ((object = m_dataList->FindObject (name.c_str())))
299 delete m_dataList->Remove (object);
300 }
301
302
303
304 void MetaObject ::
305 addReplace (TNamed *meta_swallow)
306 {
307 std::unique_ptr<TNamed> meta (meta_swallow);
309 RCU_REQUIRE_SOFT (meta_swallow != 0);
310
311 remove (meta_swallow->GetName());
312 m_dataList->Add (meta.release());
313 }
314
315
316
317 TObject *MetaObject ::
318 get (const std::string& name)
319 {
320 RCU_READ_INVARIANT (this);
321 return m_dataList->FindObject (name.c_str());
322 }
323
324
325
326 const TObject *MetaObject ::
327 get (const std::string& name) const
328 {
329 RCU_READ_INVARIANT (this);
330 return m_dataList->FindObject (name.c_str());
331 }
332
333
334
335 double MetaObject ::
336 getDouble (const std::string& name, double def_val) const
337 {
338 // no invariant used
339 return castDouble (name, def_val, CAST_NOCAST_DEFAULT);
340 }
341
342
343
344 std::string MetaObject ::
345 getString (const std::string& name, const std::string& def_val) const
346 {
347 // no invariant used
348 return castString (name, def_val, CAST_NOCAST_DEFAULT);
349 }
350
351
352
353 double MetaObject ::
354 castDouble (const std::string& name, double def_val,
355 CastMode mode) const
356 {
357 return castT (name, def_val, mode);
358 }
359
360
361
362 std::string MetaObject ::
363 castString (const std::string& name, const std::string& def_val,
364 CastMode mode) const
365 {
366 return castT(name,def_val, mode);
367 }
368
369
370 int MetaObject ::
371 castInteger (const std::string& name, int def_val,
372 CastMode mode) const
373 {
374 return castT(name,def_val, mode);
375 }
376
377
378 bool MetaObject ::
379 castBool (const std::string& name, bool def_val,
380 CastMode mode) const
381 {
382 return castT(name,def_val, mode);
383 }
384
385
386 void MetaObject ::
387 setDouble (const std::string& name, double value)
388 {
389 setT (name, value);
390 }
391
392
393
394 void MetaObject ::
395 setString (const std::string& name, const std::string& value)
396 {
397 setT(name,value);
398 }
399
400
401 void MetaObject ::
402 setInteger (const std::string& name, int value)
403 {
404 setT(name,value);
405 }
406
407
408 void MetaObject ::
409 setBool (const std::string& name, bool value)
410 {
411 setT(name,value);
412 }
413
414
415 void MetaObject ::
416 fetch (const MetaObject& source)
417 {
419
420 TIter iter (&source);
421 TObject *object = 0;
422 while ((object = iter.Next()))
423 {
424 TNamed *const named = dynamic_cast<TNamed*>(object);
425 if (!named)
426 {
427 m_dataList->Add (object->Clone());
428 } else if (strncmp (named->GetName(), "nc_", 3) != 0)
429 {
430 addReplace (dynamic_cast<TNamed*>(named->Clone ()));
431 };
432 };
433 }
434
435
436
437 void MetaObject ::
438 fetchDefaults (const MetaObject& source)
439 {
441
442 TIter iter (&source);
443 TObject *object = 0;
444 while ((object = iter.Next()))
445 {
446 if (get (object->GetName ()) == 0)
447 m_dataList->Add (object->Clone());
448 };
449 }
450
451
452
453 void MetaObject ::
454 Add (TObject *meta_swallow)
455 {
456 std::unique_ptr<TObject> meta (meta_swallow);
458 RCU_REQUIRE_SOFT (meta_swallow != 0);
459 m_dataList->Add (meta.release());
460 }
461
462
463 void MetaObject ::
464 Add (TObject *meta_swallow, Option_t*)
465 {
466 this->Add (meta_swallow);
467 }
468
469
470
471 void MetaObject ::
472 Clear (Option_t *option)
473 {
475 m_dataList->Clear (option);
476 }
477
478
479
480 void MetaObject ::
481 Delete (Option_t *option)
482 {
484 m_dataList->Delete (option);
485 }
486
487
488
489 TObject **MetaObject ::
490 GetObjectRef (const TObject *const meta) const
491 {
492 RCU_READ_INVARIANT (this);
493 RCU_REQUIRE_SOFT (meta != 0);
494
495 TObject **const result = m_dataList->GetObjectRef (meta);
496
497 RCU_PROVIDE (result != 0);
498 RCU_PROVIDE (*result == meta);
499 return result;
500 }
501
502
503
504 TIterator *MetaObject ::
505 MakeIterator (Bool_t dir) const
506 {
507 RCU_READ_INVARIANT (this);
508
509 std::unique_ptr<TIterator> result (m_dataList->MakeIterator (dir));
510
511 RCU_PROVIDE2 (result.get() != 0, "result != 0");
512 return result.release();
513 }
514
515
516
517 TObject *MetaObject ::
518 Remove (TObject *meta)
519 {
521 RCU_REQUIRE_SOFT (meta != 0);
522 return m_dataList->Remove (meta);
523 }
524
525
526
527 void MetaObject ::
528 Streamer (TBuffer& b)
529 {
530 if (b.IsReading())
531 {
533 TObject::Streamer (b);
534 m_dataList->Clear ();
535 m_dataList->Streamer (b);
536 } else
537 {
538 RCU_READ_INVARIANT (this);
539 TObject::Streamer (b);
540 m_dataList->Streamer (b);
541 };
542 }
543
544
545 template<class T> T MetaObject ::
546 castT (const std::string& name, T def_val, CastMode mode) const
547 {
548 RCU_READ_INVARIANT (this);
549 const TObject* meta = get (name);
550 if (meta == 0)
551 return def_val;
552 const MetaData<T> *const retval = dynamic_cast<const MetaData<T>*>(meta);
553 if (retval)
554 return retval->value;
555 switch (mode)
556 {
557 case CAST_ERROR_THROW:
558 {
559 T result = def_val;
560 convert (meta, result, name);
561 return result;
562 }
563 case CAST_ERROR_DEFAULT:
564 {
565 T result = def_val;
566 convert (meta, result, "");
567 return result;
568 }
569 case CAST_NOCAST_THROW:
570 RCU_THROW_MSG ("invalid input value for " + name);
571 return def_val;
572 case CAST_NOCAST_DEFAULT:
573 return def_val;
574 }
575 RCU_ASSERT0 ("invalid cast value");
576 return def_val; //compiler dummy
577 }
578
579
580 void MetaObject ::
581 fetchFromString(const std::string& source){
582 size_t pos=1;
583 size_t oldpos = 0;
584 while(pos<source.size() && pos > 0){
585 size_t split = source.find("=",oldpos);
586 pos = source.find(",",split);
587 if(split < pos){
588 std::string key = trim(source.substr(oldpos,split-oldpos));
589 std::string value = trim(source.substr(split+1,pos-split-1));
590 this->setString(key,value);
591 } else {
592 RCU_THROW_MSG ("unable to parse string '"+source+"'");
593 }
594 oldpos = pos+1;
595 }
596 }
597
598 std::string MetaObject ::
599 dumpToString(){
600 std::stringstream ss;
601 TIterator* itr = this->MakeIterator();
602 TObject* obj = NULL;
603 while( (obj = itr->Next()) ){
604 Meta* m = dynamic_cast<Meta*>(obj);
605 if(!m) continue;
606 if(!ss.str().empty()) ss << ", ";
607 ss << m->GetName() << "=";
608 MetaData<std::string>* s = dynamic_cast<MetaData<std::string>* >(m);
609 if(s){
610 ss << "\"" << s->value << "\"";
611 } else {
612 std::string mystring;
613 convert<std::string> (m, mystring, m->GetName());
614 ss << mystring;
615 }
616 }
617 return ss.str();
618 }
619
620 Int_t MetaObject ::
621 GetEntries() const {
622 return this->m_dataList->GetEntries();
623 }
624
625}
#define RCU_INVARIANT(x)
Definition Assert.h:196
#define RCU_PROVIDE2(x, y)
Definition Assert.h:212
#define RCU_DESTROY_INVARIANT(x)
Definition Assert.h:230
#define RCU_CHANGE_INVARIANT(x)
Definition Assert.h:226
#define RCU_NEW_INVARIANT(x)
Definition Assert.h:228
#define RCU_PROVIDE(x)
Definition Assert.h:210
#define RCU_REQUIRE_SOFT(x)
Definition Assert.h:148
#define RCU_ASSERT0(y)
Definition Assert.h:221
#define RCU_READ_INVARIANT(x)
Definition Assert.h:224
void swap(DataVector< T > &a, DataVector< T > &b)
See DataVector<T, BASE>::swap().
static Double_t a
static Double_t ss
#define DEFAULT_CONVERT(FROM, TO)
#define TYPENAME(X)
#define RCU_THROW_MSG(message)
Definition PrintMsg.h:53
string trim(string s)
RootType Type
STL class.
T * get(TKey *tobj)
get a TObject* from a TKey* (why can't a TObject be a TKey?)
Definition hcg.cxx:132
std::vector< std::string > split(const std::string &s, const std::string &t=":")
Definition hcg.cxx:179
CONT to(RANGE &&r)
Definition ranges.h:39
std::unique_ptr< MVAUtils::BDT > convert(TMVA::MethodBDT *bdt, bool isRegression=true, bool useYesNoLeaf=false)
unsigned long long T
-diff
STL namespace.
void swap(ElementLinkVector< DOBJ > &lhs, ElementLinkVector< DOBJ > &rhs)
setBGCode setTAP setLVL2ErrorBits bool