not_null.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. //
  2. // not_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. #include "maybe_null.hpp"
  13. class null_pointer_exception : public std::invalid_argument {
  14. using std::invalid_argument::invalid_argument;
  15. };
  16. template <typename P> class not_null<std::weak_ptr<P>>; // A weak_ptr cannot be a not_null
  17. template <typename T>
  18. class not_null {
  19. public:
  20. using element_type = typename std::pointer_traits<T>::element_type;
  21. using pointer = element_type *;
  22. using reference = element_type &;
  23. explicit not_null(std::nullptr_t) = delete;
  24. explicit not_null(int) = delete;
  25. not_null(T const & p) : _ptr(p) { validate(); }
  26. not_null(T && p) : _ptr(std::move(p)) { validate(); }
  27. not_null(not_null const&) = default;
  28. template <typename Y>
  29. explicit operator maybe_null<Y>() const {
  30. return _ptr;
  31. }
  32. template <typename Y>
  33. explicit operator not_null<Y>() const {
  34. return _ptr;
  35. }
  36. not_null& operator=(not_null const&) = default;
  37. template <typename Y> not_null& operator=(not_null<Y> const&other) {
  38. _ptr = other._ptr;
  39. return *this;
  40. }
  41. explicit operator maybe_null<T>() const;
  42. operator bool() const { return true; }
  43. pointer get() const { return std::addressof(operator*()); }
  44. reference operator*() const { return *_ptr; }
  45. pointer operator->() const { return get(); }
  46. private:
  47. void validate() {
  48. if (get() == nullptr) {
  49. throw null_pointer_exception{"not_null<T> cannot be null"};
  50. }
  51. }
  52. T _ptr;
  53. };
  54. template <typename T, typename U>
  55. bool operator==(not_null<T> const&lhs, not_null<U> const&rhs) {
  56. return lhs.get() == rhs.get();
  57. }
  58. template <typename T, typename U>
  59. bool operator!=(not_null<T> const&lhs, not_null<U> const&rhs) {
  60. return !(lhs == rhs);
  61. }
  62. template <typename T, typename U>
  63. bool operator< (not_null<T> const&lhs, not_null<U> const&rhs) {
  64. typedef typename std::common_type<
  65. typename not_null<T>::pointer,
  66. typename not_null<U>::pointer>::type V;
  67. return std::less<V>(lhs.get(), rhs.get());
  68. }
  69. template <typename T, typename U>
  70. bool operator> (not_null<T> const&lhs, not_null<U> const&rhs) {
  71. return rhs < lhs;
  72. }
  73. template <typename T, typename U>
  74. bool operator<=(not_null<T> const&lhs, not_null<U> const&rhs) {
  75. return !(rhs < lhs);
  76. }
  77. template <typename T, typename U>
  78. bool operator>=(not_null<T> const&lhs, not_null<U> const&rhs) {
  79. return !(lhs < rhs);
  80. }
  81. template <typename T>
  82. bool operator==(not_null<T> const&lhs, std::nullptr_t) {
  83. return !lhs;
  84. }
  85. template <typename T>
  86. bool operator==(std::nullptr_t, not_null<T> const&rhs) {
  87. return !rhs;
  88. }
  89. template <typename T>
  90. bool operator!=(not_null<T> const&lhs, std::nullptr_t) {
  91. return static_cast<bool>(lhs);
  92. }
  93. template <typename T>
  94. bool operator!=(std::nullptr_t, not_null<T> const&rhs) {
  95. return static_cast<bool>(rhs);
  96. }
  97. template <typename T>
  98. bool operator< (not_null<T> const&lhs, std::nullptr_t) {
  99. typedef typename not_null<T>::pointer V;
  100. return std::less<V>(lhs.get(), nullptr);
  101. }
  102. template <typename T>
  103. bool operator< (std::nullptr_t, not_null<T> const&rhs) {
  104. typedef typename not_null<T>::pointer V;
  105. return std::less<V>(nullptr, rhs.get());
  106. }
  107. template <typename T>
  108. bool operator> (not_null<T> const&lhs, std::nullptr_t) {
  109. return nullptr < lhs;
  110. }
  111. template <typename T>
  112. bool operator> (std::nullptr_t, not_null<T> const&rhs) {
  113. return rhs < nullptr;
  114. }
  115. template <typename T>
  116. bool operator<=(not_null<T> const&lhs, std::nullptr_t) {
  117. return !(nullptr < lhs);
  118. }
  119. template <typename T>
  120. bool operator<=(std::nullptr_t, not_null<T> const&rhs) {
  121. return !(rhs < nullptr);
  122. }
  123. template <typename T>
  124. bool operator>=(not_null<T> const&lhs, std::nullptr_t) {
  125. return !(lhs < nullptr);
  126. }
  127. template <typename T>
  128. bool operator>=(std::nullptr_t, not_null<T> const&rhs) {
  129. return !(nullptr < rhs);
  130. }