maybe_null.hpp 2.9 KB

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