1#ifndef PACKING_UTILITY_READER_H_
2#define PACKING_UTILITY_READER_H_
35 void open(
const std::string& file_name) {
36 file_.open(file_name, std::ios::in | std::ios::binary);
37 file_.seekg(0, std::ios::end);
61 void seek(
int off, std::ios_base::seekdir dir = std::ios::beg) {
62 file_.seekg(off, dir);
76 template <
typename WordType,
77 std::enable_if_t<std::is_integral<WordType>::value,
bool> =
true>
78 void seek(
int off, std::ios_base::seekdir dir = std::ios::beg) {
79 seek(off *
sizeof(WordType), dir);
94 template <
typename WordType>
96 return tell() /
sizeof(WordType);
111 template <
typename WordType,
112 std::enable_if_t<std::is_integral<WordType>::value,
bool> =
true>
114 file_.
read(
reinterpret_cast<char*
>(w),
sizeof(WordType) * count);
131 template <
typename WordType,
132 std::enable_if_t<std::is_integral<WordType>::value,
bool> =
true>
150 template <
typename ObjectType,
151 std::enable_if_t<std::is_class<ObjectType>::value,
bool> =
true>
153 return o.
read(*
this);
171 template <
typename ContentType>
172 Reader&
read(std::vector<ContentType>& vec, std::size_t count) {
174 for (
auto& w : vec) {
175 if (!(*
this >> w))
return *
this;
205 operator bool()
const {
return !
file_.fail(); }
Reader & read(std::vector< ContentType > &vec, std::size_t count)
Read the next 'count' objects into the input vector.
Reader(const std::string &file_name)
Constructor that also opens the input file.
int tell()
Tell by number of words.
Reader & operator>>(WordType &w)
Stream the next word into the input handle.
void open(const std::string &file_name)
Open a file with this reader.
std::ifstream file_
file stream we are reading from
~Reader()=default
destructor, close the input file stream
Reader()
default constructor
bool eof()
check if file is done
bool operator!() const
Check if reader is in a fail state.
void seek(int off, std::ios_base::seekdir dir=std::ios::beg)
Seek by number of words.
int tell()
Tell us where the reader is.
Reader & operator>>(ObjectType &o)
Stream into a class object.
Reader & read(WordType *w, std::size_t count)
Read the next 'count' words into the input handle.
std::size_t file_size_
file size in bytes
void seek(int off, std::ios_base::seekdir dir=std::ios::beg)
Go ("seek") a specific position in the file.