Class constrained_modifier

Synopsis

#include <include/type_safe/constrained_type.hpp>

template <typename T, class Constraint, class Verifier>
class constrained_modifier

Description

A proxy class to provide write access to the stored value of a [ts::constrained_type]().

The destructor will verify the value again.

Methods

constrained_modifier\effects Move constructs it
~constrained_modifier\effects Verifies the value, if there is any.
getReturns: A reference to the stored value
operator*Dereference operator.
operator->Member access operator.
operator=\effects Move assigns it

Source

Lines 348-407 in include/type_safe/constrained_type.hpp.

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

    /// \effects Move constructs it.
    /// `other` will not verify any value afterwards.
    constrained_modifier(constrained_modifier&& other) noexcept : value_(other.value_)
    {
        other.value_ = nullptr;
    }

    /// \effects Verifies the value, if there is any.
    ~constrained_modifier() noexcept(false)
    {
        if (value_)
            Verifier::verify(**value_, value_->get_constraint());
    }

    /// \effects Move assigns it.
    /// `other` will not verify any value afterwards.
    constrained_modifier& operator=(constrained_modifier&& other) noexcept
    {
        value_       = other.value_;
        other.value_ = nullptr;
        return *this;
    }

    /// Dereference operator.
    /// \returns A reference to the stored value.
    /// \requires It must not be in the moved-from state.
    value_type& operator*() noexcept
    {
        return get();
    }

    /// Member access operator.
    /// \returns A pointer to the stored value.
    /// \requires It must not be in the moved-from state.
    value_type* operator->() noexcept
    {
        return &get();
    }

    /// \returns A reference to the stored value.
    /// \requires It must not be in the moved-from state.
    value_type& get() noexcept
    {
        DEBUG_ASSERT(value_, detail::precondition_error_handler{});
        return value_->get_non_const();
    }

private:
    constrained_modifier(constrained_type<T, Constraint, Verifier>& value) noexcept : value_(&value)
    {}

    constrained_type<T, Constraint, Verifier>* value_;
    friend constrained_type<T, Constraint, Verifier>;
};





Add Discussion as Guest

Log in