Jpp
JROOTClassSelector.hh
Go to the documentation of this file.
1 #ifndef __JROOT__JROOTCLASSSELECTOR__
2 #define __JROOT__JROOTCLASSSELECTOR__
3 
4 #include <string>
5 #include <set>
6 
7 #include "TString.h"
8 #include "TRegexp.h"
9 
10 #include "JLang/JTypeList.hh"
11 #include "JLang/JType.hh"
12 #include "Jeep/JeepToolkit.hh"
13 #include "JROOT/JRootToolkit.hh"
14 
15 
16 /**
17  * \author mdejong
18  */
19 
20 namespace JROOT {}
21 namespace JPP { using namespace JROOT; }
22 
23 namespace JROOT {
24 
25  using JLANG::JType;
26  using JEEP::getClassname;
27 
28 
29  /**
30  * Auxiliary class to select ROOT class based on class name.
31  */
33  public std::string
34  {
35  /**
36  * Default contructor.
37  */
39  std::string()
40  {}
41 
42 
43  /**
44  * Contructor.
45  *
46  * \param type_name type_name
47  */
48  JROOTClassSelector(const char* type_name) :
49  std::string(type_name)
50  {}
51 
52 
53  /**
54  * Contructor.
55  *
56  * \param type_name type_name
57  */
58  JROOTClassSelector(const std::string& type_name) :
59  std::string(type_name)
60  {}
61 
62 
63  /**
64  * Contructor.
65  *
66  * \param type data type
67  */
68  template<class T>
69  JROOTClassSelector(const JType<T>& type) :
70  std::string(getClassname(getName(type)))
71  {}
72 
73 
74  /**
75  * Get status of given data type.
76  *
77  * \param type data type
78  * \return true if valid class name; else false
79  */
80  template<class T>
81  bool operator()(const JType<T>& type) const
82  {
83  return *this == getClassname(T::Class_Name());
84  }
85  };
86 
87 
88  /**
89  * Auxiliary class for ROOT class selection.
90  */
92  public std::set<JROOTClassSelector>
93  {
94  /**
95  * Default contructor.
96  */
98  {}
99 
100 
101  /**
102  * Copy contructor.
103  *
104  * \param selection selection
105  */
107  std::set<JROOTClassSelector>(selection)
108  {}
109 
110 
111  /**
112  * Contructor.
113  *
114  * \param typelist type list
115  */
116  template<class T>
117  JROOTClassSelection(const JType<T>& typelist)
118  {
119  JLANG::for_each(*this, typelist);
120  }
121 
122 
123  /**
124  * Add data type.
125  *
126  * \param type data type
127  */
128  template<class T>
129  void operator()(const JType<T>& type)
130  {
131  this->insert(JROOTClassSelector(type));
132  }
133 
134 
135  /**
136  * Get status of given data type.
137  *
138  * \param type data type
139  * \return true if valid class name; else false
140  */
141  template<class T>
142  bool operator()(const JType<T>& type) const
143  {
144  return this->find(getClassname(getName(type))) != this->end();
145  }
146 
147 
148  /**
149  * Read ROOT class selection from input.
150  *
151  * Note that if the first character of the read class name is:
152  * - '+' the remainder of the class name is added; or
153  * - '-' the remainder of the class name is removed; otherwise
154  * - the complete class name is added.
155  *
156  * \param in input stream
157  * \param object ROOT class selection
158  * \return input stream
159  */
160  friend inline std::istream& operator>>(std::istream& in, JROOTClassSelection& object)
161  {
162  for (std::string buffer; in >> buffer; ) {
163 
164  const TRegexp regexp(buffer.substr(1).c_str());
165 
166  switch (buffer[0]) {
167 
168  case '-':
169 
170  for (JROOTClassSelection::iterator i = object.begin(); i != object.end(); ) {
171 
172  if (TString(i->c_str()).Index(regexp) != -1)
173  object.erase(i++);
174  else
175  ++i;
176  }
177 
178  break;
179 
180  case '+':
181 
182  object.insert(buffer.substr(1));
183  break;
184 
185  default:
186 
187  object.insert(buffer);
188  break;
189  }
190  }
191 
192  return in;
193  }
194 
195 
196  /**
197  * Write ROOT class selection to output.
198  *
199  * \param out output stream
200  * \param object ROOT class selection
201  * \return output stream
202  */
203  friend inline std::ostream& operator<<(std::ostream& out, const JROOTClassSelection& object)
204  {
205  for (const_iterator i = object.begin(); i != object.end(); ++i) {
206  out << *i << std::endl;
207  }
208 
209  return out;
210  }
211  };
212 
213 
214  /**
215  * Get ROOT class selection.
216  *
217  * \return ROOT class selection
218  */
219  template<class T>
221  {
222  return JROOTClassSelection(JType<T>());
223  }
224 }
225 
226 #endif
JLANG::JType
Auxiliary class for a type holder.
Definition: JType.hh:19
JROOT
Auxiliary classes and methods for ROOT I/O.
Definition: JAbstractStreamer.hh:13
JROOT::JROOTClassSelector::JROOTClassSelector
JROOTClassSelector(const std::string &type_name)
Contructor.
Definition: JROOTClassSelector.hh:58
JROOT::JROOTClassSelector::JROOTClassSelector
JROOTClassSelector(const JType< T > &type)
Contructor.
Definition: JROOTClassSelector.hh:69
JROOT::JROOTClassSelector::JROOTClassSelector
JROOTClassSelector()
Default contructor.
Definition: JROOTClassSelector.hh:38
JROOT::JROOTClassSelection::JROOTClassSelection
JROOTClassSelection(const JType< T > &typelist)
Contructor.
Definition: JROOTClassSelector.hh:117
JROOT::JROOTClassSelector::JROOTClassSelector
JROOTClassSelector(const char *type_name)
Contructor.
Definition: JROOTClassSelector.hh:48
JROOT::getROOTClassSelection
std::set< JROOTClassSelector > getROOTClassSelection()
Get ROOT class selection.
Definition: JROOTClassSelector.hh:220
JROOT::JROOTClassSelection::operator>>
friend std::istream & operator>>(std::istream &in, JROOTClassSelection &object)
Read ROOT class selection from input.
Definition: JROOTClassSelector.hh:160
std::set
Definition: JSTDTypes.hh:13
JROOT::JROOTClassSelection::operator()
void operator()(const JType< T > &type)
Add data type.
Definition: JROOTClassSelector.hh:129
JROOT::JROOTClassSelection::operator()
bool operator()(const JType< T > &type) const
Get status of given data type.
Definition: JROOTClassSelector.hh:142
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
JROOT::JROOTClassSelector
Auxiliary class to select ROOT class based on class name.
Definition: JROOTClassSelector.hh:32
JROOT::JROOTClassSelection::JROOTClassSelection
JROOTClassSelection(const std::set< JROOTClassSelector > &selection)
Copy contructor.
Definition: JROOTClassSelector.hh:106
JRootToolkit.hh
JLANG::for_each
JObject_t & for_each(JObject_t &object, JType< JTypeList< JHead_t, JTail_t > > typelist)
For each data type method.
Definition: JTypeList.hh:572
JROOT::JROOTClassSelection
Auxiliary class for ROOT class selection.
Definition: JROOTClassSelector.hh:91
JROOT::getName
const char * getName()
Get ROOT name of given data type.
Definition: JRootToolkit.hh:45
JEEP::getClassname
std::string getClassname(const std::string &type_name)
Get type name, i.e.
Definition: JeepToolkit.hh:242
std
Definition: jaanetDictionary.h:36
JTypeList.hh
JROOT::JROOTClassSelection::operator<<
friend std::ostream & operator<<(std::ostream &out, const JROOTClassSelection &object)
Write ROOT class selection to output.
Definition: JROOTClassSelector.hh:203
JeepToolkit.hh
JROOT::JROOTClassSelector::operator()
bool operator()(const JType< T > &type) const
Get status of given data type.
Definition: JROOTClassSelector.hh:81
JROOT::JROOTClassSelection::JROOTClassSelection
JROOTClassSelection()
Default contructor.
Definition: JROOTClassSelector.hh:97
JType.hh