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 QtScript module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL-ONLY$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser
11 ** General Public License version 2.1 as published by the Free Software
12 ** Foundation and appearing in the file LICENSE.LGPL included in the
13 ** packaging of this file. Please review the following information to
14 ** ensure the GNU Lesser General Public License version 2.1 requirements
15 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** If you have questions regarding the use of this file, please contact
18 ** us via http://www.qt-project.org/.
22 ****************************************************************************/
28 // This file is not part of the Qt API. It exists purely as an
29 // implementation detail. This header file may change from version to
30 // version without notice, or even be removed.
35 #ifndef QSCRIPTSHAREDDATA_P_H
36 #define QSCRIPTSHAREDDATA_P_H
39 #include "qshareddata.h"
45 This class should have the same interface as the QSharedData, but implementation doesn't
46 need to be thread safe, so atomic ref count was replaced by normal integer value.
48 class QScriptSharedData
51 class ReferenceCounter {
52 // FIXME shouldn't it be uint or something longer?
54 ReferenceCounter(int ref) : m_ref(ref) {}
55 ~ReferenceCounter() { Q_ASSERT_X(!m_ref, Q_FUNC_INFO, "Memory problem found"); }
57 bool ref() { return ++m_ref; }
58 bool deref() { return --m_ref; }
59 friend class QScriptSharedData;
63 inline QScriptSharedData() : ref(0) { }
66 Q_DISABLE_COPY(QScriptSharedData)
70 template <class T> class QScriptPassPointer;
72 // FIXME: that could be reimplemented to not check for a null value.
74 class QScriptSharedDataPointer : public QExplicitlySharedDataPointer<T>
77 inline QScriptSharedDataPointer() {}
78 explicit QScriptSharedDataPointer(QScriptPassPointer<T> data) : QExplicitlySharedDataPointer<T>(data.give()) {}
79 explicit QScriptSharedDataPointer(T *data) : QExplicitlySharedDataPointer<T>(data) {}
81 inline QScriptSharedDataPointer<T> &operator=(const QScriptPassPointer<T> &other)
83 this->QExplicitlySharedDataPointer<T>::operator =(other.give());
86 inline QScriptSharedDataPointer<T> &operator=(T *other)
88 this->QExplicitlySharedDataPointer<T>::operator =(other);
93 // FIXME: that could be reimplemented to not check for a null value.
95 class QScriptPassPointer {
97 QScriptPassPointer(T *data) : m_ptr(data) {}
98 inline QScriptPassPointer() { m_ptr = 0; }
99 inline QScriptPassPointer(const QScriptPassPointer<T> &other) : m_ptr(other.give()) {}
100 inline ~QScriptPassPointer() { Q_ASSERT_X(!m_ptr, Q_FUNC_INFO, "Ownership of the QScriptPassPointer hasn't been taken"); }
102 inline T &operator*() const { return *m_ptr; }
103 inline T *operator->() { return m_ptr; }
104 inline T *operator->() const { return m_ptr; }
105 inline T *data() const { return m_ptr; }
106 inline const T *constData() const { return m_ptr; }
108 inline bool operator==(const QScriptPassPointer<T> &other) const { return m_ptr == other.m_ptr; }
109 inline bool operator!=(const QScriptPassPointer<T> &other) const { return m_ptr != other.m_ptr; }
110 inline bool operator==(const QScriptSharedDataPointer<T> &other) const { return m_ptr == other.m_ptr; }
111 inline bool operator!=(const QScriptSharedDataPointer<T> &other) const { return m_ptr != other.m_ptr; }
112 inline bool operator==(const T *ptr) const { return m_ptr == ptr; }
113 inline bool operator!=(const T *ptr) const { return m_ptr != ptr; }
115 inline operator bool () const { return m_ptr != 0; }
116 inline bool operator!() const { return !m_ptr; }
118 inline QScriptPassPointer<T> & operator=(const QScriptPassPointer<T> &other)
120 if (other.m_ptr != m_ptr) {
123 m_ptr = other.give();
128 inline QScriptPassPointer &operator=(T *other)
130 if (other != m_ptr) {
138 inline T* give() const
151 #endif // QSCRIPTSHAREDDATA_P_H