const_propogating_ptr.hpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 "detail/compare.hpp"
  10. #include "detail/get_ptr.hpp"
  11. #include "pointer_fwd.hpp"
  12. template <typename P>
  13. class const_propogating_ptr
  14. : private detail::get_ptr<P>,
  15. public detail::pointer_compare<const_propogating_ptr<P>> {
  16. public:
  17. using element_type = typename std::pointer_traits<P>::element_type;
  18. using pointer = element_type *;
  19. using reference = element_type &;
  20. using const_pointer = element_type const *;
  21. using const_reference = element_type const &;
  22. const_propogating_ptr() noexcept : _ptr() {}
  23. const_propogating_ptr(P const & p) noexcept(
  24. std::is_nothrow_copy_constructible<P>::value)
  25. : _ptr(p) {}
  26. const_propogating_ptr(P && p) noexcept(
  27. std::is_nothrow_move_constructible<P>::value)
  28. : _ptr(std::move(p)) {}
  29. template <typename Y, typename = typename std::enable_if<
  30. std::is_constructible<P, Y>::value>::type>
  31. const_propogating_ptr(Y const & p) : _ptr(p) {}
  32. template <typename Y, typename = typename std::enable_if<
  33. std::is_constructible<P, Y>::value>::type>
  34. const_propogating_ptr(Y && p) : _ptr(std::forward<Y>(p)) {}
  35. const_propogating_ptr(const_propogating_ptr &) noexcept(
  36. std::is_nothrow_copy_constructible<P>::value) = default;
  37. const_propogating_ptr(const_propogating_ptr &&) noexcept(
  38. std::is_nothrow_move_constructible<P>::value) = default;
  39. const_propogating_ptr(const_propogating_ptr const &) = delete;
  40. const_propogating_ptr & operator=(const_propogating_ptr &) noexcept(
  41. std::is_nothrow_copy_assignable<P>::value) = default;
  42. const_propogating_ptr & operator=(const_propogating_ptr &&) noexcept(
  43. std::is_nothrow_move_assignable<P>::value) = default;
  44. const_propogating_ptr & operator=(const_propogating_ptr const &) = delete;
  45. template <typename Y>
  46. explicit operator const_propogating_ptr<Y>() &
  47. noexcept(std::is_nothrow_constructible<P, Y>::value) {
  48. return _ptr;
  49. }
  50. template <typename Y>
  51. explicit operator const_ptr<Y>() const
  52. noexcept(std::is_nothrow_constructible<P, Y>::value) {
  53. return _ptr;
  54. }
  55. template <typename Y>
  56. explicit operator const_propogating_ptr<Y>() const & = delete;
  57. operator bool() const noexcept { return static_cast<bool>(_ptr); }
  58. reference operator*() noexcept(noexcept(*_ptr)) { return *_ptr; }
  59. pointer get() noexcept(noexcept(detail::get_ptr<P>::get(_ptr))) {
  60. return detail::get_ptr<P>::get(_ptr);
  61. }
  62. pointer operator->() noexcept(noexcept(get())) { return get(); }
  63. const_reference operator*() const noexcept(noexcept(*_ptr)) { return *_ptr; }
  64. const_pointer get() const noexcept(noexcept(detail::get_ptr<P>::get(_ptr))) {
  65. return detail::get_ptr<P>::get(_ptr);
  66. }
  67. const_pointer operator->() const noexcept(noexcept(get())) { return get(); }
  68. private:
  69. P _ptr;
  70. };