Class constrained_type< T &, Constraint, Verifier >

Synopsis

#include <include/type_safe/constrained_type.hpp>

template <typename T, class Constraint, class Verifier>
class constrained_type<T&, Constraint, Verifier> : Constraint

Description

Specialization of [ts::constrained_type]() for references.

It models a reference to a value that always fulfills the given constraint. The value must not be changed by other means, it is thus perfect for function parameters. \unique_name constrained_type_ref

Inheritance

Ancestors: Constraint

Methods

constrained_type overload\effects Binds the reference to the given object.
constrained_type overload\exclude
get_constraintReturns: The predicate that determines validity.
get_valueReturns: A const reference to the referred value.
modifyReturns: A proxy object to provide verified write-access to the referred value
operator*Dereference operator.
operator->Member access operator.

Source

Lines 268-339 in include/type_safe/constrained_type.hpp.

template <typename T, class Constraint, class Verifier>
class constrained_type<T&, Constraint, Verifier> : Constraint
{
public:
    using value_type           = T;
    using constraint_predicate = Constraint;

    /// \effects Binds the reference to the given object.
    explicit constexpr constrained_type(T& value, constraint_predicate predicate = {})
    : Constraint(std::move(predicate)), ref_(&Verifier::verify(value, get_constraint()))
    {}

    /// \exclude
    template <typename U,
              typename
              = typename std::enable_if<!detail::is_valid<constraint_predicate, U>::value>::type>
    constrained_type(U) = delete;

    /// \returns A proxy object to provide verified write-access to the referred value.
    /// \notes This function does not participate in overload resolution if `T` is `const`.
    template <typename Dummy = T,
              typename       = typename std::enable_if<!std::is_const<Dummy>::value>::type>
    constrained_modifier<T&, Constraint, Verifier> modify() noexcept
    {
        debug_verify();
        return constrained_modifier<T&, Constraint, Verifier>(*this);
    }

    /// Dereference operator.
    /// \returns A `const` reference to the referred value.
    constexpr const value_type& operator*() const noexcept
    {
        return get_value();
    }

    /// Member access operator.
    /// \returns A `const` pointer to the referred value.
    constexpr const value_type* operator->() const noexcept
    {
        return &get_value();
    }

    /// \returns A `const` reference to the referred value.
    constexpr const value_type& get_value() const noexcept
    {
        return debug_verify();
    }

    /// \returns The predicate that determines validity.
    constexpr const constraint_predicate& get_constraint() const noexcept
    {
        return *this;
    }

private:
    constexpr const value_type& debug_verify() const noexcept
    {
#if TYPE_SAFE_ENABLE_ASSERTIONS
        return Verifier::verify(*ref_, get_constraint());
#else
        return *ref_;
#endif
    }

    TYPE_SAFE_CONSTEXPR14 value_type& get_non_const() noexcept
    {
        return *ref_;
    }

    T* ref_;
    friend constrained_modifier<T&, Constraint, Verifier>;
};





Add Discussion as Guest

Log in