-
Notifications
You must be signed in to change notification settings - Fork 0
API muesli fluent constrainable
Defined in header <muesli/fluent/constrainable>
template<Predicate>
struct constrained_codecForward declaration of constrained_codec
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_argumenttemplate<Derived>
struct constrainableMixin 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.
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 codecs:
constrained_codec
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.
| Parameter | Description |
|---|---|
Predicate |
A callable taking const value_type& and returning bool |
-
pred— The predicate that must return true for values to be valid
Returns: A constrained_codec<Derived, Predicate> wrapping this codec
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