-
Notifications
You must be signed in to change notification settings - Fork 0
API muesli vector_codec
Defined in header <muesli/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.
| Parameter | Description |
|---|---|
ElementCodec |
The codec for individual vector elements |
Allocator |
The allocator type (default: std::allocator) |
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
Encodes the vector size followed by each element
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);explicit constexpr vector_codec(const ElementCodec& codec) noexcept : element_codec(codec)Constructs a vector_codec with the given element codec
-
codec— The codec to use for encoding/decoding vector elements
constexpr auto next_codec() const noexceptGets the next codec (pair of size and range codec)
Returns: A pair codec with size codec and range codec
static constexpr auto encode(const value_type& value) noexceptEncodes a const vector to size and range holder
-
value— The vector to encode
Returns: A pair of vector size and range holder
static constexpr value_type decode(const auto& tup)Decodes a tuple of (size, range) to a vector
-
tup— The encoded tuple
Returns: A vector with all elements from the range
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
| Parameter | Description |
|---|---|
ElementCodec |
The type of codec for vector elements |
Allocator |
The allocator type (optional, default: std::allocator) |
-
elem— The element codec to use
Returns: A vector_codec instance
auto codec = muesli::vector_of(muesli::int32_codec);