diff options
author | Petr Mrázek <peterix@gmail.com> | 2016-04-10 04:29:29 +0200 |
---|---|---|
committer | Petr Mrázek <peterix@gmail.com> | 2016-04-30 23:59:23 +0200 |
commit | 47e37635f50c09b4f9a9ee7699e3120bab3e4088 (patch) | |
tree | 061c2f675fb7e244ebe4b54ef206bfbd615c91f8 /libraries/logic/QObjectPtr.h | |
parent | fcd4a482f759cd58ee319a51082d0146b7e426e2 (diff) | |
download | PrismLauncher-47e37635f50c09b4f9a9ee7699e3120bab3e4088.tar.gz PrismLauncher-47e37635f50c09b4f9a9ee7699e3120bab3e4088.tar.bz2 PrismLauncher-47e37635f50c09b4f9a9ee7699e3120bab3e4088.zip |
NOISSUE split GUI stuff from logic library
Diffstat (limited to 'libraries/logic/QObjectPtr.h')
-rw-r--r-- | libraries/logic/QObjectPtr.h | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/libraries/logic/QObjectPtr.h b/libraries/logic/QObjectPtr.h new file mode 100644 index 00000000..b81b3234 --- /dev/null +++ b/libraries/logic/QObjectPtr.h @@ -0,0 +1,78 @@ +#pragma once + +#include <memory> +#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>; + +/** + * A shared pointer class with shared pointer semantics intended for derivates of QObject + * Calls deleteLater() instead of destroying the contained object immediately + */ +template <typename T> +class shared_qobject_ptr +{ +public: + shared_qobject_ptr(){} + shared_qobject_ptr(T * wrap) + { + 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: + 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; + } + const std::shared_ptr <T> unwrap() const + { + return m_ptr; + } + +private: + std::shared_ptr <T> m_ptr; +}; |