| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- //
- // maybe_null.hpp
- // pointer
- //
- // Created by Sam Jaffe on 9/24/15.
- //
- //
- #pragma once
- #include <memory>
- #include "pointer_fwd.hpp"
- #include "ptr_compare.hpp"
- class unchecked_pointer_exception : public std::logic_error {
- using std::logic_error::logic_error;
- };
- template <typename P> class maybe_null<not_null<P>>; // not permitted
- #if defined( DEBUG )
- #define set_tested( value ) tested_ = value
- #else
- #define set_tested( _ )
- #endif
- template <typename P>
- class maybe_null : private detail::get_ptr<P> {
- public:
- using element_type = typename std::pointer_traits<P>::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<P>::value) : _ptr(p) { }
- maybe_null(P && p) noexcept(detail::is_nt_mc<P>::value) : _ptr(std::move(p)) { }
- maybe_null(maybe_null const&) noexcept(detail::is_nt_cc<P>::value) = default;
- maybe_null(maybe_null &&) noexcept(detail::is_nt_mc<P>::value) = default;
- template <typename Y>
- maybe_null(maybe_null<Y> const&other) noexcept(detail::is_nt_c<P, Y>::value)
- : _ptr(other._ptr) { set_tested(other.tested_); }
-
- maybe_null& operator=(maybe_null const&) noexcept(detail::is_nt_ca<P>::value) = default;
- maybe_null& operator=(maybe_null &&) noexcept(detail::is_nt_ma<P>::value) = default;
-
- operator bool() const noexcept {
- set_tested(true);
- return static_cast<bool>(_ptr);
- }
-
- pointer get() const noexcept(noexcept(detail::get_ptr<P>::get(_ptr))) { return detail::get_ptr<P>::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::forward(p))); }
- private:
- template <typename Y> friend class maybe_null;
- P _ptr;
- #if defined( DEBUG )
- mutable bool tested_ = false;
- #endif
- };
- #undef set_tested
- POINTER_TEMPLATE_COMPARE( maybe_null )
|