Monarch  v3.8.2
Project 8 Data File Format Library
M2Monarch.hh
Go to the documentation of this file.
1 #ifndef MONARCH2_HH_
2 #define MONARCH2_HH_
3 
4 #include "M2IO.hh"
5 #include "M2Header.hh"
6 #include "M2Record.hh"
7 
8 #include <string>
9 
10 namespace monarch2
11 {
12 
13  class Monarch2
14  {
15  //***********************
16  // constructors and state
17  //***********************
18 
19  private:
20  //private to force use of static constructor methods
21  Monarch2();
22 
23  //current state of monarch
24  typedef enum
25  {
26  eOpen, // state when monarch has a file open but hasn't written/read the header
27  eReady, // state when monarch has dealt with the header and is writing/reading records
28  eClosed // state when monarch has no file
29  } State;
30  mutable State fState;
31 
32  public:
33  ~Monarch2();
34 
35  //********************************
36  // methods for reading (all const)
37  //********************************
38 
39  public:
40 
41  //this static method opens the file for reading.
42  //if the file exists and can be read, this returns a prepared monarch pointer, and memory is allocated for the header.
43  //upon successful return monarch is in the eOpen state.
44  static const Monarch2* OpenForReading( const std::string& filename );
45 
46  //this method parses the file for the header contents.
47  //if the header demarshalled correctly, this returns and the header may be examined, and memory is allocated for the record.
48  //upon successful return monarch is in the eReady state.
49  //an exception is thrown if the header is not read.
50  void ReadHeader() const;
51 
52  //get the pointer to the header.
53  const M2Header* GetHeader() const;
54 
55  //set the interface type to use
56  void SetInterface( InterfaceModeType aMode ) const;
57 
58  //this method parses the file for a next record.
59  //if the record demarshalled correctly, this returns true and the record is refreshed with content.
60  //when the end of the file is reached, this will return false.
61  bool ReadRecord( int anOffset = 0 ) const;
62 
63  //get the pointer to the current interleaved record.
64  const M2RecordBytes* GetRecordInterleaved() const;
65 
66  //get the pointer to the current separate channel one record.
67  const M2RecordBytes* GetRecordSeparateOne() const;
68 
69  //get the pointer to the current separate channel two record.
70  const M2RecordBytes* GetRecordSeparateTwo() const;
71 
72  //close the file pointer.
73  void Close() const;
74 
75  //*********************************
76  // methods for writing (none const)
77  //*********************************
78 
79  public:
80 
81  //this static method opens the file for writing.
82  //if the file exists and can be written, this returns a prepared monarch pointer, and memory is allocated for the header.
83  //upon successful return monarch is in the eOpen state.
84  static Monarch2* OpenForWriting( const std::string& filename );
85 
86  //this method marshals the current header to the file.
87  //if the header marshalled correctly, this returns true, memory is allocated for the record(s).
88  //upon successful return monarch is in the eReady state.
89  void WriteHeader();
90 
91  //get the pointer to the header.
93 
94  //set the interface type to use.
95  void SetInterface( InterfaceModeType aMode );
96 
97  //this method marshals the current record into the file.
98  //if the record marshalled correctly, this returns true.
99  bool WriteRecord();
100 
101  //get the pointer to the current interleaved record.
103 
104  //get the pointer to the current separate channel one record.
106 
107  //get the pointer to the current separate channel two record.
109 
110  //close the file pointer
111  void Close();
112 
113  private:
114  //the M2IO class wraps a bare C file pointer.
116 
117  //the header
118  mutable M2Header* fHeader;
119 
120  //size of the native type of the records in bytes
121  mutable size_t fDataTypeSize;
122 
123  //number of bytes in the data array in a record
124  mutable size_t fDataNBytes;
125  //number of samples in the data array in a record
126  mutable size_t fDataSize;
127 
128  //number of bytes in an interleaved record
129  mutable size_t fInterleavedRecordNBytes;
130 
131  //pointer to a M2RecordBytes occupying the interleaved record
133  //pointer to the bytes that hold the interleaved record
135 
136  //number of bytes in a separate record
137  mutable size_t fSeparateRecordNBytes;
138 
139  //pointer to a M2RecordBytes occupying the first separate record
141  //pointer to the bytes that hold the first separate record
143 
144  //pointer to a M2RecordBytes occupying the second separate record
146  //pointer to the bytes that hold the second separate record
148 
149  //the private read functions
150  mutable bool (Monarch2::*fReadFunction)( int anOffset ) const;
151  bool InterleavedFromSingle( int anOffset ) const;
152  bool InterleavedFromSeparate( int anOffset ) const;
153  bool InterleavedFromInterleaved( int anOffset ) const;
154  bool SeparateFromSingle( int anOffset ) const;
155  bool SeparateFromSeparate( int anOffset ) const;
156  bool SeparateFromInterleaved( int anOffset ) const;
157 
158  //the private write functions
159  mutable bool (Monarch2::*fWriteFunction)();
160  bool InterleavedToSingle();
161  bool InterleavedToSeparate();
163  bool SeparateToSingle();
164  bool SeparateToSeparate();
165  bool SeparateToInterleaved();
166 
167  private:
168 #ifdef __GNUG__
169  static void Zip( const size_t aSize, const size_t aDataTypeSize, const byte_type* __restrict__ aRecordOne, const byte_type* __restrict__ aRecordTwo, byte_type* __restrict__ anInterleavedRecord );
170 #else
171  static void Zip( const size_t aSize, const size_t aDataTypeSize, const byte_type* aRecordOne, const byte_type* aRecordTwo, byte_type* anInterleavedRecord );
172 #endif
173 
174 #ifdef __GNUG__
175  static void Unzip( const size_t aSize, const size_t aDataTypeSize, byte_type* __restrict__ aRecordOne, byte_type* __restrict__ aRecordTwo, const byte_type* __restrict__ anInterleavedRecord );
176 #else
177  static void Unzip( const size_t aSize, const size_t aDataTypeSize, byte_type* aRecordOne, byte_type* aRecordTwo, const byte_type* anInterleavedRecord );
178 #endif
179 
180  };
181 
182  inline const M2Header* Monarch2::GetHeader() const
183  {
184  return fHeader;
185  }
187  {
188  return fHeader;
189  }
190 
192  {
193  return fRecordSeparateOne;
194  }
196  {
197  return fRecordSeparateOne;
198  }
199 
201  {
202  return fRecordSeparateTwo;
203  }
205  {
206  return fRecordSeparateTwo;
207  }
208 
210  {
211  return fRecordInterleaved;
212  }
214  {
215  return fRecordInterleaved;
216  }
217 
218 #ifdef __GNUG__
219  inline void Monarch2::Zip( const size_t aSize, const size_t aDataTypeSize, const byte_type* __restrict__ aRecordOne, const byte_type* __restrict__ aRecordTwo, byte_type* __restrict__ anInterleavedRecord )
220 #else
221  inline void Monarch2::Zip( const size_t aSize, const size_t aDataTypeSize, const byte_type* aRecordOne, const byte_type* aRecordTwo, byte_type* anInterleavedRecord )
222 #endif
223  {
224  for( size_t anIndex = 0; anIndex < aSize; anIndex++ )
225  {
226  *anInterleavedRecord = *aRecordOne;
227  anInterleavedRecord += aDataTypeSize;
228  aRecordOne += aDataTypeSize;
229 
230  *anInterleavedRecord = *aRecordTwo;
231  anInterleavedRecord += aDataTypeSize;
232  aRecordTwo += aDataTypeSize;
233  }
234  }
235 
236 #ifdef __GNUG__
237  inline void Monarch2::Unzip( const size_t aSize, const size_t aDataTypeSize, byte_type* __restrict__ aRecordOne, byte_type* __restrict__ aRecordTwo, const byte_type* __restrict__ anInterleavedRecord )
238 #else
239  inline void Monarch2::Unzip( const size_t aSize, const size_t aDataTypeSize, byte_type* aRecordOne, byte_type* aRecordTwo, const byte_type* anInterleavedRecord )
240 #endif
241  {
242  for( size_t anIndex = 0; anIndex < aSize; anIndex++ )
243  {
244  *aRecordOne = *anInterleavedRecord;
245  anInterleavedRecord += aDataTypeSize;
246  aRecordOne += aDataTypeSize;
247 
248  *aRecordTwo = *anInterleavedRecord;
249  anInterleavedRecord += aDataTypeSize;
250  aRecordTwo += aDataTypeSize;
251  }
252  }
253 
254 }
255 
256 #endif
size_t fInterleavedRecordNBytes
Definition: M2Monarch.hh:129
bool SeparateFromSeparate(int anOffset) const
Definition: M2Monarch.cc:500
bool SeparateToSingle()
Definition: M2Monarch.cc:621
static Monarch2 * OpenForWriting(const std::string &filename)
Definition: M2Monarch.cc:82
bool ReadRecord(int anOffset=0) const
Definition: M2Monarch.cc:373
const M2RecordBytes * GetRecordInterleaved() const
Definition: M2Monarch.hh:209
uint32_t InterfaceModeType
Definition: M2Types.hh:34
bool SeparateFromSingle(int anOffset) const
Definition: M2Monarch.cc:473
bool InterleavedToSeparate()
Definition: M2Monarch.cc:587
byte_type * fRecordInterleavedBytes
Definition: M2Monarch.hh:134
M2RecordBytes * fRecordSeparateTwo
Definition: M2Monarch.hh:145
bool SeparateToInterleaved()
Definition: M2Monarch.cc:649
static void Unzip(const size_t aSize, const size_t aDataTypeSize, byte_type *aRecordOne, byte_type *aRecordTwo, const byte_type *anInterleavedRecord)
Definition: M2Monarch.hh:239
M2Header * fHeader
Definition: M2Monarch.hh:118
void SetInterface(InterfaceModeType aMode) const
Definition: M2Monarch.cc:303
bool InterleavedFromSeparate(int anOffset) const
Definition: M2Monarch.cc:405
byte_type * fRecordSeparateOneBytes
Definition: M2Monarch.hh:142
M2RecordBytes * fRecordInterleaved
Definition: M2Monarch.hh:132
const M2RecordBytes * GetRecordSeparateOne() const
Definition: M2Monarch.hh:191
size_t fSeparateRecordNBytes
Definition: M2Monarch.hh:137
byte_type * fRecordSeparateTwoBytes
Definition: M2Monarch.hh:147
bool InterleavedFromSingle(int anOffset) const
Definition: M2Monarch.cc:378
void ReadHeader() const
Definition: M2Monarch.cc:102
bool(Monarch2::* fWriteFunction)()
Definition: M2Monarch.hh:159
bool(Monarch2::* fReadFunction)(int anOffset) const
Definition: M2Monarch.hh:150
bool InterleavedToSingle()
Definition: M2Monarch.cc:576
uint8_t byte_type
Definition: M2Types.hh:10
bool SeparateFromInterleaved(int anOffset) const
Definition: M2Monarch.cc:536
const M2Header * GetHeader() const
Definition: M2Monarch.hh:182
static const Monarch2 * OpenForReading(const std::string &filename)
Definition: M2Monarch.cc:62
void Close() const
Definition: M2Monarch.cc:665
const M2RecordBytes * GetRecordSeparateTwo() const
Definition: M2Monarch.hh:200
bool SeparateToSeparate()
Definition: M2Monarch.cc:632
M2RecordBytes * fRecordSeparateOne
Definition: M2Monarch.hh:140
bool InterleavedFromInterleaved(int anOffset) const
Definition: M2Monarch.cc:446
bool InterleavedToInterleaved()
Definition: M2Monarch.cc:610
static void Zip(const size_t aSize, const size_t aDataTypeSize, const byte_type *aRecordOne, const byte_type *aRecordTwo, byte_type *anInterleavedRecord)
Definition: M2Monarch.hh:221