Jpp 21.0.0-rc.3
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"
16
17
18/**
19 * \file
20 * Scanning of objects from a single file according a format that follows from the extension of each file name.
21 * \author mdejong
22 */
23namespace JSUPPORT {}
24namespace JPP { using namespace JSUPPORT; }
25
26namespace JSUPPORT {
27
29 using JLANG::JTypeList;
30 using JLANG::JTYPELIST;
31 using JLANG::JNullType;
33 using JLANG::skip_type;
35
36
37 /**
38 * Auxiliary base class for file name.
39 */
41 public std::string
42 {
43 /**
44 * Default constructor.
45 */
48
49
50 /**
51 * Constructor.
52 *
53 * \param file_name file name
54 */
55 JSingleFileScanner_t(const std::string& file_name) :
56 std::string(file_name)
57 {}
58
59
60 /**
61 * Get file name.
62 * This method constitutes a light weight wrapper to resolve symbolic links on the fly.
63 *
64 * \return file name
65 */
66 const std::string getFilename() const
67 {
68 return JSYSTEM::resolveSymlink(static_cast<const std::string&>(*this)).string();
69 }
70 };
71
72
73 /**
74 * Object reading from a list of files.
75 */
76 template<class T = JNullType>
78
79
80 /**
81 * Template specialisation of JSingleFileScanner for undefined type.\n
82 * This specialisation is used as a virtual base class for all implementations of JSingleFileScanner for defined type(s).
83 *
84 * This class is a simple container for a single file name and has an additional counter limit by extending from JSUPPORT::JLimit.\n
85 * This counter limit will be used to limit the number of calls to the method <tt>next</tt> of the derived class.\n
86 * Note that the counter limit can only be set via the member or friend methods of JSUPPORT::JLimit.
87 */
88 template<>
91 public JLimit,
92 public JMultiEquals<JSingleFileScanner<JNullType>,
93 JTYPELIST<JSingleFileScanner_t, JLimit>::typelist>
94 {
95 public:
96
98
99 /**
100 * Default constructor.
101 */
104
105
106 /**
107 * Virtual destructor.
108 */
110 {}
111
112
113 /**
114 * Configure.
115 *
116 * \param input file name
117 * \param limit limit
118 */
119 void configure(const input_type& input, const JLimit& limit)
120 {
121 static_cast<JSingleFileScanner_t&>(*this) = input;
122
123 this->setLimit(limit);
124 }
125
126
127 /**
128 * Configure.
129 *
130 * \param input file name
131 */
132 void configure(const input_type& input)
133 {
134 this->configure(input, JLimit());
135 }
136
137
138 /**
139 * Read single file scanner from input.
140 *
141 * \param in input stream
142 * \param file_name single file scanner
143 * \return input stream
144 */
145 friend inline std::istream& operator>>(std::istream& in, JSingleFileScanner& file_name)
146 {
147 return in >> static_cast<std::string&>(file_name);
148 }
149
150
151 /**
152 * Write single file scanner to output.
153 *
154 * \param out output stream
155 * \param file_name single file scanner
156 * \return output stream
157 */
158 friend inline std::ostream& operator<<(std::ostream& out, const JSingleFileScanner& file_name)
159 {
160 return out << static_cast<const std::string&>(file_name);
161 }
162 };
163
164
165 /**
166 * Object reading from a single file name.
167 *
168 * This class implements the JLANG::JRewindableObjectIterator interface.
169 */
170 template<class T>
172 public virtual JSingleFileScanner<>,
174 {
175 public:
176
179
180 /**
181 * Default constructor.
182 */
184 counter(0)
185 {}
186
187
188 /**
189 * Copy constructor.
190 *
191 * Note that the counter limit is not copied and the counter is set to zero.
192 *
193 * \param input input
194 */
195 template<class JTypelist_t>
197 counter(0)
198 {
199 this->configure(input.getFilename(), JLimit());
200 }
201
202
203 /**
204 * Constructor.
205 *
206 * \param file_name file name
207 */
208 JSingleFileScanner(const input_type& file_name) :
209 counter(0)
210 {
211 this->configure(file_name, JLimit());
212 }
213
214
215 /**
216 * Constructor.
217 *
218 * \param file_name file name
219 * \param limit limit
220 */
221 JSingleFileScanner(const input_type& file_name, const JLimit& limit) :
222 counter(0)
223 {
224 this->configure(file_name, limit);
225 }
226
227
228 /**
229 * Get counter.
230 *
231 * \return counter
232 */
234 {
235 return counter;
236 }
237
238
239 /**
240 * Rewind.
241 */
242 virtual void rewind() override
243 {
244 if (scanner.is_open()) {
245 scanner.close();
246 }
247
248 counter = 0;
249
250 scanner.reset();
251 }
252
253
254 /**
255 * Check availability of next element.
256 *
257 * \return true if the iteration has more elements; else false
258 */
259 virtual bool hasNext() override
260 {
261 if (is_valid()) {
262
263 if (counter < getUpperLimit()) {
264
265 // first time around
266
267 if (!scanner.is_open()) {
268 scanner.open(this->getFilename().c_str());
269 }
270
271 if (counter < getLowerLimit()) {
272 counter += scanner.skip(getLowerLimit() - counter);
273 }
274
275 if (!scanner.hasNext()) {
276
277 scanner.close();
278
279 return false;
280 }
281
282 return true;
283
284 } else {
285
286 // last time around
287
288 if (scanner.is_open()) {
289 scanner.close();
290 }
291
292 scanner.reset();
293 }
294 }
295
296 return false;
297 }
298
299
300 /**
301 * Get next element.
302 *
303 * \return pointer to element
304 */
305 virtual const pointer_type& next() override
306 {
307 ++counter;
308
309 return scanner.next();
310 }
311
312
313 /**
314 * Skip items.
315 *
316 * \param ns number of items to skip
317 * \return number of items skipped
318 */
319 virtual skip_type skip(const skip_type ns) override
320 {
321 const skip_type previous = counter;
322
323 counter += scanner.skip(ns);
324
325 return counter - previous;
326 }
327
328
329 protected:
332 };
333
334
335 /**
336 * Template specialisation of JSingleFileScanner for single data types.
337 *
338 * This class implements the JLANG::JRewindableObjectIterator interface for
339 * each data type.
340 * The method rewind(), rewinds each object iterator.
341 */
342 template<class JHead_t, class JTail_t>
343 class JSingleFileScanner< JTypeList<JHead_t, JTail_t> > :
344 public virtual JSingleFileScanner<>,
345 public JSingleFileScanner<JHead_t>,
346 public JSingleFileScanner<JTail_t>,
347 public JRewindableObjectIterator< JTypeList<JHead_t, JTail_t> >
348 {
349 public:
350
352
353 using JSingleFileScanner<JHead_t>::hasNext;
354 using JSingleFileScanner<JHead_t>::next;
355
356
357 /**
358 * Default constructor.
359 */
363
364
365 /**
366 * Copy constructor.
367 *
368 * Note that the counter limit is not copied and the counter is set to zero.
369 *
370 * \param input input
371 */
372 template<class JTypelist_t>
374 {
375 this->configure(input.getFilename(), JLimit());
376 }
377
378
379 /**
380 * Constructor.
381 *
382 * \param file_name file name
383 */
385 {
386 this->configure(file_name, JLimit());
387 }
388
389
390 /**
391 * Constructor.
392 *
393 * \param file_name file name
394 * \param limit limit
395 */
396 JSingleFileScanner(const input_type& file_name, const JLimit& limit)
397 {
398 this->configure(file_name, limit);
399 }
400
401
402 /**
403 * Rewind.\n
404 * This method rewinds the JSingleFileScanner for each data type.
405 */
406 virtual void rewind() override
407 {
410 }
411
412
413 /**
414 * Skip items.
415 *
416 * \param ns number of items to skip
417 * \return number of items skipped
418 */
419 virtual skip_type skip(const skip_type ns) override
420 {
421 if (JSingleFileScanner<JHead_t>::skip(ns) == ns &&
423 return ns;
424 else
425 THROW(JException, "JSingleFileScanner::skip(): inconsistent number of items skipped.");
426 }
427 };
428
429
430 /**
431 * Terminator class of recursive JSingleFileScanner class.
432 */
433 template<class JHead_t>
435 public JSingleFileScanner<JHead_t>
436 {
437 public:
438
440
441 /**
442 * Default constructor.
443 */
445 JSingleFileScanner<JHead_t>()
446 {}
447
448
449 /**
450 * Copy constructor.
451 *
452 * Note that the counter limit is not copied and the counter is set to zero.
453 *
454 * \param input input
455 */
456 template<class JTypelist_t>
458 {
459 this->configure(input.getFilename(), JLimit());
460 }
461
462
463 /**
464 * Constructor.
465 *
466 * \param file_name file name
467 */
468 JSingleFileScanner(const input_type& file_name) :
469 JSingleFileScanner<JHead_t>()
470 {
471 this->configure(file_name, JLimit());
472 }
473
474
475 /**
476 * Constructor.
477 *
478 * \param file_name file name
479 * \param limit limit
480 */
481 JSingleFileScanner(const input_type& file_name, const JLimit& limit) :
482 JSingleFileScanner<JHead_t>()
483 {
484 this->configure(file_name, limit);
485 }
486 };
487}
488
489#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:25
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.
std::filesystem::path resolveSymlink(const std::filesystem::path &file_name)
Resolve symbolic links.
void configure(const T &value, const JAbstractCollection< JAbscissa_t > &bounds, std::false_type 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 std::string getFilename() const
Get file name.
JSingleFileScanner_t()
Default constructor.
JSingleFileScanner_t(const std::string &file_name)
Constructor.