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 {
public:
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
avoid calling. Could duplicate ownership. Prefer makeShared */
@ -132,7 +134,7 @@ class CSharedPointer {
}
/* 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 {
impl_ = ref.impl_;
increment();
@ -143,7 +145,7 @@ class CSharedPointer {
increment();
}
template <typename U, typename = validHierarchy<U>>
template <typename U, typename = isConstructible<U>>
CSharedPointer(CSharedPointer<U>&& ref) noexcept {
std::swap(impl_, ref.impl_);
}
@ -178,8 +180,8 @@ class CSharedPointer {
decrement();
}
template <typename U, typename = validHierarchy<U>>
CSharedPointer<T>& operator=(const CSharedPointer<U>& rhs) {
template <typename U>
validHierarchy<const CSharedPointer<U>&> operator=(const CSharedPointer<U>& rhs) {
if (impl_ == rhs.impl_)
return *this;
@ -199,8 +201,8 @@ class CSharedPointer {
return *this;
}
template <typename U, typename = validHierarchy<U>>
CSharedPointer<T>& operator=(CSharedPointer<U>&& rhs) {
template <typename U>
validHierarchy<const CSharedPointer<U>&> operator=(CSharedPointer<U>&& rhs) {
std::swap(impl_, rhs.impl_);
return *this;
}

View file

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