| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- //
- // copy_ptr.hpp
- // pointers
- //
- // Created by Sam Jaffe on 12/6/16.
- //
- #pragma once
- #include <functional>
- #include <memory>
- #include "pointer_fwd.hpp"
- namespace detail {
- template <typename T>
- T * copy_ptr_from_ptr(T * ptr) {
- return ptr ? new T(*ptr) : nullptr;
- }
- }
- template <typename T, T*(*Copy)(T*) = &detail::copy_ptr_from_ptr<T> >
- class copy_ptr {
- public:
- using element_type = T;
- using pointer = element_type *;
- using reference = element_type &;
-
- copy_ptr() : _ptr(nullptr) {}
- copy_ptr(P const & p) = delete;
- copy_ptr(P && p) : _ptr(std::move(p)) {}
- copy_ptr(copy_ptr const & other) : _ptr(Copy(other._ptr)) {}
- copy_ptr(copy_ptr &&) = default;
-
- template <typename Y>
- explicit operator copy_ptr<Y>() const {
- return Copy(_ptr);
- }
-
- ~copy_ptr() { delete _ptr; }
-
- copy_ptr & operator=(copy_ptr const & other) {
- swap(copy_ptr{other});
- return *this;
- }
- copy_ptr & operator=(copy_ptr &&) = default;
-
- operator bool() const {
- return static_cast<bool>(_ptr);
- }
-
- pointer get() const { return _ptr; }
- pointer operator->() const { return get(); }
- reference operator*() const { return *get(); }
- private:
- P _ptr;
- };
- template <typename T, typename U>
- bool operator==(copy_ptr<T> const&lhs, copy_ptr<U> const&rhs) {
- return lhs.get() == rhs.get();
- }
- template <typename T, typename U>
- bool operator!=(copy_ptr<T> const&lhs, copy_ptr<U> const&rhs) {
- return !(lhs == rhs);
- }
- template <typename T, typename U>
- bool operator< (copy_ptr<T> const&lhs, copy_ptr<U> const&rhs) {
- typedef typename std::common_type<
- typename copy_ptr<T>::pointer,
- typename copy_ptr<U>::pointer>::type V;
- return std::less<V>(lhs.get(), rhs.get());
- }
- template <typename T, typename U>
- bool operator> (copy_ptr<T> const&lhs, copy_ptr<U> const&rhs) {
- return rhs < lhs;
- }
- template <typename T, typename U>
- bool operator<=(copy_ptr<T> const&lhs, copy_ptr<U> const&rhs) {
- return !(rhs < lhs);
- }
- template <typename T, typename U>
- bool operator>=(copy_ptr<T> const&lhs, copy_ptr<U> const&rhs) {
- return !(lhs < rhs);
- }
- template <typename T>
- bool operator==(copy_ptr<T> const&lhs, std::nullptr_t) {
- return !lhs;
- }
- template <typename T>
- bool operator==(std::nullptr_t, copy_ptr<T> const&rhs) {
- return !rhs;
- }
- template <typename T>
- bool operator!=(copy_ptr<T> const&lhs, std::nullptr_t) {
- return static_cast<bool>(lhs);
- }
- template <typename T>
- bool operator!=(std::nullptr_t, copy_ptr<T> const&rhs) {
- return static_cast<bool>(rhs);
- }
- template <typename T>
- bool operator< (copy_ptr<T> const&lhs, std::nullptr_t) {
- typedef typename copy_ptr<T>::pointer V;
- return std::less<V>(lhs.get(), nullptr);
- }
- template <typename T>
- bool operator< (std::nullptr_t, copy_ptr<T> const&rhs) {
- typedef typename copy_ptr<T>::pointer V;
- return std::less<V>(nullptr, rhs.get());
- }
- template <typename T>
- bool operator> (copy_ptr<T> const&lhs, std::nullptr_t) {
- return nullptr < lhs;
- }
- template <typename T>
- bool operator> (std::nullptr_t, copy_ptr<T> const&rhs) {
- return rhs < nullptr;
- }
- template <typename T>
- bool operator<=(copy_ptr<T> const&lhs, std::nullptr_t) {
- return !(nullptr < lhs);
- }
- template <typename T>
- bool operator<=(std::nullptr_t, copy_ptr<T> const&rhs) {
- return !(rhs < nullptr);
- }
- template <typename T>
- bool operator>=(copy_ptr<T> const&lhs, std::nullptr_t) {
- return !(lhs < nullptr);
- }
- template <typename T>
- bool operator>=(std::nullptr_t, copy_ptr<T> const&rhs) {
- return !(nullptr < rhs);
- }
|