diff options
author | João Abecasis <joao.abecasis@nokia.com> | 2011-11-02 12:10:17 +0100 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2011-12-14 17:35:40 +0100 |
commit | 7d16ea40331dc4480af823288e6ed3ca58a677d8 (patch) | |
tree | 14642ed124dde27727df7855ba29445479142998 /src/corelib/tools/qarraydatapointer.h | |
parent | 9c04f721a6b0521f596950771e9d88a5d00a29ee (diff) |
Introducing QArrayDataPointer
This class provides RAII functionality for handling QArrayData pointers.
Together with QArrayDataHeader and QArrayDataOps, this offers common
boilerplate code for implementing a container which, itself, defines its
own interface.
Change-Id: If38eba22fbe8f69038a06fff4acb50af434d229e
Reviewed-by: Bradley T. Hughes <bradley.hughes@nokia.com>
Diffstat (limited to 'src/corelib/tools/qarraydatapointer.h')
-rw-r--r-- | src/corelib/tools/qarraydatapointer.h | 161 |
1 files changed, 161 insertions, 0 deletions
diff --git a/src/corelib/tools/qarraydatapointer.h b/src/corelib/tools/qarraydatapointer.h new file mode 100644 index 0000000000..0d072a510e --- /dev/null +++ b/src/corelib/tools/qarraydatapointer.h @@ -0,0 +1,161 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtCore module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QARRAYDATAPOINTER_H +#define QARRAYDATAPOINTER_H + +#include <QtCore/qarraydataops.h> + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +QT_MODULE(Core) + +template <class T> +struct QArrayDataPointer +{ +private: + typedef QTypedArrayData<T> Data; + typedef QArrayDataOps<T> DataOps; + +public: + QArrayDataPointer() + : d(Data::sharedNull()) + { + } + + QArrayDataPointer(const QArrayDataPointer &other) + : d((other.d->ref.ref(), other.d)) + { + } + + explicit QArrayDataPointer(QTypedArrayData<T> *ptr) + : d(ptr) + { + } + + QArrayDataPointer &operator=(const QArrayDataPointer &other) + { + QArrayDataPointer tmp(other); + this->swap(tmp); + return *this; + } + + DataOps &operator*() const + { + Q_ASSERT(d); + return *static_cast<DataOps *>(d); + } + + DataOps *operator->() const + { + Q_ASSERT(d); + return static_cast<DataOps *>(d); + } + + ~QArrayDataPointer() + { + if (!d->ref.deref()) { + (*this)->destroyAll(); + Data::deallocate(d); + } + } + + bool isNull() const + { + return d == Data::sharedNull(); + } + + Data *data() const + { + return d; + } + + void swap(QArrayDataPointer &other) + { + qSwap(d, other.d); + } + + void clear() + { + QArrayDataPointer tmp(d); + d = Data::sharedEmpty(); + } + +private: + Data *d; +}; + +template <class T> +inline bool operator==(const QArrayDataPointer<T> &lhs, const QArrayDataPointer<T> &rhs) +{ + return lhs.data() == rhs.data(); +} + +template <class T> +inline bool operator!=(const QArrayDataPointer<T> &lhs, const QArrayDataPointer<T> &rhs) +{ + return lhs.data() != rhs.data(); +} + +template <class T> +inline void qSwap(QArrayDataPointer<T> &p1, QArrayDataPointer<T> &p2) +{ + p1.swap(p2); +} + +QT_END_NAMESPACE + +namespace std +{ + template <class T> + inline void swap( + QT_PREPEND_NAMESPACE(QArrayDataPointer)<T> &p1, + QT_PREPEND_NAMESPACE(QArrayDataPointer)<T> &p2) + { + p1.swap(p2); + } +} + +QT_END_HEADER + +#endif // include guard |