1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtCore module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
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()) {
118 (*this)->destroyAll();
125 return d == Data::sharedNull();
133 bool needsDetach() const
135 return (!d->isMutable() || d->ref.isShared());
138 void setSharable(bool sharable)
141 Data *detached = clone(sharable
142 ? d->detachFlags() & ~QArrayData::Unsharable
143 : d->detachFlags() | QArrayData::Unsharable);
144 QArrayDataPointer old(d);
147 d->ref.setSharable(sharable);
151 void swap(QArrayDataPointer &other)
158 QArrayDataPointer tmp(d);
159 d = Data::sharedNull();
165 Data *copy = clone(d->detachFlags());
166 QArrayDataPointer old(d);
175 Data *clone(QArrayData::AllocationOptions options) const Q_REQUIRED_RESULT
177 QArrayDataPointer copy(Data::allocate(d->detachCapacity(d->size),
180 copy->copyAppend(d->begin(), d->end());
182 Data *result = copy.d;
183 copy.d = Data::sharedNull();
191 inline bool operator==(const QArrayDataPointer<T> &lhs, const QArrayDataPointer<T> &rhs)
193 return lhs.data() == rhs.data();
197 inline bool operator!=(const QArrayDataPointer<T> &lhs, const QArrayDataPointer<T> &rhs)
199 return lhs.data() != rhs.data();
203 inline void qSwap(QArrayDataPointer<T> &p1, QArrayDataPointer<T> &p2)
214 QT_PREPEND_NAMESPACE(QArrayDataPointer)<T> &p1,
215 QT_PREPEND_NAMESPACE(QArrayDataPointer)<T> &p2)
223 #endif // include guard