Update spec to build Qt 5.0
[profile/ivi/qtbase.git] / src / corelib / tools / qarraydatapointer.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
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.
16 **
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.
24 **
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.
28 **
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.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QARRAYDATAPOINTER_H
43 #define QARRAYDATAPOINTER_H
44
45 #include <QtCore/qarraydataops.h>
46
47 QT_BEGIN_HEADER
48
49 QT_BEGIN_NAMESPACE
50
51 template <class T>
52 struct QArrayDataPointer
53 {
54 private:
55     typedef QTypedArrayData<T> Data;
56     typedef QArrayDataOps<T> DataOps;
57
58 public:
59     QArrayDataPointer()
60         : d(Data::sharedNull())
61     {
62     }
63
64     QArrayDataPointer(const QArrayDataPointer &other)
65         : d(other.d->ref.ref()
66             ? other.d
67             : other.clone(other.d->cloneFlags()))
68     {
69     }
70
71     explicit QArrayDataPointer(QTypedArrayData<T> *ptr)
72         : d(ptr)
73     {
74         Q_CHECK_PTR(ptr);
75     }
76
77     QArrayDataPointer(QArrayDataPointerRef<T> ref)
78         : d(ref.ptr)
79     {
80     }
81
82     QArrayDataPointer &operator=(const QArrayDataPointer &other)
83     {
84         QArrayDataPointer tmp(other);
85         this->swap(tmp);
86         return *this;
87     }
88
89 #ifdef Q_COMPILER_RVALUE_REFS
90     QArrayDataPointer(QArrayDataPointer &&other)
91         : d(other.d)
92     {
93         other.d = Data::sharedNull();
94     }
95
96     QArrayDataPointer &operator=(QArrayDataPointer &&other)
97     {
98         this->swap(other);
99         return *this;
100     }
101 #endif
102
103     DataOps &operator*() const
104     {
105         Q_ASSERT(d);
106         return *static_cast<DataOps *>(d);
107     }
108
109     DataOps *operator->() const
110     {
111         Q_ASSERT(d);
112         return static_cast<DataOps *>(d);
113     }
114
115     ~QArrayDataPointer()
116     {
117         if (!d->ref.deref()) {
118             if (d->isMutable())
119                 (*this)->destroyAll();
120             Data::deallocate(d);
121         }
122     }
123
124     bool isNull() const
125     {
126         return d == Data::sharedNull();
127     }
128
129     Data *data() const
130     {
131         return d;
132     }
133
134     bool needsDetach() const
135     {
136         return (!d->isMutable() || d->ref.isShared());
137     }
138
139     void setSharable(bool sharable)
140     {
141         if (needsDetach()) {
142             Data *detached = clone(sharable
143                     ? d->detachFlags() & ~QArrayData::Unsharable
144                     : d->detachFlags() | QArrayData::Unsharable);
145             QArrayDataPointer old(d);
146             d = detached;
147         } else {
148             d->ref.setSharable(sharable);
149         }
150     }
151
152     void swap(QArrayDataPointer &other)
153     {
154         qSwap(d, other.d);
155     }
156
157     void clear()
158     {
159         QArrayDataPointer tmp(d);
160         d = Data::sharedNull();
161     }
162
163     bool detach()
164     {
165         if (needsDetach()) {
166             Data *copy = clone(d->detachFlags());
167             QArrayDataPointer old(d);
168             d = copy;
169             return true;
170         }
171
172         return false;
173     }
174
175 private:
176     Data *clone(QArrayData::AllocationOptions options) const Q_REQUIRED_RESULT
177     {
178         QArrayDataPointer copy(Data::allocate(d->detachCapacity(d->size),
179                     options));
180         if (d->size)
181             copy->copyAppend(d->begin(), d->end());
182
183         Data *result = copy.d;
184         copy.d = Data::sharedNull();
185         return result;
186     }
187
188     Data *d;
189 };
190
191 template <class T>
192 inline bool operator==(const QArrayDataPointer<T> &lhs, const QArrayDataPointer<T> &rhs)
193 {
194     return lhs.data() == rhs.data();
195 }
196
197 template <class T>
198 inline bool operator!=(const QArrayDataPointer<T> &lhs, const QArrayDataPointer<T> &rhs)
199 {
200     return lhs.data() != rhs.data();
201 }
202
203 template <class T>
204 inline void qSwap(QArrayDataPointer<T> &p1, QArrayDataPointer<T> &p2)
205 {
206     p1.swap(p2);
207 }
208
209 QT_END_NAMESPACE
210
211 namespace std
212 {
213     template <class T>
214     inline void swap(
215             QT_PREPEND_NAMESPACE(QArrayDataPointer)<T> &p1,
216             QT_PREPEND_NAMESPACE(QArrayDataPointer)<T> &p2)
217     {
218         p1.swap(p2);
219     }
220 }
221
222 QT_END_HEADER
223
224 #endif // include guard