not_null.hpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. //
  2. // not_null.hpp
  3. // pointer
  4. //
  5. // Created by Sam Jaffe on 9/24/15.
  6. //
  7. //
  8. #pragma once
  9. #include <memory>
  10. #include "pointer_fwd.hpp"
  11. #include "ptr_compare.hpp"
  12. #include "maybe_null.hpp"
  13. template <typename P> class not_null<std::weak_ptr<P>>; // A weak_ptr cannot be a not_null
  14. template <typename P>
  15. class not_null {
  16. public:
  17. using element_type = typename std::pointer_traits<P>::element_type;
  18. using pointer = element_type *;
  19. using reference = element_type &;
  20. explicit not_null(std::nullptr_t) = delete;
  21. explicit not_null(int) = delete;
  22. not_null(P const & p) : _ptr(p) { validate(); }
  23. not_null(P && p) : _ptr(std::move(p)) { validate(); }
  24. not_null(not_null const&) noexcept(detail::is_nt_cc<P>::value) = default;
  25. not_null(not_null &&) = delete;
  26. template <typename Y>
  27. explicit operator maybe_null<Y>() const noexcept(detail::is_nt_c<Y, P>::value) {
  28. return _ptr;
  29. }
  30. template <typename Y>
  31. explicit operator not_null<Y>() const noexcept(detail::is_nt_c<Y, P>::value) {
  32. return _ptr;
  33. }
  34. not_null& operator=(not_null const&) noexcept(detail::is_nt_ca<P>::value) = default;
  35. not_null& operator=(not_null &&) = delete;
  36. operator bool() const noexcept { return true; }
  37. pointer get() const noexcept { return detail::get_ptr<P>().get(_ptr); }
  38. reference operator*() const noexcept { return *_ptr; }
  39. pointer operator->() const noexcept { return get(); }
  40. void reset( P const & p ) { operator=(not_null(p)); }
  41. void reset( P && p ) { operator=(not_null(std::forward(p))); }
  42. private:
  43. void validate() {
  44. if (get() == nullptr) {
  45. throw null_pointer_exception{"not_null<P> cannot be null"};
  46. }
  47. }
  48. P _ptr;
  49. };
  50. POINTER_TEMPLATE_COMPARE( not_null )