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
9namespace packing {
10namespace utility {
11
19class Writer {
20 public:
26 Writer() { file_.unsetf(std::ios::skipws); }
27
35 void open(const std::string& file_name) {
36 file_.open(file_name, std::ios::out | std::ios::binary);
37 }
38
44 Writer(const std::string& file_name) : Writer() { this->open(file_name); }
45
47 ~Writer() = default;
48
61 template <typename WordType,
62 std::enable_if_t<std::is_integral<WordType>::value, bool> = true>
63 Writer& write(const WordType* w, std::size_t num) {
64 file_.write(reinterpret_cast<const char*>(w), sizeof(WordType) * num);
65 return *this;
66 }
67
77 template <typename WordType,
78 std::enable_if_t<std::is_integral<WordType>::value, bool> = true>
79 Writer& operator<<(const WordType& w) {
80 return write(&w, 1);
81 }
82
92 template <typename WordType,
93 std::enable_if_t<std::is_integral<WordType>::value, bool> = true>
94 Writer& operator<<(const std::vector<WordType>& vec) {
95 return write(vec.data(), vec.size());
96 }
97
112 template <typename ObjectType,
113 std::enable_if_t<std::is_class<ObjectType>::value, bool> = true>
114 Writer& operator<<(const ObjectType& o) {
115 return o.write(*this);
116 }
117
128 template <typename ObjectType,
129 std::enable_if_t<std::is_class<ObjectType>::value, bool> = true>
130 Writer& operator<<(const std::vector<ObjectType>& vec) {
131 for (auto const& o : vec)
132 if (!o.write(*this)) return *this;
133 return *this;
134 }
135
143 bool operator!() const { return file_.fail(); }
144
152 operator bool() const { return !file_.fail(); }
153
154 private:
156 std::ofstream file_;
157}; // RawDataFile
158
159} // namespace utility
160} // namespace packing
161
162#endif // PACKING_UTILITY_WRITER_H_
Writing a raw data file.
Definition Writer.h:19
Writer()
default constructor
Definition Writer.h:26
Writer & operator<<(const ObjectType &o)
Write out a class object.
Definition Writer.h:114
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:63
~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:130
Writer & operator<<(const WordType &w)
Write a single integral-type word to the output file stream.
Definition Writer.h:79
Writer(const std::string &file_name)
Open the input file name upon construction of this writer.
Definition Writer.h:44
Writer & operator<<(const std::vector< WordType > &vec)
Write out a vector of integral-type objects.
Definition Writer.h:94
std::ofstream file_
file stream we are writing to
Definition Writer.h:156
void open(const std::string &file_name)
Open a file with this writer.
Definition Writer.h:35
bool operator!() const
Check if writer is in a fail state.
Definition Writer.h:143