-
Notifications
You must be signed in to change notification settings - Fork 0
API muesli variant_codec
Felix Jones edited this page Jan 15, 2026
·
1 revision
Defined in header <muesli/variant_codec>
struct variant_codec : fluent::optionable<variant_codec<Codecs...>>,
fluent::constrainable<variant_codec<Codecs...>>,
fluent::transformable<variant_codec<Codecs...>>,
fluent::projectable<variant_codec<Codecs...>>,
fluent::constructable<variant_codec<Codecs...>>,
fluent::nullableable<variant_codec<Codecs...>>
Codec for std::variant types.
The variant_codec encodes/decodes std::variant<Ts...> using a discriminant (variant index) followed by the encoded value for the active alternative. This enables polymorphic serialisation with compact storage.
| Parameter | Description |
|---|---|
Codecs |
Variadic list of codecs (one per variant alternative) Perfect for: - Message types (command, response, error) - Polymorphic data (int or string or floating point) - State machines (idle, running, paused, done) - Result types (success or error) |
value_type
using value_type = std::variant<typename Codecs::value_type...>
Value type is a variant of all codec value types
Constructs a variant_codec from the given alternative codecs
explicit constexpr variant_codec(const Codecs&... cs) noexcept : codecs(cs...)
Discriminant is encoded as uint8_t, limiting variants to ≤256 alternatives
// Message type: either an integer ID or string name
auto msg_codec = variant_codec(int32_codec, string_codec);
std::variant<int32_t, std::string> msg1 = 42; // int alternative
std::variant<int32_t, std::string> msg2 = "alert"; // string alternative
auto encoded1 = msg_codec.encode(msg1);
auto encoded2 = msg_codec.encode(msg2);
// Response type: success with data or error with message
auto response = variant_codec(
tuple_codec(int32_codec, string_codec), // success: (status_code, data)
string_codec // error: (error message)
);
using Response = std::variant<
std::tuple<int32_t, std::string>, // success: code and data
std::string // error: message only
>;
Response ok = std::tuple<int32_t, std::string>{200, "OK"};
Response err = std::string{"Not found"};
auto encoded_ok = response.encode(ok);
auto encoded_err = response.encode(err);
// State machine: idle, running with progress, or done with result
enum class State { Idle, Running, Done };
auto state = variant_codec(
monostate_codec, // Idle state (no data)
int32_codec, // Running state (progress 0-100)
string_codec // Done state (result message)
);
using StateValue = std::variant<std::monostate, int32_t, std::string>;
using value_type = std::variant<typename Codecs::value_type...>; /** @brief Tuple of codecs, one for each variant alternative */ std::tuple<Codecs...> codecs; /** @brief Constructs a variant_codec from the given alternative codecs */ explicit constexpr variant_codec(const Codecs&... cs) noexcept : codecs(cs...)
Value type is a variant of all codec value types
constexpr auto next_codec() const noexcept
Returns a variant_codec of the next codecs in the chain
constexpr auto encode(const value_type& value) const noexcept
Encodes a variant by encoding the active alternative
constexpr value_type decode(auto&& value) const noexcept
Decodes an encoded variant by decoding the appropriate alternative