// // maybe_null.hpp // pointer // // Created by Sam Jaffe on 9/24/15. // // #pragma once #include #include "detail/compare.hpp" #include "detail/get_ptr.hpp" #include "exception.hpp" #include "pointer_fwd.hpp" #if defined(DEBUG) #define set_tested(value) tested_ = value #else #define set_tested(_) #endif namespace pointers { template class maybe_null>; // not permitted template class maybe_null : private detail::get_ptr

, public detail::pointer_compare> { public: using element_type = typename std::pointer_traits

::element_type; using pointer = element_type *; using reference = element_type &; maybe_null() noexcept : _ptr(nullptr) {} maybe_null(P const & p) noexcept( std::is_nothrow_copy_constructible

::value) : _ptr(p) {} maybe_null(P && p) noexcept(std::is_nothrow_move_constructible

::value) : _ptr(std::move(p)) {} template ::value>::type> maybe_null(Y const & p) : _ptr(p) {} template ::value>::type> maybe_null(Y && p) : _ptr(std::forward(p)) {} maybe_null(maybe_null const &) noexcept( std::is_nothrow_copy_constructible

::value) = default; maybe_null(maybe_null &&) noexcept( std::is_nothrow_move_constructible

::value) = default; template maybe_null(maybe_null const & other) noexcept( std::is_nothrow_constructible::value) : _ptr(other._ptr) { set_tested(other.tested_); } maybe_null & operator=(maybe_null const &) noexcept( std::is_nothrow_copy_assignable

::value) = default; maybe_null & operator=(maybe_null &&) noexcept( std::is_nothrow_move_assignable

::value) = default; operator bool() const noexcept { set_tested(true); return static_cast(_ptr); } pointer get() const noexcept(noexcept(detail::get_ptr

::get(_ptr))) { return detail::get_ptr

::get(_ptr); } pointer operator->() const /*throw(unchecked_pointer_exception)*/ { return std::addressof(operator*()); } reference operator*() const /*throw(unchecked_pointer_exception)*/ { #if defined(DEBUG) if (!tested_) { throw unchecked_pointer_exception{ "did not verify that pointer was non-null"}; } #endif if (!_ptr) { throw null_pointer_exception{"dereferencing maybe_null in null state"}; } return *_ptr; } void reset(P const & p) { operator=(maybe_null(p)); } void reset(P && p = P()) { operator=(maybe_null(std::move(p))); } private: template friend class maybe_null; P _ptr; #if defined(DEBUG) mutable bool tested_ = false; #endif }; } #undef set_tested