Jpp  master_rocky
the software that should make you happy
JDAQFileReader.hh
Go to the documentation of this file.
1 #ifndef __JSUPPORT__JDAQFILEREADER__
2 #define __JSUPPORT__JDAQFILEREADER__
3 
4 #include <vector>
5 #include <string.h>
6 
7 #include "JIO/JStreamIO.hh"
9 #include "JIO/JByteArrayIO.hh"
10 #include "JIO/JBinaryFileReader.hh"
11 #include "JDAQ/JDAQPreambleIO.hh"
14 #include "JLang/JObjectIterator.hh"
15 #include "JLang/JConversion.hh"
16 
17 
18 /**
19  * \author mdejong
20  */
21 
22 namespace JSUPPORT {}
23 namespace JPP { using namespace JSUPPORT; }
24 
25 namespace JSUPPORT {
26 
27  using JIO::JStreamReader;
34  using JLANG::JNullAccess;
35 
36 
37  /**
38  * DAQ object reading from binary file (i.e.\ .dat).
39  */
40  template<class T, bool = JLANG::JConversion<T,JDAQPreamble>::is_derived>
42 
43 
44  /**
45  * Template specialisation of JDAQFileReader for DAQ compatible data types.
46  *
47  * This class provides for an implementation of the JLANG::JAccessibleObjectIterator interface.\n
48  * The method JLANG::JAbstractObjectIterator::setObject is overwritten so that
49  * the desired object is read from the file which may contain also other objects.
50  */
51  template<class T>
52  class JDAQFileReader<T,true> :
54  public JStreamReader,
55  public JReaderObjectIterator<T>,
57  {
58  public:
59  /**
60  * Default constructor.
61  */
64  JStreamReader (static_cast<std::istream&> (*this)),
65  JReaderObjectIterator<T>(static_cast<JStreamReader&>(*this))
66  {}
67 
68 
69  /**
70  * Set object.
71  *
72  * \param object reference to object to be set
73  * \return true if set; else false
74  */
75  virtual bool setObject(T& object) override
76  {
77  using namespace std;
78  using namespace KM3NETDAQ;
79  using namespace JIO;
80 
81  for (JStreamReader& in = static_cast<JStreamReader&>(*this); in >> preamble; ) {
82 
83  if (preamble.getLength() < (int) getSizeof<JDAQPreamble>()) {
84 
85  this->setstate(ios::badbit);
86 
87  return false;
88 
89  } else if (preamble.getDataType() == getDataType<T>()) {
90 
91  buffer.resize(preamble.getLength());
92 
93  memcpy(buffer.data(), static_cast<JDAQAbstractPreamble*>(&preamble), getSizeof<JDAQPreamble>());
94 
95  in.read(buffer.data() + getSizeof<JDAQPreamble>(),
96  preamble.getLength() - getSizeof<JDAQPreamble>());
97 
98  JByteArrayReader bin(buffer.data(), buffer.size());
99 
100  bin >> object;
101 
102  return (bool) in;
103 
104  } else {
105 
106  this->ignore((streamsize) (preamble.getLength() - getSizeof<JDAQPreamble>()));
107  }
108  }
109 
110  return false;
111  }
112 
113  private:
116  };
117 
118 
119  /**
120  * Template specialisation of JDAQFileReader for DAQ incompatible data types.
121  *
122  * This class provides for a null implementation of the JLANG::JAccessibleObjectIterator interface.
123  */
124  template<class T>
125  class JDAQFileReader<T,false> :
126  public JAccessibleObjectIterator<T>,
127  public JNullAccess,
128  public JNullIterator<T>
129  {};
130 }
131 
132 #endif
Object reading from binary file (i.e. .jpp).
Byte array binary input.
Definition: JByteArrayIO.hh:27
JReader object iterator.
Binary input based on std::istream.
Definition: JStreamIO.hh:26
Accessible binary input stream.
Interface for object iteration with named access.
Interface for null access.
Definition: JAccessible.hh:67
JDAQFileReader()
Default constructor.
virtual bool setObject(T &object) override
Set object.
DAQ object reading from binary file (i.e. .dat).
Simple data structure for the DAQ preamble required for a correct calculation of the object size for ...
Auxiliary classes and methods for binary I/O.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Support classes and methods for experiment specific I/O.
Definition: JDataWriter.cc:38
KM3NeT DAQ data structures and auxiliaries.
Definition: DataQueue.cc:39
size_t getSizeof< JDAQPreamble >()
Get size of type.
Definition: JSTDTypes.hh:14
Implementation for null iteration.