Skip to content

API muesli fluent constrainable

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

muesli/fluent/constrainable

Defined in header <muesli/fluent/constrainable>

constrained_codec

template<Predicate>
struct constrained_codec

Forward declaration of constrained_codec

Member Functions

constrain

Wraps this codec with constraint checking

Creates a constrained_codec that validates all encoded/decoded values against a user-provided predicate function. Returns the predicate as-is if validation passes, throws std::invalid_argument if it fails.

template<typename Predicate> constexpr auto constrain(Predicate pred) const noexcept requires Codec<Derived>

Parameters:

  • pred — The predicate that must return true for values to be valid

Returns: A constrained_codec<Derived, Predicate> wrapping this codec

Example:

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

constrainable

template<Derived>
struct constrainable

Mixin providing .constrain() method for codecs

This mixin allows any codec to be wrapped in a constrained_codec, which validates encoded/decoded values against a user-provided predicate.

Member Functions

constrain

Wraps this codec with constraint checking

Creates a constrained_codec that validates all encoded/decoded values against a user-provided predicate function. Returns the predicate as-is if validation passes, throws std::invalid_argument if it fails.

template<typename Predicate> constexpr auto constrain(Predicate pred) const noexcept requires Codec<Derived>

Parameters:

  • pred — The predicate that must return true for values to be valid

Returns: A constrained_codec<Derived, Predicate> wrapping this codec

Example:

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

Related


constrain

template<typename Predicate> constexpr auto constrain(Predicate pred) const noexcept requires Codec<Derived>

Wraps this codec with constraint checking

Creates a constrained_codec that validates all encoded/decoded values against a user-provided predicate function. Returns the predicate as-is if validation passes, throws std::invalid_argument if it fails.

Template Parameters

Parameter Description
Predicate A callable taking const value_type& and returning bool

Parameters

  • pred — The predicate that must return true for values to be valid

Returns: A constrained_codec<Derived, Predicate> wrapping this codec

Example

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

Clone this wiki locally