maybe_null.hpp 2.7 KB

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