1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtCore module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
42 #ifndef QARRAYDATAPOINTER_H
43 #define QARRAYDATAPOINTER_H
45 #include <QtCore/qarraydataops.h>
52 struct QArrayDataPointer
55 typedef QTypedArrayData<T> Data;
56 typedef QArrayDataOps<T> DataOps;
60 : d(Data::sharedNull())
64 QArrayDataPointer(const QArrayDataPointer &other)
65 : d(other.d->ref.ref()
67 : other.clone(other.d->cloneFlags()))
71 explicit QArrayDataPointer(QTypedArrayData<T> *ptr)
77 QArrayDataPointer(QArrayDataPointerRef<T> ref)
82 QArrayDataPointer &operator=(const QArrayDataPointer &other)
84 QArrayDataPointer tmp(other);
89 #ifdef Q_COMPILER_RVALUE_REFS
90 QArrayDataPointer(QArrayDataPointer &&other)
93 other.d = Data::sharedNull();
96 QArrayDataPointer &operator=(QArrayDataPointer &&other)
103 DataOps &operator*() const
106 return *static_cast<DataOps *>(d);
109 DataOps *operator->() const
112 return static_cast<DataOps *>(d);
117 if (!d->ref.deref()) {
119 (*this)->destroyAll();
126 return d == Data::sharedNull();
134 bool needsDetach() const
136 return (!d->isMutable() || d->ref.isShared());
139 void setSharable(bool sharable)
142 Data *detached = clone(sharable
143 ? d->detachFlags() & ~QArrayData::Unsharable
144 : d->detachFlags() | QArrayData::Unsharable);
145 QArrayDataPointer old(d);
148 d->ref.setSharable(sharable);
152 void swap(QArrayDataPointer &other)
159 QArrayDataPointer tmp(d);
160 d = Data::sharedNull();
166 Data *copy = clone(d->detachFlags());
167 QArrayDataPointer old(d);
176 Data *clone(QArrayData::AllocationOptions options) const Q_REQUIRED_RESULT
178 QArrayDataPointer copy(Data::allocate(d->detachCapacity(d->size),
181 copy->copyAppend(d->begin(), d->end());
183 Data *result = copy.d;
184 copy.d = Data::sharedNull();
192 inline bool operator==(const QArrayDataPointer<T> &lhs, const QArrayDataPointer<T> &rhs)
194 return lhs.data() == rhs.data();
198 inline bool operator!=(const QArrayDataPointer<T> &lhs, const QArrayDataPointer<T> &rhs)
200 return lhs.data() != rhs.data();
204 inline void qSwap(QArrayDataPointer<T> &p1, QArrayDataPointer<T> &p2)
215 QT_PREPEND_NAMESPACE(QArrayDataPointer)<T> &p1,
216 QT_PREPEND_NAMESPACE(QArrayDataPointer)<T> &p2)
224 #endif // include guard