maybe_null.hpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. //
  2. // maybe_null.hpp
  3. // pointer
  4. //
  5. // Created by Sam Jaffe on 9/24/15.
  6. //
  7. //
  8. #pragma once
  9. #include <memory>
  10. #include "pointer_fwd.hpp"
  11. #include "ptr_compare.hpp"
  12. class unchecked_pointer_exception : public std::logic_error {
  13. using std::logic_error::logic_error;
  14. };
  15. template <typename P> class maybe_null<not_null<P>>; // not permitted
  16. #if defined( DEBUG )
  17. #define set_tested( value ) tested_ = value
  18. #else
  19. #define set_tested( _ )
  20. #endif
  21. template <typename P>
  22. class maybe_null : private detail::get_ptr<P> {
  23. public:
  24. using element_type = typename std::pointer_traits<P>::element_type;
  25. using pointer = element_type *;
  26. using reference = element_type &;
  27. maybe_null() noexcept : _ptr(nullptr) {}
  28. maybe_null(P const & p) noexcept(detail::is_nt_cc<P>::value) : _ptr(p) { }
  29. maybe_null(P && p) noexcept(detail::is_nt_mc<P>::value) : _ptr(std::move(p)) { }
  30. template <typename Y, typename = typename std::enable_if<std::is_constructible<P, Y>::value>::type>
  31. maybe_null(Y const & p) : _ptr(p) { }
  32. template <typename Y, typename = typename std::enable_if<std::is_constructible<P, Y>::value>::type>
  33. maybe_null(Y && p) : _ptr(std::move(p)) { }
  34. maybe_null(maybe_null const&) noexcept(detail::is_nt_cc<P>::value) = default;
  35. maybe_null(maybe_null &&) noexcept(detail::is_nt_mc<P>::value) = default;
  36. template <typename Y>
  37. maybe_null(maybe_null<Y> const&other) noexcept(detail::is_nt_c<P, Y>::value)
  38. : _ptr(other._ptr) { set_tested(other.tested_); }
  39. maybe_null& operator=(maybe_null const&) noexcept(detail::is_nt_ca<P>::value) = default;
  40. maybe_null& operator=(maybe_null &&) noexcept(detail::is_nt_ma<P>::value) = default;
  41. operator bool() const noexcept {
  42. set_tested(true);
  43. return static_cast<bool>(_ptr);
  44. }
  45. pointer get() const noexcept(noexcept(detail::get_ptr<P>::get(_ptr))) { return detail::get_ptr<P>::get(_ptr); }
  46. pointer operator->() const /*throw(unchecked_pointer_exception)*/ {
  47. return std::addressof(operator*());
  48. }
  49. reference operator*() const /*throw(unchecked_pointer_exception)*/ {
  50. #if defined( DEBUG )
  51. if ( ! tested_ ) { throw unchecked_pointer_exception{"did not verify that pointer was non-null"}; }
  52. #endif
  53. if ( ! _ptr ) { throw null_pointer_exception{"dereferencing maybe_null in null state"}; }
  54. return *_ptr;
  55. }
  56. void reset( P const & p ) { operator=(maybe_null(p)); }
  57. void reset( P && p = P() ) { operator=(maybe_null(std::move(p))); }
  58. private:
  59. template <typename Y> friend class maybe_null;
  60. P _ptr;
  61. #if defined( DEBUG )
  62. mutable bool tested_ = false;
  63. #endif
  64. };
  65. #undef set_tested
  66. POINTER_TEMPLATE_COMPARE( maybe_null )