maybe_null.hpp 2.7 KB

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