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;}; };
45#undef TYPENAME
46
47
48 template<class From,class To> struct Convert
49 {
50 static void convert (
const std::string& , To& ,
const std::string& field)
51 {
53 return;
54 throw std::runtime_error ("no conversion defined from type " + TypeName<From>::name() + " to type " + TypeName<To>::name() + " for field " + field);
55 }
56 };
57
58
59
60
61
62
63
64
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);
72 if (!(
ss.fail() ||
ss.rdbuf()->in_avail() > 0))
73 {
75 return;
76 }
78 return;
79 throw std::runtime_error ("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 {
86 {
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& )
94 {
95 std::ostringstream
ss;
98 }
99 };
100
102 {
103 static void convert (
const bool& from, std::string& to,
const std::string& )
104 {
105 if (from)
107 else
109 }
110 };
111
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 {
119 return;
120 }
121 if (from == "false" || from == "False" || from == "FALSE" || from == "0")
122 {
124 return;
125 }
127 return;
128 throw std::runtime_error ("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& ) { \
135 to = from; } };
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 == nullptr)
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;
165 throw std::runtime_error (std::format ("unknown input type {} for field {}", typeid(*from).name(), field));
166 }
167
173 std::string
trim (
const std::string&
str)
174 {
175 const char *const whitespace = " \t\n\r\f\v";
176 size_t startpos =
str.find_first_not_of (whitespace);
177 if (std::string::npos == startpos) return "";
178 size_t endpos =
str.find_last_not_of (whitespace);
179
180
181 if (endpos > startpos &&
182 (
str[startpos] ==
'"' ||
str[startpos] ==
'\'') &&
183 str[endpos] ==
str[startpos])
184 {
185 ++ startpos;
186 -- endpos;
187 }
188 return str.substr (startpos, endpos + 1 - startpos);
189 }
190
191
192
203 public:
204 template<class T> T getT(const std::string& name, T def_val) const;
205
215 public:
216 template<class T> static T convertT(const TObject* meta, T defval);
217 */
218 }
219
220
221
222 std::string
dbg (
const MetaObject& ,
unsigned )
223 {
224 return "MetaObject";
225 }
226
227
228
229 void swap (MetaObject&
a, MetaObject& b)
230 {
232 }
233
234
235
236 void MetaObject ::
237 testInvariant () const
238 {
240 }
241
242
243
244 MetaObject ::
245 MetaObject ()
246 : m_dataList (
new TList)
247 {
248 m_dataList->SetOwner(true);
250 }
251
252
253
254 MetaObject ::
255 MetaObject (const MetaObject& that)
256 : TCollection (), m_dataList (
new TList)
257 {
258 m_dataList->SetOwner(true);
260 TObject *object = nullptr;
261 while ((
object =
iter.Next()))
262 {
263 m_dataList->Add (
object->Clone());
264 }
265
267 }
268
269
270
271 MetaObject ::
272 ~MetaObject ()
273 {
275
276 delete m_dataList;
277 }
278
279
280
281 MetaObject& MetaObject ::
282 operator = (const MetaObject& that)
283 {
284
285 MetaObject
tmp (that);
287 return *this;
288 }
289
290
291
292 void MetaObject ::
293 swap (MetaObject& that)
294 {
296 }
297
298
299
300 void MetaObject ::
301 remove (const std::string& name)
302 {
304
305 TObject *object = nullptr;
306 while ((
object = m_dataList->FindObject (
name.c_str())))
307 delete m_dataList->Remove (object);
308 }
309
310
311
312 void MetaObject ::
313 addReplace (TNamed *meta_swallow)
314 {
315 std::unique_ptr<TNamed>
meta (meta_swallow);
318
319 remove (meta_swallow->GetName());
320 m_dataList->Add (
meta.release());
321 }
322
323
324
325 TObject *MetaObject ::
326 get (const std::string& name)
327 {
329 return m_dataList->FindObject (
name.c_str());
330 }
331
332
333
334 const TObject *MetaObject ::
335 get (const std::string& name) const
336 {
338 return m_dataList->FindObject (
name.c_str());
339 }
340
341
342
343 double MetaObject ::
344 getDouble (const std::string& name, double def_val) const
345 {
346
347 return castDouble (name, def_val, CAST_NOCAST_DEFAULT);
348 }
349
350
351
352 std::string MetaObject ::
353 getString (const std::string& name, const std::string& def_val) const
354 {
355
356 return castString (name, def_val, CAST_NOCAST_DEFAULT);
357 }
358
359
360
361 double MetaObject ::
362 castDouble (const std::string& name, double def_val,
363 CastMode mode) const
364 {
365 return castT (name, def_val, mode);
366 }
367
368
369
370 std::string MetaObject ::
371 castString (const std::string& name, const std::string& def_val,
372 CastMode mode) const
373 {
374 return castT(name,def_val, mode);
375 }
376
377
378 int MetaObject ::
379 castInteger (const std::string& name, int def_val,
380 CastMode mode) const
381 {
382 return castT(name,def_val, mode);
383 }
384
385
386 bool MetaObject ::
387 castBool (const std::string& name, bool def_val,
388 CastMode mode) const
389 {
390 return castT(name,def_val, mode);
391 }
392
393
394 void MetaObject ::
395 setDouble (const std::string& name, double value)
396 {
397 setT (name, value);
398 }
399
400
401
402 void MetaObject ::
403 setString (const std::string& name, const std::string& value)
404 {
405 setT(name,value);
406 }
407
408
409 void MetaObject ::
410 setInteger (const std::string& name, int value)
411 {
412 setT(name,value);
413 }
414
415
416 void MetaObject ::
417 setBool (const std::string& name, bool value)
418 {
419 setT(name,value);
420 }
421
422
423 void MetaObject ::
424 fetch (const MetaObject& source)
425 {
427
428 TIter
iter (&source);
429 TObject *object = nullptr;
430 while ((
object =
iter.Next()))
431 {
432 TNamed *
const named =
dynamic_cast<TNamed*
>(
object);
433 if (!named)
434 {
435 m_dataList->Add (
object->Clone());
436 }
else if (strncmp (
named->GetName(),
"nc_", 3) != 0)
437 {
438 addReplace (
dynamic_cast<TNamed*
>(
named->Clone ()));
439 }
440 }
441 }
442
443
444
445 void MetaObject ::
446 fetchDefaults (const MetaObject& source)
447 {
449
450 TIter
iter (&source);
451 TObject *object = nullptr;
452 while ((
object =
iter.Next()))
453 {
455 m_dataList->Add (
object->Clone());
456 }
457 }
458
459
460
461 void MetaObject ::
462 Add (TObject *meta_swallow)
463 {
464 std::unique_ptr<TObject>
meta (meta_swallow);
467 m_dataList->Add (
meta.release());
468 }
469
470
471 void MetaObject ::
472 Add (TObject *meta_swallow, Option_t*)
473 {
474 this->Add (meta_swallow);
475 }
476
477
478
479 void MetaObject ::
480 Clear (Option_t *option)
481 {
483 m_dataList->Clear (option);
484 }
485
486
487
488 void MetaObject ::
489 Delete (Option_t *option)
490 {
492 m_dataList->Delete (option);
493 }
494
495
496
497 TObject **MetaObject ::
498 GetObjectRef (
const TObject *
const meta)
const
499 {
502
503 TObject **
const result = m_dataList->GetObjectRef (
meta);
504
508 }
509
510
511
512 TIterator *MetaObject ::
513 MakeIterator (Bool_t dir) const
514 {
516
517 std::unique_ptr<TIterator>
result (m_dataList->MakeIterator (dir));
518
521 }
522
523
524
525 TObject *MetaObject ::
526 Remove (TObject *
meta)
527 {
530 return m_dataList->Remove (
meta);
531 }
532
533
534
535 void MetaObject ::
536 Streamer (TBuffer& b)
537 {
539 {
541 TObject::Streamer (b);
542 m_dataList->Clear ();
543 m_dataList->Streamer (b);
544
545
546
547
548 m_dataList->SetOwner (true);
549 } else
550 {
552 TObject::Streamer (b);
553 m_dataList->Streamer (b);
554 }
555 }
556
557
558 template<class T> T MetaObject ::
559 castT (const std::string& name, T def_val, CastMode mode) const
560 {
562 const TObject*
meta =
get (name);
564 return def_val;
565 const MetaData<T> *
const retval =
dynamic_cast<const MetaData<T>*
>(
meta);
566 if (retval)
568 switch (mode)
569 {
570 case CAST_ERROR_THROW:
571 {
575 }
576 case CAST_ERROR_DEFAULT:
577 {
581 }
582 case CAST_NOCAST_THROW:
583 throw std::runtime_error ("invalid input value for " + name);
584 case CAST_NOCAST_DEFAULT:
585 return def_val;
586 }
588 return def_val;
589 }
590
591
592 void MetaObject ::
593 fetchFromString(const std::string& source){
594 size_t oldpos = 0;
595 while (oldpos <
source.size())
596 {
599 if (pos == std::string::npos)
601
602
603 if (pos == oldpos)
604 {
606 continue;
607 }
609 {
612 this->setString (key, value);
613 } else {
614 throw std::runtime_error ("unable to parse string '" + source + "'");
615 }
617 }
618 }
619
620 std::string MetaObject ::
621 dumpToString() const {
622 std::stringstream
ss;
623 std::unique_ptr<TIterator> itr (this->MakeIterator());
624 TObject*
obj =
nullptr;
625 while( (obj = itr->Next()) ){
626 Meta*
m =
dynamic_cast<Meta*
>(
obj);
627 if(!m) continue;
628 if(!
ss.str().empty())
ss <<
", ";
629 ss <<
m->GetName() <<
"=";
630 MetaData<std::string>*
s =
dynamic_cast<MetaData<std::string>*
>(
m);
631 if(s){
632 ss <<
"\"" <<
s->value <<
"\"";
633 } else {
634 std::string mystring;
635 convert<std::string> (m, mystring,
m->GetName());
637 }
638 }
640 }
641
642 Int_t MetaObject ::
643 GetEntries() const {
644 return this->m_dataList->GetEntries();
645 }
646
647 Int_t MetaObject ::
648 GetSize() const {
649
650
651
652 return this->m_dataList->GetSize();
653 }
654
655}
#define RCU_PROVIDE2(x, y)
#define RCU_DESTROY_INVARIANT(x)
#define RCU_CHANGE_INVARIANT(x)
#define RCU_NEW_INVARIANT(x)
#define RCU_REQUIRE_SOFT(x)
#define RCU_READ_INVARIANT(x)
void swap(DataVector< T > &a, DataVector< T > &b)
See DataVector<T, BASE>::swap().
T * get(TKey *tobj)
get a TObject* from a TKey* (why can't a TObject be a TKey?)
std::vector< std::string > split(const std::string &s, const std::string &t=":")
std::unique_ptr< MVAUtils::BDT > convert(TMVA::MethodBDT *bdt, bool isRegression=true, bool useYesNoLeaf=false)
void swap(ElementLinkVector< DOBJ > &lhs, ElementLinkVector< DOBJ > &rhs)
setBGCode setTAP setLVL2ErrorBits bool