forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinaryStreamWriter.cpp
217 lines (195 loc) · 7.49 KB
/
BinaryStreamWriter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2019 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
//------------------------------------------------------------------------------
// Includes
//------------------------------------------------------------------------------
#include "MantidKernel/BinaryStreamWriter.h"
#include "MantidKernel/Matrix.h"
#include <cassert>
#include <fstream>
#include <stdexcept>
namespace Mantid::Kernel {
//------------------------------------------------------------------------------
// Anonymous functions
//------------------------------------------------------------------------------
namespace {
/**
* write a value to the stream based on the template type
* @param stream The open stream on which to perform the write
* @param value An object of type T to fill with the value from the file
*/
template <typename T> inline void writeToStream(std::ostream &stream, const T &value) {
stream.write(reinterpret_cast<const char *>(&value), sizeof(T));
}
/**
* Overload to write an array of values to the stream based on the template
* type for the element of the array.
* @param stream The open stream on which to perform the write
* @param value An object of type std::vector<T> to fill with the value from
* the file
* @param nvals The number of values to write
*/
template <typename T> inline void writeToStream(std::ostream &stream, const std::vector<T> &value, size_t nvals) {
stream.write(reinterpret_cast<const char *>(value.data()), nvals * sizeof(T));
}
} // namespace
//------------------------------------------------------------------------------
// Public members
//------------------------------------------------------------------------------
/**
* Constructor taking the stream to write.
* @param ofstrm An open stream from which data will be write. The object does
* not take ownership of the stream. The caller is responsible for closing
* it.
*/
BinaryStreamWriter::BinaryStreamWriter(std::ostream &ofstrm)
: m_ofstrm(ofstrm), m_strLengthSize(static_cast<uint64_t>(sizeof(int32_t))) {
if (ofstrm.fail()) {
throw std::runtime_error("BinaryStreamWriter: Output stream is in a bad "
"state. Cannot continue.");
}
}
/**
* write a int16_t to the stream
* @param value the value is stored in the given stream
* @return BinaryStreamWriter&
*/
BinaryStreamWriter &BinaryStreamWriter::operator<<(const int16_t &value) {
writeToStream(m_ofstrm, value);
return *this;
}
/**
* write a int32_t to the stream
* @param value The value is stored in the given stream
* @return A reference to the BinaryStreamWriter object
*/
BinaryStreamWriter &BinaryStreamWriter::operator<<(const int32_t &value) {
writeToStream(m_ofstrm, value);
return *this;
}
/**
* write a int64_t to the stream
* @param value The value is stored in the given stream
* @return A reference to the BinaryStreamWriter object
*/
BinaryStreamWriter &BinaryStreamWriter::operator<<(const int64_t &value) {
writeToStream(m_ofstrm, value);
return *this;
}
/**
* write a float (4-bytes) to the stream
* @param value The value is stored in the given stream
* @return A reference to the BinaryStreamWriter object
*/
BinaryStreamWriter &BinaryStreamWriter::operator<<(const float &value) {
writeToStream(m_ofstrm, value);
return *this;
}
/**
* write a double (8-bytes) to the stream
* @param value The value is stored in this object
* @return A reference to the BinaryStreamWriter object
*/
BinaryStreamWriter &BinaryStreamWriter::operator<<(const double &value) {
writeToStream(m_ofstrm, value);
return *this;
}
/**
* write a string of characters from given object. This method assumes that
* the stream currently points at a type specifying the length followed directly
* by the string itself.
* @param value The string value is stored in the given object. It is resized
* to the appropriate length
* @return A reference to the BinaryStreamWriter object
*/
BinaryStreamWriter &BinaryStreamWriter::operator<<(const std::string &value) {
// First write the size.
decltype(m_strLengthSize) length(value.length());
m_ofstrm.write(reinterpret_cast<char *>(&length), m_strLengthSize);
// Now the value
m_ofstrm.write(const_cast<char *>(value.data()), static_cast<size_t>(length));
return *this;
}
/**
* write a uint16_t to the stream
* @param value The value is stored in the given stream
* @return A reference to the BinaryStreamWriter object
*/
BinaryStreamWriter &BinaryStreamWriter::operator<<(const uint16_t &value) {
writeToStream(m_ofstrm, value);
return *this;
}
/**
* write a uint32_t to the stream
* @param value The value is stored in the given stream
* @return A reference to the BinaryStreamWriter object
*/
BinaryStreamWriter &BinaryStreamWriter::operator<<(const uint32_t &value) {
writeToStream(m_ofstrm, value);
return *this;
}
/**
* write an array of int16_t from the given vector.
* @param value The array to fille. Its size is increased if necessary
* @param nvals The number values to attempt to write to the stream
* @return A reference to the BinaryStreamWriter object
*/
BinaryStreamWriter &BinaryStreamWriter::write(const std::vector<int16_t> &value, const size_t nvals) {
writeToStream(m_ofstrm, value, nvals);
return *this;
}
/**
* write an array of int32_t from the given vector.
* @param value The array to fill. Its size is increased if necessary
* @param nvals The number values to attempt to write to the stream
* @return A reference to the BinaryStreamWriter object
*/
BinaryStreamWriter &BinaryStreamWriter::write(const std::vector<int32_t> &value, const size_t nvals) {
writeToStream(m_ofstrm, value, nvals);
return *this;
}
/**
* write an array of int64_t from the given vector.
* @param value The array to fill. Its size is increased if necessary
* @param nvals The number values to attempt to write to the stream
* @return A reference to the BinaryStreamWriter object
*/
BinaryStreamWriter &BinaryStreamWriter::write(const std::vector<int64_t> &value, const size_t nvals) {
writeToStream(m_ofstrm, value, nvals);
return *this;
}
/**
* write an array of float balues from the given vector.
* @param value The array to fill. Its size is increased if necessary
* @param nvals The number values to attempt to write to the stream
* @return A reference to the BinaryStreamWriter object
*/
BinaryStreamWriter &BinaryStreamWriter::write(const std::vector<float> &value, const size_t nvals) {
writeToStream(m_ofstrm, value, nvals);
return *this;
}
/**
* write an array of double values from the given vector.
* @param value The array to fill. Its size is increased if necessary
* @param nvals The number values to attempt to write to the stream
* @return A reference to the BinaryStreamWriter object
*/
BinaryStreamWriter &BinaryStreamWriter::write(const std::vector<double> &value, const size_t nvals) {
writeToStream(m_ofstrm, value, nvals);
return *this;
}
/**
* write a series of characters from a string object.
* @param value The string to fill. Its size is increased if necessary
* @param length The number characters to attempt to write to the stream
* @return A reference to the BinaryStreamWriter object
*/
BinaryStreamWriter &BinaryStreamWriter::write(const std::string &value, const size_t length) {
m_ofstrm.write(const_cast<char *>(value.data()), length);
return *this;
}
} // namespace Mantid::Kernel