|
|
@@ -7,19 +7,27 @@
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
-#include <functional>
|
|
|
#include <memory>
|
|
|
|
|
|
#include "pointer_fwd.hpp"
|
|
|
+#include "ptr_compare.hpp"
|
|
|
|
|
|
namespace detail {
|
|
|
template <typename T>
|
|
|
- T * copy_ptr_from_ptr(T * ptr) {
|
|
|
+ T * copy_ptr_from_ptr(T const * ptr) {
|
|
|
return ptr ? new T(*ptr) : nullptr;
|
|
|
}
|
|
|
+
|
|
|
+ template <typename T>
|
|
|
+ using clone_func = T* (T::*)() const;
|
|
|
+
|
|
|
+ template <typename T, clone_func<T> Clone>
|
|
|
+ T * clone_ptr_from_ptr(T const * ptr) {
|
|
|
+ return ptr ? (ptr->*Clone)() : nullptr;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-template <typename T, T*(*Copy)(T*) = &detail::copy_ptr_from_ptr<T> >
|
|
|
+template <typename T, T*(*Copy)(T const *) = &detail::copy_ptr_from_ptr<T> >
|
|
|
class copy_ptr {
|
|
|
public:
|
|
|
using element_type = T;
|
|
|
@@ -27,8 +35,8 @@ public:
|
|
|
using reference = element_type &;
|
|
|
|
|
|
copy_ptr() : _ptr(nullptr) {}
|
|
|
- copy_ptr(P const & p) = delete;
|
|
|
- copy_ptr(P && p) : _ptr(std::move(p)) {}
|
|
|
+ copy_ptr(T * const & p) = delete;
|
|
|
+ copy_ptr(T * && p) : _ptr(std::move(p)) {}
|
|
|
copy_ptr(copy_ptr const & other) : _ptr(Copy(other._ptr)) {}
|
|
|
copy_ptr(copy_ptr &&) = default;
|
|
|
|
|
|
@@ -53,100 +61,12 @@ public:
|
|
|
pointer operator->() const { return get(); }
|
|
|
reference operator*() const { return *get(); }
|
|
|
private:
|
|
|
- P _ptr;
|
|
|
+ T * _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, detail::clone_func<T> Clone>
|
|
|
+using clone_ptr = copy_ptr<T, &detail::clone_ptr_from_ptr<T, Clone>>;
|
|
|
|
|
|
-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);
|
|
|
-}
|
|
|
+POINTER_TEMPLATE_COMPARE_FULL( copy_ptr,
|
|
|
+ (typename T1, T1*(*C1)(T1*)), (T1, C1),
|
|
|
+ (typename T2, T2*(*C2)(T2*)), (T2, C2))
|