1
/****************************************************************************
2
**
3
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4
** All rights reserved.
5
** Contact: Nokia Corporation (qt-info@nokia.com)
6
**
7
** This file is part of the QtCore module of the Qt Toolkit.
8
**
9
** $QT_BEGIN_LICENSE:LGPL$
10
** No Commercial Usage
11
** This file contains pre-release code and may not be distributed.
12
** You may use this file in accordance with the terms and conditions
13
** contained in the Technology Preview License Agreement accompanying
14
** this package.
15
**
16
** GNU Lesser General Public License Usage
17
** Alternatively, this file may be used under the terms of the GNU Lesser
18
** General Public License version 2.1 as published by the Free Software
19
** Foundation and appearing in the file LICENSE.LGPL included in the
20
** packaging of this file.  Please review the following information to
21
** ensure the GNU Lesser General Public License version 2.1 requirements
22
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23
**
24
** In addition, as a special exception, Nokia gives you certain additional
25
** rights.  These rights are described in the Nokia Qt LGPL Exception
26
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27
**
28
** If you have questions regarding the use of this file, please contact
29
** Nokia at qt-info@nokia.com.
30
**
31
**
32
**
33
**
34
**
35
**
36
**
37
**
38
** $QT_END_LICENSE$
39
**
40
****************************************************************************/
41
42
#ifndef QGUARD_P_H
43
#define QGUARD_P_H
44
45
//
46
//  W A R N I N G
47
//  -------------
48
//
49
// This file is not part of the Qt API.  It exists for the convenience
50
// of qapplication_*.cpp, qwidget*.cpp and qfiledialog.cpp.  This header
51
// file may change from version to version without notice, or even be removed.
52
//
53
// We mean it.
54
//
55
56
#include "QtCore/qglobal.h"
57
58
QT_BEGIN_NAMESPACE
59
60
class QObject;
61
template<class T>
62
class QGuard
63
{
64
    QObject *o;
65
    QGuard<QObject> *next;
66
    QGuard<QObject> **prev;
67
    friend void q_guard_addGuard(QGuard<QObject> *);
68
    friend void q_guard_removeGuard(QGuard<QObject> *);
69
    friend class QObjectPrivate;
70
public:
71
    inline QGuard();
72
    inline QGuard(T *);
73
    inline QGuard(const QGuard<T> &);
74
    inline virtual ~QGuard();
75
76
    inline QGuard<T> &operator=(const QGuard<T> &o);
77
    inline QGuard<T> &operator=(T *);
78
    
79
    inline bool isNull() const
80
        { return !o; }
81
82
    inline T* operator->() const
83
        { return static_cast<T*>(const_cast<QObject*>(o)); }
84
    inline T& operator*() const
85
        { return *static_cast<T*>(const_cast<QObject*>(o)); }
86
    inline operator T*() const
87
        { return static_cast<T*>(const_cast<QObject*>(o)); }
88
    inline T* data() const
89
        { return static_cast<T*>(const_cast<QObject*>(o)); }
90
91
protected:
92
    virtual void objectDestroyed(T *) {}
93
};
94
95
QT_END_NAMESPACE
96
97
#include "private/qobject_p.h"
98
99
QT_BEGIN_NAMESPACE
100
101
inline void q_guard_addGuard(QGuard<QObject> *);
102
inline void q_guard_removeGuard(QGuard<QObject> *);
103
104
template<class T>
105
QGuard<T>::QGuard()
106
: o(0), next(0), prev(0)
107
{
108
}
109
110
template<class T>
111
QGuard<T>::QGuard(T *g)
112
: o(g), next(0), prev(0)
113
{
114
    if (o) q_guard_addGuard(reinterpret_cast<QGuard<QObject> *>(this));
115
}
116
117
template<class T>
118
QGuard<T>::QGuard(const QGuard<T> &g)
119
: o(g.o), next(0), prev(0)
120
{
121
    if (o) q_guard_addGuard(reinterpret_cast<QGuard<QObject> *>(this));
122
}
123
124
template<class T>
125
QGuard<T>::~QGuard()
126
{
127
    if (prev) q_guard_removeGuard(reinterpret_cast<QGuard<QObject> *>(this));
128
    o = 0;
129
}
130
131
template<class T>
132
QGuard<T> &QGuard<T>::operator=(const QGuard<T> &g)
133
{
134
    if (g.o != o) {
135
        if (prev) 
136
            q_guard_removeGuard(reinterpret_cast<QGuard<QObject> *>(this));
137
        o = g.o;
138
        if (o) q_guard_addGuard(reinterpret_cast<QGuard<QObject> *>(this));
139
    }
140
    return *this;
141
}
142
143
template<class T>
144
inline QGuard<T> &QGuard<T>::operator=(T *g)
145
{
146
    if (g != o) {
147
        if (prev) 
148
            q_guard_removeGuard(reinterpret_cast<QGuard<QObject> *>(this));
149
        o = g;
150
        if (o) q_guard_addGuard(reinterpret_cast<QGuard<QObject> *>(this));
151
    }
152
    return *this;
153
}
154
155
QT_END_NAMESPACE
156
157
#endif // QGUARD_P_H