Remove "All rights reserved" line from license headers.
[profile/ivi/qtdeclarative.git] / src / declarative / qml / v8 / qscriptshareddata_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtScript module of the Qt Toolkit.
7 **
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.
16 **
17 ** If you have questions regarding the use of this file, please contact
18 ** us via http://www.qt-project.org/.
19 **
20 ** $QT_END_LICENSE$
21 **
22 ****************************************************************************/
23
24 //
25 //  W A R N I N G
26 //  -------------
27 //
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.
31 //
32 // We mean it.
33 //
34
35 #ifndef QSCRIPTSHAREDDATA_P_H
36 #define QSCRIPTSHAREDDATA_P_H
37
38 #include "qglobal.h"
39 #include "qshareddata.h"
40
41 QT_BEGIN_NAMESPACE
42
43 /*!
44   \internal
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.
47 */
48 class QScriptSharedData
49 {
50 public:
51     class ReferenceCounter {
52         // FIXME shouldn't it be uint or something longer?
53         mutable int m_ref;
54         ReferenceCounter(int ref) : m_ref(ref) {}
55         ~ReferenceCounter() { Q_ASSERT_X(!m_ref, Q_FUNC_INFO, "Memory problem found"); }
56     public:
57         bool ref() { return ++m_ref; }
58         bool deref() { return --m_ref; }
59         friend class QScriptSharedData;
60     };
61
62     ReferenceCounter ref;
63     inline QScriptSharedData() : ref(0) { }
64
65 private:
66     Q_DISABLE_COPY(QScriptSharedData)
67 };
68
69
70 template <class T> class QScriptPassPointer;
71
72 // FIXME: that could be reimplemented to not check for a null value.
73 template<class T>
74 class QScriptSharedDataPointer : public QExplicitlySharedDataPointer<T>
75 {
76 public:
77     inline QScriptSharedDataPointer() {}
78     explicit QScriptSharedDataPointer(QScriptPassPointer<T> data) : QExplicitlySharedDataPointer<T>(data.give()) {}
79     explicit QScriptSharedDataPointer(T *data) : QExplicitlySharedDataPointer<T>(data) {}
80
81     inline QScriptSharedDataPointer<T> &operator=(const QScriptPassPointer<T> &other)
82     {
83         this->QExplicitlySharedDataPointer<T>::operator =(other.give());
84         return *this;
85     }
86     inline QScriptSharedDataPointer<T> &operator=(T *other)
87     {
88         this->QExplicitlySharedDataPointer<T>::operator =(other);
89         return *this;
90     }
91 };
92
93 // FIXME: that could be reimplemented to not check for a null value.
94 template <class T>
95 class QScriptPassPointer {
96 public:
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"); }
101
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; }
107
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; }
114
115     inline operator bool () const { return m_ptr != 0; }
116     inline bool operator!() const { return !m_ptr; }
117
118     inline QScriptPassPointer<T> & operator=(const QScriptPassPointer<T> &other)
119     {
120         if (other.m_ptr != m_ptr) {
121             if (m_ptr)
122                 delete m_ptr;
123             m_ptr = other.give();
124         }
125         return *this;
126     }
127
128     inline QScriptPassPointer &operator=(T *other)
129     {
130         if (other != m_ptr) {
131             if (m_ptr)
132                 delete m_ptr;
133             m_ptr = other;
134         }
135         return *this;
136     }
137
138     inline T* give() const
139     {
140         T* result = m_ptr;
141         m_ptr = 0;
142         return result;
143     }
144
145 private:
146     mutable T* m_ptr;
147 };
148
149 QT_END_NAMESPACE
150
151 #endif // QSCRIPTSHAREDDATA_P_H