| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- //
- // maybe_null.hpp
- // pointer
- //
- // Created by Sam Jaffe on 9/24/15.
- //
- //
- #pragma once
- #include <memory>
- #include "detail/compare.hpp"
- #include "detail/get_ptr.hpp"
- #include "pointer_fwd.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 detail::pointer_compare<maybe_null<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)) {}
- template <typename Y, typename = typename std::enable_if<
- std::is_constructible<P, Y>::value>::type>
- maybe_null(Y const & p) : _ptr(p) {}
- template <typename Y, typename = typename std::enable_if<
- std::is_constructible<P, Y>::value>::type>
- maybe_null(Y && p) : _ptr(std::forward<Y>(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::move(p))); }
- private:
- template <typename Y> friend class maybe_null;
- P _ptr;
- #if defined(DEBUG)
- mutable bool tested_ = false;
- #endif
- };
- #undef set_tested
|