LDMX Software
Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
packing::utility::BufferReader< WordType > Class Template Reference

This class is a helper class for reading the buffer stored in the raw data format. More...

#include <BufferReader.h>

Public Member Functions

 BufferReader (const std::vector< uint8_t > &b)
 Initialize a reader by wrapping a buffer to read.
 
 operator bool ()
 Return state of buffer.
 
BufferReaderoperator>> (WordType &w)
 Streaming operator We get the next word if we are still in the buffer.
 

Private Member Functions

WordType next ()
 Go to next word in buffer.
 

Private Attributes

const std::vector< uint8_t > & buffer_
 
std::size_t i_word_
 

Static Private Attributes

static const std::size_t n_bytes_ {sizeof(WordType)}
 

Detailed Description

template<typename WordType>
class packing::utility::BufferReader< WordType >

This class is a helper class for reading the buffer stored in the raw data format.

The raw data format specifies that the buffer for all data is a specific type (vector of bytes), but the users in different subsystems/translators may want the buffer to be read in different size words than single bytes. This class helps in that translation.

A basic idea for using this reader is provided in the Ecal/Hcal raw decoders.

BufferReader<uint32_t> r{buffer_}; while (r >> head1 >> head2) { // do decoding, using r >> w; // to get next word in buffer }

Template Parameters

in] WordType type of word user wants to read out from buffer

Definition at line 34 of file BufferReader.h.

Constructor & Destructor Documentation

◆ BufferReader()

template<typename WordType >
packing::utility::BufferReader< WordType >::BufferReader ( const std::vector< uint8_t > &  b)
inline

Initialize a reader by wrapping a buffer to read.

Definition at line 39 of file BufferReader.h.

39: buffer_{b}, i_word_{0} {}

Member Function Documentation

◆ next()

template<typename WordType >
WordType packing::utility::BufferReader< WordType >::next ( )
inlineprivate

Go to next word in buffer.

Note
We assume that we are always in the buffer.
Exceptions
std::out_of_rangeif try to go past end of buffer
Returns
next word in buffer

Definition at line 74 of file BufferReader.h.

74 {
75 WordType w{0};
76 for (std::size_t i_byte{0}; i_byte < n_bytes_; i_byte++) {
77 w |= (buffer_.at(i_word_ + i_byte) << 8 * i_byte);
78 }
79 i_word_ += n_bytes_;
80 return w;
81 }

Referenced by packing::utility::BufferReader< WordType >::operator>>().

◆ operator bool()

template<typename WordType >
packing::utility::BufferReader< WordType >::operator bool ( )
inline

Return state of buffer.

false if buffer is done being read, true otherwise.

Definition at line 46 of file BufferReader.h.

46{ return (i_word_ < buffer_.size()); }

◆ operator>>()

template<typename WordType >
BufferReader & packing::utility::BufferReader< WordType >::operator>> ( WordType &  w)
inline

Streaming operator We get the next word if we are still in the buffer.

We always return ourselves so statements like

if (reader >> word1 >> word2)

can correctly fail on the first or second word.

See also
next for implementation of reading the next word
Parameters
[in]wreference to word to put data into
Returns
reference to us

Definition at line 61 of file BufferReader.h.

61 {
62 if (*this) w = next();
63 return *this;
64 }
WordType next()
Go to next word in buffer.

References packing::utility::BufferReader< WordType >::next().

Member Data Documentation

◆ buffer_

template<typename WordType >
const std::vector<uint8_t>& packing::utility::BufferReader< WordType >::buffer_
private

Definition at line 87 of file BufferReader.h.

◆ i_word_

template<typename WordType >
std::size_t packing::utility::BufferReader< WordType >::i_word_
private

Definition at line 89 of file BufferReader.h.

◆ n_bytes_

template<typename WordType >
const std::size_t packing::utility::BufferReader< WordType >::n_bytes_ {sizeof(WordType)}
staticprivate

Definition at line 85 of file BufferReader.h.

85{sizeof(WordType)};

The documentation for this class was generated from the following file: