-
Notifications
You must be signed in to change notification settings - Fork 0
API muesli tuple_codec
Defined in header <muesli/tuple_codec>
struct tuple_codec : fluent::optionable<tuple_codec<Codecs...>>,
fluent::constrainable<tuple_codec<Codecs...>>,
fluent::transformable<tuple_codec<Codecs...>>,
fluent::projectable<tuple_codec<Codecs...>>,
fluent::constructable<tuple_codec<Codecs...>>,
fluent::nullableable<tuple_codec<Codecs...>>Codec for tuples of codecs.
The tuple_codec combines multiple element codecs into a single codec that encodes/decodes tuples by transforming each element through its corresponding codec. This is the foundation for encoding data structures.
| Parameter | Description |
|---|---|
Codecs |
Variadic list of codecs (one per tuple element) Perfect for: - Protocol message fields (id, timestamp, status) - Heterogeneous data aggregation - Complex struct serialisation with member_codec - Function parameter bundles |
value_type
using value_type = std::tuple<typename Codecs::value_type...>Value type is a tuple of all codec value types
Constructs a tuple_codec from the given element codecs
explicit constexpr tuple_codec(const Codecs&... cs) noexcept : codecs(cs...)Tuple elements are encoded/decoded in order
// Simple tuple: (int32, string, float)
auto simple = tuple_codec(int32_codec, string_codec, float_codec);
std::tuple<int32_t, std::string, float> data = {42, "hello", 3.14f};
auto encoded = simple.encode(data);
auto decoded = simple.decode(encoded);
// Protocol message: version (uint8), id (uint32), timestamp (uint64)
auto protocol_msg = tuple_codec(uint8_codec, uint32_codec, uint64_codec);
std::tuple<uint8_t, uint32_t, uint64_t> msg = {1, 12345, time(nullptr)};
auto encoded_msg = protocol_msg.encode(msg);
// Nested tuples: person data with address
auto address = tuple_codec(string_codec, string_codec, string_codec); // road, city, code
auto person = tuple_codec(string_codec, uint32_codec, address); // name, age, address
std::tuple<std::string, uint32_t, std::tuple<std::string, std::string, std::string>> billy
= {"Billy", 54, {"123 Fake Street", "That Island from Myst", "123 ABC"}};
auto encoded = person.encode(billy);
// With constraints: ensure name is not empty, age is reasonable
auto constrained_person = person
.constrain([](const auto& t) { return !std::get<0>(t).empty() && std::get<1>(t) < 150; });- member_codec - For binding tuple elements to struct members
- pair_codec - Specialised codec for 2-element tuples
using value_type = std::tuple<typename Codecs::value_type...>; /** @brief Tuple of codecs, one for each element */ std::tuple<Codecs...> codecs; /** @brief Constructs a tuple_codec from the given element codecs */ explicit constexpr tuple_codec(const Codecs&... cs) noexcept : codecs(cs...)Value type is a tuple of all codec value types
constexpr auto next_codec() const noexceptReturns a tuple_codec of the next codecs in the chain
template<typename Owner> constexpr value_type project(const Owner& obj) const noexcept requires(HasProjectFromOwner<Codecs, Owner> && ...)Projects values from an owner object using each codec's projector
constexpr auto encode(const value_type& value) const noexceptEncodes each tuple element using its corresponding codec
constexpr value_type decode(const auto& value) const noexceptDecodes each encoded element using its corresponding codec
struct pair_codec : fluent::optionable<pair_codec<Codec1, Codec2>>,
fluent::constrainable<pair_codec<Codec1, Codec2>>,
fluent::transformable<pair_codec<Codec1, Codec2>>,
fluent::projectable<pair_codec<Codec1, Codec2>>,
fluent::constructable<pair_codec<Codec1, Codec2>>,
fluent::nullableable<pair_codec<Codec1, Codec2>>Codec for std::pair that delegates to tuple_codec
The pair_codec is a specialised wrapper around tuple_codec that provides convenient encoding/decoding of std::pair types by converting to/from tuples.
| Parameter | Description |
|---|---|
Codec1 |
Codec for the first element of the pair |
Codec2 |
Codec for the second element of the pair |
value_type
using value_type = std::pair<typename Codec1::value_type, typename Codec2::value_type>Value type is a std::pair of the codec value types
tuple_type
using tuple_type = std::tuple<typename Codec1::value_type, typename Codec2::value_type>Internal tuple representation
Constructs a pair_codec from two element codecs
explicit constexpr pair_codec(const Codec1& c1, const Codec2& c2) noexcept : codec(c1, c2)auto codec = pair_codec(int_codec, string_codec);
std::pair<int, std::string> data = {42, "hello"};
auto encoded = codec.encode(data);- Related codecs:
tuple_codec
using value_type = std::pair<typename Codec1::value_type, typename Codec2::value_type>; /** @brief Internal tuple representation */ using tuple_type = std::tuple<typename Codec1::value_type, typename Codec2::value_type>; /** @brief The underlying tuple_codec that performs actual encoding/decoding */ tuple_codec<Codec1, Codec2> codec; /** @brief Constructs a pair_codec from two element codecs */ explicit constexpr pair_codec(const Codec1& c1, const Codec2& c2) noexcept : codec(c1, c2)Value type is a std::pair of the codec value types