Skip to content

API muesli array_codec

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

muesli/array_codec

Defined in header <muesli/array_codec>

array_codec

struct array_codec : fluent::optionable<array_codec<ElementCodec, N>>,
                     fluent::constrainable<array_codec<ElementCodec, N>>,
                     fluent::transformable<array_codec<ElementCodec, N>>,
                     fluent::projectable<array_codec<ElementCodec, N>>,
                     fluent::constructable<array_codec<ElementCodec, N>>,
                     fluent::nullableable<array_codec<ElementCodec, N>>

Codec for fixed-size arrays.

The array_codec handles std::array<T, N> by encoding it as a sequence of element-encoded values. The size is compile-time known, so no size prefix is encoded - just the sequence of elements.

Template Parameters

Parameter Description
ElementCodec Codec for individual array elements
N Compile-time array size Useful for: - Fixed-size buffers or data structures - Protocol message headers with fixed fields - Hash values - Coordinate tuples or vectors

Member Types

value_type

using value_type = std::array<typename ElementCodec::value_type, N>

Value type is a fixed-size array of element values

next_codec_type

using next_codec_type = range_codec<ElementCodec>

The next codec in the chain is a range_codec

encoded_type

using encoded_type = typename next_codec_type::value_type

Encoded type produced by the range_codec

Member Functions

element_codec

Constructs an array_codec with the given element codec

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

Example

// Encode/decode a 3D coordinate as an array
auto coords = array_codec<3>(float_codec);
std::array<float, 3> position = {1.5f, 2.5f, 3.5f};
auto encoded = coords.encode(position);

// Use with constraints (array elements must be positive)
auto positive_coords = coords.constrain([](const auto& arr) {
    return std::all_of(arr.begin(), arr.end(), [](float v) { return v > 0; });
});

// Use with optional for sparse data
auto optional_coords = coords.optional();
std::optional<std::array<float, 3>> maybe_pos = position;

element_codec

using value_type = std::array<typename ElementCodec::value_type, N>; /** @brief The next codec in the chain is a range_codec */ using next_codec_type = range_codec<ElementCodec>; /** @brief Encoded type produced by the range_codec */ using encoded_type = typename next_codec_type::value_type; /** @brief Codec for encoding/decoding individual array elements */ ElementCodec element_codec; /** @brief Constructs an array_codec with the given element codec */ explicit constexpr array_codec(const ElementCodec& codec) noexcept : element_codec(codec)

Value type is a fixed-size array of element values


next_codec

constexpr next_codec_type next_codec() const noexcept

Returns the next codec in the encoding chain


encode

static constexpr encoded_type encode(const value_type& value) noexcept

Encodes an array as a range


decode

static constexpr value_type decode(const encoded_type& value) noexcept

Decodes a range back into a fixed-size array


array_of

template<std::size_t N, Codec ElementCodec> constexpr auto array_of(const ElementCodec& elem)

Helper function to create an array_codec with deduced element type

Template Parameters

Parameter Description
N The size of the array

Parameters

  • elem — The codec for array elements

Returns: An array_codec configured for arrays of size N


Clone this wiki locally