Monarch  v3.8.2
Project 8 Data File Format Library
M3DataInterface.hh
Go to the documentation of this file.
1 /*
2  * M3DataInterface.hh
3  *
4  * Created on: Jan 12, 2015
5  * Author: nsoblath
6  */
7 
8 #ifndef M3DATAINTERFACE_HH_
9 #define M3DATAINTERFACE_HH_
10 
11 #include "M3Constants.hh"
12 #include "M3Exception.hh"
13 #include "M3Types.hh"
14 
15 namespace monarch3
16 {
17 
19  typedef float f4_complex[ 2 ];
21  typedef double f8_complex[ 2 ];
22 
42  template< typename SetType >
44  {
45  public:
46  M3DataWriter( byte_type* aData, unsigned aDataTypeSize, uint32_t aDataFormat ) :
47  fUByteData( aData )
48  {
49  SetInterface( aDataTypeSize, aDataFormat );
50  }
52  {
53  }
54 
55  void set_at( SetType value, unsigned index )
56  {
57  (this->*fArrayFcn)( value, index );
58  }
59 
60  void SetInterface( unsigned aDataTypeSize, uint32_t aDataFormat )
61  {
62  if( aDataFormat == sDigitizedUS )
63  {
64  if( aDataTypeSize == 1 ) fArrayFcn = &M3DataWriter< SetType >::set_at_u1;
65  else if( aDataTypeSize == 2 ) fArrayFcn = &M3DataWriter< SetType >::set_at_u2;
66  else if( aDataTypeSize == 4 ) fArrayFcn = &M3DataWriter< SetType >::set_at_u4;
67  else if( aDataTypeSize == 8 ) fArrayFcn = &M3DataWriter< SetType >::set_at_u8;
68  else
69  {
70  throw M3Exception() << "Unable to make a digitized unsigned data interface with data type size " << aDataTypeSize;
71  }
72  }
73  else if( aDataFormat == sDigitizedS )
74  {
75  if( aDataTypeSize == 1 ) fArrayFcn = &M3DataWriter< SetType >::set_at_i1;
76  else if( aDataTypeSize == 2 ) fArrayFcn = &M3DataWriter< SetType >::set_at_i2;
77  else if( aDataTypeSize == 4 ) fArrayFcn = &M3DataWriter< SetType >::set_at_i4;
78  else if( aDataTypeSize == 8 ) fArrayFcn = &M3DataWriter< SetType >::set_at_i8;
79  else
80  {
81  throw M3Exception() << "Unable to make a digitized signed data interface with data type size " << aDataTypeSize;
82  }
83  }
84  else if( aDataFormat == sAnalog )
85  {
86  if( aDataTypeSize == 4 ) fArrayFcn = &M3DataWriter< SetType >::set_at_f4;
87  else if( aDataTypeSize == 8 ) fArrayFcn = &M3DataWriter< SetType >::set_at_f8;
88  else
89  {
90  throw M3Exception() << "Unable to make a analog data interface with data type size " << aDataTypeSize;
91  }
92  }
93  else
94  {
95  throw M3Exception() << "Invalid combination of data format <" << aDataFormat << ">, data type size <" << aDataTypeSize << ">";
96  }
97  return;
98  }
99 
100  void SetData( const byte_type* aData )
101  {
102  fUByteData = aData;
103  }
104 
105  private:
106  void set_at_u1( SetType value, unsigned index )
107  {
108  fUByteData[ index ] = value;
109  }
110 
111  void set_at_u2( SetType value, unsigned index )
112  {
113  fU2BytesData[ index ] = value;
114  }
115 
116  void set_at_u4( SetType value, unsigned index )
117  {
118  fU4BytesData[ index ] = value;
119  }
120 
121  void set_at_u8( SetType value, unsigned index )
122  {
123  fU8BytesData[ index ] = value;
124  }
125 
126  void set_at_i1( SetType value, unsigned index )
127  {
128  fIByteData[ index ] = value;
129  }
130 
131  void set_at_i2( SetType value, unsigned index )
132  {
133  fI2BytesData[ index ] = value;
134  }
135 
136  void set_at_i4( SetType value, unsigned index )
137  {
138  fI4BytesData[ index ] = value;
139  }
140 
141  void set_at_i8( SetType value, unsigned index )
142  {
143  fI8BytesData[ index ] = value;
144  }
145 
146  void set_at_f4( SetType value, unsigned index )
147  {
148  fF4BytesData[ index ] = value;
149  }
150 
151  void set_at_f8( SetType value, unsigned index )
152  {
153  fF8BytesData[ index ] = value;
154  }
155 
156  void (M3DataWriter::*fArrayFcn)( SetType, unsigned );
157 
158  union
159  {
160  uint8_t* fUByteData;
161  uint16_t* fU2BytesData;
162  uint32_t* fU4BytesData;
163  uint64_t* fU8BytesData;
164  int8_t* fIByteData;
165  int16_t* fI2BytesData;
166  int32_t* fI4BytesData;
167  int64_t* fI8BytesData;
168  float* fF4BytesData;
169  double* fF8BytesData;
170  };
171  };
172 
184  template< typename SetType >
186  {
187  public:
188  M3ComplexDataWriter( byte_type* aData, unsigned aDataTypeSize, uint32_t aDataFormat, unsigned aSampleSize = 2 ) :
189  fUByteData( aData )
190  {
191  SetInterface( aDataTypeSize, aDataFormat, aSampleSize );
192  }
194  {
195  }
196 
197  void set_at( SetType value, unsigned index )
198  {
199  (this->*fArrayFcn)( value, index );
200  }
201 
202  void SetInterface( unsigned aDataTypeSize, uint32_t aDataFormat, unsigned aSampleSize = 2 )
203  {
204  if( aDataFormat == sDigitizedUS && aSampleSize == 1 )
205  {
206  if( aDataTypeSize == 1 ) fArrayFcn = &M3ComplexDataWriter< SetType >::set_at_u1;
207  else
208  {
209  throw M3Exception() << "Unable to make a digitized unsigned data interface with data type size " << aDataTypeSize;
210  }
211  }
212  else if( aDataFormat == sDigitizedS && aSampleSize == 1 )
213  {
214  if( aDataTypeSize == 1 ) fArrayFcn = &M3ComplexDataWriter< SetType >::set_at_i1;
215  else
216  {
217  throw M3Exception() << "Unable to make a digitized signed data interface with data type size " << aDataTypeSize;
218  }
219  }
220 
221  else if( aDataFormat == sAnalog && aSampleSize == 2 )
222  {
223  if( aDataTypeSize == 4 ) fArrayFcn = &M3ComplexDataWriter< SetType >::set_at_f4_comp;
224  else if( aDataTypeSize == 8 ) fArrayFcn = &M3ComplexDataWriter< SetType >::set_at_f8_comp;
225  else
226  {
227  throw M3Exception() << "Unable to make a analog data interface with data type size " << aDataTypeSize;
228  }
229  }
230  else
231  {
232  throw M3Exception() << "Invalid combination of data format <" << aDataFormat << ">, data type size <" << aDataTypeSize << "> and sample size <" << aSampleSize << ">";
233  }
234  return;
235  }
236 
237  void SetData( const byte_type* aData )
238  {
239  fUByteData = aData;
240  }
241 
242  private:
243  void set_at_u1( SetType value, unsigned index )
244  {
245  fUByteData[ index ] = value[ 0 ];
246  }
247 
248  void set_at_i1( SetType value, unsigned index )
249  {
250  fIByteData[ index ] = value[ 0 ];
251  }
252 
253  void set_at_f4_comp( SetType value, unsigned index )
254  {
255  fF4CompBytesData[ index ][ 0 ] = value[ 0 ];
256  fF4CompBytesData[ index ][ 1 ] = value[ 1 ];
257  }
258 
259  void set_at_f8_comp( SetType value, unsigned index )
260  {
261  fF8CompBytesData[ index ][ 0 ] = value[ 0 ];
262  fF8CompBytesData[ index ][ 1 ] = value[ 1 ];
263  }
264 
265  void (M3ComplexDataWriter::*fArrayFcn)( SetType, unsigned );
266 
267  union
268  {
269  uint8_t* fUByteData;
270  int8_t* fIByteData;
271  f4_complex* fF4CompBytesData;
272  f8_complex* fF8CompBytesData;
273  };
274  };
275 
276 
296  template< typename ReturnType >
298  {
299  public:
300  M3DataReader( const byte_type* aData, unsigned aDataTypeSize, uint32_t aDataFormat ) :
301  fUByteData( aData )
302  {
303  SetInterface( aDataTypeSize, aDataFormat );
304  }
306  {
307  }
308 
309  ReturnType at( unsigned index ) const
310  {
311  return (this->*fArrayFcn)( index );
312  }
313 
314  void SetInterface( unsigned aDataTypeSize, uint32_t aDataFormat )
315  {
316  if( aDataFormat == sDigitizedUS )
317  {
318  if( aDataTypeSize == 1 ) fArrayFcn = &M3DataReader< ReturnType >::at_u1;
319  else if( aDataTypeSize == 2 ) fArrayFcn = &M3DataReader< ReturnType >::at_u2;
320  else if( aDataTypeSize == 4 ) fArrayFcn = &M3DataReader< ReturnType >::at_u4;
321  else if( aDataTypeSize == 8 ) fArrayFcn = &M3DataReader< ReturnType >::at_u8;
322  else
323  {
324  throw M3Exception() << "Unable to make a digitized unsigned data interface with data type size " << aDataTypeSize;
325  }
326  }
327  else if( aDataFormat == sDigitizedS )
328  {
329  if( aDataTypeSize == 1 ) fArrayFcn = &M3DataReader< ReturnType >::at_i1;
330  else if( aDataTypeSize == 2 ) fArrayFcn = &M3DataReader< ReturnType >::at_i2;
331  else if( aDataTypeSize == 4 ) fArrayFcn = &M3DataReader< ReturnType >::at_i4;
332  else if( aDataTypeSize == 8 ) fArrayFcn = &M3DataReader< ReturnType >::at_i8;
333  else
334  {
335  throw M3Exception() << "Unable to make a digitized signed data interface with data type size " << aDataTypeSize;
336  }
337  }
338 
339  else if( aDataFormat == sAnalog )
340  {
341  if( aDataTypeSize == 4 ) fArrayFcn = &M3DataReader< ReturnType >::at_f4;
342  else if( aDataTypeSize == 8 ) fArrayFcn = &M3DataReader< ReturnType >::at_f8;
343  else
344  {
345  throw M3Exception() << "Unable to make a analog data interface with data type size " << aDataTypeSize;
346  }
347  }
348  else
349  {
350  throw M3Exception() << "Invalid combination of data format <" << aDataFormat << ">, data type size <" << aDataTypeSize << ">";
351  }
352  return;
353  }
354 
355  void SetData( const byte_type* aData )
356  {
357  fUByteData = aData;
358  }
359 
360  private:
361  ReturnType at_u1( unsigned index ) const
362  {
363  return fUByteData[ index ];
364  }
365 
366  ReturnType at_u2( unsigned index ) const
367  {
368  return fU2BytesData[ index ];
369  }
370 
371  ReturnType at_u4( unsigned index ) const
372  {
373  return fU4BytesData[ index ];
374  }
375 
376  ReturnType at_u8( unsigned index ) const
377  {
378  return fU8BytesData[ index ];
379  }
380 
381  ReturnType at_i1( unsigned index ) const
382  {
383  return fIByteData[ index ];
384  }
385 
386  ReturnType at_i2( unsigned index ) const
387  {
388  return fI2BytesData[ index ];
389  }
390 
391  ReturnType at_i4( unsigned index ) const
392  {
393  return fI4BytesData[ index ];
394  }
395 
396  ReturnType at_i8( unsigned index ) const
397  {
398  return fI8BytesData[ index ];
399  }
400 
401  ReturnType at_f4( unsigned index ) const
402  {
403  return fF4BytesData[ index ];
404  }
405 
406  ReturnType at_f8( unsigned index ) const
407  {
408  return fF8BytesData[ index ];
409  }
410 
411  ReturnType (M3DataReader::*fArrayFcn)( unsigned ) const;
412 
413  union
414  {
415  const uint8_t* fUByteData;
416  const uint16_t* fU2BytesData;
417  const uint32_t* fU4BytesData;
418  const uint64_t* fU8BytesData;
419  const int8_t* fIByteData;
420  const int16_t* fI2BytesData;
421  const int32_t* fI4BytesData;
422  const int64_t* fI8BytesData;
423  const float* fF4BytesData;
424  const double* fF8BytesData;
425  };
426  };
427 
439  template< typename ReturnType >
441  {
442  public:
443  M3ComplexDataReader( const byte_type* aData, unsigned aDataTypeSize, uint32_t aDataFormat, unsigned aSampleSize = 2 ) :
444  fUByteData( aData )
445  {
446  SetInterface( aDataTypeSize, aDataFormat, aSampleSize );
447  }
449  {
450  }
451 
452  const ReturnType& at( unsigned index ) const
453  {
454  return (this->*fArrayFcn)( index );
455  }
456 
457  void SetInterface( unsigned aDataTypeSize, uint32_t aDataFormat, unsigned aSampleSize = 2 )
458  {
459  if( aDataFormat == sDigitizedUS && aSampleSize == 1 )
460  {
461  if( aDataTypeSize == 1 ) fArrayFcn = &M3ComplexDataReader< ReturnType >::at_u1;
462  else
463  {
464  throw M3Exception() << "Unable to make a digitized (unsigned) data interface with data type size " << aDataTypeSize;
465  }
466  }
467  else if( aDataFormat == sDigitizedS && aSampleSize == 1 )
468  {
469  if( aDataTypeSize == 1 ) fArrayFcn = &M3ComplexDataReader< ReturnType >::at_i1;
470  else
471  {
472  throw M3Exception() << "Unable to make a digitized (signed) data interface with data type size " << aDataTypeSize;
473  }
474  }
475  else if( aDataFormat == sAnalog && aSampleSize == 2 )
476  {
477  if( aDataTypeSize == 4 ) fArrayFcn = &M3ComplexDataReader< ReturnType >::at_f4_comp;
478  else if( aDataTypeSize == 8 ) fArrayFcn = &M3ComplexDataReader< ReturnType >::at_f8_comp;
479  else
480  {
481  throw M3Exception() << "Unable to make a analog data interface with data type size " << aDataTypeSize;
482  }
483  }
484  else
485  {
486  throw M3Exception() << "Invalid combination of data format <" << aDataFormat << ">, data type size <" << aDataTypeSize << "> and sample size <" << aSampleSize << ">";
487  }
488  return;
489  }
490 
491  void SetData( const byte_type* aData )
492  {
493  fUByteData = aData;
494  }
495 
496  private:
497  const ReturnType& at_u1( unsigned index ) const
498  {
499  fBuffer[ 0 ] = fUByteData[ index ];
500  fBuffer[ 1 ] = fUByteData[ index ];
501  return fBuffer;
502  }
503 
504  const ReturnType& at_i1( unsigned index ) const
505  {
506  fBuffer[ 0 ] = fIByteData[ index ];
507  fBuffer[ 1 ] = fIByteData[ index ];
508  return fBuffer;
509  }
510 
511  const ReturnType& at_f4_comp( unsigned index ) const
512  {
513  fBuffer[ 0 ] = fF4CompBytesData[ index ][ 0 ];
514  fBuffer[ 1 ] = fF4CompBytesData[ index ][ 1 ];
515  return fBuffer;
516  }
517 
518  const ReturnType& at_f8_comp( unsigned index ) const
519  {
520  fBuffer[ 0 ] = fF8CompBytesData[ index ][ 0 ];
521  fBuffer[ 1 ] = fF8CompBytesData[ index ][ 1 ];
522  return fBuffer;
523  }
524 
525  const ReturnType& (M3ComplexDataReader::*fArrayFcn)( unsigned ) const;
526 
527  union
528  {
529  const uint8_t* fUByteData;
530  const int8_t* fIByteData;
531  const f4_complex* fF4CompBytesData;
532  const f8_complex* fF8CompBytesData;
533  };
534 
535  mutable ReturnType fBuffer;
536  };
537 
538 }
539 
540 #endif /* M3DATAINTERFACE_HH_ */
void set_at(SetType value, unsigned index)
void SetInterface(unsigned aDataTypeSize, uint32_t aDataFormat, unsigned aSampleSize=2)
const ReturnType & at(unsigned index) const
const f8_complex * fF8CompBytesData
void set_at_f8_comp(SetType value, unsigned index)
ReturnType at_i1(unsigned index) const
void SetInterface(unsigned aDataTypeSize, uint32_t aDataFormat)
void set_at_u2(SetType value, unsigned index)
static const uint32_t sDigitizedUS
Definition: M3Constants.hh:38
M3ComplexDataWriter(byte_type *aData, unsigned aDataTypeSize, uint32_t aDataFormat, unsigned aSampleSize=2)
ReturnType at_u2(unsigned index) const
void SetData(const byte_type *aData)
void set_at_u1(SetType value, unsigned index)
ReturnType at_u4(unsigned index) const
ReturnType at_f8(unsigned index) const
ReturnType at_i4(unsigned index) const
void set_at_i4(SetType value, unsigned index)
uint8_t byte_type
Definition: M3Types.hh:22
void set_at(SetType value, unsigned index)
ReturnType at(unsigned index) const
const uint64_t * fU8BytesData
void set_at_i8(SetType value, unsigned index)
ReturnType at_f4(unsigned index) const
const int16_t * fI2BytesData
static const uint32_t sDigitizedS
Definition: M3Constants.hh:39
ReturnType at_i2(unsigned index) const
M3DataReader(const byte_type *aData, unsigned aDataTypeSize, uint32_t aDataFormat)
void SetInterface(unsigned aDataTypeSize, uint32_t aDataFormat)
void SetData(const byte_type *aData)
double f8_complex[2]
Binary-compatible to fftw_complex.
const ReturnType & at_i1(unsigned index) const
const ReturnType & at_f4_comp(unsigned index) const
const uint16_t * fU2BytesData
Interface class for complex data types.
void set_at_f4(SetType value, unsigned index)
Specialized exception class for Monarch3.
Definition: M3Exception.hh:28
const f4_complex * fF4CompBytesData
M3DataWriter(byte_type *aData, unsigned aDataTypeSize, uint32_t aDataFormat)
void(M3DataWriter::* fArrayFcn)(SetType, unsigned)
M3ComplexDataReader(const byte_type *aData, unsigned aDataTypeSize, uint32_t aDataFormat, unsigned aSampleSize=2)
void set_at_u8(SetType value, unsigned index)
const uint8_t * fUByteData
const ReturnType &(M3ComplexDataReader::* fArrayFcn)(unsigned) const
float f4_complex[2]
Binary-compatible to fftwf_complex.
void SetData(const byte_type *aData)
const ReturnType & at_f8_comp(unsigned index) const
void set_at_i2(SetType value, unsigned index)
Interface class for complex data types.
Interface class for a variety of data types.
void set_at_i1(SetType value, unsigned index)
void set_at_u4(SetType value, unsigned index)
void set_at_u1(SetType value, unsigned index)
const ReturnType & at_u1(unsigned index) const
void set_at_i1(SetType value, unsigned index)
void SetInterface(unsigned aDataTypeSize, uint32_t aDataFormat, unsigned aSampleSize=2)
Interface class for a variety of data types.
static const uint32_t sAnalog
Definition: M3Constants.hh:40
ReturnType at_u1(unsigned index) const
void SetData(const byte_type *aData)
const int32_t * fI4BytesData
ReturnType at_i8(unsigned index) const
void set_at_f8(SetType value, unsigned index)
const uint32_t * fU4BytesData
const double * fF8BytesData
const int64_t * fI8BytesData
void set_at_f4_comp(SetType value, unsigned index)
ReturnType at_u8(unsigned index) const