6 #ifndef KAGOME_COMMON_BUFFER 7 #define KAGOME_COMMON_BUFFER 12 #include <fmt/format.h> 13 #include <boost/container_hash/hash.hpp> 14 #include <boost/operators.hpp> 28 template <
size_t MaxSize>
33 template <
size_t OtherMaxSize>
44 template <
size_t OtherMaxSize>
47 template <
size_t OtherMaxSize>
53 SLBuffer(
const std::array<typename Base::value_type, N> &other)
54 :
Base(other.begin(), other.end()) {}
56 SLBuffer(
const uint8_t *begin,
const uint8_t *end) :
Base(begin, end){};
59 using Base::operator=;
92 const auto *begin =
reinterpret_cast<uint8_t *
>(&n);
93 const auto *end = begin +
sizeof(n) /
sizeof(uint8_t);
106 const auto *begin =
reinterpret_cast<uint8_t *
>(&n);
107 const auto *end = begin +
sizeof(n) /
sizeof(uint8_t);
136 return static_cast<const typename
Base::Base &
>(*this);
140 return static_cast<typename
Base::Base &
>(*this);
144 return static_cast<typename
Base::Base &
>(*this);
148 return std::move(static_cast<typename Base::Base &>(*
this));
177 static outcome::result<SLBuffer>
fromHex(std::string_view hex) {
178 OUTCOME_TRY(bytes,
unhex(hex));
179 return outcome::success(
SLBuffer(std::move(bytes)));
188 return std::string{Base::cbegin(), Base::cend()};
198 return std::string_view(reinterpret_cast<const char *>(Base::data()),
206 return {src.begin(), src.end()};
209 using Base::operator==;
213 Base::cbegin(), Base::cend(), other.cbegin(), other.cend());
218 const std::array<typename Base::value_type, N> &other)
const noexcept {
220 Base::cbegin(), Base::cend(), other.cbegin(), other.cend());
223 using Base::operator<;
226 return std::lexicographical_compare(
227 Base::cbegin(), Base::cend(), other.cbegin(), other.cend());
232 const std::array<typename Base::value_type, N> &other)
const noexcept {
233 return std::lexicographical_compare(
234 Base::cbegin(), Base::cend(), other.cbegin(), other.cend());
238 template <
size_t MaxSize>
254 inline Buffer
operator""_buf(
const char *c,
size_t s) {
255 std::vector<uint8_t> chars(c, c + s);
256 return Buffer(std::move(chars));
261 inline Buffer
operator""_hex2buf(
const char *c,
size_t s) {
269 struct std::hash<
kagome::common::SLBuffer<N>> {
271 return boost::hash_range(x.begin(), x.end());
277 : fmt::formatter<kagome::common::BufferView> {};
279 #endif // KAGOME_COMMON_BUFFER
Base::iterator insert(Iter pos, const typename Base::value_type &value)
static outcome::result< SLBuffer > fromHex(std::string_view hex)
Construct SLBuffer from hex string.
Class represents arbitrary (including empty) byte buffer.
SLBuffer & putUint64(uint64_t n)
Put a 64-bit {.
size_t operator()(const kagome::common::SLBuffer< N > &x) const
std::ostream & operator<<(std::ostream &os, const Blob< N > &blob)
bool operator<(const BufferView &other) const noexcept
SLBuffer & resize(size_t size)
std::string hex_lower(const gsl::span< const uint8_t > bytes) noexcept
Converts bytes to hex representation.
SLBuffer(OtherSLBuffer< OtherMaxSize > &&other)
void resize(typename Base::size_type size)
std::reference_wrapper< Buffer > BufferMutRef
const std::vector< uint8_t > & asVector() const
getter for vector of bytes
common::BufferView BufferView
SLBuffer & operator+=(const BufferView &view) noexcept
SLBuffer(const uint8_t *begin, const uint8_t *end)
std::string_view asString() const
return content of bytearray as a string copy data
std::string toString() const
return content of bytearray as string
SLBuffer & put(const BufferView &view)
Put a sequence of bytes as view into byte buffer.
gsl::span< const uint8_t > make_span(const rocksdb::Slice &s)
SLBuffer< std::numeric_limits< size_t >::max()> Buffer
void push_back(V &&value)
SLBuffer & put(std::string_view view)
Put a string into byte buffer.
SLBuffer subbuffer(size_t offset=0, size_t length=-1) const
void reserve(typename Base::size_type size)
std::vector< uint8_t > & asVector()
bool operator<(const std::array< typename Base::value_type, N > &other) const noexcept
std::string toHex() const
encode bytearray as hex
SLBuffer & reserve(size_t size)
bool operator==(const BufferView &other) const noexcept
SLBuffer & putUint32(uint32_t n)
Put a 32-bit {.
std::reference_wrapper< const Buffer > BufferConstRef
outcome::result< std::vector< uint8_t > > unhex(std::string_view hex)
Converts hex representation to bytes.
static const Buffer kEmptyBuffer
std::vector< uint8_t > toVector()&&
SLBuffer & putUint8(uint8_t n)
Put a 8-bit {.
static SLBuffer fromString(const std::string_view &src)
stores content of a string to byte array
SLBuffer(const typename Base::Base &other)
lvalue construct buffer from a byte vector
SLBuffer(const OtherSLBuffer< OtherMaxSize > &other)
bool operator==(const std::array< typename Base::value_type, N > &other) const noexcept
SLBuffer(const std::array< typename Base::value_type, N > &other)
BufferView view(size_t offset=0, size_t length=-1) const
std::vector< uint8_t > toVector()&
SLBuffer(const BufferView &s)
SLBuffer(typename Base::Base &&other)