refactor+fix: use QSharedPointer for shared_qobject_ptr
Signed-off-by: flow <flowlnlnln@gmail.com>
This commit is contained in:
parent
6fd3672618
commit
362ecdb583
@ -1,91 +1,48 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <QObject>
|
||||||
|
#include <QSharedPointer>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <QObject>
|
|
||||||
|
|
||||||
namespace details
|
namespace details {
|
||||||
|
[[maybe_unused]] static void do_delete_later(QObject* obj)
|
||||||
{
|
{
|
||||||
struct DeleteQObjectLater
|
if (obj)
|
||||||
{
|
|
||||||
void operator()(QObject *obj) const
|
|
||||||
{
|
|
||||||
obj->deleteLater();
|
obj->deleteLater();
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
struct DeleteQObjectLater {
|
||||||
|
void operator()(QObject* obj) const { do_delete_later(obj); }
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace details
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A unique pointer class with unique pointer semantics intended for derivates of QObject
|
* A unique pointer class with unique pointer semantics intended for derivates of QObject
|
||||||
* Calls deleteLater() instead of destroying the contained object immediately
|
* Calls deleteLater() instead of destroying the contained object immediately
|
||||||
*/
|
*/
|
||||||
template<typename T> using unique_qobject_ptr = std::unique_ptr<T, details::DeleteQObjectLater>;
|
template <typename T>
|
||||||
|
using unique_qobject_ptr = std::unique_ptr<T, details::DeleteQObjectLater>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A shared pointer class with shared pointer semantics intended for derivates of QObject
|
* A shared pointer class with shared pointer semantics intended for derivates of QObject
|
||||||
* Calls deleteLater() instead of destroying the contained object immediately
|
* Calls deleteLater() instead of destroying the contained object immediately
|
||||||
*/
|
*/
|
||||||
template <typename T>
|
template <typename T>
|
||||||
class shared_qobject_ptr
|
class shared_qobject_ptr : public QSharedPointer<T> {
|
||||||
{
|
public:
|
||||||
public:
|
constexpr shared_qobject_ptr() : QSharedPointer<T>() {}
|
||||||
shared_qobject_ptr(){}
|
constexpr shared_qobject_ptr(T* ptr) : QSharedPointer<T>(ptr, details::do_delete_later) {}
|
||||||
shared_qobject_ptr(T * wrap)
|
constexpr shared_qobject_ptr(std::nullptr_t null_ptr) : QSharedPointer<T>(null_ptr, details::do_delete_later) {}
|
||||||
{
|
|
||||||
reset(wrap);
|
|
||||||
}
|
|
||||||
shared_qobject_ptr(const shared_qobject_ptr<T>& other)
|
|
||||||
{
|
|
||||||
m_ptr = other.m_ptr;
|
|
||||||
}
|
|
||||||
template<typename Derived>
|
|
||||||
shared_qobject_ptr(const shared_qobject_ptr<Derived> &other)
|
|
||||||
{
|
|
||||||
m_ptr = other.unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
public:
|
template <typename Derived>
|
||||||
void reset(T * wrap)
|
constexpr shared_qobject_ptr(const shared_qobject_ptr<Derived>& other) : QSharedPointer<T>(other)
|
||||||
{
|
{}
|
||||||
using namespace std::placeholders;
|
|
||||||
m_ptr.reset(wrap, std::bind(&QObject::deleteLater, _1));
|
|
||||||
}
|
|
||||||
void reset(const shared_qobject_ptr<T> &other)
|
|
||||||
{
|
|
||||||
m_ptr = other.m_ptr;
|
|
||||||
}
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
const std::shared_ptr <T> unwrap() const
|
|
||||||
{
|
|
||||||
return m_ptr;
|
|
||||||
}
|
|
||||||
template<typename U>
|
|
||||||
bool operator==(const shared_qobject_ptr<U>& other) const {
|
|
||||||
return m_ptr == other.m_ptr;
|
|
||||||
}
|
|
||||||
template<typename U>
|
|
||||||
bool operator!=(const shared_qobject_ptr<U>& other) const {
|
|
||||||
return m_ptr != other.m_ptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
void reset() { QSharedPointer<T>::reset(); }
|
||||||
std::shared_ptr <T> m_ptr;
|
void reset(const shared_qobject_ptr<T>& other)
|
||||||
|
{
|
||||||
|
shared_qobject_ptr<T> t(other);
|
||||||
|
this->swap(t);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
@ -43,7 +43,7 @@ void MinecraftUpdate::executeTask()
|
|||||||
m_tasks.clear();
|
m_tasks.clear();
|
||||||
// create folders
|
// create folders
|
||||||
{
|
{
|
||||||
m_tasks.append(std::make_shared<FoldersTask>(m_inst));
|
m_tasks.append(new FoldersTask(m_inst));
|
||||||
}
|
}
|
||||||
|
|
||||||
// add metadata update task if necessary
|
// add metadata update task if necessary
|
||||||
@ -53,23 +53,23 @@ void MinecraftUpdate::executeTask()
|
|||||||
auto task = components->getCurrentTask();
|
auto task = components->getCurrentTask();
|
||||||
if(task)
|
if(task)
|
||||||
{
|
{
|
||||||
m_tasks.append(task.unwrap());
|
m_tasks.append(task);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// libraries download
|
// libraries download
|
||||||
{
|
{
|
||||||
m_tasks.append(std::make_shared<LibrariesTask>(m_inst));
|
m_tasks.append(new LibrariesTask(m_inst));
|
||||||
}
|
}
|
||||||
|
|
||||||
// FML libraries download and copy into the instance
|
// FML libraries download and copy into the instance
|
||||||
{
|
{
|
||||||
m_tasks.append(std::make_shared<FMLLibrariesTask>(m_inst));
|
m_tasks.append(new FMLLibrariesTask(m_inst));
|
||||||
}
|
}
|
||||||
|
|
||||||
// assets update
|
// assets update
|
||||||
{
|
{
|
||||||
m_tasks.append(std::make_shared<AssetUpdateTask>(m_inst));
|
m_tasks.append(new AssetUpdateTask(m_inst));
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!m_preFailure.isEmpty())
|
if(!m_preFailure.isEmpty())
|
||||||
|
@ -50,7 +50,7 @@ private:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
MinecraftInstance *m_inst = nullptr;
|
MinecraftInstance *m_inst = nullptr;
|
||||||
QList<std::shared_ptr<Task>> m_tasks;
|
QList<Task::Ptr> m_tasks;
|
||||||
QString m_preFailure;
|
QString m_preFailure;
|
||||||
int m_currentTask = -1;
|
int m_currentTask = -1;
|
||||||
bool m_abort = false;
|
bool m_abort = false;
|
||||||
|
@ -238,7 +238,7 @@ void MinecraftAccount::authFailed(QString reason)
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool MinecraftAccount::isActive() const {
|
bool MinecraftAccount::isActive() const {
|
||||||
return m_currentTask;
|
return !m_currentTask.isNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MinecraftAccount::shouldRefresh() const {
|
bool MinecraftAccount::shouldRefresh() const {
|
||||||
|
@ -115,5 +115,5 @@ MinecraftAccountPtr LoginDialog::newAccount(QWidget *parent, QString msg)
|
|||||||
{
|
{
|
||||||
return dlg.m_account;
|
return dlg.m_account;
|
||||||
}
|
}
|
||||||
return 0;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
@ -169,5 +169,5 @@ MinecraftAccountPtr MSALoginDialog::newAccount(QWidget *parent, QString msg)
|
|||||||
{
|
{
|
||||||
return dlg.m_account;
|
return dlg.m_account;
|
||||||
}
|
}
|
||||||
return 0;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
@ -103,5 +103,5 @@ MinecraftAccountPtr OfflineLoginDialog::newAccount(QWidget *parent, QString msg)
|
|||||||
{
|
{
|
||||||
return dlg.m_account;
|
return dlg.m_account;
|
||||||
}
|
}
|
||||||
return 0;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user