summaryrefslogtreecommitdiffstats
path: root/logic/QObjectPtr.h
blob: 74c6e82acfdf170ff0446823e68e5da0221d93d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#pragma once

#include <memory>

/**
 * A pointer class with the usual shared pointer semantics intended for derivates of QObject
 * Calls deleteLater() instead of destroying the contained object immediately
 */
template <typename T>
class QObjectPtr
{
public:
	QObjectPtr(){}
	QObjectPtr(T * wrap)
	{
		reset(wrap);
	}
	QObjectPtr(const QObjectPtr<T>& other)
	{
		m_ptr = other.m_ptr;
	}

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;
	}

private:
	std::shared_ptr <T> m_ptr;
};