6 #ifndef KAGOME_MESSAGE_READ_WRITER_HPP 7 #define KAGOME_MESSAGE_READ_WRITER_HPP 9 #include <boost/assert.hpp> 10 #include <boost/system/error_code.hpp> 27 template <
typename Adapter,
typename Ancestor>
46 static
size_t need_to_reserve(const T &t) {
47 return AdapterType::size(t) + AncestorType::need_to_reserve(t);
51 static BufferContainer::iterator
write(
const T &t,
53 size_t reserved = 0ull) {
54 const auto need_to_reserve = SelfType::need_to_reserve(t);
55 if (need_to_reserve > out.size()) out.resize(need_to_reserve);
57 const size_t r = AdapterType::size(t) + reserved;
58 auto loaded = AncestorType::write(t, out, r);
60 BOOST_ASSERT(std::distance(out.begin(), loaded) >= 0);
61 BOOST_ASSERT(static_cast<size_t>(std::distance(out.begin(), loaded))
63 return AdapterType::write(t, out, loaded);
67 static outcome::result<BufferContainer::const_iterator>
read(
70 BufferContainer::const_iterator from) {
71 if (from == src.end())
72 return outcome::failure(boost::system::error_code{});
74 OUTCOME_TRY(it, AdapterType::read(out, src, from));
75 return AncestorType::read(out, src, it);
79 template <
typename Adapter>
99 size_t reserved = 0) {
100 const auto need_to_reserve = SelfType::need_to_reserve(t);
101 if (need_to_reserve > out.size()) out.resize(need_to_reserve);
103 BOOST_ASSERT(std::distance(out.begin(), out.end()) >= 0);
104 BOOST_ASSERT(static_cast<size_t>(std::distance(out.begin(), out.end()))
105 >= AdapterType::size(t) + reserved);
106 return AdapterType::write(t, out, out.end());
109 template <
typename T>
110 static outcome::result<BufferContainer::const_iterator>
read(
113 BufferContainer::const_iterator from) {
114 if (from == src.end())
115 return outcome::failure(boost::system::error_code{});
117 return AdapterType::read(out, src, from);
120 template <
typename T>
122 return AdapterType::size(t);
128 #endif // KAGOME_SCALE_MESSAGE_READ_WRITER_HPP std::vector< uint8_t > BufferContainer
std::vector< uint8_t > BufferContainer
static outcome::result< BufferContainer::const_iterator > read(T &out, const BufferContainer &src, BufferContainer::const_iterator from)
static size_t need_to_reserve(const T &t)
static outcome::result< BufferContainer::const_iterator > read(T &out, const BufferContainer &src, BufferContainer::const_iterator from)
static BufferContainer::iterator write(const T &t, BufferContainer &out, size_t reserved=0ull)