maybe_null.hpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. #include "not_null.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. template <typename T>
  18. class maybe_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. maybe_null() : _ptr(nullptr) {}
  24. template <typename... Args> maybe_null(Args && ...args) : _ptr(std::forward<Args>(args)...) { }
  25. maybe_null(maybe_null const&) = default;
  26. template <typename Y> maybe_null(maybe_null<Y> const& other) : _ptr(other.ptr_) { }
  27. template <typename Y> maybe_null(not_null<Y> const& other) : _ptr(other.get()) { }
  28. maybe_null& operator=(T ptr) { return operator=(maybe_null<T>(ptr)); }
  29. maybe_null& operator=(maybe_null const&) = default;
  30. template <typename Y> maybe_null& operator=(maybe_null<Y> const&other) {
  31. if (_ptr != other.get()) {
  32. _ptr = other.get();
  33. #if defined( DEBUG )
  34. tested_ = other.tested_;
  35. #endif
  36. }
  37. return *this;
  38. }
  39. template <typename Y> maybe_null& operator=(not_null<Y> const&other) {
  40. _ptr = other.get();
  41. #if defined( DEBUG )
  42. tested_ = true;
  43. #endif
  44. return *this;
  45. }
  46. operator bool() const {
  47. #if defined( DEBUG )
  48. tested_ = true;
  49. #endif
  50. return bool(_ptr);
  51. }
  52. pointer get() const {
  53. #if defined( DEBUG )
  54. if ( !tested_ ) {
  55. throw unchecked_pointer_exception{"did not verify that pointer was non-null"};
  56. }
  57. #endif
  58. return std::addressof(operator*());
  59. }
  60. // operator T() const { return get(); }
  61. pointer operator->() const { return get(); }
  62. reference operator*() const { return *_ptr; }
  63. bool operator==(maybe_null const&rhs) const { return _ptr == rhs._ptr; }
  64. bool operator!=(maybe_null const&rhs) const { return _ptr != rhs._ptr; }
  65. bool operator<=(maybe_null const&rhs) const { return _ptr <= rhs._ptr; }
  66. bool operator>=(maybe_null const&rhs) const { return _ptr >= rhs._ptr; }
  67. bool operator< (maybe_null const&rhs) const { return _ptr < rhs._ptr; }
  68. bool operator> (maybe_null const&rhs) const { return _ptr > rhs._ptr; }
  69. private:
  70. T _ptr;
  71. #if defined( DEBUG )
  72. mutable bool tested_ = false;
  73. #endif
  74. };