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