Jpp 19.3.0-rc.1
the software that should make you happy
Loading...
Searching...
No Matches
JTools/JSelector.hh
Go to the documentation of this file.
1#ifndef __JTOOLS__JSELECTOR__
2#define __JTOOLS__JSELECTOR__
3
4#include <istream>
5#include <ostream>
6#include <map>
7
10#include "JLang/JParameter.hh"
11#include "JLang/JClass.hh"
12#include "JLang/JType.hh"
13#include "JLang/JException.hh"
14
15#include "JTools/JDriver.hh"
16
17
18/**
19 * \author mdejong
20 */
21
22namespace JTOOLS {}
23namespace JPP { using namespace JTOOLS; }
24
25namespace JTOOLS {
26
30 using JLANG::JType;
32
33
34 /**
35 * Abstract class of an automatic pointer.
36 * This abstract class can be used for loading and storing of objects.
37 */
38 template<class JBase_t>
40 public JSharedPointer<JBase_t>,
41 public JStreamInput,
42 public JStreamOutput
43 {
44 public:
45 /**
46 * Default constructor.
47 */
53 };
54
55
56 /**
57 * Template class for loading and storing of objects.
58 * This class implements the JAbstractAutoPointer interface.
59 */
60 template<class JDerived_t, class JBase_t>
62 public JAbstractAutoPointer<JBase_t>,
63 private JDriver<JDerived_t>
64 {
65 public:
66 /**
67 * Default constructor.
68 */
70 JAbstractAutoPointer<JBase_t>()
71 {}
72
73
74 /**
75 * Constructor.
76 * This class owns the pointer.
77 *
78 * \param p pointer to object
79 */
80 JAutoPointer(JDerived_t* p) :
81 JAbstractAutoPointer<JBase_t>()
82 {
83 this->reset(p);
84 }
85
86
87 /**
88 * Stream input.
89 * This method creates a new object and read the corresponding input data
90 * if the redirect operator >>(isteam&) is defined for this data type.
91 *
92 * \param in input stream
93 * \return input stream
94 */
95 virtual std::istream& read(std::istream& in) override
96 {
97 JDerived_t* p = this->load(in);
98
99 if (p != NULL) {
100 this->reset(p);
101 }
102
103 return in;
104 }
105
106
107 /**
108 * Stream output.
109 * This method writes the corresponding data
110 * if the redirect operator <<(osteam&) is defined for this data type.
111 *
112 * \param out output stream
113 * \return output stream
114 */
115 virtual std::ostream& write(std::ostream& out) const override
116 {
117 this->store(out, this->get());
118
119 return out;
120 }
121 };
122
123
124 /**
125 * Handler class for automatic pointer.
126 */
127 template<class JBase_t>
129 public JSharedPointer< JAbstractAutoPointer<JBase_t> >
130 {
131
135
136 public:
137 /**
138 * Default constructor.
139 */
143
144
145 /**
146 * Constructor.
147 *
148 * \param type data type
149 */
150 template<class JDerived_t>
153 {
154 this->set(type);
155 }
156
157
158 /**
159 * Constructor.
160 * This class owns the pointer.
161 *
162 * \param p pointer to object
163 */
164 template<class JDerived_t>
165 JAutoElement(JDerived_t* p) :
167 {
168 this->set(p);
169 }
170
171
172 /**
173 * Set the automatic pointer to the given data type.
174 */
175 template<class JDerived_t>
176 void set()
177 {
178 this->set(JType<JDerived_t>());
179 }
180
181
182 /**
183 * Set the automatic pointer to the given data type.
184 *
185 * \param type data type
186 */
187 template<class JDerived_t>
189 {
191 }
192
193
194 /**
195 * Set the automatic pointer to the given data type.
196 *
197 * \param p pointer to object
198 */
199 template<class JDerived_t>
200 void set(JDerived_t* p)
201 {
203 }
204
205
206 /**
207 * Assign given data type to the automatic pointer.
208 *
209 * \param type data type
210 * \return this JAutoElement
211 */
212 template<class JDerived_t>
214 {
215 set(type);
216
217 return *this;
218 }
219
220
221 /**
222 * Assign given data type to the automatic pointer.
223 *
224 * \param p pointer to object
225 * \return this JAutoElement
226 */
227 template<class JDerived_t>
228 JAutoElement& operator=(JDerived_t* p)
229 {
230 set(p);
231
232 return *this;
233 }
234
235
236 /**
237 * Stream input.
238 *
239 * \param in input stream
240 * \param element element
241 * \return input stream
242 */
243 friend inline std::istream& operator>>(std::istream& in, JAutoElement_t& element)
244 {
245 if (element.is_valid())
246 return element.get()->read(in);
247 else
248 return in;
249 }
250
251
252 /**
253 * Stream output.
254 *
255 * \param out output stream
256 * \param element element
257 * \return output stream
258 */
259 friend inline std::ostream& operator<<(std::ostream& out, const JAutoElement_t& element)
260 {
261 if (element.is_valid())
262 return element.get()->write(out);
263 else
264 return out;
265 }
266 };
267
268
269 /**
270 * Template selector class.
271 * This class can be used to set up a list of keys that are mapped to handlers
272 * which create new objects by stream input operations.
273 */
274 template<class JKey_t, class JBase_t>
275 class JSelector :
276 public std::map<JKey_t, JAutoElement<JBase_t> >
277 {
278 public:
279
280 typedef std::map <JKey_t, JAutoElement<JBase_t> > map_type;
281 typedef typename map_type::const_iterator const_iterator;
282 typedef typename map_type::iterator iterator;
284 typedef typename map_type::value_type value_type;
285
286
287 /**
288 * Default constructor.
289 */
291 map_type()
292 {}
293
294
295 /**
296 * Insert data type at given key.
297 *
298 * \param key key
299 * \param type data type
300 */
301 template<class JDerived_t>
303 {
304 map_type::insert(value_type(key, JAutoElement<JBase_t>(type)));
305 }
306
307
308 /**
309 * Insert data type at given key.
310 *
311 * \param key key
312 */
313 template<class JDerived_t>
318
319
320 /**
321 * Insert data type at given key.
322 * This object pointed to will become the default value.
323 *
324 * \param key key
325 * \param p pointer to object
326 */
327 template<class JDerived_t>
328 void insert(argument_type key, JDerived_t* p)
329 {
330 this->key = key;
331
332 map_type::insert(value_type(key, JAutoElement<JBase_t>(p)));
333 }
334
335
336 /**
337 * Get key.
338 *
339 * \return key
340 */
341 JKey_t getKey() const
342 {
343 return this->key;
344 }
345
346
347 /**
348 * Check validy of given key.
349 *
350 * \param key key
351 * \return true if given key maps to a non-NULL value; else false
352 */
353 bool is_valid(argument_type key) const
354 {
355 const_iterator i = this->find(key);
356
357 return i != this->end() && i->second.is_valid() && i->second.get()->is_valid();
358 }
359
360
361 /**
362 * Check validy of selected key.
363 *
364 * \return true if selected key maps to a valid object; else false
365 */
366 bool is_valid() const
367 {
368 return key.isDefined() && is_valid(key);
369 }
370
371
372 /**
373 * Get the object corresponding to the given key.
374 *
375 * \param key key
376 * \return object
377 */
378 JBase_t& get(argument_type key) const
379 {
380 const_iterator i = this->find(key);
381
382 if (i != this->end() && i->second.is_valid() && i->second.get()->is_valid())
383 return *(i->second.get()->get());
384 else
385 throw JNullPointerException("JSelecor<>: no object.");
386 }
387
388
389 /**
390 * Get the object corresponding to the selected key.
391 *
392 * \return selected object
393 */
394 JBase_t& get() const
395 {
396 if (key.isDefined())
397 return this->get(key);
398 else
399 throw JNullPointerException("JSelecor<>: no selection.");
400 }
401
402
403 /**
404 * Read selector from input stream.
405 *
406 * \param in input stream
407 * \param selector selector
408 * \return input stream
409 */
410 friend inline std::istream& operator>>(std::istream& in, JSelector<JKey_t, JBase_t>& selector)
411 {
412 if (in >> selector.key) {
413
414 typename JSelector<JKey_t, JBase_t>::iterator i = selector.find(selector.key);
415
416 if (i != selector.end() && i->second.is_valid())
417 in >> i->second;
418 else
419 in.setstate(std::ios_base::failbit);
420 }
421
422 return in;
423 }
424
425
426 /**
427 * Write selector to output stream.
428 *
429 * \param out output stream
430 * \param selector selector
431 * \return output stream
432 */
433 friend inline std::ostream& operator<<(std::ostream& out, const JSelector<JKey_t, JBase_t>& selector)
434 {
435 if (selector.key.isDefined()) {
436
437 typename JSelector<JKey_t, JBase_t>::const_iterator i = selector.find(selector.key);
438
439 if (i != selector.end() && i->second.is_valid()) {
440 return out << selector.key << ' ' << *(i->second);
441 }
442 }
443
444 return out;
445 }
446
447
448 protected:
450 };
451}
452
453#endif
Exceptions.
bool is_valid() const
Check validity of pointer.
Exception for null pointer operation.
Parameter class.
Definition JParameter.hh:36
const bool isDefined() const
Get status of parameter.
virtual JClass_t * get() const override
Get pointer.
Definition JPointer.hh:64
The template JSharedPointer class can be used to share a pointer to an object.
virtual void reset() override
Reset pointer.
Interface for ASCII input using standard streams.
Interface for ASCII output using standard streams.
Abstract class of an automatic pointer.
JAbstractAutoPointer()
Default constructor.
Handler class for automatic pointer.
friend std::ostream & operator<<(std::ostream &out, const JAutoElement_t &element)
Stream output.
void set(JDerived_t *p)
Set the automatic pointer to the given data type.
JAutoElement & operator=(JDerived_t *p)
Assign given data type to the automatic pointer.
JAutoElement< JBase_t > JAutoElement_t
JAutoElement(JType< JDerived_t > type)
Constructor.
JAutoElement & operator=(JType< JDerived_t > type)
Assign given data type to the automatic pointer.
friend std::istream & operator>>(std::istream &in, JAutoElement_t &element)
Stream input.
void set()
Set the automatic pointer to the given data type.
JAbstractAutoPointer< JBase_t > JAbstractAutoPointer_t
JAutoElement(JDerived_t *p)
Constructor.
void set(JType< JDerived_t > type)
Set the automatic pointer to the given data type.
JSharedPointer< JAbstractAutoPointer_t > JSharedPointer_t
JAutoElement()
Default constructor.
Template class for loading and storing of objects.
virtual std::ostream & write(std::ostream &out) const override
Stream output.
virtual std::istream & read(std::istream &in) override
Stream input.
JAutoPointer(JDerived_t *p)
Constructor.
JAutoPointer()
Default constructor.
Auxiliary class to load and store objects.
Definition JDriver.hh:27
static JDerived_t * load()
Definition JDriver.hh:111
static void store(std::ostream &out, const JDerived_t *p, JBool< false > option)
Definition JDriver.hh:86
Template selector class.
JLANG::JClass< JKey_t >::argument_type argument_type
JBase_t & get(argument_type key) const
Get the object corresponding to the given key.
void insert(argument_type key)
Insert data type at given key.
bool is_valid() const
Check validy of selected key.
void insert(argument_type key, JType< JDerived_t > type)
Insert data type at given key.
friend std::istream & operator>>(std::istream &in, JSelector< JKey_t, JBase_t > &selector)
Read selector from input stream.
friend std::ostream & operator<<(std::ostream &out, const JSelector< JKey_t, JBase_t > &selector)
Write selector to output stream.
JBase_t & get() const
Get the object corresponding to the selected key.
map_type::iterator iterator
map_type::const_iterator const_iterator
JSelector()
Default constructor.
map_type::value_type value_type
void insert(argument_type key, JDerived_t *p)
Insert data type at given key.
JLANG::JParameter< JKey_t > key
JKey_t getKey() const
Get key.
std::map< JKey_t, JAutoElement< JBase_t > > map_type
bool is_valid(argument_type key) const
Check validy of given key.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary classes and methods for multi-dimensional interpolations and histograms.
JArgument< T >::argument_type argument_type
Definition JClass.hh:82
Auxiliary class for a type holder.
Definition JType.hh:19