1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the core module of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
29
#include <qshareddata.h>
31
/*! \class QSharedData
32
\brief The QSharedData class is a base class for shared data objects.
37
QSharedData is designed to be used together with
38
QSharedDataPointer to implement custom \l{implicitly shared}
39
classes. It provides thread-safe reference counting.
41
See the QSharedDataPointer documentation for details.
44
/*! \fn QSharedData::QSharedData()
46
Constructs a QSharedData object with a reference count of 0.
49
/*! \fn QSharedData::QSharedData(const QSharedData &other)
51
Constructs a QSharedData object with a reference count of 0. (\a
55
/*! \class QSharedDataPointer
56
\brief The QSharedDataPointer class provides a pointer to a shared data object.
62
QSharedDataPointer\<T\> makes it easier to write your own
63
implicitly shared classes. It handles reference counting behind
64
the scenes in a thread-safe manner, ensuring that classes that use
65
it can be \l{reentrant}.
67
Implicit sharing is used throughout Qt to combine the memory and
68
speed efficiency of pointers with the ease of use of value types.
69
See the \l{Shared Classes} page for more information.
71
Let's suppose that you want to make an \c Employee class
72
implicitly shared. The procedure is:
75
\i Define the \c Employee class with a single data member variable of
76
type QSharedDataPointer<\c{EmployeeData}>.
77
\i Define an \c EmployeeData class that derives from \l QSharedData
78
and that contains all the variables that you would normally
82
To show how this works in practice, we will review the entire
83
source code for an implicitly shared \c Employee class. Here's the
84
header file that defines the \c Employee class:
86
\include snippets/sharedemployee/employee.h
88
All accesses to the data in the setter and getter functions are
89
made through the QSharedDataPointer object \c d. For non-const
90
functions, operator->() automatically calls detach(), ensuring
91
that modifications to one \c Employee object don't affect other
94
In this example, the \c EmployeeData type is a simple class with a
95
default constructor and a copy constructor provided by C++. If
96
member-per-member copy isn't sufficient for your own data type,
97
you must implement your own copy constructor.
99
Let's now see how to implement the \c Employee constructors:
101
\quotefile snippets/sharedemployee/employee.cpp
106
In the default constructor, we create an object of type
107
\c EmployeeData and assign it to the \c d pointer using operator=().
109
\skipto ::Employee(int
112
In the constructor that takes an ID and an employee's name, we
113
also create an object of type \c EmployeeData and assign it to the
116
In this example, we don't need to provide a copy constructor, an
117
assignment operator, or a destructor for \c Employee. The default
118
implementations provided by C++, which invoke QSharedDataPointer's
119
copy constructor, assignment operator, or destructor, are
120
sufficient. And this is true in general, i.e. for any QSharedData
121
subclass which only stores values (or implicitly shared classes),
122
such as int, double, QString, QStringList, QList\<QWidget*\>, and
125
Behind the scenes, QSharedDataPointer automatically increments or
126
decrements the reference count of the shared data object pointed
127
to by \c d, and deletes shared objects when the reference count
133
/*! \fn T &QSharedDataPointer::operator*()
135
Provides access to the shared object's members.
137
This function does a detach().
140
/*! \fn const T &QSharedDataPointer::operator*() const
144
This function does not call detach().
147
/*! \fn T *QSharedDataPointer::operator->()
149
Provides access to the shared object's members.
151
This function does a detach().
154
/*! \fn const T *QSharedDataPointer::operator->() const
158
This function does not call detach().
161
/*! \fn QSharedDataPointer::operator T *()
163
Returns a pointer to the shared object.
165
This function does a detach().
167
\sa data(), constData()
170
/*! \fn QSharedDataPointer::operator const T *() const
172
Returns a pointer to the shared object.
174
This function does not call detach().
177
/*! \fn T * QSharedDataPointer::data()
179
Returns a pointer to the shared object.
181
This function does a detach().
186
/*! \fn const T * QSharedDataPointer::data() const
190
This function does not call detach().
193
/*! \fn const T * QSharedDataPointer::constData() const
195
Returns a const pointer to the shared object.
197
This function does not call detach().
202
/*! \fn bool QSharedDataPointer::operator==(const QSharedDataPointer<T> &other) const
204
Returns a true if the pointer to the shared object in \a other is equal to
205
to the pointer to the shared data in this else returns false.
207
This function does not call detach().
210
/*! \fn bool QSharedDataPointer::operator!=(const QSharedDataPointer<T> &other) const
212
Returns a true if the pointer to the shared object in \a other is not equal to
213
to the pointer to the shared data in this else returns false.
215
This function does not call detach().
218
/*! \fn QSharedDataPointer::QSharedDataPointer()
220
Constructs a QSharedDataPointer initialized with a null pointer.
223
/*! \fn QSharedDataPointer::~QSharedDataPointer()
225
Destroys the QSharedDataPointer.
227
This function automatically decrements the reference count of the
228
shared object and deletes the object if the reference count
232
/*! \fn QSharedDataPointer::QSharedDataPointer(T *sharedData)
234
Constructs a QSharedDataPointer that points to \a sharedData.
236
This function automatically increments \a{sharedData}'s reference
240
/*! \fn QSharedDataPointer::QSharedDataPointer(const QSharedDataPointer &other)
242
Constructs a copy of \a other.
244
This function automatically increments the reference count of the
245
shared data object pointed to by \a{other}.
248
/*! \fn QSharedDataPointer &QSharedDataPointer::operator=(const QSharedDataPointer &other)
250
Assigns \a other to this pointer.
252
This function automatically increments the reference count of the
253
shared data object pointed to by \a{other}, and decrements the
254
reference count of the object previously pointed to by this
255
QSharedDataPointer. If the reference count reaches 0, the shared
256
data object is deleted.
259
/*! \fn QSharedDataPointer &QSharedDataPointer::operator=(T *sharedData)
263
Sets this QSharedDataPointer to point to \a sharedData.
265
This function automatically increments \a{sharedData}'s reference
266
count, and decrements the reference count of the object
267
previously pointed to by this QSharedDataPointer. If the
268
reference count reaches 0, the shared data object is deleted.
271
/*! \fn bool QSharedDataPointer::operator!() const
273
Returns true if this pointer is null; otherwise returns false.
276
/*! \fn void QSharedDataPointer::detach()
278
If the shared data's reference count is greater than 1, creates a
279
deep copy of the shared data.
281
This function is automatically called by QSharedDataPointer when
282
necessary. You should never need to call it yourself.