maybe_null.hpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. bool operator==(maybe_null const&rhs) const { return _ptr == rhs._ptr; }
  59. bool operator!=(maybe_null const&rhs) const { return _ptr != rhs._ptr; }
  60. bool operator<=(maybe_null const&rhs) const { return _ptr <= rhs._ptr; }
  61. bool operator>=(maybe_null const&rhs) const { return _ptr >= rhs._ptr; }
  62. bool operator< (maybe_null const&rhs) const { return _ptr < rhs._ptr; }
  63. bool operator> (maybe_null const&rhs) const { return _ptr > rhs._ptr; }
  64. private:
  65. T _ptr;
  66. #if defined( DEBUG )
  67. mutable bool tested_ = false;
  68. #endif
  69. };