memory: fix SP/WP hierarchy templates

This commit is contained in:
Vaxry 2024-05-06 21:36:31 +01:00
parent fa69de8ab6
commit 0c446ec5f4
2 changed files with 19 additions and 15 deletions

View File

@ -122,7 +122,9 @@ template <typename T>
class CSharedPointer { class CSharedPointer {
public: public:
template <typename X> template <typename X>
using validHierarchy = typename std::enable_if<std::is_assignable<T*, X*>::value>; using validHierarchy = typename std::enable_if<std::is_assignable<CSharedPointer<T>&, X>::value, CSharedPointer&>::type;
template <typename X>
using isConstructible = typename std::enable_if<std::is_constructible<T&, X&>::value>::type;
/* creates a new shared pointer managing a resource /* creates a new shared pointer managing a resource
avoid calling. Could duplicate ownership. Prefer makeShared */ avoid calling. Could duplicate ownership. Prefer makeShared */
@ -132,7 +134,7 @@ class CSharedPointer {
} }
/* creates a shared pointer from a reference */ /* creates a shared pointer from a reference */
template <typename U, typename = validHierarchy<U>> template <typename U, typename = isConstructible<U>>
CSharedPointer(const CSharedPointer<U>& ref) noexcept { CSharedPointer(const CSharedPointer<U>& ref) noexcept {
impl_ = ref.impl_; impl_ = ref.impl_;
increment(); increment();
@ -143,7 +145,7 @@ class CSharedPointer {
increment(); increment();
} }
template <typename U, typename = validHierarchy<U>> template <typename U, typename = isConstructible<U>>
CSharedPointer(CSharedPointer<U>&& ref) noexcept { CSharedPointer(CSharedPointer<U>&& ref) noexcept {
std::swap(impl_, ref.impl_); std::swap(impl_, ref.impl_);
} }
@ -178,8 +180,8 @@ class CSharedPointer {
decrement(); decrement();
} }
template <typename U, typename = validHierarchy<U>> template <typename U>
CSharedPointer<T>& operator=(const CSharedPointer<U>& rhs) { validHierarchy<const CSharedPointer<U>&> operator=(const CSharedPointer<U>& rhs) {
if (impl_ == rhs.impl_) if (impl_ == rhs.impl_)
return *this; return *this;
@ -199,8 +201,8 @@ class CSharedPointer {
return *this; return *this;
} }
template <typename U, typename = validHierarchy<U>> template <typename U>
CSharedPointer<T>& operator=(CSharedPointer<U>&& rhs) { validHierarchy<const CSharedPointer<U>&> operator=(CSharedPointer<U>&& rhs) {
std::swap(impl_, rhs.impl_); std::swap(impl_, rhs.impl_);
return *this; return *this;
} }

View File

@ -14,10 +14,12 @@ template <typename T>
class CWeakPointer { class CWeakPointer {
public: public:
template <typename X> template <typename X>
using validHierarchy = typename std::enable_if<std::is_assignable<T*, X*>::value>; using validHierarchy = typename std::enable_if<std::is_assignable<CWeakPointer<T>&, X>::value, CWeakPointer&>::type;
template <typename X>
using isConstructible = typename std::enable_if<std::is_constructible<T&, X&>::value>::type;
/* create a weak ptr from a reference */ /* create a weak ptr from a reference */
template <typename U, typename = validHierarchy<U>> template <typename U, typename = isConstructible<U>>
CWeakPointer(const CSharedPointer<U>& ref) noexcept { CWeakPointer(const CSharedPointer<U>& ref) noexcept {
if (!ref.impl_) if (!ref.impl_)
return; return;
@ -27,7 +29,7 @@ class CWeakPointer {
} }
/* create a weak ptr from another weak ptr */ /* create a weak ptr from another weak ptr */
template <typename U, typename = validHierarchy<U>> template <typename U, typename = isConstructible<U>>
CWeakPointer(const CWeakPointer<U>& ref) noexcept { CWeakPointer(const CWeakPointer<U>& ref) noexcept {
if (!ref.impl_) if (!ref.impl_)
return; return;
@ -44,7 +46,7 @@ class CWeakPointer {
incrementWeak(); incrementWeak();
} }
template <typename U, typename = validHierarchy<U>> template <typename U, typename = isConstructible<U>>
CWeakPointer(CWeakPointer<U>&& ref) noexcept { CWeakPointer(CWeakPointer<U>&& ref) noexcept {
std::swap(impl_, ref.impl_); std::swap(impl_, ref.impl_);
} }
@ -54,8 +56,8 @@ class CWeakPointer {
} }
/* create a weak ptr from another weak ptr with assignment */ /* create a weak ptr from another weak ptr with assignment */
template <typename U, typename = validHierarchy<U>> template <typename U>
CWeakPointer<T>& operator=(const CWeakPointer<U>& rhs) { validHierarchy<const CWeakPointer<U>&> operator=(const CWeakPointer<U>& rhs) {
if (impl_ == rhs.impl_) if (impl_ == rhs.impl_)
return *this; return *this;
@ -76,8 +78,8 @@ class CWeakPointer {
} }
/* create a weak ptr from a shared ptr with assignment */ /* create a weak ptr from a shared ptr with assignment */
template <typename U, typename = validHierarchy<U>> template <typename U>
CWeakPointer<T>& operator=(const CSharedPointer<U>& rhs) { validHierarchy<const CWeakPointer<U>&> operator=(const CSharedPointer<U>& rhs) {
if ((uintptr_t)impl_ == (uintptr_t)rhs.impl_) if ((uintptr_t)impl_ == (uintptr_t)rhs.impl_)
return *this; return *this;