OpenCBDC Transaction Processor
Loading...
Searching...
No Matches
util/raft/serialization.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
6#include "serialization.hpp"
7
8#include <cstring>
9#include <libnuraft/buffer.hxx>
10#include <vector>
11
12namespace cbdc {
13 nuraft_serializer::nuraft_serializer(nuraft::buffer& buf) : m_buf(buf) {
15 }
16
17 nuraft_serializer::operator bool() const {
18 return m_valid;
19 }
20
22 m_buf.pos(m_buf.pos() + len);
23 }
24
26 m_buf.pos(0);
27 m_valid = true;
28 }
29
30 [[nodiscard]] auto nuraft_serializer::end_of_buffer() const -> bool {
31 return m_buf.pos() >= m_buf.size();
32 }
33
34 auto nuraft_serializer::write(const void* data, size_t len) -> bool {
35 if(m_buf.pos() + len > m_buf.size()) {
36 m_valid = false;
37 return false;
38 }
39 auto data_vec = std::vector<nuraft::byte>(len);
40 std::memcpy(data_vec.data(), data, len);
41 m_buf.put_raw(data_vec.data(), len);
42 return true;
43 }
44
45 auto nuraft_serializer::read(void* data, size_t len) -> bool {
46 if(m_buf.pos() + len > m_buf.size()) {
47 m_valid = false;
48 return false;
49 }
50 std::memcpy(data, m_buf.get_raw(len), len);
51 return true;
52 }
53}
auto end_of_buffer() const -> bool final
Returns whether the cursor is currently at the end of the buffer.
void reset() final
Resets the cursor to the start of the buffer.
auto read(void *data, size_t len) -> bool final
Reads the given number of bytes from the current cursor in the buffer into the given destination.
auto write(const void *data, size_t len) -> bool final
Writes the given raw bytes to the buffer from the current position of the cursor.
void advance_cursor(size_t len) final
Moves the cursor inside the buffer forward by the given number of bytes.
nuraft_serializer(nuraft::buffer &buf)
Constructor.