Monarch  v3.8.2
Project 8 Data File Format Library
M2Record.hh
Go to the documentation of this file.
1 #ifndef M2RECORD_HH_
2 #define M2RECORD_HH_
3 
4 #include "M2Exception.hh"
5 #include "M2Types.hh"
6 
7 namespace monarch2
8 {
9  template< typename DataType >
10  struct M2Record
11  {
15  DataType fData[];
16  };
17 
19 
20  template< typename ReturnType >
22  {
23  public:
24  M2RecordDataInterface( const byte_type* aData, unsigned aDataTypeSize ) :
25  fByteData( aData )
26  {
27  SetDataTypeSize( aDataTypeSize );
28  }
30  {
31  }
32 
33  ReturnType at( unsigned index ) const
34  {
35  return (this->*fArrayFcn)( index );
36  }
37 
38  void SetDataTypeSize( unsigned aDataTypeSize )
39  {
40  if( aDataTypeSize == 1 ) fArrayFcn = &M2RecordDataInterface< ReturnType >::at_1_byte;
41  else if( aDataTypeSize == 2 ) fArrayFcn = &M2RecordDataInterface< ReturnType >::at_2_byte;
42  else if( aDataTypeSize == 4 ) fArrayFcn = &M2RecordDataInterface< ReturnType >::at_4_byte;
43  else if( aDataTypeSize == 8 ) fArrayFcn = &M2RecordDataInterface< ReturnType >::at_8_byte;
44  else
45  {
46  throw M2Exception() << "unable to make a record data interface with data type size " << aDataTypeSize;
47  }
48  return;
49  }
50 
51  void SetData( const byte_type* aData )
52  {
53  fByteData = aData;
54  }
55 
56  private:
57  ReturnType at_1_byte( unsigned index ) const
58  {
59  return fByteData[ index ];
60  }
61 
62  ReturnType at_2_byte( unsigned index ) const
63  {
64  return fTwoBytesData[ index ];
65  }
66 
67  ReturnType at_4_byte( unsigned index ) const
68  {
69  return fFourBytesData[ index ];
70  }
71 
72  ReturnType at_8_byte( unsigned index ) const
73  {
74  return fEightBytesData[ index ];
75  }
76 
77  ReturnType (M2RecordDataInterface::*fArrayFcn)( unsigned ) const;
78 
79  union
80  {
82  const uint16_t* fTwoBytesData;
83  const uint32_t* fFourBytesData;
84  const uint64_t* fEightBytesData;
85  };
86  };
87 
88 }
89 
90 #endif
ReturnType at_4_byte(unsigned index) const
Definition: M2Record.hh:67
const uint32_t * fFourBytesData
Definition: M2Record.hh:83
TimeType fTime
Definition: M2Record.hh:14
const uint16_t * fTwoBytesData
Definition: M2Record.hh:82
void SetDataTypeSize(unsigned aDataTypeSize)
Definition: M2Record.hh:38
void SetData(const byte_type *aData)
Definition: M2Record.hh:51
DataType fData[]
Definition: M2Record.hh:15
const uint64_t * fEightBytesData
Definition: M2Record.hh:84
M2RecordDataInterface(const byte_type *aData, unsigned aDataTypeSize)
Definition: M2Record.hh:24
ReturnType at_2_byte(unsigned index) const
Definition: M2Record.hh:62
ReturnType at_1_byte(unsigned index) const
Definition: M2Record.hh:57
AcquisitionIdType fAcquisitionId
Definition: M2Record.hh:12
uint64_t AcquisitionIdType
Definition: M2Types.hh:57
M2Record< byte_type > M2RecordBytes
Definition: M2Record.hh:18
ReturnType at(unsigned index) const
Definition: M2Record.hh:33
RecordIdType fRecordId
Definition: M2Record.hh:13
uint8_t byte_type
Definition: M2Types.hh:10
ReturnType at_8_byte(unsigned index) const
Definition: M2Record.hh:72
uint64_t RecordIdType
Definition: M2Types.hh:58
const byte_type * fByteData
Definition: M2Record.hh:81
uint64_t TimeType
Definition: M2Types.hh:59