maybe_null.hpp 2.7 KB

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