-
Notifications
You must be signed in to change notification settings - Fork 0
API muesli array_codec
Defined in header <muesli/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.
| 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 |
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_typeEncoded type produced by the range_codec
Constructs an array_codec with the given element codec
explicit constexpr array_codec(const ElementCodec& codec) noexcept : element_codec(codec)// 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;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
constexpr next_codec_type next_codec() const noexceptReturns the next codec in the encoding chain
static constexpr encoded_type encode(const value_type& value) noexceptEncodes an array as a range
static constexpr value_type decode(const encoded_type& value) noexceptDecodes a range back into a fixed-size array
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
| Parameter | Description |
|---|---|
N |
The size of the array |
-
elem— The codec for array elements
Returns: An array_codec configured for arrays of size N