-
Notifications
You must be signed in to change notification settings - Fork 0
API muesli optional_codec
Defined in header <muesli/optional_codec>
template<Predicate>
struct constrained_codecForward declaration of constrained_codec
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_typeEncoded type from the next 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
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().
| Parameter | Description |
|---|---|
ValueCodec |
The codec for the wrapped value type |
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_typeEncoded type from the next 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
Encoding null values uses variant index 0 with std::monostate
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);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
constexpr const next_codec_type& next_codec() const noexceptGets the next codec in the chain (variant codec)
Returns: The underlying variant codec
static constexpr encoded_type encode(value_type&& value) noexceptEncodes an rvalue optional to its variant form
-
value— The optional value to encode
Returns: Encoded variant with index 0 (monostate) or 1 (value)
constexpr value_type decode(encoded_type&& value) const noexceptDecodes a variant to an optional
-
value— The encoded variant
Returns: Optional with value if variant index is 1, std::nullopt if index is 0
constexpr auto constrain() constConstrains this codec to reject null values
Returns: A constrained_codec that throws on std::nullopt