Jpp
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"
14 #include "JSupport/JFileScanner.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  */
22 namespace JSUPPORT {}
23 namespace JPP { using namespace JSUPPORT; }
24 
25 namespace JSUPPORT {
26 
28  using JLANG::JTypeList;
29  using JLANG::JTYPELIST;
30  using JLANG::JNullType;
31  using JLANG::JMultiEquals;
32  using JLANG::skip_type;
33  using JLANG::JException;
34 
35 
36  /**
37  * Auxiliary base class for file name.
38  */
40  public std::string
41  {
42  /**
43  * Default constructor.
44  */
46  {}
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<>
88  public JSingleFileScanner_t,
89  public JLimit,
90  public JMultiEquals<JSingleFileScanner<JNullType>,
91  JTYPELIST<JSingleFileScanner_t, JLimit>::typelist>
92  {
93  public:
94 
96 
97  /**
98  * Default constructor.
99  */
101  {}
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  * Read single file scanner from input.
127  *
128  * \param in input stream
129  * \param file_name single file scanner
130  * \return input stream
131  */
132  friend inline std::istream& operator>>(std::istream& in, JSingleFileScanner& file_name)
133  {
134  return in >> static_cast<std::string&>(file_name);
135  }
136 
137 
138  /**
139  * Write single file scanner to output.
140  *
141  * \param out output stream
142  * \param file_name single file scanner
143  * \return output stream
144  */
145  friend inline std::ostream& operator<<(std::ostream& out, const JSingleFileScanner& file_name)
146  {
147  return out << static_cast<const std::string&>(file_name);
148  }
149  };
150 
151 
152  /**
153  * Object reading from a single file name.
154  *
155  * This class implements the JLANG::JRewindableObjectIterator interface.
156  */
157  template<class T>
158  class JSingleFileScanner :
159  public virtual JSingleFileScanner<>,
160  public JRewindableObjectIterator<T>
161  {
162  public:
163 
166 
167  /**
168  * Default constructor.
169  */
171  counter(0)
172  {}
173 
174 
175  /**
176  * Copy constructor.
177  *
178  * Note that the counter limit is not copied and the counter is set to zero.
179  *
180  * \param input input
181  */
182  template<class JTypelist_t>
184  counter(0)
185  {
186  this->configure(input.getFilename(), JLimit());
187  }
188 
189 
190  /**
191  * Constructor.
192  *
193  * \param file_name file name
194  * \param limit limit
195  */
196  JSingleFileScanner(const input_type& file_name, const JLimit& limit = JLimit()) :
197  counter(0)
198  {
199  this->configure(file_name, limit);
200  }
201 
202 
203  /**
204  * Get counter.
205  *
206  * \return counter
207  */
209  {
210  return counter;
211  }
212 
213 
214  /**
215  * Rewind.
216  */
217  virtual void rewind()
218  {
219  if (scanner.is_open()) {
220  scanner.close();
221  }
222 
223  counter = 0;
224 
225  scanner.reset();
226  }
227 
228 
229  /**
230  * Check availability of next element.
231  *
232  * \return true if the iteration has more elements; else false
233  */
234  virtual bool hasNext()
235  {
236  if (is_valid()) {
237 
238  if (counter < getUpperLimit()) {
239 
240  // first time around
241 
242  if (!scanner.is_open()) {
243  scanner.open(this->getFilename().c_str());
244  }
245 
246  if (counter < getLowerLimit()) {
247  counter += scanner.skip(getLowerLimit() - counter);
248  }
249 
250  if (!scanner.hasNext()) {
251 
252  scanner.close();
253 
254  return false;
255  }
256 
257  return true;
258 
259  } else {
260 
261  // last time around
262 
263  if (scanner.is_open()) {
264  scanner.close();
265  }
266 
267  scanner.reset();
268  }
269  }
270 
271  return false;
272  }
273 
274 
275  /**
276  * Get next element.
277  *
278  * \return pointer to element
279  */
280  virtual const pointer_type& next()
281  {
282  ++counter;
283 
284  return scanner.next();
285  }
286 
287 
288  /**
289  * Skip items.
290  *
291  * \param ns number of items to skip
292  * \return number of items skipped
293  */
294  virtual skip_type skip(const skip_type ns)
295  {
296  const skip_type previous = counter;
297 
298  counter += scanner.skip(ns);
299 
300  return counter - previous;
301  }
302 
303 
304  protected:
307  };
308 
309 
310  /**
311  * Template specialisation of JSingleFileScanner for single data types.
312  *
313  * This class implements the JLANG::JRewindableObjectIterator interface for
314  * each data type.
315  * The method rewind(), rewinds each object iterator.
316  */
317  template<class JHead_t, class JTail_t>
318  class JSingleFileScanner< JTypeList<JHead_t, JTail_t> > :
319  public virtual JSingleFileScanner<>,
320  public JSingleFileScanner<JHead_t>,
321  public JSingleFileScanner<JTail_t>,
322  public JRewindableObjectIterator< JTypeList<JHead_t, JTail_t> >
323  {
324  public:
325 
327 
330 
331 
332  /**
333  * Default constructor.
334  */
337  {}
338 
339 
340  /**
341  * Copy constructor.
342  *
343  * Note that the counter limit is not copied and the counter is set to zero.
344  *
345  * \param input input
346  */
347  template<class JTypelist_t>
349  {
350  this->configure(input.getFilename(), JLimit());
351  }
352 
353 
354  /**
355  * Constructor.
356  *
357  * \param file_name file name
358  * \param limit limit
359  */
360  JSingleFileScanner(const input_type& file_name, const JLimit& limit = JLimit())
361  {
362  this->configure(file_name, limit);
363  }
364 
365 
366  /**
367  * Rewind.\n
368  * This method rewinds the JSingleFileScanner for each data type.
369  */
370  virtual void rewind()
371  {
374  }
375 
376 
377  /**
378  * Skip items.
379  *
380  * \param ns number of items to skip
381  * \return number of items skipped
382  */
383  virtual skip_type skip(const skip_type ns)
384  {
385  if (JSingleFileScanner<JHead_t>::skip(ns) == ns &&
387  return ns;
388  else
389  throw JException("JSingleFileScanner::skip(): inconsistent number of items skipped.");
390  }
391  };
392 
393 
394  /**
395  * Terminator class of recursive JSingleFileScanner class.
396  */
397  template<class JHead_t>
399  public JSingleFileScanner<JHead_t>
400  {
401  public:
402 
404 
405  /**
406  * Default constructor.
407  */
409  JSingleFileScanner<JHead_t>()
410  {}
411 
412 
413  /**
414  * Copy constructor.
415  *
416  * Note that the counter limit is not copied and the counter is set to zero.
417  *
418  * \param input input
419  */
420  template<class JTypelist_t>
422  {
423  this->configure(input.getFilename(), JLimit());
424  }
425 
426 
427  /**
428  * Constructor.
429  *
430  * \param file_name file name
431  * \param limit limit
432  */
433  JSingleFileScanner(const input_type& file_name, const JLimit& limit = JLimit()) :
434  JSingleFileScanner<JHead_t>()
435  {
436  this->configure(file_name, limit);
437  }
438  };
439 }
440 
441 #endif
JException.hh
JLANG::skip_type
unsigned int skip_type
Type definition for number of objects to skip.
Definition: JObjectIterator.hh:25
JSUPPORT::JFileScanner
Object reading from file.
Definition: JFileScanner.hh:34
JSUPPORT::JSingleFileScanner::getCounter
counter_type getCounter() const
Get counter.
Definition: JSingleFileScanner.hh:208
JSUPPORT::JSingleFileScanner< JNullType >::~JSingleFileScanner
virtual ~JSingleFileScanner()
Virtual destructor.
Definition: JSingleFileScanner.hh:107
JSUPPORT::JLimit
Auxiliary class for defining the range of iterations of objects.
Definition: JLimit.hh:41
JSUPPORT::JSingleFileScanner< JTypeList< JHead_t, JNullType > >::JSingleFileScanner
JSingleFileScanner(const JSingleFileScanner< JTypelist_t > &input)
Copy constructor.
Definition: JSingleFileScanner.hh:421
JAANET::is_valid
bool is_valid(const T &value)
Check validity of given value.
Definition: JHead.hh:823
JSUPPORT::JSingleFileScanner_t::JSingleFileScanner_t
JSingleFileScanner_t()
Default constructor.
Definition: JSingleFileScanner.hh:45
JSUPPORT::JSingleFileScanner::JSingleFileScanner
JSingleFileScanner()
Default constructor.
Definition: JSingleFileScanner.hh:170
JLimit.hh
JObjectIterator.hh
JSUPPORT::JSingleFileScanner::next
virtual const pointer_type & next()
Get next element.
Definition: JSingleFileScanner.hh:280
JLANG::JNullType
Auxiliary class for no type definition.
Definition: JNullType.hh:19
JLANG::JTYPELIST
Auxiliary class for recursive type list generation.
Definition: JTypeList.hh:377
JSUPPORT::JSingleFileScanner::pointer_type
JRewindableObjectIterator< T >::pointer_type pointer_type
Definition: JSingleFileScanner.hh:165
JSUPPORT::JSingleFileScanner::hasNext
virtual bool hasNext()
Check availability of next element.
Definition: JSingleFileScanner.hh:234
JSUPPORT::JSingleFileScanner::JSingleFileScanner
JSingleFileScanner(const input_type &file_name, const JLimit &limit=JLimit())
Constructor.
Definition: JSingleFileScanner.hh:196
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
JLANG::JPointer< T >
JSUPPORT::JSingleFileScanner::counter
counter_type counter
Definition: JSingleFileScanner.hh:306
JSUPPORT::JSingleFileScanner< JTypeList< JHead_t, JTail_t > >::JSingleFileScanner
JSingleFileScanner(const JSingleFileScanner< JTypelist_t > &input)
Copy constructor.
Definition: JSingleFileScanner.hh:348
JSUPPORT::JSingleFileScanner::scanner
JFileScanner< T > scanner
Definition: JSingleFileScanner.hh:305
JSUPPORT::JSingleFileScanner< JTypeList< JHead_t, JTail_t > >::JSingleFileScanner
JSingleFileScanner()
Default constructor.
Definition: JSingleFileScanner.hh:335
JMultiEquals.hh
JSUPPORT::JSingleFileScanner::rewind
virtual void rewind()
Rewind.
Definition: JSingleFileScanner.hh:217
JSUPPORT::JSingleFileScanner< JTypeList< JHead_t, JTail_t > >::skip
virtual skip_type skip(const skip_type ns)
Skip items.
Definition: JSingleFileScanner.hh:383
JEEP::getFilename
std::string getFilename(const std::string &file_name)
Get file name part, i.e.
Definition: JeepToolkit.hh:86
JSUPPORT::JSingleFileScanner_t::JSingleFileScanner_t
JSingleFileScanner_t(const std::string &file_name)
Constructor.
Definition: JSingleFileScanner.hh:54
JSUPPORT::JSingleFileScanner< JTypeList< JHead_t, JTail_t > >::input_type
JSingleFileScanner ::input_type input_type
Definition: JSingleFileScanner.hh:326
JLANG::JTypeList
Type list.
Definition: JTypeList.hh:22
JSUPPORT::JSingleFileScanner< JTypeList< JHead_t, JNullType > >::input_type
JSingleFileScanner ::input_type input_type
Definition: JSingleFileScanner.hh:403
JSUPPORT::JSingleFileScanner< JNullType >::configure
void configure(const input_type &input, const JLimit &limit)
Configure.
Definition: JSingleFileScanner.hh:117
JLANG::JRewindableObjectIterator
Interface for object iteration with rewinding.
Definition: JObjectIterator.hh:362
JLANG::JMultiEquals
Template definition of auxiliary base class for composite data structures composed of base classes wi...
Definition: JMultiEquals.hh:31
JSUPPORT::JSingleFileScanner::skip
virtual skip_type skip(const skip_type ns)
Skip items.
Definition: JSingleFileScanner.hh:294
JSUPPORT::JSingleFileScanner_t
Auxiliary base class for file name.
Definition: JSingleFileScanner.hh:39
JSUPPORT::JSingleFileScanner< JTypeList< JHead_t, JTail_t > >::JSingleFileScanner
JSingleFileScanner(const input_type &file_name, const JLimit &limit=JLimit())
Constructor.
Definition: JSingleFileScanner.hh:360
JSUPPORT::JSingleFileScanner< JNullType >::operator>>
friend std::istream & operator>>(std::istream &in, JSingleFileScanner &file_name)
Read single file scanner from input.
Definition: JSingleFileScanner.hh:132
JSUPPORT::JSingleFileScanner< JTypeList< JHead_t, JTail_t > >::rewind
virtual void rewind()
Rewind.
Definition: JSingleFileScanner.hh:370
JSUPPORT::JSingleFileScanner::input_type
JSingleFileScanner ::input_type input_type
Definition: JSingleFileScanner.hh:164
std
Definition: jaanetDictionary.h:36
JSUPPORT
Support classes and methods for experiment specific I/O.
Definition: JDataWriter.cc:38
JTypeList.hh
JSUPPORT::JSingleFileScanner_t::getFilename
const JSingleFileScanner_t & getFilename() const
Get file name.
Definition: JSingleFileScanner.hh:64
JNullType.hh
JSUPPORT::JSingleFileScanner< JTypeList< JHead_t, JNullType > >::JSingleFileScanner
JSingleFileScanner(const input_type &file_name, const JLimit &limit=JLimit())
Constructor.
Definition: JSingleFileScanner.hh:433
JFileScanner.hh
JSUPPORT::JSingleFileScanner< JNullType >::JSingleFileScanner
JSingleFileScanner()
Default constructor.
Definition: JSingleFileScanner.hh:100
JSUPPORT::JSingleFileScanner< JTypeList< JHead_t, JNullType > >::JSingleFileScanner
JSingleFileScanner()
Default constructor.
Definition: JSingleFileScanner.hh:408
JSUPPORT::JSingleFileScanner::JSingleFileScanner
JSingleFileScanner(const JSingleFileScanner< JTypelist_t > &input)
Copy constructor.
Definition: JSingleFileScanner.hh:183
JROOT::counter_type
Long64_t counter_type
Type definition for counter.
Definition: JCounter.hh:24
JSUPPORT::JSingleFileScanner< JNullType >::input_type
JSingleFileScanner_t input_type
Definition: JSingleFileScanner.hh:95
JSUPPORT::JSingleFileScanner< JNullType >::operator<<
friend std::ostream & operator<<(std::ostream &out, const JSingleFileScanner &file_name)
Write single file scanner to output.
Definition: JSingleFileScanner.hh:145
JTOOLS::configure
void configure(const T &value, const JAbstractCollection< JAbscissa_t > &bounds, JBool< false > option)
Configuration of value.
Definition: JToolsToolkit.hh:285
JLANG::JException
General exception.
Definition: JException.hh:23
JSUPPORT::JSingleFileScanner
Object reading from a list of files.
Definition: JSingleFileScanner.hh:75