// // const_propogating_ptr.hpp // pointers // // Created by Sam Jaffe on 12/3/16. // #pragma once #include #include "pointer_fwd.hpp" template class const_propogating_ptr { public: using element_type = typename std::pointer_traits::element_type; using pointer = element_type *; using reference = element_type &; using const_pointer = element_type const *; using const_reference = element_type const &; const_propogating_ptr() : _ptr(nullptr) {} const_propogating_ptr(T const & p) : _ptr(p) {} const_propogating_ptr(T && p) : _ptr(std::move(p)) {} const_propogating_ptr(const_propogating_ptr & other) : _ptr(other._ptr) {} const_propogating_ptr(const_propogating_ptr &&) = default; const_propogating_ptr(const_propogating_ptr const &) = delete; const_propogating_ptr& operator=(const_propogating_ptr & other) { _ptr = other._ptr; return *this; } const_propogating_ptr& operator=(const_propogating_ptr &&) = default; const_propogating_ptr& operator=(const_propogating_ptr const &) = delete; template explicit operator const_propogating_ptr() & { return _ptr; } template explicit operator const_ptr() const { return _ptr; } template explicit operator const_propogating_ptr() const & = delete; operator bool() const { return bool(_ptr); } reference operator*() { return *_ptr; } pointer get() { return std::addressof(operator*()); } pointer operator->() { return get(); } const_reference operator*() const { return *_ptr; } const_pointer get() const { return std::addressof(operator*()); } const_pointer operator->() const { return get(); } private: T _ptr; }; template bool operator==(const_propogating_ptr const&lhs, const_propogating_ptr const&rhs) { return lhs.get() == rhs.get(); } template bool operator!=(const_propogating_ptr const&lhs, const_propogating_ptr const&rhs) { return !(lhs == rhs); } template bool operator< (const_propogating_ptr const&lhs, const_propogating_ptr const&rhs) { typedef typename std::common_type< typename const_propogating_ptr::pointer, typename const_propogating_ptr::pointer>::type V; return std::less(lhs.get(), rhs.get()); } template bool operator> (const_propogating_ptr const&lhs, const_propogating_ptr const&rhs) { return rhs < lhs; } template bool operator<=(const_propogating_ptr const&lhs, const_propogating_ptr const&rhs) { return !(rhs < lhs); } template bool operator>=(const_propogating_ptr const&lhs, const_propogating_ptr const&rhs) { return !(lhs < rhs); } template bool operator==(const_propogating_ptr const&lhs, std::nullptr_t) { return !lhs; } template bool operator==(std::nullptr_t, const_propogating_ptr const&rhs) { return !rhs; } template bool operator!=(const_propogating_ptr const&lhs, std::nullptr_t) { return static_cast(lhs); } template bool operator!=(std::nullptr_t, const_propogating_ptr const&rhs) { return static_cast(rhs); } template bool operator< (const_propogating_ptr const&lhs, std::nullptr_t) { typedef typename const_propogating_ptr::pointer V; return std::less(lhs.get(), nullptr); } template bool operator< (std::nullptr_t, const_propogating_ptr const&rhs) { typedef typename const_propogating_ptr::pointer V; return std::less(nullptr, rhs.get()); } template bool operator> (const_propogating_ptr const&lhs, std::nullptr_t) { return nullptr < lhs; } template bool operator> (std::nullptr_t, const_propogating_ptr const&rhs) { return rhs < nullptr; } template bool operator<=(const_propogating_ptr const&lhs, std::nullptr_t) { return !(nullptr < lhs); } template bool operator<=(std::nullptr_t, const_propogating_ptr const&rhs) { return !(rhs < nullptr); } template bool operator>=(const_propogating_ptr const&lhs, std::nullptr_t) { return !(lhs < nullptr); } template bool operator>=(std::nullptr_t, const_propogating_ptr const&rhs) { return !(nullptr < rhs); }