Jpp 21.0.0-rc.3
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 <memory>
7#include <map>
8
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
29 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 std::shared_ptr<JBase_t>,
41 public JStreamInput,
42 public JStreamOutput
43 {
44 public:
45 /**
46 * Default constructor.
47 */
50 };
51
52
53 /**
54 * Template class for loading and storing of objects.
55 * This class implements the JAbstractAutoPointer interface.
56 */
57 template<class JDerived_t, class JBase_t>
59 public JAbstractAutoPointer<JBase_t>,
60 private JDriver<JDerived_t>
61 {
62 public:
63 /**
64 * Default constructor.
65 */
67 JAbstractAutoPointer<JBase_t>()
68 {}
69
70
71 /**
72 * Constructor.
73 * This class owns the pointer.
74 *
75 * \param p pointer to object
76 */
77 JAutoPointer(JDerived_t* p) :
78 JAbstractAutoPointer<JBase_t>()
79 {
80 this->reset(p);
81 }
82
83
84 /**
85 * Stream input.
86 * This method creates a new object and read the corresponding input data
87 * if the redirect operator >>(isteam&) is defined for this data type.
88 *
89 * \param in input stream
90 * \return input stream
91 */
92 virtual std::istream& read(std::istream& in) override
93 {
94 JDerived_t* p = this->load(in);
95
96 if (p != NULL) {
97 this->reset(p);
98 }
99
100 return in;
101 }
102
103
104 /**
105 * Stream output.
106 * This method writes the corresponding data
107 * if the redirect operator <<(osteam&) is defined for this data type.
108 *
109 * \param out output stream
110 * \return output stream
111 */
112 virtual std::ostream& write(std::ostream& out) const override
113 {
114 this->store(out, this->get());
115
116 return out;
117 }
118 };
119
120
121 /**
122 * Handler class for automatic pointer.
123 */
124 template<class JBase_t>
126 public std::shared_ptr< JAbstractAutoPointer<JBase_t> >
127 {
128 public:
129
131
132
133 /**
134 * Default constructor.
135 */
137 {}
138
139
140 /**
141 * Constructor.
142 *
143 * \param type data type
144 */
145 template<class JDerived_t>
147 {
148 this->set(type);
149 }
150
151
152 /**
153 * Constructor.
154 * This class owns the pointer.
155 *
156 * \param p pointer to object
157 */
158 template<class JDerived_t>
159 JAutoElement(JDerived_t* p)
160 {
161 this->set(p);
162 }
163
164
165 /**
166 * Set the automatic pointer to the given data type.
167 */
168 template<class JDerived_t>
169 void set()
170 {
171 this->set(JType<JDerived_t>());
172 }
173
174
175 /**
176 * Set the automatic pointer to the given data type.
177 *
178 * \param type data type
179 */
180 template<class JDerived_t>
182 {
184 }
185
186
187 /**
188 * Set the automatic pointer to the given data type.
189 *
190 * \param p pointer to object
191 */
192 template<class JDerived_t>
193 void set(JDerived_t* p)
194 {
196 }
197
198
199 /**
200 * Assign given data type to the automatic pointer.
201 *
202 * \param type data type
203 * \return this JAutoElement
204 */
205 template<class JDerived_t>
207 {
208 set(type);
209
210 return *this;
211 }
212
213
214 /**
215 * Assign given data type to the automatic pointer.
216 *
217 * \param p pointer to object
218 * \return this JAutoElement
219 */
220 template<class JDerived_t>
221 JAutoElement& operator=(JDerived_t* p)
222 {
223 set(p);
224
225 return *this;
226 }
227
228
229 /**
230 * Stream input.
231 *
232 * \param in input stream
233 * \param element element
234 * \return input stream
235 */
236 friend inline std::istream& operator>>(std::istream& in, JAutoElement_t& element)
237 {
238 if (element.is_valid())
239 return element.get()->read(in);
240 else
241 return in;
242 }
243
244
245 /**
246 * Stream output.
247 *
248 * \param out output stream
249 * \param element element
250 * \return output stream
251 */
252 friend inline std::ostream& operator<<(std::ostream& out, const JAutoElement_t& element)
253 {
254 if (element.is_valid())
255 return element.get()->write(out);
256 else
257 return out;
258 }
259 };
260
261
262 /**
263 * Template selector class.
264 * This class can be used to set up a list of keys that are mapped to handlers
265 * which create new objects by stream input operations.
266 */
267 template<class JKey_t, class JBase_t>
268 class JSelector :
269 public std::map<JKey_t, JAutoElement<JBase_t> >
270 {
271 public:
272
273 typedef std::map <JKey_t, JAutoElement<JBase_t> > map_type;
274 typedef typename map_type::const_iterator const_iterator;
275 typedef typename map_type::iterator iterator;
277 typedef typename map_type::value_type value_type;
278
279
280 /**
281 * Default constructor.
282 */
284 map_type()
285 {}
286
287
288 /**
289 * Insert data type at given key.
290 *
291 * \param key key
292 * \param type data type
293 */
294 template<class JDerived_t>
296 {
297 map_type::insert(value_type(key, JAutoElement<JBase_t>(type)));
298 }
299
300
301 /**
302 * Insert data type at given key.
303 *
304 * \param key key
305 */
306 template<class JDerived_t>
311
312
313 /**
314 * Insert data type at given key.
315 * This object pointed to will become the default value.
316 *
317 * \param key key
318 * \param p pointer to object
319 */
320 template<class JDerived_t>
321 void insert(argument_type key, JDerived_t* p)
322 {
323 this->key = key;
324
325 map_type::insert(value_type(key, JAutoElement<JBase_t>(p)));
326 }
327
328
329 /**
330 * Get key.
331 *
332 * \return key
333 */
334 JKey_t getKey() const
335 {
336 return this->key;
337 }
338
339
340 /**
341 * Check validy of given key.
342 *
343 * \param key key
344 * \return true if given key maps to a non-NULL value; else false
345 */
346 bool is_valid(argument_type key) const
347 {
348 const_iterator i = this->find(key);
349
350 return i != this->end() && i->second.is_valid() && i->second.get()->is_valid();
351 }
352
353
354 /**
355 * Check validy of selected key.
356 *
357 * \return true if selected key maps to a valid object; else false
358 */
359 bool is_valid() const
360 {
361 return key.isDefined() && is_valid(key);
362 }
363
364
365 /**
366 * Get the object corresponding to the given key.
367 *
368 * \param key key
369 * \return object
370 */
371 JBase_t& get(argument_type key) const
372 {
373 const_iterator i = this->find(key);
374
375 if (i != this->end() && i->second.is_valid() && i->second.get()->is_valid())
376 return *(i->second.get()->get());
377 else
378 throw JNullPointerException("JSelecor<>: no object.");
379 }
380
381
382 /**
383 * Get the object corresponding to the selected key.
384 *
385 * \return selected object
386 */
387 JBase_t& get() const
388 {
389 if (key.isDefined())
390 return this->get(key);
391 else
392 throw JNullPointerException("JSelecor<>: no selection.");
393 }
394
395
396 /**
397 * Read selector from input stream.
398 *
399 * \param in input stream
400 * \param selector selector
401 * \return input stream
402 */
403 friend inline std::istream& operator>>(std::istream& in, JSelector<JKey_t, JBase_t>& selector)
404 {
405 if (in >> selector.key) {
406
407 typename JSelector<JKey_t, JBase_t>::iterator i = selector.find(selector.key);
408
409 if (i != selector.end() && i->second.is_valid())
410 in >> i->second;
411 else
412 in.setstate(std::ios_base::failbit);
413 }
414
415 return in;
416 }
417
418
419 /**
420 * Write selector to output stream.
421 *
422 * \param out output stream
423 * \param selector selector
424 * \return output stream
425 */
426 friend inline std::ostream& operator<<(std::ostream& out, const JSelector<JKey_t, JBase_t>& selector)
427 {
428 if (selector.key.isDefined()) {
429
430 typename JSelector<JKey_t, JBase_t>::const_iterator i = selector.find(selector.key);
431
432 if (i != selector.end() && i->second.is_valid()) {
433 return out << selector.key << ' ' << *(i->second);
434 }
435 }
436
437 return out;
438 }
439
440
441 protected:
443 };
444}
445
446#endif
Exceptions.
Exception for null pointer operation.
Parameter class.
Definition JParameter.hh:36
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.
JAutoElement(JDerived_t *p)
Constructor.
void set(JType< JDerived_t > type)
Set the automatic pointer to the given data type.
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:26
static JDerived_t * load()
Definition JDriver.hh:110
static void store(std::ostream &out, const JDerived_t *p, std::false_type option)
Definition JDriver.hh:85
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
JParameter< JKey_t > key
JSelector()
Default constructor.
map_type::value_type value_type
void insert(argument_type key, JDerived_t *p)
Insert data type at given 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.
void reset(T &value)
Reset value.
JArgument< T >::argument_type argument_type
Definition JClass.hh:82
Auxiliary class for a type holder.
Definition JType.hh:19