Skip to content

API muesli optional_codec

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

muesli/optional_codec

Defined in header <muesli/optional_codec>

constrained_codec

template<Predicate>
struct constrained_codec

Forward declaration of constrained_codec

Member Types

value_type

using value_type = std::optional<typename ValueCodec::value_type>

Value type is std::optional of the inner codec's value type

next_codec_type

using next_codec_type =

Next codec type is a variant codec (monostate or value)

encoded_type

using encoded_type = next_codec_type::value_type

Encoded type from the next codec

Member Functions

variant_codec

Constructs optional_codec with a value codec

explicit constexpr optional_codec(const ValueCodec& codec) noexcept : value_codec(codec), m_next_codec(variant_codec(constant_codec<std::monostate>

Parameters:

  • codec — The codec to use for encoding/decoding wrapped values

optional_codec

struct optional_codec : fluent::transformable<optional_codec<ValueCodec>>,
                        fluent::projectable<optional_codec<ValueCodec>>,
                        fluent::constructable<optional_codec<ValueCodec>>

Wraps a codec to encode/decode std::optional values

The optional_codec transforms a codec for type T into a codec for std::optional. It uses a variant under the hood: std::variant<std::monostate, T> where monostate represents the absence of a value.

This codec intentionally does NOT use the optionable mixin to prevent the nonsensical pattern of calling .optional().optional().

Template Parameters

Parameter Description
ValueCodec The codec for the wrapped value type

Member Types

value_type

using value_type = std::optional<typename ValueCodec::value_type>

Value type is std::optional of the inner codec's value type

next_codec_type

using next_codec_type =

Next codec type is a variant codec (monostate or value)

encoded_type

using encoded_type = next_codec_type::value_type

Encoded type from the next codec

Member Functions

variant_codec

Constructs optional_codec with a value codec

explicit constexpr optional_codec(const ValueCodec& codec) noexcept : value_codec(codec), m_next_codec(variant_codec(constant_codec<std::monostate>

Parameters:

  • codec — The codec to use for encoding/decoding wrapped values

Notes

Encoding null values uses variant index 0 with std::monostate

Example

auto inner = muesli::int32_codec;
auto opt_codec = muesli::optional_codec(inner);

std::optional<int32_t> value = 42;
auto encoded = opt_codec.encode(value);

std::optional<int32_t> empty = std::nullopt;
auto encoded_empty = opt_codec.encode(empty);

variant_codec

using value_type = std::optional<typename ValueCodec::value_type>; /** @brief Next codec type is a variant codec (monostate or value) */ using next_codec_type = decltype(variant_codec(constant_codec<std::monostate>

Value type is std::optional of the inner codec's value type


next_codec

constexpr const next_codec_type& next_codec() const noexcept

Gets the next codec in the chain (variant codec)

Returns: The underlying variant codec


encode

static constexpr encoded_type encode(value_type&& value) noexcept

Encodes an rvalue optional to its variant form

Parameters

  • value — The optional value to encode

Returns: Encoded variant with index 0 (monostate) or 1 (value)


decode

constexpr value_type decode(encoded_type&& value) const noexcept

Decodes a variant to an optional

Parameters

  • value — The encoded variant

Returns: Optional with value if variant index is 1, std::nullopt if index is 0


constrain

constexpr auto constrain() const

Constrains this codec to reject null values

Returns: A constrained_codec that throws on std::nullopt


Clone this wiki locally