41 template<
class T>
struct TypeName
43 static std::string name () {
return typeid (T).name();}
46 template<> struct TypeName<X> { \
47 static std::string name () {return #X;}; };
55 template<
class From,
class To>
struct Convert
57 static void convert (
const std::string& , To& ,
const std::string& field)
61 RCU_THROW_MSG (
"no conversion defined from type " + TypeName<From>::name +
" to type " + TypeName<To>::name +
" for field " + field);
72 template<
class To>
struct Convert<
std::
string,To>
74 static void convert (
const std::string& from, To& to,
const std::string& field)
76 std::stringstream
ss (from);
79 if (!(
ss.fail() ||
ss.rdbuf()->in_avail() > 0))
86 RCU_THROW_MSG (
"unable to convert string '" + from +
"' to type " + TypeName<To>::name() +
" for field " + field);
90 template<
class Type>
struct Convert<
Type,
Type>
92 static void convert (
const Type& from,
Type& to,
const std::string& )
98 template<
class From>
struct Convert<From,
std::
string>
100 static void convert (
const From& from, std::string& to,
const std::string& )
102 std::ostringstream
ss;
108 template<>
struct Convert<bool,
std::
string>
110 static void convert (
const bool& from, std::string& to,
const std::string& )
119 template<>
struct Convert<
std::
string,bool>
121 static void convert (
const std::string& from,
bool& to,
const std::string& field)
123 if (from ==
"true" || from ==
"True" || from ==
"TRUE" || from ==
"1")
128 if (from ==
"false" || from ==
"False" || from ==
"FALSE" || from ==
"0")
135 RCU_THROW_MSG (
"unable to convert string '" + from +
"' to type bool for field " + field);
139#define DEFAULT_CONVERT(FROM,TO) \
140 template<> struct Convert<FROM,TO> { \
141 static void convert (const FROM& from, TO& to, const std::string& ) { \
150#undef DEFAULT_CONVERT
153 template<
class From,
class To>
bool
154 convertSingle (
const TObject *from, To& to,
const std::string& field)
156 const MetaData<From> *myfrom =
157 dynamic_cast<const MetaData<From>*
>(from);
160 Convert<From,To>::convert (myfrom->value, to, field);
164 template<
class To>
void
165 convert (
const TObject *from, To& to,
const std::string& field)
167 if (convertSingle<std::string> (from, to, field))
return;
168 if (convertSingle<double> (from, to, field))
return;
169 if (convertSingle<int> (from, to, field))
return;
170 if (convertSingle<bool> (from, to, field))
return;
172 RCU_THROW_MSG (
"unkown input type " + std::string (
typeid(*from).name()) +
" for field " + field);
180 std::string
trim (
const std::string&
str)
182 size_t endpos =
str.find_last_not_of(
" \t");
183 size_t startpos =
str.find_first_not_of(
" \t");
184 if( std::string::npos == endpos )
return "";
185 if(
str[startpos] ==
'"' ||
str[startpos] ==
'\'') startpos++;
186 if(
str[endpos] ==
'"' ||
str[endpos] ==
'\'') endpos--;
187 return str.substr( startpos, endpos+1-startpos );
203 template<class T> T getT(const std::string& name, T def_val) const;
215 template<class T> static T convertT(const TObject* meta, T defval);
221 std::string
dbg (
const MetaObject& ,
unsigned )
228 void swap (MetaObject&
a, MetaObject& b)
236 testInvariant ()
const
245 : m_dataList (
new TList)
247 m_dataList->SetOwner(
true);
254 MetaObject (
const MetaObject& that)
255 : TCollection (), m_dataList (
new TList)
257 m_dataList->SetOwner(
true);
260 while ((
object = iter.Next()))
262 m_dataList->Add (
object->Clone());
280 MetaObject& MetaObject ::
281 operator = (
const MetaObject& that)
284 MetaObject tmp (that);
292 swap (MetaObject& that)
300 remove (
const std::string& name)
305 while ((
object = m_dataList->FindObject (name.c_str())))
306 delete m_dataList->Remove (
object);
312 addReplace (TNamed *meta_swallow)
314 std::unique_ptr<TNamed>
meta (meta_swallow);
318 remove (meta_swallow->GetName());
319 m_dataList->Add (
meta.release());
324 TObject *MetaObject ::
325 get (
const std::string& name)
328 return m_dataList->FindObject (name.c_str());
333 const TObject *MetaObject ::
334 get (
const std::string& name)
const
337 return m_dataList->FindObject (name.c_str());
343 getDouble (
const std::string& name,
double def_val)
const
346 return castDouble (name, def_val, CAST_NOCAST_DEFAULT);
351 std::string MetaObject ::
352 getString (
const std::string& name,
const std::string& def_val)
const
355 return castString (name, def_val, CAST_NOCAST_DEFAULT);
361 castDouble (
const std::string& name,
double def_val,
364 return castT (name, def_val, mode);
369 std::string MetaObject ::
370 castString (
const std::string& name,
const std::string& def_val,
373 return castT(name,def_val, mode);
378 castInteger (
const std::string& name,
int def_val,
381 return castT(name,def_val, mode);
386 castBool (
const std::string& name,
bool def_val,
389 return castT(name,def_val, mode);
394 setDouble (
const std::string& name,
double value)
402 setString (
const std::string& name,
const std::string& value)
409 setInteger (
const std::string& name,
int value)
416 setBool (
const std::string& name,
bool value)
423 fetch (
const MetaObject& source)
427 TIter iter (&source);
429 while ((
object = iter.Next()))
431 TNamed *
const named =
dynamic_cast<TNamed*
>(
object);
434 m_dataList->Add (
object->Clone());
435 }
else if (strncmp (named->GetName(),
"nc_", 3) != 0)
437 addReplace (
dynamic_cast<TNamed*
>(named->Clone ()));
445 fetchDefaults (
const MetaObject& source)
449 TIter iter (&source);
451 while ((
object = iter.Next()))
454 m_dataList->Add (
object->Clone());
461 Add (TObject *meta_swallow)
463 std::unique_ptr<TObject>
meta (meta_swallow);
466 m_dataList->Add (
meta.release());
472 Clear (Option_t *option)
475 m_dataList->Clear (option);
481 Delete (Option_t *option)
484 m_dataList->Delete (option);
489 TObject **MetaObject ::
490 GetObjectRef (
const TObject *
const meta)
const
495 TObject **
const result = m_dataList->GetObjectRef (
meta);
504 TIterator *MetaObject ::
505 MakeIterator (Bool_t dir)
const
509 std::unique_ptr<TIterator>
result (m_dataList->MakeIterator (dir));
517 TObject *MetaObject ::
518 Remove (TObject *
meta)
522 return m_dataList->Remove (
meta);
528 Streamer (TBuffer& b)
533 TObject::Streamer (b);
534 m_dataList->Clear ();
535 m_dataList->Streamer (b);
539 TObject::Streamer (b);
540 m_dataList->Streamer (b);
545 template<
class T> T MetaObject ::
546 castT (
const std::string& name, T def_val, CastMode mode)
const
549 const TObject*
meta =
get (name);
552 const MetaData<T> *
const retval =
dynamic_cast<const MetaData<T>*
>(
meta);
554 return retval->value;
557 case CAST_ERROR_THROW:
563 case CAST_ERROR_DEFAULT:
569 case CAST_NOCAST_THROW:
572 case CAST_NOCAST_DEFAULT:
581 fetchFromString(
const std::string& source){
584 while(pos<source.size() && pos > 0){
585 size_t split = source.find(
"=",oldpos);
586 pos = source.find(
",",
split);
588 std::string key =
trim(source.substr(oldpos,
split-oldpos));
590 this->setString(key,value);
598 std::string MetaObject ::
600 std::stringstream
ss;
601 TIterator* itr = this->MakeIterator();
603 while( (obj = itr->Next()) ){
604 Meta* m =
dynamic_cast<Meta*
>(obj);
606 if(!
ss.str().empty())
ss <<
", ";
607 ss << m->GetName() <<
"=";
608 MetaData<std::string>* s =
dynamic_cast<MetaData<std::string>*
>(m);
610 ss <<
"\"" << s->value <<
"\"";
612 std::string mystring;
613 convert<std::string> (m, mystring, m->GetName());
622 return this->m_dataList->GetEntries();