maybe_null.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //
  2. // maybe_null.hpp
  3. // pointer
  4. //
  5. // Created by Sam Jaffe on 9/24/15.
  6. //
  7. //
  8. #pragma once
  9. #include <stdexcept>
  10. #include <memory>
  11. #include "pointer_fwd.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. template <typename T>
  17. class maybe_null {
  18. public:
  19. using element_type = typename std::pointer_traits<T>::element_type;
  20. using pointer = element_type *;
  21. using reference = element_type &;
  22. maybe_null() : _ptr(nullptr) {}
  23. maybe_null(T const & p) : _ptr(p) { }
  24. maybe_null(T && p) : _ptr(std::move(p)) { }
  25. maybe_null(maybe_null const&) = default;
  26. template <typename Y>
  27. explicit operator maybe_null<Y>() const {
  28. return _ptr;
  29. }
  30. maybe_null& operator=(maybe_null const&) = default;
  31. template <typename Y> maybe_null& operator=(maybe_null<Y> const&other) {
  32. if (_ptr != other._ptr) {
  33. _ptr = other._ptr;
  34. #if defined( DEBUG )
  35. tested_ = other.tested_;
  36. #endif
  37. }
  38. return *this;
  39. }
  40. operator bool() const {
  41. #if defined( DEBUG )
  42. tested_ = true;
  43. #endif
  44. return static_cast<bool>(_ptr);
  45. }
  46. pointer get() const { return std::addressof(*_ptr); }
  47. pointer operator->() const {
  48. return std::addressof(operator*());
  49. }
  50. reference operator*() const {
  51. #if defined( DEBUG )
  52. if ( !tested_ ) {
  53. throw unchecked_pointer_exception{"did not verify that pointer was non-null"};
  54. }
  55. #endif
  56. return *_ptr;
  57. }
  58. private:
  59. T _ptr;
  60. #if defined( DEBUG )
  61. mutable bool tested_ = false;
  62. #endif
  63. };
  64. template <typename T, typename U>
  65. bool operator==(maybe_null<T> const&lhs, maybe_null<U> const&rhs) {
  66. return lhs.get() == rhs.get();
  67. }
  68. template <typename T, typename U>
  69. bool operator!=(maybe_null<T> const&lhs, maybe_null<U> const&rhs) {
  70. return !(lhs == rhs);
  71. }
  72. template <typename T, typename U>
  73. bool operator< (maybe_null<T> const&lhs, maybe_null<U> const&rhs) {
  74. typedef typename std::common_type<
  75. typename maybe_null<T>::pointer,
  76. typename maybe_null<U>::pointer>::type V;
  77. return std::less<V>(lhs.get(), rhs.get());
  78. }
  79. template <typename T, typename U>
  80. bool operator> (maybe_null<T> const&lhs, maybe_null<U> const&rhs) {
  81. return rhs < lhs;
  82. }
  83. template <typename T, typename U>
  84. bool operator<=(maybe_null<T> const&lhs, maybe_null<U> const&rhs) {
  85. return !(rhs < lhs);
  86. }
  87. template <typename T, typename U>
  88. bool operator>=(maybe_null<T> const&lhs, maybe_null<U> const&rhs) {
  89. return !(lhs < rhs);
  90. }
  91. template <typename T>
  92. bool operator==(maybe_null<T> const&lhs, std::nullptr_t) {
  93. return !lhs;
  94. }
  95. template <typename T>
  96. bool operator==(std::nullptr_t, maybe_null<T> const&rhs) {
  97. return !rhs;
  98. }
  99. template <typename T>
  100. bool operator!=(maybe_null<T> const&lhs, std::nullptr_t) {
  101. return static_cast<bool>(lhs);
  102. }
  103. template <typename T>
  104. bool operator!=(std::nullptr_t, maybe_null<T> const&rhs) {
  105. return static_cast<bool>(rhs);
  106. }
  107. template <typename T>
  108. bool operator< (maybe_null<T> const&lhs, std::nullptr_t) {
  109. typedef typename maybe_null<T>::pointer V;
  110. return std::less<V>(lhs.get(), nullptr);
  111. }
  112. template <typename T>
  113. bool operator< (std::nullptr_t, maybe_null<T> const&rhs) {
  114. typedef typename maybe_null<T>::pointer V;
  115. return std::less<V>(nullptr, rhs.get());
  116. }
  117. template <typename T>
  118. bool operator> (maybe_null<T> const&lhs, std::nullptr_t) {
  119. return nullptr < lhs;
  120. }
  121. template <typename T>
  122. bool operator> (std::nullptr_t, maybe_null<T> const&rhs) {
  123. return rhs < nullptr;
  124. }
  125. template <typename T>
  126. bool operator<=(maybe_null<T> const&lhs, std::nullptr_t) {
  127. return !(nullptr < lhs);
  128. }
  129. template <typename T>
  130. bool operator<=(std::nullptr_t, maybe_null<T> const&rhs) {
  131. return !(rhs < nullptr);
  132. }
  133. template <typename T>
  134. bool operator>=(maybe_null<T> const&lhs, std::nullptr_t) {
  135. return !(lhs < nullptr);
  136. }
  137. template <typename T>
  138. bool operator>=(std::nullptr_t, maybe_null<T> const&rhs) {
  139. return !(nullptr < rhs);
  140. }