maybe_null.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 "detail/compare.hpp"
  11. #include "detail/get_ptr.hpp"
  12. #include "exception.hpp"
  13. #include "pointer_fwd.hpp"
  14. template <typename P> class maybe_null<not_null<P>>; // not permitted
  15. #if defined(DEBUG)
  16. #define set_tested(value) tested_ = value
  17. #else
  18. #define set_tested(_)
  19. #endif
  20. template <typename P>
  21. class maybe_null : private detail::get_ptr<P>,
  22. public detail::pointer_compare<maybe_null<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(std::is_nothrow_copy_constructible<P>::value)
  29. : _ptr(p) {}
  30. maybe_null(P && p) noexcept(std::is_nothrow_move_constructible<P>::value)
  31. : _ptr(std::move(p)) {}
  32. template <typename Y, typename = typename std::enable_if<
  33. std::is_constructible<P, Y>::value>::type>
  34. maybe_null(Y const & p) : _ptr(p) {}
  35. template <typename Y, typename = typename std::enable_if<
  36. std::is_constructible<P, Y>::value>::type>
  37. maybe_null(Y && p) : _ptr(std::forward<Y>(p)) {}
  38. maybe_null(maybe_null const &) noexcept(
  39. std::is_nothrow_copy_constructible<P>::value) = default;
  40. maybe_null(maybe_null &&) noexcept(
  41. std::is_nothrow_move_constructible<P>::value) = default;
  42. template <typename Y>
  43. maybe_null(maybe_null<Y> const & other) noexcept(
  44. std::is_nothrow_constructible<P, Y>::value)
  45. : _ptr(other._ptr) {
  46. set_tested(other.tested_);
  47. }
  48. maybe_null & operator=(maybe_null const &) noexcept(
  49. std::is_nothrow_copy_assignable<P>::value) = default;
  50. maybe_null & operator=(maybe_null &&) noexcept(
  51. std::is_nothrow_move_assignable<P>::value) = default;
  52. operator bool() const noexcept {
  53. set_tested(true);
  54. return static_cast<bool>(_ptr);
  55. }
  56. pointer get() const noexcept(noexcept(detail::get_ptr<P>::get(_ptr))) {
  57. return detail::get_ptr<P>::get(_ptr);
  58. }
  59. pointer operator->() const /*throw(unchecked_pointer_exception)*/ {
  60. return std::addressof(operator*());
  61. }
  62. reference operator*() const /*throw(unchecked_pointer_exception)*/ {
  63. #if defined(DEBUG)
  64. if (!tested_) {
  65. throw unchecked_pointer_exception{
  66. "did not verify that pointer was non-null"};
  67. }
  68. #endif
  69. if (!_ptr) {
  70. throw null_pointer_exception{"dereferencing maybe_null in null state"};
  71. }
  72. return *_ptr;
  73. }
  74. void reset(P const & p) { operator=(maybe_null(p)); }
  75. void reset(P && p = P()) { operator=(maybe_null(std::move(p))); }
  76. private:
  77. template <typename Y> friend class maybe_null;
  78. P _ptr;
  79. #if defined(DEBUG)
  80. mutable bool tested_ = false;
  81. #endif
  82. };
  83. #undef set_tested