Skip to content

API muesli vector_codec

Felix Jones edited this page Jan 15, 2026 · 1 revision

muesli/vector_codec

Defined in header <muesli/vector_codec>

vector_codec

template<Allocator, ElementCodec>
struct vector_codec : fluent::optionable<vector_codec<ElementCodec, Allocator>>,
                      fluent::constrainable<vector_codec<ElementCodec, Allocator>>,
                      fluent::transformable<vector_codec<ElementCodec, Allocator>>,
                      fluent::projectable<vector_codec<ElementCodec, Allocator>>,
                      fluent::constructable<vector_codec<ElementCodec, Allocator>>,
                      fluent::nullableable<vector_codec<ElementCodec, Allocator>>

Codec for encoding/decoding std::vector containers

The vector_codec handles serialisation of std::vector by encoding it as a pair of (size, range) where the range yields all elements. This allows efficient serialisation of arbitrary-sized vectors.

Template Parameters

Parameter Description
ElementCodec The codec for individual vector elements
Allocator The allocator type (default: std::allocator)

Member Functions

element_codec

Constructs a vector_codec with the given element codec

explicit constexpr vector_codec(const ElementCodec& codec) noexcept : element_codec(codec)

Parameters:

  • codec — The codec to use for encoding/decoding vector elements

Notes

Encodes the vector size followed by each element

Example

auto elem_codec = muesli::int32_codec;
auto vec_codec = muesli::vector_of(elem_codec);

std::vector<int32_t> vec = {1, 2, 3, 4, 5};
auto encoded = vec_codec.encode(vec);
auto decoded = vec_codec.decode(encoded);

element_codec

explicit constexpr vector_codec(const ElementCodec& codec) noexcept : element_codec(codec)

Constructs a vector_codec with the given element codec

Parameters

  • codec — The codec to use for encoding/decoding vector elements

next_codec

constexpr auto next_codec() const noexcept

Gets the next codec (pair of size and range codec)

Returns: A pair codec with size codec and range codec


encode

static constexpr auto encode(const value_type& value) noexcept

Encodes a const vector to size and range holder

Parameters

  • value — The vector to encode

Returns: A pair of vector size and range holder


decode

static constexpr value_type decode(const auto& tup)

Decodes a tuple of (size, range) to a vector

Parameters

  • tup — The encoded tuple

Returns: A vector with all elements from the range


vector_of

template<Codec ElementCodec, class Allocator = std::allocator<typename ElementCodec::value_type>> constexpr auto vector_of(const ElementCodec& elem)

Helper function to create a vector_codec

Template Parameters

Parameter Description
ElementCodec The type of codec for vector elements
Allocator The allocator type (optional, default: std::allocator)

Parameters

  • elem — The element codec to use

Returns: A vector_codec instance

Example

auto codec = muesli::vector_of(muesli::int32_codec);

Clone this wiki locally