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) {
242 return decode_res->second;
287 if (!
res_)
return *
this;
378 constexpr size_t size() const noexcept {
379 return static_cast<const T *
>(
this)->accumulate_fields(
385 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:245
const capabilities::value_type caps_
Definition: classic_protocol_codec_base.h:246
result_type res_
Definition: classic_protocol_codec_base.h:249
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:247
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:374
stdx::expected< size_t, std::error_code > encode(net::mutable_buffer buffer) const
Definition: classic_protocol_codec_base.h:383
constexpr size_t size() const noexcept
Definition: classic_protocol_codec_base.h:378
constexpr capabilities::value_type caps() const noexcept
Definition: classic_protocol_codec_base.h:389
const capabilities::value_type caps_
Definition: classic_protocol_codec_base.h:392
constexpr EncodeBase(capabilities::value_type caps)
Definition: classic_protocol_codec_base.h:376
accumulator of encoded buffers.
Definition: classic_protocol_codec_base.h:265
result_type result() const
get result the steps().
Definition: classic_protocol_codec_base.h:305
EncodeBufferAccumulator & step(const T &v)
encode a T into the buffer and move position forward.
Definition: classic_protocol_codec_base.h:286
EncodeBufferAccumulator(net::mutable_buffer buffer, capabilities::value_type caps, size_t consumed=0)
construct a encode-buffer-accumulator.
Definition: classic_protocol_codec_base.h:276
const net::mutable_buffer buffer_
Definition: classic_protocol_codec_base.h:312
const capabilities::value_type caps_
Definition: classic_protocol_codec_base.h:313
result_type res_
Definition: classic_protocol_codec_base.h:316
size_t consumed_
Definition: classic_protocol_codec_base.h:314
accumulates the sizes of encoded T's.
Definition: classic_protocol_codec_base.h:332
constexpr result_type result() const
Definition: classic_protocol_codec_base.h:357
constexpr EncodeSizeAccumulator(capabilities::value_type caps)
construct a EncodeSizeAccumulator.
Definition: classic_protocol_codec_base.h:339
size_t result_type
Definition: classic_protocol_codec_base.h:334
size_t consumed_
Definition: classic_protocol_codec_base.h:360
const capabilities::value_type caps_
Definition: classic_protocol_codec_base.h:361
constexpr EncodeSizeAccumulator & step(const T &v) noexcept
accumulate the size() of encoded T.
Definition: classic_protocol_codec_base.h:348
constexpr const error_type & error() const &
Definition: expected.h:751
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: http_server_component.cc:34
mutable_buffer buffer(void *p, size_t n) noexcept
Definition: buffer.h:418
unexpected(E) -> unexpected< E >