const_ptr.hpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. //
  2. // const_ptr.hpp
  3. // pointers
  4. //
  5. // Created by Sam Jaffe on 1/5/17.
  6. //
  7. #pragma once
  8. #include <memory>
  9. #include "pointer_fwd.hpp"
  10. #include "ptr_compare.hpp"
  11. template <typename P>
  12. class const_ptr : private detail::get_ptr<P> {
  13. public:
  14. using element_type = typename std::pointer_traits<P>::element_type;
  15. using pointer = element_type const *;
  16. using reference = element_type const &;
  17. const_ptr() noexcept : _ptr(nullptr) {}
  18. const_ptr(P const & p) noexcept(detail::is_nt_cc<P>::value) : _ptr(p) {}
  19. const_ptr(P && p) noexcept(detail::is_nt_mc<P>::value) : _ptr(std::move(p)) {}
  20. template <typename Y>
  21. explicit operator const_ptr<Y>() const noexcept(detail::is_nt_c<P, Y>::value) {
  22. return _ptr;
  23. }
  24. operator bool() const noexcept {
  25. return static_cast<bool>(_ptr);
  26. }
  27. reference operator*() const noexcept(noexcept(*_ptr)) { return *_ptr; }
  28. pointer get() const noexcept(noexcept(detail::get_ptr<P>::get(_ptr))) { return detail::get_ptr<P>::get(_ptr); }
  29. pointer operator->() const noexcept(noexcept(get())) { return get(); }
  30. private:
  31. P _ptr;
  32. };
  33. POINTER_TEMPLATE_COMPARE( const_ptr )