34 template<
class T>
struct TypeName
36 static std::string name () {
return typeid (T).name();}
39 template<> struct TypeName<X> { \
40 static std::string name () {return #X;}; };
48 template<
class From,
class To>
struct Convert
50 static void convert (
const std::string& , To& ,
const std::string& field)
54 RCU_THROW_MSG (
"no conversion defined from type " + TypeName<From>::name +
" to type " + TypeName<To>::name +
" for field " + field);
65 template<
class To>
struct Convert<
std::
string,To>
67 static void convert (
const std::string& from, To& to,
const std::string& field)
69 std::stringstream
ss (from);
72 if (!(
ss.fail() ||
ss.rdbuf()->in_avail() > 0))
79 RCU_THROW_MSG (
"unable to convert string '" + from +
"' to type " + TypeName<To>::name() +
" for field " + field);
83 template<
class Type>
struct Convert<
Type,
Type>
85 static void convert (
const Type& from,
Type& to,
const std::string& )
91 template<
class From>
struct Convert<From,
std::
string>
93 static void convert (
const From& from, std::string& to,
const std::string& )
95 std::ostringstream
ss;
101 template<>
struct Convert<bool,
std::
string>
103 static void convert (
const bool& from, std::string& to,
const std::string& )
112 template<>
struct Convert<
std::
string,bool>
114 static void convert (
const std::string& from,
bool& to,
const std::string& field)
116 if (from ==
"true" || from ==
"True" || from ==
"TRUE" || from ==
"1")
121 if (from ==
"false" || from ==
"False" || from ==
"FALSE" || from ==
"0")
128 RCU_THROW_MSG (
"unable to convert string '" + from +
"' to type bool for field " + field);
132#define DEFAULT_CONVERT(FROM,TO) \
133 template<> struct Convert<FROM,TO> { \
134 static void convert (const FROM& from, TO& to, const std::string& ) { \
143#undef DEFAULT_CONVERT
146 template<
class From,
class To>
bool
147 convertSingle (
const TObject *from, To& to,
const std::string& field)
149 const MetaData<From> *myfrom =
150 dynamic_cast<const MetaData<From>*
>(from);
153 Convert<From,To>::convert (myfrom->value, to, field);
157 template<
class To>
void
158 convert (
const TObject *from, To& to,
const std::string& field)
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 RCU_THROW_MSG (
"unkown input type " + std::string (
typeid(*from).name()) +
" for field " + field);
173 std::string
trim (
const std::string&
str)
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 );
196 template<class T> T getT(const std::string& name, T def_val) const;
208 template<class T> static T convertT(const TObject* meta, T defval);
214 std::string
dbg (
const MetaObject& ,
unsigned )
221 void swap (MetaObject&
a, MetaObject& b)
229 testInvariant ()
const
238 : m_dataList (
new TList)
240 m_dataList->SetOwner(
true);
247 MetaObject (
const MetaObject& that)
248 : TCollection (), m_dataList (
new TList)
250 m_dataList->SetOwner(
true);
253 while ((
object = iter.Next()))
255 m_dataList->Add (object->Clone());
273 MetaObject& MetaObject ::
274 operator = (
const MetaObject& that)
277 MetaObject tmp (that);
285 swap (MetaObject& that)
293 remove (
const std::string& name)
298 while ((
object = m_dataList->FindObject (name.c_str())))
299 delete m_dataList->Remove (
object);
305 addReplace (TNamed *meta_swallow)
307 std::unique_ptr<TNamed>
meta (meta_swallow);
311 remove (meta_swallow->GetName());
312 m_dataList->Add (
meta.release());
317 TObject *MetaObject ::
318 get (
const std::string& name)
321 return m_dataList->FindObject (name.c_str());
326 const TObject *MetaObject ::
327 get (
const std::string& name)
const
330 return m_dataList->FindObject (name.c_str());
336 getDouble (
const std::string& name,
double def_val)
const
339 return castDouble (name, def_val, CAST_NOCAST_DEFAULT);
344 std::string MetaObject ::
345 getString (
const std::string& name,
const std::string& def_val)
const
348 return castString (name, def_val, CAST_NOCAST_DEFAULT);
354 castDouble (
const std::string& name,
double def_val,
357 return castT (name, def_val, mode);
362 std::string MetaObject ::
363 castString (
const std::string& name,
const std::string& def_val,
366 return castT(name,def_val, mode);
371 castInteger (
const std::string& name,
int def_val,
374 return castT(name,def_val, mode);
379 castBool (
const std::string& name,
bool def_val,
382 return castT(name,def_val, mode);
387 setDouble (
const std::string& name,
double value)
395 setString (
const std::string& name,
const std::string& value)
402 setInteger (
const std::string& name,
int value)
409 setBool (
const std::string& name,
bool value)
416 fetch (
const MetaObject& source)
420 TIter iter (&source);
422 while ((
object = iter.Next()))
424 TNamed *
const named =
dynamic_cast<TNamed*
>(object);
427 m_dataList->Add (object->Clone());
428 }
else if (strncmp (named->GetName(),
"nc_", 3) != 0)
430 addReplace (
dynamic_cast<TNamed*
>(named->Clone ()));
438 fetchDefaults (
const MetaObject& source)
442 TIter iter (&source);
444 while ((
object = iter.Next()))
446 if (
get (object->GetName ()) == 0)
447 m_dataList->Add (object->Clone());
454 Add (TObject *meta_swallow)
456 std::unique_ptr<TObject>
meta (meta_swallow);
459 m_dataList->Add (
meta.release());
464 Add (TObject *meta_swallow, Option_t*)
466 this->Add (meta_swallow);
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));
512 return result.release();
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:
560 convert (
meta, result, name);
563 case CAST_ERROR_DEFAULT:
566 convert (
meta, result,
"");
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();