OpenCBDC Transaction Processor
Loading...
Searching...
No Matches
buffer_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
10namespace cbdc {
12
13 buffer_serializer::operator bool() const {
14 return m_valid;
15 }
16
18 m_cursor += len;
19 }
20
22 m_cursor = 0;
23 m_valid = true;
24 }
25
26 [[nodiscard]] auto buffer_serializer::end_of_buffer() const -> bool {
27 return m_cursor >= m_pkt.size();
28 }
29
30 auto buffer_serializer::write(const void* data, size_t len) -> bool {
31 if(m_cursor + len > m_pkt.size()) {
32 m_pkt.extend(m_cursor + len - m_pkt.size());
33 }
34 std::memcpy(m_pkt.data_at(m_cursor), data, len);
35 m_cursor += len;
36 return true;
37 }
38
39 auto buffer_serializer::read(void* data, size_t len) -> bool {
40 if(m_cursor + len > m_pkt.size()) {
41 m_valid = false;
42 return false;
43 }
44 std::memcpy(data, m_pkt.data_at(m_cursor), len);
45 m_cursor += len;
46 return true;
47 }
48}
void advance_cursor(size_t len) final
Moves the cursor forward by the given number of bytes.
auto read(void *data, size_t len) -> bool final
Read the given number of bytes from the buffer from the current cursor position into the given memory...
auto write(const void *data, size_t len) -> bool final
Write the given bytes into the buffer from the current cursor position.
auto end_of_buffer() const -> bool final
Indicates whether the cursor is at or beyond the end of the buffer.
void reset() final
Resets the cursor to the start of the buffer.
buffer_serializer(cbdc::buffer &pkt)
Constructor.
Buffer to store and retrieve byte data.
Definition buffer.hpp:15
auto size() const -> size_t
Returns the number of bytes contained in the buffer.
Definition buffer.cpp:24