Jpp  19.1.0
the software that should make you happy
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 "JLang/JEquals.hh"
13 #include "Jeep/JeepToolkit.hh"
14 #include "JROOT/JRootToolkit.hh"
15 
16 
17 /**
18  * \author mdejong
19  */
20 
21 namespace JROOT {}
22 namespace JPP { using namespace JROOT; }
23 
24 namespace JROOT {
25 
26  using JLANG::JType;
27  using JLANG::JEquals;
28  using JEEP::getNamespace;
29  using JEEP::getClassname;
30 
31 
32  /**
33  * Auxiliary class to select ROOT class based on class name.
34  */
36  public std::string,
37  public JEquals<JROOTClassSelector, const char* const>
38  {
39  /**
40  * Default contructor.
41  */
43  std::string()
44  {}
45 
46 
47  /**
48  * Contructor.
49  *
50  * \param type_name type_name
51  */
52  JROOTClassSelector(const char* const type_name) :
53  std::string(type_name)
54  {}
55 
56 
57  /**
58  * Contructor.
59  *
60  * \param type_name type_name
61  */
62  JROOTClassSelector(const std::string& type_name) :
63  std::string(type_name)
64  {}
65 
66 
67  /**
68  * Contructor.
69  *
70  * \param type data type
71  * \param option include namespace
72  */
73  template<class T>
74  JROOTClassSelector(const JType<T>& type, const bool option) :
75  std::string(option ? getName(type) : getClassname(getName(type)))
76  {}
77 
78 
79  /**
80  * Equals method.
81  *
82  * Note that if one of the selectors has an empty name space,
83  * the equality is evaluated by class name only else by class name and name space.
84  *
85  * \param selector selector
86  * \return true if this selector equals given selector; else false
87  */
88  bool equals(const JROOTClassSelector& selector) const
89  {
90  if (getNamespace(*this) == "" || getNamespace(selector) == "")
91  return (getClassname(*this) == getClassname(selector));
92  else
93  return (getNamespace(*this) == getNamespace(selector) &&
94  getClassname(*this) == getClassname(selector));
95  }
96 
97 
98  /**
99  * Equals method.
100  *
101  * \param selector selector
102  * \return true if this selector equals given selector; else false
103  */
104  bool equals(const char* const selector) const
105  {
106  return equals(JROOTClassSelector(selector));
107  }
108 
109 
110  /**
111  * Get status of given data type.
112  *
113  * \param type data type
114  * \return true if valid class name; else false
115  */
116  template<class T>
117  bool operator()(const JType<T>& type) const
118  {
119  return equals(JROOTClassSelector(type, true));
120  }
121  };
122 
123 
124  /**
125  * Auxiliary class for ROOT class selection.
126  */
128  public std::set<JROOTClassSelector>
129  {
130  /**
131  * Contructor.
132  *
133  * \param option include namespace
134  */
135  JROOTClassSelection(const bool option = false) :
136  option(option)
137  {}
138 
139 
140  /**
141  * Contructor.
142  *
143  * \param selection selection
144  * \param option include namespace
145  */
146  JROOTClassSelection(const std::set<JROOTClassSelector>& selection, const bool option = false) :
147  std::set<JROOTClassSelector>(selection),
148  option(option)
149  {}
150 
151 
152  /**
153  * Contructor.
154  *
155  * \param typelist type list
156  * \param option include namespace
157  */
158  template<class T>
159  JROOTClassSelection(const JType<T>& typelist, const bool option = false) :
160  option(option)
161  {
162  JLANG::for_each(*this, typelist);
163  }
164 
165 
166  /**
167  * Get option to include name space.
168  *
169  * \return option
170  */
171  bool getOption() const
172  {
173  return option;
174  }
175 
176 
177  /**
178  * Add given data type.
179  */
180  template<class T>
181  void add()
182  {
183  this->insert(JROOTClassSelector(JType<T>(), option));
184  }
185 
186 
187  /**
188  * Remove data type.
189  */
190  template<class T>
191  void remove()
192  {
193  this->erase(JROOTClassSelector(JType<T>(), option));
194  }
195 
196 
197  /**
198  * Add data type.
199  *
200  * \param type data type
201  */
202  template<class T>
203  void operator()(const JType<T>& type)
204  {
205  add<T>();
206  }
207 
208 
209  /**
210  * Get status of given data type.
211  *
212  * \param type data type
213  * \return true if valid class name; else false
214  */
215  template<class T>
216  bool operator()(const JType<T>& type) const
217  {
218  const JROOTClassSelector selector(type, option);
219 
220  for (const_iterator i = this->begin(); i != this->end(); ++i) {
221  if (*i == selector) {
222  return true;
223  }
224  }
225 
226  return false;
227  }
228 
229 
230  /**
231  * Get status of given data type.
232  *
233  * \return true if valid class name; else false
234  */
235  template<class T>
236  bool is_valid() const
237  {
238  return (*this)(JType<T>());
239  }
240 
241 
242  /**
243  * Read ROOT class selection from input.
244  *
245  * Note that if the first character of the read class name is:
246  * - a '+', the remainder of the class name is added; or
247  * - a '-', the remainder of the class name is removed; otherwise
248  * - the complete class name is added.
249  *
250  * \param in input stream
251  * \param object ROOT class selection
252  * \return input stream
253  */
254  friend inline std::istream& operator>>(std::istream& in, JROOTClassSelection& object)
255  {
256  for (std::string buffer; in >> buffer; ) {
257 
258  const TRegexp regexp(buffer.substr(1).c_str());
259 
260  switch (buffer[0]) {
261 
262  case '-':
263 
264  for (JROOTClassSelection::iterator i = object.begin(); i != object.end(); ) {
265 
266  if (TString(i->c_str()).Contains(regexp))
267  object.erase(i++);
268  else
269  ++i;
270  }
271 
272  break;
273 
274  case '+':
275 
276  object.insert(buffer.substr(1));
277  break;
278 
279  default:
280 
281  object.insert(buffer);
282  break;
283  }
284  }
285 
286  return in;
287  }
288 
289 
290  /**
291  * Write ROOT class selection to output.
292  *
293  * \param out output stream
294  * \param object ROOT class selection
295  * \return output stream
296  */
297  friend inline std::ostream& operator<<(std::ostream& out, const JROOTClassSelection& object)
298  {
299  for (const_iterator i = object.begin(); i != object.end(); ++i) {
300  out << *i << std::endl;
301  }
302 
303  return out;
304  }
305 
306  private:
307  bool option; //!< include namespace
308  };
309 
310 
311  /**
312  * Get ROOT class selection.
313  *
314  * \param option include namespace
315  * \return ROOT class selection
316  */
317  template<class T>
318  inline std::set<JROOTClassSelector> getROOTClassSelection(const bool option = false)
319  {
320  return JROOTClassSelection(JType<T>(), option);
321  }
322 }
323 
324 #endif
Auxiliary methods for handling file names, type names and environment.
std::string getClassname(const std::string &type_name)
Get type name, i.e. part after JEEP::TYPENAME_SEPARATOR.
Definition: JeepToolkit.hh:289
std::string getNamespace(const std::string &type_name)
Get name space, i.e. part before JEEP::TYPENAME_SEPARATOR.
Definition: JeepToolkit.hh:270
JObject_t & for_each(JObject_t &object, JType< JTypeList< JHead_t, JTail_t > > typelist)
For each data type method.
Definition: JTypeList.hh:415
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary classes and methods for ROOT I/O.
const char * getName()
Get ROOT name of given data type.
Definition: JRootToolkit.hh:62
std::set< JROOTClassSelector > getROOTClassSelection(const bool option=false)
Get ROOT class selection.
Definition: JSTDTypes.hh:14
Template definition of auxiliary base class for comparison of data structures.
Definition: JEquals.hh:84
Auxiliary class for a type holder.
Definition: JType.hh:19
Auxiliary class for ROOT class selection.
bool getOption() const
Get option to include name space.
JROOTClassSelection(const std::set< JROOTClassSelector > &selection, const bool option=false)
Contructor.
void remove()
Remove data type.
friend std::ostream & operator<<(std::ostream &out, const JROOTClassSelection &object)
Write ROOT class selection to output.
void add()
Add given data type.
bool is_valid() const
Get status of given data type.
friend std::istream & operator>>(std::istream &in, JROOTClassSelection &object)
Read ROOT class selection from input.
void operator()(const JType< T > &type)
Add data type.
JROOTClassSelection(const bool option=false)
Contructor.
bool option
include namespace
JROOTClassSelection(const JType< T > &typelist, const bool option=false)
Contructor.
bool operator()(const JType< T > &type) const
Get status of given data type.
Auxiliary class to select ROOT class based on class name.
bool operator()(const JType< T > &type) const
Get status of given data type.
JROOTClassSelector()
Default contructor.
bool equals(const char *const selector) const
Equals method.
bool equals(const JROOTClassSelector &selector) const
Equals method.
JROOTClassSelector(const JType< T > &type, const bool option)
Contructor.
JROOTClassSelector(const char *const type_name)
Contructor.
JROOTClassSelector(const std::string &type_name)
Contructor.