Struct clamping_verifier

Synopsis

#include <include/type_safe/bounded_type.hpp>

}

/// Returns a copy of `val` so that it is in the given [ts::constraints::closed_interval]().
/// \effects If it is not in the interval, returns the bound that is closer to the value.
/// \output_section clamped_type
template <typename T, typename LowerBound, typename UpperBound, typename U>
constexpr auto clamp(const constraints::closed_interval<T, LowerBound, UpperBound>& interval,
                     U&& val) -> typename std::decay<U>::type

Description

A Verifier for [ts::constrained_type]() that clamps the value to make it valid.

It must be used together with [ts::constraints::less_equal](), [ts::constraints::greater_equal]() or [ts::constraints::closed_interval]().

Methods

verify overloadReturns: If val is greater than the bound of p, returns the bound
verify overloadReturns: If val is less than the bound of p, returns the bound
verify overloadReturns: Same as clamp(interval, val).

Source

Lines 484-516 in include/type_safe/bounded_type.hpp.

}

/// Returns a copy of `val` so that it is in the given [ts::constraints::closed_interval]().
/// \effects If it is not in the interval, returns the bound that is closer to the value.
/// \output_section clamped_type
template <typename T, typename LowerBound, typename UpperBound, typename U>
constexpr auto clamp(const constraints::closed_interval<T, LowerBound, UpperBound>& interval,
                     U&& val) -> typename std::decay<U>::type
{
    return val < interval.get_lower_bound()
               ? static_cast<typename std::decay<U>::type>(interval.get_lower_bound())
               : (val > interval.get_upper_bound()
                      ? static_cast<typename std::decay<U>::type>(interval.get_upper_bound())
                      : std::forward<U>(val));
}

/// A `Verifier` for [ts::constrained_type]() that clamps the value to make it valid.
///
/// It must be used together with [ts::constraints::less_equal](),
/// [ts::constraints::greater_equal]() or [ts::constraints::closed_interval]().
struct clamping_verifier
{
    /// \returns If `val` is greater than the bound of `p`,
    /// returns the bound.
    /// Otherwise returns the value unchanged
    template <typename Value, typename T, typename Bound>
    static constexpr auto verify(Value&& val, const constraints::less_equal<T, Bound>& p) ->
        typename std::decay<Value>::type
    {
        return p(val) ? std::forward<Value>(val)
                      : static_cast<typename std::decay<Value>::type>(p.get_bound());
    }






Add Discussion as Guest

Log in