| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- //
- // maybe_null.hpp
- // pointer
- //
- // Created by Sam Jaffe on 9/24/15.
- //
- //
- #pragma once
- #include <stdexcept>
- #include <memory>
- #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
- template <typename T>
- class maybe_null {
- public:
- using element_type = typename std::pointer_traits<T>::element_type;
- using pointer = element_type *;
- using reference = element_type &;
-
- maybe_null() : _ptr(nullptr) {}
- maybe_null(T const & p) : _ptr(p) { }
- maybe_null(T && p) : _ptr(std::move(p)) { }
- maybe_null(maybe_null const&) = default;
-
- template <typename Y>
- explicit operator maybe_null<Y>() const {
- return _ptr;
- }
-
- maybe_null& operator=(maybe_null const&) = default;
- template <typename Y> maybe_null& operator=(maybe_null<Y> const&other) {
- if (_ptr != other._ptr) {
- _ptr = other._ptr;
- #if defined( DEBUG )
- tested_ = other.tested_;
- #endif
- }
- return *this;
- }
-
- operator bool() const {
- #if defined( DEBUG )
- tested_ = true;
- #endif
- return static_cast<bool>(_ptr);
- }
-
- pointer get() const { return std::addressof(*_ptr); }
- pointer operator->() const {
- return std::addressof(operator*());
- }
- reference operator*() const {
- #if defined( DEBUG )
- if ( !tested_ ) {
- throw unchecked_pointer_exception{"did not verify that pointer was non-null"};
- }
- #endif
- return *_ptr;
- }
- private:
- T _ptr;
- #if defined( DEBUG )
- mutable bool tested_ = false;
- #endif
- };
- template <typename T, typename U>
- bool operator==(maybe_null<T> const&lhs, maybe_null<U> const&rhs) {
- return lhs.get() == rhs.get();
- }
- template <typename T, typename U>
- bool operator!=(maybe_null<T> const&lhs, maybe_null<U> const&rhs) {
- return !(lhs == rhs);
- }
- template <typename T, typename U>
- bool operator< (maybe_null<T> const&lhs, maybe_null<U> const&rhs) {
- typedef typename std::common_type<
- typename maybe_null<T>::pointer,
- typename maybe_null<U>::pointer>::type V;
- return std::less<V>(lhs.get(), rhs.get());
- }
- template <typename T, typename U>
- bool operator> (maybe_null<T> const&lhs, maybe_null<U> const&rhs) {
- return rhs < lhs;
- }
- template <typename T, typename U>
- bool operator<=(maybe_null<T> const&lhs, maybe_null<U> const&rhs) {
- return !(rhs < lhs);
- }
- template <typename T, typename U>
- bool operator>=(maybe_null<T> const&lhs, maybe_null<U> const&rhs) {
- return !(lhs < rhs);
- }
- template <typename T>
- bool operator==(maybe_null<T> const&lhs, std::nullptr_t) {
- return !lhs;
- }
- template <typename T>
- bool operator==(std::nullptr_t, maybe_null<T> const&rhs) {
- return !rhs;
- }
- template <typename T>
- bool operator!=(maybe_null<T> const&lhs, std::nullptr_t) {
- return static_cast<bool>(lhs);
- }
- template <typename T>
- bool operator!=(std::nullptr_t, maybe_null<T> const&rhs) {
- return static_cast<bool>(rhs);
- }
- template <typename T>
- bool operator< (maybe_null<T> const&lhs, std::nullptr_t) {
- typedef typename maybe_null<T>::pointer V;
- return std::less<V>(lhs.get(), nullptr);
- }
- template <typename T>
- bool operator< (std::nullptr_t, maybe_null<T> const&rhs) {
- typedef typename maybe_null<T>::pointer V;
- return std::less<V>(nullptr, rhs.get());
- }
- template <typename T>
- bool operator> (maybe_null<T> const&lhs, std::nullptr_t) {
- return nullptr < lhs;
- }
- template <typename T>
- bool operator> (std::nullptr_t, maybe_null<T> const&rhs) {
- return rhs < nullptr;
- }
- template <typename T>
- bool operator<=(maybe_null<T> const&lhs, std::nullptr_t) {
- return !(nullptr < lhs);
- }
- template <typename T>
- bool operator<=(std::nullptr_t, maybe_null<T> const&rhs) {
- return !(rhs < nullptr);
- }
- template <typename T>
- bool operator>=(maybe_null<T> const&lhs, std::nullptr_t) {
- return !(lhs < nullptr);
- }
- template <typename T>
- bool operator>=(std::nullptr_t, maybe_null<T> const&rhs) {
- return !(nullptr < rhs);
- }
|