Skip to content

API muesli constrained_codec

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

muesli/constrained_codec

Defined in header <muesli/constrained_codec>

constrained_codec

template<Predicate>
struct constrained_codec : fluent::transformable<constrained_codec<InnerCodec, Predicate>>,
                           fluent::projectable<constrained_codec<InnerCodec, Predicate>>,
                           fluent::constructable<constrained_codec<InnerCodec, Predicate>>,
                           fluent::nullableable<constrained_codec<InnerCodec, Predicate>>

Codec for constrained values with validation predicates.

The constrained_codec wraps another codec and validates encoded/decoded values against a user-provided predicate. Throws std::invalid_argument if validation fails.

Template Parameters

Parameter Description
InnerCodec The wrapped codec
Predicate Function taking const value_type& and returning bool

Member Types

value_type

using value_type = typename InnerCodec::value_type

The value type from the inner codec

next_codec_type

using next_codec_type = std::decay_t<decltype(std::declval<InnerCodec>().next_codec())>

The next codec type from the inner codec

encoded_type

using encoded_type = typename next_codec_type::value_type

The encoded type from the next codec

Member Functions

move

Constructs a constrained_codec with the given inner codec and predicate

constexpr constrained_codec(const InnerCodec& c, Predicate pred) : inner(c), predicate(std::move(pred))

Example

auto positive_int = int_codec.constrain([](int v) { return v > 0; });
auto encoded = positive_int.encode(42);    // OK
// auto bad = positive_int.encode(-5);     // Throws

move

using value_type = typename InnerCodec::value_type; /** @brief The next codec type from the inner codec */ using next_codec_type = std::decay_t<decltype(std::declval<InnerCodec>().next_codec())>; /** @brief The encoded type from the next codec */ using encoded_type = typename next_codec_type::value_type; /** @brief The inner codec to use for encoding/decoding */ InnerCodec inner; /** @brief The predicate to validate values */ Predicate predicate; /** @brief Constructs a constrained_codec with the given inner codec and predicate */ constexpr constrained_codec(const InnerCodec& c, Predicate pred) : inner(c), predicate(std::move(pred))

The value type from the inner codec


next_codec

constexpr const next_codec_type& next_codec() const noexcept

Returns the next codec from the inner codec


encode

constexpr encoded_type encode(const value_type& value) const

Encodes a value after validating it passes the constraint


decode

constexpr value_type decode(const auto& encoded) const

Decodes a value and validates it passes the constraint


constrain

template<typename NewPredicate> constexpr auto constrain(NewPredicate new_pred) const noexcept

Adds an additional constraint by merging predicates with AND logic

Parameters

  • new_pred — The new predicate to add

Returns: A constrained_codec with merged predicates


optional

template<Codec InnerCodec, typename Predicate> constexpr auto constrained_codec<InnerCodec, Predicate>::optional() const noexcept

Implementation of constrained_codec::optional() method

This must be defined after optional_codec is complete to avoid circular dependencies.


Clone this wiki locally