Repository navigation
Expand file tree
/
Copy pathpimpl.h
More file actions
60 lines (48 loc) · 1.39 KB
/
Copy pathpimpl.h
File metadata and controls
60 lines (48 loc) · 1.39 KB
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
53
54
55
56
57
58
59
60
#ifndef UTILS_PIMPL_H
#define UTILS_PIMPL_H
#include <QtCore/QtGlobal>
namespace Utils {
/*!
\brief Use this class as a pointer and you don't need to read documentation.
This class is supposed to be a simplification and replacement of Qt pimpl
(private implementation) mechanism.
Instead of using Qt macros Q_DECLARE_PRIVATE, Q_DECLARE_PUBLIC, Q_D and Q_Q
which use d_ptr and q_ptr variables and declare d_func and q_func functions
you can declare just one variable per class.
In public class that will access private class create variable
<tt>DQPtr\<MyPrivateClass\> d</tt> and in private class that will
access public class create <tt>DQPtr\<MyPublicClass\> q</tt>. Then use
both variables, <b>d</b> and <b>q</b> as a normal pointer.
*/
template <class T>
class DQPtr
{
public:
//! Constructs a class that operates on given class \a p.
DQPtr(T *p) : p(p)
{ Q_ASSERT(p != 0); }
public:
//@{
//! \name Imitate a pointer
T *operator->()
{ return p; }
const T *operator->() const
{ return p; }
operator T *()
{ return p; }
operator const T *() const
{ return p; }
//@}
//! Use this in combination with delete
T *detach()
{
Q_ASSERT(p != 0);
T *const p = this->p;
this->p = 0;
return p;
}
private:
T *p;
};
} //namespace Utils
#endif // UTILS_PIMPL_H