const_propogating_ptr.hpp 2.3 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 : private detail::get_ptr<P> {
  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() noexcept : _ptr(nullptr) {}
  20. const_propogating_ptr(P const & p) noexcept(detail::is_nt_cc<P>::value) : _ptr(p) {}
  21. const_propogating_ptr(P && p) noexcept(detail::is_nt_mc<P>::value) : _ptr(std::move(p)) {}
  22. const_propogating_ptr(const_propogating_ptr &) noexcept(detail::is_nt_cc<P>::value) = default;
  23. const_propogating_ptr(const_propogating_ptr &&) noexcept(detail::is_nt_mc<P>::value) = default;
  24. const_propogating_ptr(const_propogating_ptr const &) = delete;
  25. const_propogating_ptr& operator=(const_propogating_ptr &) noexcept(detail::is_nt_ca<P>::value) = default;
  26. const_propogating_ptr& operator=(const_propogating_ptr &&) noexcept(detail::is_nt_ma<P>::value) = default;
  27. const_propogating_ptr& operator=(const_propogating_ptr const &) = delete;
  28. template <typename Y>
  29. explicit operator const_propogating_ptr<Y>() & noexcept(detail::is_nt_c<P, Y>::value) {
  30. return _ptr;
  31. }
  32. template <typename Y>
  33. explicit operator const_ptr<Y>() const noexcept(detail::is_nt_c<P, Y>::value) {
  34. return _ptr;
  35. }
  36. template <typename Y>
  37. explicit operator const_propogating_ptr<Y>() const & = delete;
  38. operator bool() const noexcept {
  39. return static_cast<bool>(_ptr);
  40. }
  41. reference operator*() noexcept(noexcept(*_ptr)) { return *_ptr; }
  42. pointer get() noexcept(noexcept(detail::get_ptr<P>::get(_ptr))) { return detail::get_ptr<P>::get(_ptr); }
  43. pointer operator->() noexcept(noexcept(get())) { return get(); }
  44. const_reference operator*() const noexcept(noexcept(*_ptr)) { return *_ptr; }
  45. const_pointer get() const noexcept(noexcept(detail::get_ptr<P>::get(_ptr))) { return detail::get_ptr<P>::get(_ptr); }
  46. const_pointer operator->() const noexcept(noexcept(get())) { return get(); }
  47. private:
  48. P _ptr;
  49. };
  50. POINTER_TEMPLATE_COMPARE( const_propogating_ptr )