pflib v3.0.0-rc1-29-g3a901ac
Pretty Fine HGCROC Interaction Library
All Classes Namespaces Files Functions Variables Typedefs Pages
Writer.h
1#pragma once
2
3#include <fstream>
4#include <iostream> //debuggin
5#include <string>
6#include <type_traits>
7
8namespace pflib::packing {
9
17class Writer {
18 public:
24 Writer() { file_.unsetf(std::ios::skipws); }
25
33 void open(const std::string& file_name) {
34 file_.open(file_name, std::ios::out | std::ios::binary);
35 }
36
42 Writer(const std::string& file_name) : Writer() { this->open(file_name); }
43
45 ~Writer() = default;
46
59 template <typename WordType,
60 std::enable_if_t<std::is_integral<WordType>::value, bool> = true>
61 Writer& write(const WordType* w, std::size_t num) {
62 file_.write(reinterpret_cast<const char*>(w), sizeof(WordType) * num);
63 return *this;
64 }
65
75 template <typename WordType,
76 std::enable_if_t<std::is_integral<WordType>::value, bool> = true>
77 Writer& operator<<(const WordType& w) {
78 return write(&w, 1);
79 }
80
90 template <typename WordType,
91 std::enable_if_t<std::is_integral<WordType>::value, bool> = true>
93 return write(vec.data(), vec.size());
94 }
95
110 template <typename ObjectType,
111 std::enable_if_t<std::is_class<ObjectType>::value, bool> = true>
112 Writer& operator<<(const ObjectType& o) {
113 return o.write(*this);
114 }
115
126 template <typename ObjectType,
127 std::enable_if_t<std::is_class<ObjectType>::value, bool> = true>
129 for (auto const& o : vec)
130 if (!o.write(*this)) return *this;
131 return *this;
132 }
133
141 bool operator!() const { return file_.fail(); }
142
150 operator bool() const { return !file_.fail(); }
151
152 private:
155}; // Writer
156
157} // namespace pflib::packing
Writing a raw data file.
Definition Writer.h:17
Writer & operator<<(const WordType &w)
Write a single integral-type word to the output file stream.
Definition Writer.h:77
Writer & operator<<(const std::vector< ObjectType > &vec)
Write out a vector fo class objects.
Definition Writer.h:128
Writer & write(const WordType *w, std::size_t num)
Write a certain number of words from the input array to the output file stream.
Definition Writer.h:61
Writer & operator<<(const ObjectType &o)
Write out a class object.
Definition Writer.h:112
Writer & operator<<(const std::vector< WordType > &vec)
Write out a vector of integral-type objects.
Definition Writer.h:92
bool operator!() const
Check if writer is in a fail state.
Definition Writer.h:141
Writer(const std::string &file_name)
Open the input file name upon construction of this writer.
Definition Writer.h:42
Writer()
default constructor
Definition Writer.h:24
void open(const std::string &file_name)
Open a file with this writer.
Definition Writer.h:33
~Writer()=default
destructor, close the input file stream
std::ofstream file_
file stream we are writing to
Definition Writer.h:154
T data(T... args)
T fail(T... args)
T open(T... args)
T size(T... args)
T unsetf(T... args)
T write(T... args)