LDMX Software
BufferReader.h
1#ifndef PACKING_BUFFER_H_
2#define PACKING_BUFFER_H_
3
4#include <cstdint>
5#include <stdexcept>
6#include <vector>
7
8namespace packing {
9namespace utility {
10
34template <typename WordType>
36 public:
40 BufferReader(const std::vector<uint8_t>& b) : buffer_{b}, i_word_{0} {}
41
47 operator bool() { return (i_word_ < buffer_.size()); }
48
62 BufferReader& operator>>(WordType& w) {
63 if (*this) w = next();
64 return *this;
65 }
66
67 private:
75 WordType next() {
76 WordType w{0};
77 for (std::size_t i_byte{0}; i_byte < N_BYTES; i_byte++) {
78 w |= (buffer_.at(i_word_ + i_byte) << 8 * i_byte);
79 }
80 i_word_ += N_BYTES;
81 return w;
82 }
83
84 private:
85 // number of bytes in the words we are reading
86 static const std::size_t N_BYTES{sizeof(WordType)};
87 // current buffer we are reading
88 const std::vector<uint8_t>& buffer_;
89 // current index in buffer we are reading
90 std::size_t i_word_;
91}; // BufferReader
92
93} // namespace utility
94} // namespace packing
95
96#endif // PACKING_BUFFERREADER_H_
This class is a helper class for reading the buffer stored in the raw data format.
BufferReader(const std::vector< uint8_t > &b)
Initialize a reader by wrapping a buffer to read.
BufferReader & operator>>(WordType &w)
Streaming operator We get the next word if we are still in the buffer.
WordType next()
Go to next word in buffer.