not_null.hpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. //
  2. // not_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. #include "maybe_null.hpp"
  14. class null_pointer_exception : public std::invalid_argument {
  15. using std::invalid_argument::invalid_argument;
  16. };
  17. template <typename P> class not_null<std::weak_ptr<P>>; // A weak_ptr cannot be a not_null
  18. template <typename T>
  19. class not_null {
  20. public:
  21. using element_type = typename std::pointer_traits<T>::element_type;
  22. using pointer = element_type *;
  23. using reference = element_type &;
  24. explicit not_null(std::nullptr_t) = delete;
  25. explicit not_null(int) = delete;
  26. not_null(T const & p) : _ptr(p) { validate(); }
  27. not_null(T && p) : _ptr(std::move(p)) { validate(); }
  28. not_null(not_null const&) = default;
  29. template <typename Y>
  30. explicit operator maybe_null<Y>() const {
  31. return _ptr;
  32. }
  33. template <typename Y>
  34. explicit operator not_null<Y>() const {
  35. return _ptr;
  36. }
  37. not_null& operator=(not_null const&) = default;
  38. template <typename Y> not_null& operator=(not_null<Y> const&other) {
  39. _ptr = other._ptr;
  40. return *this;
  41. }
  42. explicit operator maybe_null<T>() const;
  43. operator bool() const { return true; }
  44. pointer get() const { return std::addressof(operator*()); }
  45. reference operator*() const { return *_ptr; }
  46. pointer operator->() const { return get(); }
  47. private:
  48. void validate() {
  49. if (get() == nullptr) {
  50. throw null_pointer_exception{"not_null<T> cannot be null"};
  51. }
  52. }
  53. T _ptr;
  54. };
  55. POINTER_TEMPLATE_COMPARE( not_null )