pflib v3.9.0-rc3-11-g2537d8f
Pretty Fine HGCROC Interaction Library
Loading...
Searching...
No Matches
Reader.h
1#pragma once
2
3#include <type_traits>
4#include <vector>
5
6namespace pflib::packing {
7
19class Reader {
20 public:
26 Reader() = default;
27
31 virtual ~Reader() = default;
32
42 virtual void seek(int off) = 0;
43
55 template <typename WordType,
56 std::enable_if_t<std::is_integral<WordType>::value, bool> = true>
57 void seek(int off) {
58 seek(off * sizeof(WordType));
59 }
60
66 virtual int tell() = 0;
67
73 template <typename WordType>
74 int tell() {
75 return tell() / sizeof(WordType);
76 }
77
85 virtual Reader& read(char* w, std::size_t count) = 0;
86
99 template <typename WordType,
100 std::enable_if_t<std::is_integral<WordType>::value, bool> = true>
101 Reader& read(WordType* w, std::size_t count) {
102 return read(reinterpret_cast<char*>(w), sizeof(WordType) * count);
103 }
104
118 template <typename WordType,
119 std::enable_if_t<std::is_integral<WordType>::value, bool> = true>
120 Reader& operator>>(WordType& w) {
121 return read(&w, 1);
122 }
123
137 template <typename ObjectType,
138 std::enable_if_t<std::is_class<ObjectType>::value, bool> = true>
139 Reader& operator>>(ObjectType& o) {
140 return o.read(*this);
141 }
142
158 template <typename ContentType>
160 std::size_t offset = 0) {
161 vec.resize(count + offset);
162 for (std::size_t i{offset}; i < vec.size(); i++) {
163 if (!(*this >> vec[i])) return *this;
164 }
165 return *this;
166 }
167
173 virtual bool good() const = 0;
174
180 virtual bool operator!() const { return !good() or eof(); }
181
196 virtual operator bool() const { return good() and not eof(); }
197
203 virtual bool eof() const = 0;
204}; // Reader
205
206} // namespace pflib::packing
Reading a raw data stream with some underlying backend.
Definition Reader.h:19
void seek(int off)
Seek by number of words.
Definition Reader.h:57
Reader & read(std::vector< ContentType > &vec, std::size_t count, std::size_t offset=0)
Read the next 'count' objects into the input vector.
Definition Reader.h:159
virtual Reader & read(char *w, std::size_t count)=0
Read the next 'count' bytes into the input handle w.
virtual bool operator!() const
Check if reader is in a fail or done state.
Definition Reader.h:180
int tell()
Tell by number of words.
Definition Reader.h:74
virtual ~Reader()=default
virtual destructor for inheritance
Reader & read(WordType *w, std::size_t count)
Read the next 'count' words into the input handle.
Definition Reader.h:101
virtual void seek(int off)=0
Go ("seek") a specific position in the stream.
Reader()=default
default constructor
virtual bool eof() const =0
check if file is done
Reader & operator>>(WordType &w)
Stream the next word into the input handle.
Definition Reader.h:120
virtual int tell()=0
Tell us where the reader is.
Reader & operator>>(ObjectType &o)
Stream into a class object.
Definition Reader.h:139
virtual bool good() const =0
Check if reader is in a good state (e.g.
T resize(T... args)
T size(T... args)