// // maybe_null.hpp // pointer // // Created by Sam Jaffe on 9/24/15. // // #pragma once #include #include "pointer_fwd.hpp" #include "ptr_compare.hpp" class unchecked_pointer_exception : public std::logic_error { using std::logic_error::logic_error; }; template class maybe_null>; // not permitted #if defined( DEBUG ) #define set_tested( value ) tested_ = value #else #define set_tested( _ ) #endif template class maybe_null : private detail::get_ptr

{ 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(detail::is_nt_cc

::value) : _ptr(p) { } maybe_null(P && p) noexcept(detail::is_nt_mc

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

::value) = default; maybe_null(maybe_null &&) noexcept(detail::is_nt_mc

::value) = default; template maybe_null(maybe_null const&other) noexcept(detail::is_nt_c::value) : _ptr(other._ptr) { set_tested(other.tested_); } maybe_null& operator=(maybe_null const&) noexcept(detail::is_nt_ca

::value) = default; maybe_null& operator=(maybe_null &&) noexcept(detail::is_nt_ma

::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 POINTER_TEMPLATE_COMPARE( maybe_null )