OpenCBDC Transaction Processor
Loading...
Searching...
No Matches
ostream_serializer.cpp
Go to the documentation of this file.
1// Copyright (c) 2021 MIT Digital Currency Initiative,
2// Federal Reserve Bank of Boston
3// Distributed under the MIT software license, see the accompanying
4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
7
8#include <cstring>
9#include <vector>
10
11namespace cbdc {
14 m_str(s) {}
15
16 auto ostream_serializer::end_of_buffer() const -> bool {
17 auto current_pos = m_str.tellp();
18 m_str.seekp(0, std::ios::end);
19 if(m_str.tellp() == current_pos) {
20 return true;
21 }
22 m_str.seekp(current_pos);
23 return false;
24 }
25
27 m_str.seekp(static_cast<off_type>(len), std::ios::cur);
28 }
29
31 m_str.clear();
32 m_str.seekp(0);
33 }
34
35 auto ostream_serializer::write(const void* data, size_t len) -> bool {
36 auto write_vec = std::vector<char>(len);
37 std::memcpy(write_vec.data(), data, len);
38 return static_cast<bool>(
39 m_str.write(write_vec.data(), static_cast<off_type>(len)));
40 }
41
42 auto ostream_serializer::read(void* /* data */, size_t /* len */) -> bool {
43 m_str.setstate(std::ios::failbit);
44 return false;
45 }
46}
auto write(const void *data, size_t len) -> bool final
Attempts to write the given number of bytes from the given memory location to the stream's current po...
void advance_cursor(size_t len) final
Moves the stream forward without writing to the stream.
ostream_serializer(std::ostream &s)
Constructor.
void reset() final
Seeks the stream position to the beginning.
auto end_of_buffer() const -> bool final
Indicates whether the serializer has reached the end of the stream.
auto read(void *data, size_t len) -> bool final
Not implemented for ostream.
Implementation of serializer for std::ios.