LDMX Software
Writer.h
1#ifndef PACKING_UTILITY_WRITER_H_
2#define PACKING_UTILITY_WRITER_H_
3
4#include <fstream>
5#include <iostream> //debuggin
6#include <string>
7#include <type_traits>
8#include <vector>
9
10namespace packing {
11namespace utility {
12
20class Writer {
21 public:
27 Writer() { file_.unsetf(std::ios::skipws); }
28
36 void open(const std::string& file_name) {
37 file_.open(file_name, std::ios::out | std::ios::binary);
38 }
39
45 Writer(const std::string& file_name) : Writer() { this->open(file_name); }
46
48 ~Writer() = default;
49
62 template <typename WordType,
63 std::enable_if_t<std::is_integral<WordType>::value, bool> = true>
64 Writer& write(const WordType* w, std::size_t num) {
65 file_.write(reinterpret_cast<const char*>(w), sizeof(WordType) * num);
66 return *this;
67 }
68
78 template <typename WordType,
79 std::enable_if_t<std::is_integral<WordType>::value, bool> = true>
80 Writer& operator<<(const WordType& w) {
81 return write(&w, 1);
82 }
83
93 template <typename WordType,
94 std::enable_if_t<std::is_integral<WordType>::value, bool> = true>
95 Writer& operator<<(const std::vector<WordType>& vec) {
96 return write(vec.data(), vec.size());
97 }
98
113 template <typename ObjectType,
114 std::enable_if_t<std::is_class<ObjectType>::value, bool> = true>
115 Writer& operator<<(const ObjectType& o) {
116 return o.write(*this);
117 }
118
129 template <typename ObjectType,
130 std::enable_if_t<std::is_class<ObjectType>::value, bool> = true>
131 Writer& operator<<(const std::vector<ObjectType>& vec) {
132 for (auto const& o : vec)
133 if (!o.write(*this)) return *this;
134 return *this;
135 }
136
144 bool operator!() const { return file_.fail(); }
145
153 operator bool() const { return !file_.fail(); }
154
155 private:
157 std::ofstream file_;
158}; // RawDataFile
159
160} // namespace utility
161} // namespace packing
162
163#endif // PACKING_UTILITY_WRITER_H_
Writing a raw data file.
Definition Writer.h:20
Writer()
default constructor
Definition Writer.h:27
Writer & operator<<(const ObjectType &o)
Write out a class object.
Definition Writer.h:115
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:64
~Writer()=default
destructor, close the input file stream
Writer & operator<<(const std::vector< ObjectType > &vec)
Write out a vector fo class objects.
Definition Writer.h:131
Writer & operator<<(const WordType &w)
Write a single integral-type word to the output file stream.
Definition Writer.h:80
Writer(const std::string &file_name)
Open the input file name upon construction of this writer.
Definition Writer.h:45
Writer & operator<<(const std::vector< WordType > &vec)
Write out a vector of integral-type objects.
Definition Writer.h:95
std::ofstream file_
file stream we are writing to
Definition Writer.h:157
void open(const std::string &file_name)
Open a file with this writer.
Definition Writer.h:36
bool operator!() const
Check if writer is in a fail state.
Definition Writer.h:144