OpenCBDC Transaction Processor
Loading...
Searching...
No Matches
istream_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 istream_serializer::end_of_buffer() const -> bool {
17 auto current_pos = m_str.tellg();
18 m_str.seekg(0, std::ios::end);
19 if(m_str.tellg() == current_pos) {
20 return true;
21 }
22 m_str.seekg(current_pos);
23 return false;
24 }
25
27 m_str.seekg(static_cast<off_type>(len), std::ios::cur);
28 }
29
31 m_str.clear();
32 m_str.seekg(0);
33 }
34
35 auto istream_serializer::write(const void* /* data */, size_t /* len */)
36 -> bool {
37 m_str.setstate(std::ios::failbit);
38 return false;
39 }
40
41 auto istream_serializer::read(void* data, size_t len) -> bool {
42 auto read_vec = std::vector<char>(len);
43 if(!m_str.read(read_vec.data(), static_cast<off_type>(len))) {
44 return false;
45 }
46
47 std::memcpy(data, read_vec.data(), len);
48
49 return true;
50 }
51}
auto read(void *data, size_t len) -> bool final
Attempts to read the given number of bytes from the current position in the stream.
void advance_cursor(size_t len) final
Moves the stream forward without reading from the stream.
auto write(const void *data, size_t len) -> bool final
Not implemented for istream.
istream_serializer(std::istream &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.
Implementation of serializer for std::ios.