maybe_null.hpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 "ptr_compare.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. maybe_null(T const & p) : _ptr(p) { }
  25. maybe_null(T && p) : _ptr(std::move(p)) { }
  26. maybe_null(maybe_null const&) = default;
  27. template <typename Y>
  28. explicit operator maybe_null<Y>() const {
  29. return _ptr;
  30. }
  31. maybe_null& operator=(maybe_null const&) = default;
  32. template <typename Y> maybe_null& operator=(maybe_null<Y> const&other) {
  33. if (_ptr != other._ptr) {
  34. _ptr = other._ptr;
  35. #if defined( DEBUG )
  36. tested_ = other.tested_;
  37. #endif
  38. }
  39. return *this;
  40. }
  41. operator bool() const {
  42. #if defined( DEBUG )
  43. tested_ = true;
  44. #endif
  45. return static_cast<bool>(_ptr);
  46. }
  47. pointer get() const { return std::addressof(*_ptr); }
  48. pointer operator->() const {
  49. return std::addressof(operator*());
  50. }
  51. reference operator*() const {
  52. #if defined( DEBUG )
  53. if ( !tested_ ) {
  54. throw unchecked_pointer_exception{"did not verify that pointer was non-null"};
  55. }
  56. #endif
  57. return *_ptr;
  58. }
  59. private:
  60. T _ptr;
  61. #if defined( DEBUG )
  62. mutable bool tested_ = false;
  63. #endif
  64. };
  65. POINTER_TEMPLATE_COMPARE( maybe_null )