2015-01-12 02:34:31 +05:30
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <memory>
|
2015-10-20 20:48:53 +05:30
|
|
|
#include <QObject>
|
|
|
|
|
|
|
|
namespace details
|
|
|
|
{
|
|
|
|
struct DeleteQObjectLater
|
|
|
|
{
|
|
|
|
void operator()(QObject *obj) const
|
|
|
|
{
|
|
|
|
obj->deleteLater();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* A unique pointer class with unique pointer semantics intended for derivates of QObject
|
|
|
|
* Calls deleteLater() instead of destroying the contained object immediately
|
|
|
|
*/
|
|
|
|
template<typename T> using unique_qobject_ptr = std::unique_ptr<T, details::DeleteQObjectLater>;
|
2015-01-12 02:34:31 +05:30
|
|
|
|
|
|
|
/**
|
2015-10-20 20:48:53 +05:30
|
|
|
* A shared pointer class with shared pointer semantics intended for derivates of QObject
|
2015-01-12 02:34:31 +05:30
|
|
|
* Calls deleteLater() instead of destroying the contained object immediately
|
|
|
|
*/
|
|
|
|
template <typename T>
|
2015-10-20 20:48:53 +05:30
|
|
|
class shared_qobject_ptr
|
2015-01-12 02:34:31 +05:30
|
|
|
{
|
|
|
|
public:
|
2015-10-20 20:48:53 +05:30
|
|
|
shared_qobject_ptr(){}
|
|
|
|
shared_qobject_ptr(T * wrap)
|
2015-01-12 02:34:31 +05:30
|
|
|
{
|
|
|
|
reset(wrap);
|
|
|
|
}
|
2015-10-20 20:48:53 +05:30
|
|
|
shared_qobject_ptr(const shared_qobject_ptr<T>& other)
|
2015-01-12 02:34:31 +05:30
|
|
|
{
|
|
|
|
m_ptr = other.m_ptr;
|
|
|
|
}
|
2015-05-28 23:08:29 +05:30
|
|
|
template<typename Derived>
|
2015-10-20 20:48:53 +05:30
|
|
|
shared_qobject_ptr(const shared_qobject_ptr<Derived> &other)
|
2015-05-28 23:08:29 +05:30
|
|
|
{
|
|
|
|
m_ptr = other.unwrap();
|
|
|
|
}
|
2015-01-12 02:34:31 +05:30
|
|
|
|
|
|
|
public:
|
|
|
|
void reset(T * wrap)
|
|
|
|
{
|
|
|
|
using namespace std::placeholders;
|
|
|
|
m_ptr.reset(wrap, std::bind(&QObject::deleteLater, _1));
|
|
|
|
}
|
|
|
|
void reset()
|
|
|
|
{
|
|
|
|
m_ptr.reset();
|
|
|
|
}
|
|
|
|
T * get() const
|
|
|
|
{
|
|
|
|
return m_ptr.get();
|
|
|
|
}
|
|
|
|
T * operator->() const
|
|
|
|
{
|
|
|
|
return m_ptr.get();
|
|
|
|
}
|
|
|
|
T & operator*() const
|
|
|
|
{
|
|
|
|
return *m_ptr.get();
|
|
|
|
}
|
|
|
|
operator bool() const
|
|
|
|
{
|
|
|
|
return m_ptr.get() != nullptr;
|
|
|
|
}
|
2015-04-05 18:20:58 +05:30
|
|
|
const std::shared_ptr <T> unwrap() const
|
|
|
|
{
|
|
|
|
return m_ptr;
|
|
|
|
}
|
2015-01-12 02:34:31 +05:30
|
|
|
|
|
|
|
private:
|
|
|
|
std::shared_ptr <T> m_ptr;
|
|
|
|
};
|