Mantid
Loading...
Searching...
No Matches
BinaryStreamWriter.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2019 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7//------------------------------------------------------------------------------
8// Includes
9//------------------------------------------------------------------------------
11#include "MantidKernel/Matrix.h"
12
13#include <cassert>
14#include <fstream>
15#include <stdexcept>
16
17namespace Mantid::Kernel {
18
19//------------------------------------------------------------------------------
20// Anonymous functions
21//------------------------------------------------------------------------------
22namespace {
23
29template <typename T> inline void writeToStream(std::ostream &stream, const T &value) {
30 stream.write(reinterpret_cast<const char *>(&value), sizeof(T));
31}
32
41template <typename T> inline void writeToStream(std::ostream &stream, const std::vector<T> &value, size_t nvals) {
42 stream.write(reinterpret_cast<const char *>(value.data()), nvals * sizeof(T));
43}
44} // namespace
45
46//------------------------------------------------------------------------------
47// Public members
48//------------------------------------------------------------------------------
49
57 : m_ofstrm(ofstrm), m_strLengthSize(static_cast<uint64_t>(sizeof(int32_t))) {
58 if (ofstrm.fail()) {
59 throw std::runtime_error("BinaryStreamWriter: Output stream is in a bad "
60 "state. Cannot continue.");
61 }
62}
63
70 writeToStream(m_ofstrm, value);
71 return *this;
72}
73
80 writeToStream(m_ofstrm, value);
81 return *this;
82}
83
90 writeToStream(m_ofstrm, value);
91 return *this;
92}
93
100 writeToStream(m_ofstrm, value);
101 return *this;
102}
103
110 writeToStream(m_ofstrm, value);
111 return *this;
112}
113
123 // First write the size.
124 decltype(m_strLengthSize) length(value.length());
125 m_ofstrm.write(reinterpret_cast<char *>(&length), m_strLengthSize);
126 // Now the value
127 m_ofstrm.write(const_cast<char *>(value.data()), static_cast<size_t>(length));
128 return *this;
129}
130
137 writeToStream(m_ofstrm, value);
138 return *this;
139}
140
147 writeToStream(m_ofstrm, value);
148 return *this;
149}
150
157BinaryStreamWriter &BinaryStreamWriter::write(const std::vector<int16_t> &value, const size_t nvals) {
158 writeToStream(m_ofstrm, value, nvals);
159 return *this;
160}
161
168BinaryStreamWriter &BinaryStreamWriter::write(const std::vector<int32_t> &value, const size_t nvals) {
169 writeToStream(m_ofstrm, value, nvals);
170 return *this;
171}
172
179BinaryStreamWriter &BinaryStreamWriter::write(const std::vector<int64_t> &value, const size_t nvals) {
180 writeToStream(m_ofstrm, value, nvals);
181 return *this;
182}
183
190BinaryStreamWriter &BinaryStreamWriter::write(const std::vector<float> &value, const size_t nvals) {
191 writeToStream(m_ofstrm, value, nvals);
192 return *this;
193}
194
201BinaryStreamWriter &BinaryStreamWriter::write(const std::vector<double> &value, const size_t nvals) {
202 writeToStream(m_ofstrm, value, nvals);
203 return *this;
204}
205
212BinaryStreamWriter &BinaryStreamWriter::write(const std::string &value, const size_t length) {
213 m_ofstrm.write(const_cast<char *>(value.data()), length);
214 return *this;
215}
216
217} // namespace Mantid::Kernel
double value
The value of the point.
Definition: FitMW.cpp:51
Assists with writing a binary file by providing standard overloads for the ostream operators (<<) to ...
std::ostream & m_ofstrm
Reference to the stream being written to.
BinaryStreamWriter & write(const std::vector< int16_t > &value, const size_t nvals)
write an array of int16_t from the given vector.
BinaryStreamWriter(std::ostream &ofstrm)
Constructor taking the stream to write.
uint64_t m_strLengthSize
The default size in bytes of the type used to encode the length of a string in the file.
BinaryStreamWriter & operator<<(const int16_t &value)
write a int16_t to the stream