const_propogating_ptr.hpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. //
  2. // const_propogating_ptr.hpp
  3. // pointers
  4. //
  5. // Created by Sam Jaffe on 12/3/16.
  6. //
  7. #pragma once
  8. #include <memory>
  9. #include "pointer_fwd.hpp"
  10. #include "ptr_compare.hpp"
  11. template <typename P>
  12. class const_propogating_ptr {
  13. public:
  14. using element_type = typename std::pointer_traits<P>::element_type;
  15. using pointer = element_type *;
  16. using reference = element_type &;
  17. using const_pointer = element_type const *;
  18. using const_reference = element_type const &;
  19. const_propogating_ptr() : _ptr(nullptr) {}
  20. const_propogating_ptr(P const & p) : _ptr(p) {}
  21. const_propogating_ptr(P && p) : _ptr(std::move(p)) {}
  22. const_propogating_ptr(const_propogating_ptr & other) : _ptr(other._ptr) {}
  23. const_propogating_ptr(const_propogating_ptr &&) = default;
  24. const_propogating_ptr(const_propogating_ptr const &) = delete;
  25. const_propogating_ptr& operator=(const_propogating_ptr & other) { _ptr = other._ptr; return *this; }
  26. const_propogating_ptr& operator=(const_propogating_ptr &&) = default;
  27. const_propogating_ptr& operator=(const_propogating_ptr const &) = delete;
  28. template <typename Y>
  29. explicit operator const_propogating_ptr<Y>() & {
  30. return _ptr;
  31. }
  32. template <typename Y>
  33. explicit operator const_ptr<Y>() const {
  34. return _ptr;
  35. }
  36. template <typename Y>
  37. explicit operator const_propogating_ptr<Y>() const & = delete;
  38. operator bool() const {
  39. return bool(_ptr);
  40. }
  41. reference operator*() { return *_ptr; }
  42. pointer get() { return std::addressof(operator*()); }
  43. pointer operator->() { return get(); }
  44. const_reference operator*() const { return *_ptr; }
  45. const_pointer get() const { return std::addressof(operator*()); }
  46. const_pointer operator->() const { return get(); }
  47. private:
  48. P _ptr;
  49. };
  50. POINTER_TEMPLATE_COMPARE( const_propogating_ptr )