aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRenato Araujo Oliveira Filho <renato.araujo@kdab.com>2020-10-06 09:42:31 -0300
committerRenato Araujo Oliveira Filho <renato.araujo@kdab.com>2020-10-20 16:04:53 -0300
commit47deb5b96fa6db5999cff4fd90bb964fec669a4e (patch)
treecac02ad9069ff6d14e4cad10f19a4c6403150ebc
parenta14ea7738f493280f5c7b6e4a8834a7d1daacd1e (diff)
Extend SharedPtr API with std::shared_ptr
Make sure that we have copy constructor for SharedPtr with derived types. This will be necessary for test SharedPtr implicit conversions. Task-number: PYSIDE-1397 Change-Id: Ic970c84bef025403a0fbef79dce97433254d2f56 Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
-rw-r--r--sources/shiboken2/tests/libsmart/smart_sharedptr.h74
-rw-r--r--sources/shiboken2/tests/smartbinding/typesystem_smart.xml1
2 files changed, 26 insertions, 49 deletions
diff --git a/sources/shiboken2/tests/libsmart/smart_sharedptr.h b/sources/shiboken2/tests/libsmart/smart_sharedptr.h
index 84184e1f8..65a489c68 100644
--- a/sources/shiboken2/tests/libsmart/smart_sharedptr.h
+++ b/sources/shiboken2/tests/libsmart/smart_sharedptr.h
@@ -29,19 +29,9 @@
#ifndef SMART_SHARED_PTR_H
#define SMART_SHARED_PTR_H
-#include "libsmartmacros.h"
+#include <memory>
-template <class T>
-class RefData {
-public:
- RefData(T *ptr) : m_refCount(1), m_heldPtr(ptr) {}
- ~RefData() { delete m_heldPtr; }
- int inc() { return ++m_refCount; }
- int dec() { return --m_refCount; }
- int useCount() { return m_refCount; }
- int m_refCount;
- T *m_heldPtr;
-};
+#include "libsmartmacros.h"
struct SharedPtrBase
{
@@ -57,84 +47,70 @@ class SharedPtr : public SharedPtrBase {
public:
SharedPtr() { logDefaultConstructor(this); }
- SharedPtr(T *v)
+ SharedPtr(T *v) : mPtr(v)
{
logConstructor(this, v);
- if (v)
- m_refData = new RefData<T>(v);
}
- SharedPtr(const SharedPtr<T> &other) : m_refData(other.m_refData)
+ SharedPtr(const SharedPtr<T> &other) : mPtr(other.mPtr)
{
- logCopyConstructor(this, other.m_refData);
- if (m_refData)
- m_refData->inc();
+ logCopyConstructor(this, data());
}
- SharedPtr<T> &operator=(const SharedPtr<T>& other)
+ template<class X>
+ SharedPtr(const SharedPtr<X> &other) : mPtr(other.mPtr)
{
- if (this != &other) {
- logAssignment(this, other.m_refData);
- if (m_refData && m_refData->dec() == 0)
- delete m_refData;
- m_refData = other.m_refData;
- if (m_refData)
- m_refData->inc();
- }
+ logCopyConstructor(this, data());
+ }
+
+ SharedPtr& operator=(const SharedPtr& other)
+ {
+ mPtr = other.mPtr;
return *this;
}
T *data() const
{
- return m_refData ? m_refData->m_heldPtr : nullptr;
+ return mPtr.get();
}
int useCount() const
{
- return m_refData ? m_refData->useCount() : 0;
+ return mPtr.use_count();
}
void dummyMethod1()
{
+ }
+ bool isNull() const
+ {
+ return mPtr.get() == nullptr;
}
T& operator*() const
{
// Crashes if smart pointer is empty (just like std::shared_ptr).
- return *(m_refData->m_heldPtr);
+ return *mPtr;
}
T *operator->() const
{
- return m_refData ? m_refData->m_heldPtr : nullptr;
+ return mPtr.get();
}
bool operator!() const
{
- return !m_refData || !m_refData->m_heldPtr;
- }
-
- bool isNull() const
- {
- return !m_refData || !m_refData->m_heldPtr;
- }
-
- operator bool() const
- {
- return m_refData && m_refData->m_heldPtr;
+ return !mPtr;
}
~SharedPtr()
{
- if (m_refData)
- logDestructor(this, m_refData->useCount() - 1);
- if (m_refData && m_refData->dec() == 0)
- delete m_refData;
+ if (mPtr.use_count() >= 1)
+ logDestructor(this, mPtr.use_count() - 1);
}
-private:
- RefData<T> *m_refData = nullptr;
+ std::shared_ptr<T> mPtr;
};
#endif // SMART_SHARED_PTR_H
diff --git a/sources/shiboken2/tests/smartbinding/typesystem_smart.xml b/sources/shiboken2/tests/smartbinding/typesystem_smart.xml
index 8fb3082a0..46cb08267 100644
--- a/sources/shiboken2/tests/smartbinding/typesystem_smart.xml
+++ b/sources/shiboken2/tests/smartbinding/typesystem_smart.xml
@@ -34,6 +34,7 @@
</conversion-rule>
</container-type>
+ <custom-type name="std::shared_ptr" />
<!-- Used in tests to check what C++ objects are allocated. -->
<object-type name="Registry" />