LDMX Software
BufferReader.h
1#ifndef PACKING_BUFFER_H_
2#define PACKING_BUFFER_H_
3
4#include <stdexcept>
5#include <vector>
6
7namespace packing {
8namespace utility {
9
33template <typename WordType>
35 public:
39 BufferReader(const std::vector<uint8_t>& b) : buffer_{b}, i_word_{0} {}
40
46 operator bool() { return (i_word_ < buffer_.size()); }
47
61 BufferReader& operator>>(WordType& w) {
62 if (*this) w = next();
63 return *this;
64 }
65
66 private:
74 WordType next() {
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 }
82
83 private:
84 // number of bytes in the words we are reading
85 static const std::size_t n_bytes_{sizeof(WordType)};
86 // current buffer we are reading
87 const std::vector<uint8_t>& buffer_;
88 // current index in buffer we are reading
89 std::size_t i_word_;
90}; // BufferReader
91
92} // namespace utility
93} // namespace packing
94
95#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.