-
Notifications
You must be signed in to change notification settings - Fork 0
API muesli constrained_codec
Defined in header <muesli/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.
| Parameter | Description |
|---|---|
InnerCodec |
The wrapped codec |
Predicate |
Function taking const value_type& and returning bool |
value_type
using value_type = typename InnerCodec::value_typeThe 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_typeThe encoded type from the next codec
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))auto positive_int = int_codec.constrain([](int v) { return v > 0; });
auto encoded = positive_int.encode(42); // OK
// auto bad = positive_int.encode(-5); // Throwsusing 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
constexpr const next_codec_type& next_codec() const noexceptReturns the next codec from the inner codec
constexpr encoded_type encode(const value_type& value) constEncodes a value after validating it passes the constraint
constexpr value_type decode(const auto& encoded) constDecodes a value and validates it passes the constraint
template<typename NewPredicate> constexpr auto constrain(NewPredicate new_pred) const noexceptAdds an additional constraint by merging predicates with AND logic
-
new_pred— The new predicate to add
Returns: A constrained_codec with merged predicates
template<Codec InnerCodec, typename Predicate> constexpr auto constrained_codec<InnerCodec, Predicate>::optional() const noexceptImplementation of constrained_codec::optional() method
This must be defined after optional_codec is complete to avoid circular dependencies.