maybe_null.hpp 2.0 KB

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