Jpp 19.3.0-rc.1
the software that should make you happy
Loading...
Searching...
No Matches
JSingleFileScanner.hh
Go to the documentation of this file.
1#ifndef __JSUPPORT__JSINGLEFILESCANNER__
2#define __JSUPPORT__JSINGLEFILESCANNER__
3
4#include <string>
5#include <limits>
6
8#include "JLang/JTypeList.hh"
9#include "JLang/JNullType.hh"
10#include "JLang/JMultiEquals.hh"
11#include "JLang/JException.hh"
12
13#include "JSupport/JLimit.hh"
15
16
17/**
18 * \file
19 * Scanning of objects from a single file according a format that follows from the extension of each file name.
20 * \author mdejong
21 */
22namespace JSUPPORT {}
23namespace JPP { using namespace JSUPPORT; }
24
25namespace JSUPPORT {
26
28 using JLANG::JTypeList;
29 using JLANG::JTYPELIST;
30 using JLANG::JNullType;
32 using JLANG::skip_type;
34
35
36 /**
37 * Auxiliary base class for file name.
38 */
40 public std::string
41 {
42 /**
43 * Default constructor.
44 */
47
48
49 /**
50 * Constructor.
51 *
52 * \param file_name file name
53 */
54 JSingleFileScanner_t(const std::string& file_name) :
55 std::string(file_name)
56 {}
57
58
59 /**
60 * Get file name.
61 *
62 * \return file name
63 */
65 {
66 return static_cast<const JSingleFileScanner_t&>(*this);
67 }
68 };
69
70
71 /**
72 * Object reading from a list of files.
73 */
74 template<class T = JNullType>
76
77
78 /**
79 * Template specialisation of JSingleFileScanner for undefined type.\n
80 * This specialisation is used as a virtual base class for all implementations of JSingleFileScanner for defined type(s).
81 *
82 * This class is a simple container for a single file name and has an additional counter limit by extending from JSUPPORT::JLimit.\n
83 * This counter limit will be used to limit the number of calls to the method <tt>next</tt> of the derived class.\n
84 * Note that the counter limit can only be set via the member or friend methods of JSUPPORT::JLimit.
85 */
86 template<>
89 public JLimit,
90 public JMultiEquals<JSingleFileScanner<JNullType>,
91 JTYPELIST<JSingleFileScanner_t, JLimit>::typelist>
92 {
93 public:
94
96
97 /**
98 * Default constructor.
99 */
102
103
104 /**
105 * Virtual destructor.
106 */
108 {}
109
110
111 /**
112 * Configure.
113 *
114 * \param input file name
115 * \param limit limit
116 */
117 void configure(const input_type& input, const JLimit& limit)
118 {
119 static_cast<JSingleFileScanner_t&>(*this) = input;
120
121 this->setLimit(limit);
122 }
123
124
125 /**
126 * Configure.
127 *
128 * \param input file name
129 */
130 void configure(const input_type& input)
131 {
132 this->configure(input, JLimit());
133 }
134
135
136 /**
137 * Read single file scanner from input.
138 *
139 * \param in input stream
140 * \param file_name single file scanner
141 * \return input stream
142 */
143 friend inline std::istream& operator>>(std::istream& in, JSingleFileScanner& file_name)
144 {
145 return in >> static_cast<std::string&>(file_name);
146 }
147
148
149 /**
150 * Write single file scanner to output.
151 *
152 * \param out output stream
153 * \param file_name single file scanner
154 * \return output stream
155 */
156 friend inline std::ostream& operator<<(std::ostream& out, const JSingleFileScanner& file_name)
157 {
158 return out << static_cast<const std::string&>(file_name);
159 }
160 };
161
162
163 /**
164 * Object reading from a single file name.
165 *
166 * This class implements the JLANG::JRewindableObjectIterator interface.
167 */
168 template<class T>
170 public virtual JSingleFileScanner<>,
172 {
173 public:
174
177
178 /**
179 * Default constructor.
180 */
182 counter(0)
183 {}
184
185
186 /**
187 * Copy constructor.
188 *
189 * Note that the counter limit is not copied and the counter is set to zero.
190 *
191 * \param input input
192 */
193 template<class JTypelist_t>
195 counter(0)
196 {
197 this->configure(input.getFilename(), JLimit());
198 }
199
200
201 /**
202 * Constructor.
203 *
204 * \param file_name file name
205 */
206 JSingleFileScanner(const input_type& file_name) :
207 counter(0)
208 {
209 this->configure(file_name, JLimit());
210 }
211
212
213 /**
214 * Constructor.
215 *
216 * \param file_name file name
217 * \param limit limit
218 */
219 JSingleFileScanner(const input_type& file_name, const JLimit& limit) :
220 counter(0)
221 {
222 this->configure(file_name, limit);
223 }
224
225
226 /**
227 * Get counter.
228 *
229 * \return counter
230 */
232 {
233 return counter;
234 }
235
236
237 /**
238 * Rewind.
239 */
240 virtual void rewind() override
241 {
242 if (scanner.is_open()) {
243 scanner.close();
244 }
245
246 counter = 0;
247
248 scanner.reset();
249 }
250
251
252 /**
253 * Check availability of next element.
254 *
255 * \return true if the iteration has more elements; else false
256 */
257 virtual bool hasNext() override
258 {
259 if (is_valid()) {
260
261 if (counter < getUpperLimit()) {
262
263 // first time around
264
265 if (!scanner.is_open()) {
266 scanner.open(this->getFilename().c_str());
267 }
268
269 if (counter < getLowerLimit()) {
270 counter += scanner.skip(getLowerLimit() - counter);
271 }
272
273 if (!scanner.hasNext()) {
274
275 scanner.close();
276
277 return false;
278 }
279
280 return true;
281
282 } else {
283
284 // last time around
285
286 if (scanner.is_open()) {
287 scanner.close();
288 }
289
290 scanner.reset();
291 }
292 }
293
294 return false;
295 }
296
297
298 /**
299 * Get next element.
300 *
301 * \return pointer to element
302 */
303 virtual const pointer_type& next() override
304 {
305 ++counter;
306
307 return scanner.next();
308 }
309
310
311 /**
312 * Skip items.
313 *
314 * \param ns number of items to skip
315 * \return number of items skipped
316 */
317 virtual skip_type skip(const skip_type ns) override
318 {
319 const skip_type previous = counter;
320
321 counter += scanner.skip(ns);
322
323 return counter - previous;
324 }
325
326
327 protected:
330 };
331
332
333 /**
334 * Template specialisation of JSingleFileScanner for single data types.
335 *
336 * This class implements the JLANG::JRewindableObjectIterator interface for
337 * each data type.
338 * The method rewind(), rewinds each object iterator.
339 */
340 template<class JHead_t, class JTail_t>
341 class JSingleFileScanner< JTypeList<JHead_t, JTail_t> > :
342 public virtual JSingleFileScanner<>,
343 public JSingleFileScanner<JHead_t>,
344 public JSingleFileScanner<JTail_t>,
345 public JRewindableObjectIterator< JTypeList<JHead_t, JTail_t> >
346 {
347 public:
348
350
351 using JSingleFileScanner<JHead_t>::hasNext;
352 using JSingleFileScanner<JHead_t>::next;
353
354
355 /**
356 * Default constructor.
357 */
361
362
363 /**
364 * Copy constructor.
365 *
366 * Note that the counter limit is not copied and the counter is set to zero.
367 *
368 * \param input input
369 */
370 template<class JTypelist_t>
372 {
373 this->configure(input.getFilename(), JLimit());
374 }
375
376
377 /**
378 * Constructor.
379 *
380 * \param file_name file name
381 */
383 {
384 this->configure(file_name, JLimit());
385 }
386
387
388 /**
389 * Constructor.
390 *
391 * \param file_name file name
392 * \param limit limit
393 */
394 JSingleFileScanner(const input_type& file_name, const JLimit& limit)
395 {
396 this->configure(file_name, limit);
397 }
398
399
400 /**
401 * Rewind.\n
402 * This method rewinds the JSingleFileScanner for each data type.
403 */
404 virtual void rewind() override
405 {
408 }
409
410
411 /**
412 * Skip items.
413 *
414 * \param ns number of items to skip
415 * \return number of items skipped
416 */
417 virtual skip_type skip(const skip_type ns) override
418 {
419 if (JSingleFileScanner<JHead_t>::skip(ns) == ns &&
421 return ns;
422 else
423 THROW(JException, "JSingleFileScanner::skip(): inconsistent number of items skipped.");
424 }
425 };
426
427
428 /**
429 * Terminator class of recursive JSingleFileScanner class.
430 */
431 template<class JHead_t>
433 public JSingleFileScanner<JHead_t>
434 {
435 public:
436
438
439 /**
440 * Default constructor.
441 */
443 JSingleFileScanner<JHead_t>()
444 {}
445
446
447 /**
448 * Copy constructor.
449 *
450 * Note that the counter limit is not copied and the counter is set to zero.
451 *
452 * \param input input
453 */
454 template<class JTypelist_t>
456 {
457 this->configure(input.getFilename(), JLimit());
458 }
459
460
461 /**
462 * Constructor.
463 *
464 * \param file_name file name
465 */
466 JSingleFileScanner(const input_type& file_name) :
467 JSingleFileScanner<JHead_t>()
468 {
469 this->configure(file_name, JLimit());
470 }
471
472
473 /**
474 * Constructor.
475 *
476 * \param file_name file name
477 * \param limit limit
478 */
479 JSingleFileScanner(const input_type& file_name, const JLimit& limit) :
480 JSingleFileScanner<JHead_t>()
481 {
482 this->configure(file_name, limit);
483 }
484 };
485}
486
487#endif
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Auxiliaries for defining the range of iterations of objects.
General exception.
Definition JException.hh:24
Interface for object iteration with rewinding.
Object reading from file.
friend std::ostream & operator<<(std::ostream &out, const JSingleFileScanner &file_name)
Write single file scanner to output.
void configure(const input_type &input, const JLimit &limit)
Configure.
friend std::istream & operator>>(std::istream &in, JSingleFileScanner &file_name)
Read single file scanner from input.
void configure(const input_type &input)
Configure.
virtual ~JSingleFileScanner()
Virtual destructor.
JSingleFileScanner(const input_type &file_name, const JLimit &limit)
Constructor.
JSingleFileScanner(const JSingleFileScanner< JTypelist_t > &input)
Copy constructor.
JSingleFileScanner(const input_type &file_name, const JLimit &limit)
Constructor.
virtual skip_type skip(const skip_type ns) override
Skip items.
JSingleFileScanner(const JSingleFileScanner< JTypelist_t > &input)
Copy constructor.
Object reading from a list of files.
JSingleFileScanner()
Default constructor.
virtual void rewind() override
Rewind.
JRewindableObjectIterator< T >::pointer_type pointer_type
virtual bool hasNext() override
Check availability of next element.
counter_type getCounter() const
Get counter.
virtual const pointer_type & next() override
Get next element.
JSingleFileScanner(const input_type &file_name)
Constructor.
virtual skip_type skip(const skip_type ns) override
Skip items.
JSingleFileScanner(const JSingleFileScanner< JTypelist_t > &input)
Copy constructor.
JSingleFileScanner(const input_type &file_name, const JLimit &limit)
Constructor.
JSingleFileScanner ::input_type input_type
std::string getFilename(const std::string &file_name)
Get file name part, i.e. part after last JEEP::PATHNAME_SEPARATOR if any.
unsigned int skip_type
Type definition for number of objects to skip.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Long64_t counter_type
Type definition for counter.
bool is_valid(const json &js)
Check validity of JSon data.
Support classes and methods for experiment specific I/O.
void configure(const T &value, const JAbstractCollection< JAbscissa_t > &bounds, JBool< false > option)
Configuration of value.
Template definition of auxiliary base class for data structures composed of multiple base classes wit...
Auxiliary class for no type definition.
Definition JNullType.hh:19
Auxiliary class for recursive type list generation.
Definition JTypeList.hh:351
Type list.
Definition JTypeList.hh:23
Auxiliary class for defining the range of iterations of objects.
Definition JLimit.hh:45
Auxiliary base class for file name.
const JSingleFileScanner_t & getFilename() const
Get file name.
JSingleFileScanner_t()
Default constructor.
JSingleFileScanner_t(const std::string &file_name)
Constructor.