26#ifndef MYSQL_ROUTER_CLASSIC_PROTOCOL_CODEC_BASE_H_
27#define MYSQL_ROUTER_CLASSIC_PROTOCOL_CODEC_BASE_H_
32#include <system_error>
83template <
class T,
class DynamicBuffer>
86 DynamicBuffer &&dyn_buffer) {
92 const auto orig_size = dyn_buffer.size();
93 const auto codec_size = codec.size();
96 dyn_buffer.grow(codec_size);
98 const auto res = codec.encode(dyn_buffer.data(orig_size, codec_size));
100 dyn_buffer.shrink(codec_size);
104 dyn_buffer.shrink(codec_size - res.value());
136template <
class T,
class... Args>
186 size_t sz = std::numeric_limits<size_t>::max()) {
189 auto step_res = step_<T>(sz);
206 size_t sz = std::numeric_limits<size_t>::max()) {
231 if (sz != std::numeric_limits<size_t>::max()) {
233 if (
buf.size() < sz) {
243 return decode_res->second;
288 if (!
res_)
return *
this;
379 constexpr size_t size() const noexcept {
380 return static_cast<const T *
>(
this)->accumulate_fields(
386 return static_cast<const T *
>(
this)->accumulate_fields(
Codec for a type.
Definition: classic_protocol_codec_base.h:72
Generator of decoded Types of a buffer.
Definition: classic_protocol_codec_base.h:153
stdx::expected< typename Codec< T >::value_type, std::error_code > step_(size_t sz)
Definition: classic_protocol_codec_base.h:227
net::const_buffer buffer_
Definition: classic_protocol_codec_base.h:246
const capabilities::value_type caps_
Definition: classic_protocol_codec_base.h:247
result_type res_
Definition: classic_protocol_codec_base.h:250
DecodeBufferAccumulator(const net::const_buffer &buffer, capabilities::value_type caps, size_t consumed=0)
construct a DecodeBufferAccumulator.
Definition: classic_protocol_codec_base.h:165
size_t consumed_
Definition: classic_protocol_codec_base.h:248
stdx::expected< typename Codec< T >::value_type, std::error_code > try_step(size_t sz=std::numeric_limits< size_t >::max())
try decoding a Type from the buffer sequence.
Definition: classic_protocol_codec_base.h:205
result_type result() const
get result of the step().
Definition: classic_protocol_codec_base.h:219
stdx::expected< typename Codec< T >::value_type, std::error_code > step(size_t sz=std::numeric_limits< size_t >::max())
decode a Type from the buffer sequence.
Definition: classic_protocol_codec_base.h:185
CRTP base for the Codec's encode part.
Definition: classic_protocol_codec_base.h:375
stdx::expected< size_t, std::error_code > encode(net::mutable_buffer buffer) const
Definition: classic_protocol_codec_base.h:384
constexpr size_t size() const noexcept
Definition: classic_protocol_codec_base.h:379
constexpr capabilities::value_type caps() const noexcept
Definition: classic_protocol_codec_base.h:390
const capabilities::value_type caps_
Definition: classic_protocol_codec_base.h:393
constexpr EncodeBase(capabilities::value_type caps)
Definition: classic_protocol_codec_base.h:377
accumulator of encoded buffers.
Definition: classic_protocol_codec_base.h:266
result_type result() const
get result the steps().
Definition: classic_protocol_codec_base.h:306
EncodeBufferAccumulator & step(const T &v)
encode a T into the buffer and move position forward.
Definition: classic_protocol_codec_base.h:287
EncodeBufferAccumulator(net::mutable_buffer buffer, capabilities::value_type caps, size_t consumed=0)
construct a encode-buffer-accumulator.
Definition: classic_protocol_codec_base.h:277
const net::mutable_buffer buffer_
Definition: classic_protocol_codec_base.h:313
const capabilities::value_type caps_
Definition: classic_protocol_codec_base.h:314
result_type res_
Definition: classic_protocol_codec_base.h:317
size_t consumed_
Definition: classic_protocol_codec_base.h:315
accumulates the sizes of encoded T's.
Definition: classic_protocol_codec_base.h:333
constexpr result_type result() const
Definition: classic_protocol_codec_base.h:358
constexpr EncodeSizeAccumulator(capabilities::value_type caps)
construct a EncodeSizeAccumulator.
Definition: classic_protocol_codec_base.h:340
size_t result_type
Definition: classic_protocol_codec_base.h:335
size_t consumed_
Definition: classic_protocol_codec_base.h:361
const capabilities::value_type caps_
Definition: classic_protocol_codec_base.h:362
constexpr EncodeSizeAccumulator & step(const T &v) noexcept
accumulate the size() of encoded T.
Definition: classic_protocol_codec_base.h:349
constexpr const error_type & error() const &
Definition: expected.h:737
Definition: buf0block_hint.cc:30
std::bitset< 32 > value_type
Definition: classic_protocol_constants.h:73
Definition: classic_protocol_binary.h:39
stdx::expected< size_t, std::error_code > encode(const T &v, capabilities::value_type caps, DynamicBuffer &&dyn_buffer)
encode a message into a dynamic buffer.
Definition: classic_protocol_codec_base.h:84
std::error_code make_error_code(codec_errc e) noexcept
Definition: classic_protocol_codec_error.h:86
stdx::expected< std::pair< size_t, T >, std::error_code > decode(const net::const_buffer &buffer, capabilities::value_type caps, Args &&... args)
decode a message from a buffer.
Definition: classic_protocol_codec_base.h:137
stdx::expected< std::pair< size_t, T >, std::error_code > decode(const net::const_buffer &buffer, capabilities::value_type caps)
decode a message from a buffer.
Definition: classic_protocol_codec_base.h:119
constexpr size_t bytes_per_bits(size_t bits)
Definition: classic_protocol_codec_base.h:55
Definition: authentication.cc:36
mutable_buffer buffer(void *p, size_t n) noexcept
Definition: buffer.h:420
constexpr auto make_unexpected(E &&e) -> unexpected< std::decay_t< E > >
Definition: expected.h:125