not_null.hpp 2.0 KB

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