Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 
8 #include "JIO/JByteArrayIO.hh"
9 #include "JDAQ/JDAQDataTypes.hh"
10 #include "JDAQ/JDAQPreamble.hh"
11 #include "JLang/JObjectIterator.hh"
12 #include "JLang/JConversion.hh"
13 
14 
15 /**
16  * \author mdejong
17  */
18 
19 namespace JSUPPORT {}
20 namespace JPP { using namespace JSUPPORT; }
21 
22 namespace JSUPPORT {
23 
28  using JLANG::JNullAccess;
29 
30 
31  /**
32  * DAQ object reading from binary file (i.e. .dat).
33  */
34  template<class T, bool = JLANG::JConversion<T,JDAQPreamble>::is_derived>
36 
37 
38  /**
39  * Template specialisation of JDAQFileReader for DAQ compatible data types.
40  *
41  * This class provides for an implementation of the JLANG::JAccessibleObjectIterator interface.
42  * It overwrites the method setObject of the JLANG::JAbstractObjectIterator interface so that
43  * the desired object is read from the file which may contain also other objects.
44  */
45  template<class T>
46  class JDAQFileReader<T,true> :
47  public JBinaryFileReader<T>
48  {
49  public:
50  /**
51  * Set object.
52  *
53  * \param object reference to object to be set
54  * \return true if set; else false
55  */
56  virtual bool setObject(T& object)
57  {
58  using namespace std;
59  using namespace KM3NETDAQ;
60  using namespace JIO;
61 
62  for (JStreamReader& in = static_cast<JStreamReader&>(*this); in >> preamble; ) {
63 
64  if (preamble.getLength() < JDAQPreamble::sizeOf()) {
65 
66  this->setstate(ios::badbit);
67 
68  return false;
69 
70  } else if (preamble.getDataType() == getDataType<T>()) {
71 
72  buffer.resize(preamble.getLength());
73 
74  memcpy(buffer.data(), static_cast<JDAQAbstractPreamble*>(&preamble), JDAQPreamble::sizeOf());
75 
76  in.read(buffer.data() + JDAQPreamble::sizeOf(),
77  preamble.getLength() - JDAQPreamble::sizeOf());
78 
79  JByteArrayReader bin(buffer.data(), buffer.size());
80 
81  bin >> object;
82 
83  return (bool) in;
84 
85  } else {
86 
87  this->ignore((streamsize) (preamble.getLength() - JDAQPreamble::sizeOf()));
88  }
89  }
90 
91  return false;
92  }
93 
94  private:
97  };
98 
99 
100  /**
101  * Template specialisation of JDAQFileReader for DAQ incompatible data types.
102  *
103  * This class provides for a null implementation of the JLANG::JAccessibleObjectIterator interface.
104  */
105  template<class T>
106  class JDAQFileReader<T,false> :
107  public JAccessibleObjectIterator<T>,
108  public JNullAccess,
109  public JNullIterator<T>
110  {};
111 }
112 
113 #endif
Object reading from binary file.
virtual bool setObject(T &object)
Set object.
Interface for null access.
Definition: JAccessible.hh:65
Binary input based on std::istream.
Definition: JStreamIO.hh:24
Byte array binary input.
Definition: JByteArrayIO.hh:25
Implementation for null iteration.
Simple datastructure for the DAQ preamble without ROOT functionality.
Interface for object iteration with named access.
DAQ object reading from binary file (i.e.
static int sizeOf()
Get size of object.