10#ifndef MEMORY_STREAM_H
11#define MEMORY_STREAM_H
22#if (defined(__GNUC__) && (__GNUC__ < 5) && !defined(__clang__)) || (defined(__clang__) && (__clang_major__ < 7))
24#define IS_TRIVIALLY_COPYABLE(T) __has_trivial_copy(T)
26#define IS_TRIVIALLY_COPYABLE(T) std::is_trivially_copyable<T>::value
72 inline explicit operator bool()
const {
return state_ == std::ios::goodbit; }
75 template <
typename T>
void write_object(T
const &t);
81 template <
typename T>
void write_vector(std::vector<T>
const &t);
88 template <
typename T>
void read_object(T &t);
94 template <
typename T>
void read_vector(std::vector<T> &t);
109 inline void setf(
decltype(std::ios::fmtflags(0)),
decltype(std::ios::floatfield)) {}
112 inline void setf(
decltype(std::ios::fmtflags(0))) {}
115 inline void flags(
decltype(std::ios::fmtflags(0))) {}
118 inline decltype(std::ios::fmtflags(0))
flags()
const {
return std::ios::fmtflags(0); }
147 std::ios::iostate
state_ = std::ios::goodbit;
169 inline bool has_remaining(
size_t c) {
return c <= (data_length_ - read_pos_); }
174 static_assert(IS_TRIVIALLY_COPYABLE(T),
"Cannot use write_object() on complex type");
175 size_t const new_data_size =
sizeof(T);
190 static_assert(IS_TRIVIALLY_COPYABLE(T),
"Cannot use write_vector() on complex type");
191 size_t const vector_length = t.size();
192 size_t const new_data_size =
sizeof(size_t) +
sizeof(T) * vector_length;
193 if (expand_output_buffer(new_data_size)) {
194 std::memcpy(output_location(), &vector_length,
sizeof(
size_t));
195 incr_write_pos(
sizeof(T));
196 std::memcpy(output_location(), t.data(), t.size() *
sizeof(T));
197 incr_write_pos(t.size() *
sizeof(T));
210 static_assert(IS_TRIVIALLY_COPYABLE(T),
"Cannot use read_object() on complex type");
212 if (has_remaining(
sizeof(T))) {
213 std::memcpy(&t, input_location(),
sizeof(T));
214 incr_read_pos(
sizeof(T));
227 static_assert(IS_TRIVIALLY_COPYABLE(T),
"Cannot use read_vector() on complex type");
229 size_t vector_length = 0;
230 if (has_remaining(
sizeof(
size_t))) {
231 std::memcpy(&vector_length, input_location(),
sizeof(
size_t));
232 incr_read_pos(
sizeof(
size_t));
233 if (has_remaining(vector_length *
sizeof(T))) {
234 t.resize(vector_length);
235 std::memcpy(t.data(), input_location(), vector_length *
sizeof(T));
236 incr_read_pos(vector_length *
sizeof(T));
239 setstate(std::ios::failbit);
251 decltype(std::setprecision(10))
const &)
256#if !defined(_MSC_VER) && !defined(__SUNPRO_CC)
259 decltype(std::setw(10))
const &)
Arbitrary size array (one dimensions) suitable for linear algebra operations (i.e....
Definition: colvartypes.h:37
Value of a collective variable: this is a metatype which can be set at runtime. By default it is set ...
Definition: colvarvalue.h:43
Definition: colvars_memstream.h:30
bool has_remaining(size_t c)
Definition: colvars_memstream.h:169
unsigned char * output_location()
Next location to write to.
Definition: colvars_memstream.h:60
std::ios::iostate state_
Error status.
Definition: colvars_memstream.h:147
void done_reading()
Mark the reading attempt succesful.
Definition: colvars_memstream.h:162
memory_stream(size_t n, unsigned char const *buf)
Set up a stream based on an external input buffer.
Definition: colvars_memstream.h:39
unsigned char * output_buffer()
Output buffer.
Definition: colvars_memstream.h:54
void write_object(T const &t)
Write a simple object to the output buffer.
Definition: colvars_memstream.h:172
std::ios::iostate rdstate() const
Get the error code.
Definition: colvars_memstream.h:121
void read_vector(std::vector< T > &t)
Read a vector of simple objects from the buffer.
Definition: colvars_memstream.h:225
void setf(decltype(std::ios::fmtflags(0)))
Ignore formatting operators.
Definition: colvars_memstream.h:112
std::vector< unsigned char > internal_buffer_
Internal buffer (may server for both input and output)
Definition: colvars_memstream.h:138
size_t read_pos_
Current position when reading from the buffer.
Definition: colvars_memstream.h:156
size_t length() const
Length of the buffer.
Definition: colvars_memstream.h:51
void write_vector(std::vector< T > const &t)
Write a vector of simple objects to the output buffer.
Definition: colvars_memstream.h:188
void incr_write_pos(size_t c)
Move the buffer position past the data just written.
Definition: colvars_memstream.h:153
void flags(decltype(std::ios::fmtflags(0)))
Ignore formatting operators.
Definition: colvars_memstream.h:115
void setstate(std::ios::iostate new_state)
Set the error code.
Definition: colvars_memstream.h:124
size_t data_length_
Length of the data buffer (either internal or external)
Definition: colvars_memstream.h:141
decltype(std::ios::fmtflags(0)) flags() const
Get the current formatting flags (i.e. none because this stream is unformatted)
Definition: colvars_memstream.h:118
unsigned char const * input_buffer() const
Input buffer.
Definition: colvars_memstream.h:63
memory_stream & seekg(size_t pos)
Report the current position in the buffer.
Definition: colvars_memstream.h:106
memory_stream(size_t max_length=(static_cast< size_t >(1L)<< 36))
Definition: colvars_memstream.h:36
size_t const max_length_
Largest allowed capacity of the data buffer.
Definition: colvars_memstream.h:144
unsigned char const * external_input_buffer_
External input buffer.
Definition: colvars_memstream.h:135
void incr_read_pos(size_t c)
Move the buffer position past the data just read.
Definition: colvars_memstream.h:165
std::vector< unsigned char > * external_output_buffer_
External output buffer.
Definition: colvars_memstream.h:132
unsigned char const * input_location() const
Next location to read from.
Definition: colvars_memstream.h:69
void read_object(T &t)
Read a simple object from the buffer.
Definition: colvars_memstream.h:208
void begin_reading()
Begin an attempt to read an object; assume EOF unless there is space remaining.
Definition: colvars_memstream.h:159
memory_stream(std::vector< unsigned char > &buf)
Set up a stream based on an external output buffer.
Definition: colvars_memstream.h:45
bool expand_output_buffer(size_t add_bytes)
Add the requester number of bytes to the array capacity; return false if buffer is external.
Definition: colvars_memstream.cpp:17
void setf(decltype(std::ios::fmtflags(0)), decltype(std::ios::floatfield))
Ignore formatting operators.
Definition: colvars_memstream.h:109
void clear()
Clear the error code.
Definition: colvars_memstream.h:127
size_t tellg() const
Report the current position in the buffer.
Definition: colvars_memstream.h:103