xrpld
Loading...
Searching...
No Matches
EncodedBlob.h
1#pragma once
2
3#include <xrpl/beast/utility/instrumentation.h>
4#include <xrpl/nodestore/NodeObject.h>
5
6#include <boost/align/align_up.hpp>
7
8#include <algorithm>
9#include <array>
10#include <cstddef>
11#include <cstdint>
12#include <memory>
13#include <stdexcept>
14
15namespace xrpl::node_store {
16
37
39{
44
52 std::array<std::uint8_t, boost::alignment::align_up(9 + 1024, alignof(std::uint32_t))>
54
59
67
68public:
70 : size_([&obj]() {
71 XRPL_ASSERT(obj, "xrpl::node_store::EncodedBlob::EncodedBlob : non-null input");
72
73 if (!obj)
74 throw std::runtime_error("EncodedBlob: unseated std::shared_ptr used.");
75
76 return obj->getData().size() + 9;
77 }())
79 {
81 ptr_[8] = static_cast<std::uint8_t>(obj->getType());
82 std::copy_n(obj->getData().data(), obj->getData().size(), ptr_ + 9);
83 std::copy_n(obj->getHash().data(), obj->getHash().size(), key_.data());
84 }
85
87 {
88 XRPL_ASSERT(
89 ((ptr_ == payload_.data()) && (size_ <= payload_.size())) ||
90 ((ptr_ != payload_.data()) && (size_ > payload_.size())),
91 "xrpl::node_store::EncodedBlob::~EncodedBlob : valid payload "
92 "pointer");
93
94 if (ptr_ != payload_.data())
95 delete[] ptr_;
96 }
97
98 [[nodiscard]] void const*
99 getKey() const noexcept
100 {
101 return static_cast<void const*>(key_.data());
102 }
103
104 [[nodiscard]] std::size_t
105 getSize() const noexcept
106 {
107 return size_;
108 }
109
110 [[nodiscard]] void const*
111 getData() const noexcept
112 {
113 return static_cast<void const*>(ptr_);
114 }
115};
116
117} // namespace xrpl::node_store
std::uint32_t size_
The size of the serialized data.
Definition EncodedBlob.h:58
std::array< std::uint8_t, 32 > key_
The 32-byte key of the serialized object.
Definition EncodedBlob.h:43
EncodedBlob(std::shared_ptr< NodeObject > const &obj)
Definition EncodedBlob.h:69
void const * getData() const noexcept
std::array< std::uint8_t, boost::alignment::align_up(9+1024, alignof(std::uint32_t))> payload_
A pre-allocated buffer for the serialized object.
Definition EncodedBlob.h:53
std::size_t getSize() const noexcept
void const * getKey() const noexcept
Definition EncodedBlob.h:99
std::uint8_t *const ptr_
A pointer to the serialized data.
Definition EncodedBlob.h:66
T copy_n(T... args)
T data(T... args)
T fill_n(T... args)
T size(T... args)