not_null.hpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 "detail/compare.hpp"
  11. #include "exception.hpp"
  12. #include "maybe_null.hpp"
  13. #include "pointer_fwd.hpp"
  14. namespace pointers {
  15. template <typename P>
  16. class not_null<std::weak_ptr<P>>; // A weak_ptr cannot be a not_null
  17. template <typename P>
  18. class not_null : public detail::pointer_compare<not_null<P>> {
  19. public:
  20. using element_type = typename std::pointer_traits<P>::element_type;
  21. using pointer = element_type *;
  22. using reference = element_type &;
  23. explicit not_null(std::nullptr_t) = delete;
  24. explicit not_null(int) = delete;
  25. not_null(P const & p) : _ptr(p) { validate(); }
  26. not_null(P && p) : _ptr(std::move(p)) { validate(); }
  27. template <typename Y, typename = typename std::enable_if<
  28. std::is_constructible<P, Y>::value>::type>
  29. not_null(Y const & p) : _ptr(p) {
  30. validate();
  31. }
  32. template <typename Y, typename = typename std::enable_if<
  33. std::is_constructible<P, Y>::value>::type>
  34. not_null(Y && p) : _ptr(std::forward<Y>(p)) {
  35. validate();
  36. }
  37. not_null(not_null const &) noexcept(
  38. std::is_nothrow_copy_constructible<P>::value) = default;
  39. not_null(not_null &&) = delete;
  40. template <typename Y>
  41. explicit operator maybe_null<Y>() const
  42. noexcept(std::is_nothrow_constructible<Y, P>::value) {
  43. return _ptr;
  44. }
  45. template <typename Y>
  46. explicit operator not_null<Y>() const
  47. noexcept(std::is_nothrow_constructible<Y, P>::value) {
  48. return _ptr;
  49. }
  50. not_null & operator=(not_null const &) noexcept(
  51. std::is_nothrow_copy_assignable<P>::value) = default;
  52. not_null & operator=(not_null &&) = delete;
  53. operator bool() const noexcept { return true; }
  54. pointer get() const noexcept { return detail::get_ptr<P>().get(_ptr); }
  55. reference operator*() const noexcept { return *_ptr; }
  56. pointer operator->() const noexcept { return get(); }
  57. void reset(P const & p) { operator=(not_null(p)); }
  58. void reset(P && p) { operator=(not_null(std::forward(p))); }
  59. private:
  60. void validate() {
  61. if (get() == nullptr) {
  62. throw null_pointer_exception{"not_null<P> cannot be null"};
  63. }
  64. }
  65. P _ptr;
  66. };
  67. }