pflib v3.0.0-rc1-25-gb91774e
Pretty Fine HGCROC Interaction Library
All Classes Namespaces Files Functions Variables Typedefs Pages
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 vec.resize(count+offset);
161 for (std::size_t i{offset}; i < vec.size(); i++) {
162 if (!(*this >> vec[i])) return *this;
163 }
164 return *this;
165 }
166
172 virtual bool good() const = 0;
173
179 virtual bool operator!() const {
180 return !good();
181 }
182
197 virtual operator bool() const {
198 return good();
199 }
200
206 virtual bool eof() = 0;
207}; // Reader
208
209} // 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 state.
Definition Reader.h:179
virtual bool eof()=0
check if file is done
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
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.
T resize(T... args)
T size(T... args)