#ifndef __JROOT__JROOTCLASSSELECTOR__ #define __JROOT__JROOTCLASSSELECTOR__ #include #include #include "TString.h" #include "TRegexp.h" #include "JLang/JTypeList.hh" #include "JLang/JType.hh" #include "Jeep/JeepToolkit.hh" #include "JROOT/JRootToolkit.hh" /** * \author mdejong */ namespace JROOT {} namespace JPP { using namespace JROOT; } namespace JROOT { using JLANG::JType; using JEEP::getClassname; /** * Auxiliary class to select ROOT class based on class name. */ struct JROOTClassSelector : public std::string { /** * Default contructor. */ JROOTClassSelector() : std::string() {} /** * Contructor. * * \param type_name type_name */ JROOTClassSelector(const char* type_name) : std::string(type_name) {} /** * Contructor. * * \param type_name type_name */ JROOTClassSelector(const std::string& type_name) : std::string(type_name) {} /** * Contructor. * * \param type data type */ template JROOTClassSelector(const JType& type) : std::string(getClassname(getName(type))) {} /** * Get status of given data type. * * \param type data type * \return true if valid class name; else false */ template bool operator()(const JType& type) const { return *this == getClassname(T::Class_Name()); } }; /** * Auxiliary class for ROOT class selection. */ struct JROOTClassSelection : public std::set { /** * Default contructor. */ JROOTClassSelection() {} /** * Copy contructor. * * \param selection selection */ JROOTClassSelection(const std::set& selection) : std::set(selection) {} /** * Contructor. * * \param typelist type list */ template JROOTClassSelection(const JType& typelist) { JLANG::for_each(*this, typelist); } /** * Add given data type. */ template void add() { this->insert(JROOTClassSelector(JType())); } /** * Remove data type. */ template void remove() { this->erase(JROOTClassSelector(JType())); } /** * Add data type. * * \param type data type */ template void operator()(const JType& type) { add(); } /** * Get status of given data type. * * \param type data type * \return true if valid class name; else false */ template bool operator()(const JType& type) const { return this->find(JROOTClassSelector(JType())) != this->end(); } /** * Get status of given data type. * * \return true if valid class name; else false */ template bool is_valid() const { return (*this)(JType()); } /** * Read ROOT class selection from input. * * Note that if the first character of the read class name is: * - a '+', the remainder of the class name is added; or * - a '-', the remainder of the class name is removed; otherwise * - the complete class name is added. * * \param in input stream * \param object ROOT class selection * \return input stream */ friend inline std::istream& operator>>(std::istream& in, JROOTClassSelection& object) { for (std::string buffer; in >> buffer; ) { const TRegexp regexp(buffer.substr(1).c_str()); switch (buffer[0]) { case '-': for (JROOTClassSelection::iterator i = object.begin(); i != object.end(); ) { if (TString(i->c_str()).Index(regexp) != -1) object.erase(i++); else ++i; } break; case '+': object.insert(buffer.substr(1)); break; default: object.insert(buffer); break; } } return in; } /** * Write ROOT class selection to output. * * \param out output stream * \param object ROOT class selection * \return output stream */ friend inline std::ostream& operator<<(std::ostream& out, const JROOTClassSelection& object) { for (const_iterator i = object.begin(); i != object.end(); ++i) { out << *i << std::endl; } return out; } }; /** * Get ROOT class selection. * * \return ROOT class selection */ template inline std::set getROOTClassSelection() { return JROOTClassSelection(JType()); } } #endif