| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com)
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of the QtScript module of the Qt Toolkit.
|
|---|
| 7 | **
|
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 9 | ** Commercial Usage
|
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 13 | ** a written agreement between you and Nokia.
|
|---|
| 14 | **
|
|---|
| 15 | ** GNU Lesser General Public License Usage
|
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 19 | ** packaging of this file. Please review the following information to
|
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 22 | **
|
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain
|
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|---|
| 26 | ** package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please
|
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com.
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #ifndef QSCRIPTBUFFER_P_H
|
|---|
| 43 | #define QSCRIPTBUFFER_P_H
|
|---|
| 44 |
|
|---|
| 45 | #include <QtCore/qglobal.h>
|
|---|
| 46 |
|
|---|
| 47 | QT_BEGIN_NAMESPACE
|
|---|
| 48 |
|
|---|
| 49 | //
|
|---|
| 50 | // W A R N I N G
|
|---|
| 51 | // -------------
|
|---|
| 52 | //
|
|---|
| 53 | // This file is not part of the Qt API. It exists purely as an
|
|---|
| 54 | // implementation detail. This header file may change from version to
|
|---|
| 55 | // version without notice, or even be removed.
|
|---|
| 56 | //
|
|---|
| 57 | // We mean it.
|
|---|
| 58 | //
|
|---|
| 59 |
|
|---|
| 60 | namespace QScript
|
|---|
| 61 | {
|
|---|
| 62 | template <typename T> class Buffer
|
|---|
| 63 | {
|
|---|
| 64 | public:
|
|---|
| 65 | typedef T *iterator;
|
|---|
| 66 | typedef const T *const_iterator;
|
|---|
| 67 |
|
|---|
| 68 | Buffer() : m_data(0), m_capacity(0), m_size(0) { }
|
|---|
| 69 | ~Buffer() { delete [] m_data; }
|
|---|
| 70 |
|
|---|
| 71 | inline void reserve(int num);
|
|---|
| 72 | inline void reset();
|
|---|
| 73 | inline void clear();
|
|---|
| 74 | inline void append(const T &t);
|
|---|
| 75 |
|
|---|
| 76 | inline iterator begin();
|
|---|
| 77 | inline iterator end();
|
|---|
| 78 |
|
|---|
| 79 | inline const_iterator begin() const;
|
|---|
| 80 | inline const_iterator end() const;
|
|---|
| 81 |
|
|---|
| 82 | inline int size() const;
|
|---|
| 83 | inline void resize(int s);
|
|---|
| 84 |
|
|---|
| 85 | inline int capacity() const;
|
|---|
| 86 | inline T *data();
|
|---|
| 87 | inline const T *constData() const;
|
|---|
| 88 |
|
|---|
| 89 | inline T &last();
|
|---|
| 90 | inline T &takeLast();
|
|---|
| 91 |
|
|---|
| 92 | inline T &at(int i) { return (*this)[i]; }
|
|---|
| 93 | inline const T &at(int i) const { return (*this)[i]; }
|
|---|
| 94 |
|
|---|
| 95 | inline T &operator[](int i);
|
|---|
| 96 | inline const T &operator[](int i) const;
|
|---|
| 97 |
|
|---|
| 98 | inline bool isEmpty() const;
|
|---|
| 99 |
|
|---|
| 100 | private:
|
|---|
| 101 | T *m_data;
|
|---|
| 102 | int m_capacity;
|
|---|
| 103 | int m_size;
|
|---|
| 104 |
|
|---|
| 105 | private:
|
|---|
| 106 | Q_DISABLE_COPY(Buffer)
|
|---|
| 107 | };
|
|---|
| 108 |
|
|---|
| 109 | } // namespace QScript
|
|---|
| 110 |
|
|---|
| 111 | template <typename T> T *QScript::Buffer<T>::data() { return m_data; }
|
|---|
| 112 | template <typename T> const T *QScript::Buffer<T>::constData() const { return m_data; }
|
|---|
| 113 | template <typename T> void QScript::Buffer<T>::reset() { m_size = 0; }
|
|---|
| 114 | template <typename T> int QScript::Buffer<T>::capacity() const { return m_capacity; }
|
|---|
| 115 |
|
|---|
| 116 | template <typename T> int QScript::Buffer<T>::size() const { return m_size; }
|
|---|
| 117 | template <typename T> void QScript::Buffer<T>::resize(int s)
|
|---|
| 118 | {
|
|---|
| 119 | if (m_capacity < s)
|
|---|
| 120 | reserve (s << 1);
|
|---|
| 121 |
|
|---|
| 122 | m_size = s;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | template <typename T> void QScript::Buffer<T>::clear()
|
|---|
| 126 | {
|
|---|
| 127 | delete [] m_data;
|
|---|
| 128 | m_data = 0;
|
|---|
| 129 | m_size = 0;
|
|---|
| 130 | m_capacity = 0;
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | template <typename T> void QScript::Buffer<T>::reserve(int x)
|
|---|
| 134 | {
|
|---|
| 135 | // its an ever expanding buffer so it never gets smaller..
|
|---|
| 136 | if (x < m_capacity)
|
|---|
| 137 | return;
|
|---|
| 138 | m_capacity = x;
|
|---|
| 139 | T *new_data = new T[m_capacity];
|
|---|
| 140 | for (int i=0; i<m_size; ++i)
|
|---|
| 141 | new_data[i] = m_data[i];
|
|---|
| 142 | delete [] m_data;
|
|---|
| 143 | m_data = new_data;
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | template <typename T> void QScript::Buffer<T>::append(const T &t)
|
|---|
| 147 | {
|
|---|
| 148 | if (m_size == m_capacity)
|
|---|
| 149 | reserve(m_capacity + 32);
|
|---|
| 150 | m_data[m_size++] = t;
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | template <typename T> T &QScript::Buffer<T>::operator[](int i)
|
|---|
| 154 | {
|
|---|
| 155 | Q_ASSERT(i >= 0);
|
|---|
| 156 | Q_ASSERT(i < m_size);
|
|---|
| 157 | return m_data[i];
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | template <typename T> const T &QScript::Buffer<T>::operator[](int i) const
|
|---|
| 161 | {
|
|---|
| 162 | Q_ASSERT(i >= 0);
|
|---|
| 163 | Q_ASSERT(i < m_size);
|
|---|
| 164 | return m_data[i];
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | template <typename T> bool QScript::Buffer<T>::isEmpty() const
|
|---|
| 168 | {
|
|---|
| 169 | return m_size == 0;
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | template <typename T> T &QScript::Buffer<T>::takeLast()
|
|---|
| 173 | {
|
|---|
| 174 | Q_ASSERT(!isEmpty());
|
|---|
| 175 | --m_size;
|
|---|
| 176 | return m_data[m_size];
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | template <typename T> T &QScript::Buffer<T>::last()
|
|---|
| 180 | {
|
|---|
| 181 | return m_data[m_size - 1];
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | template <typename T> typename QScript::Buffer<T>::iterator QScript::Buffer<T>::begin()
|
|---|
| 185 | {
|
|---|
| 186 | return m_data;
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | template <typename T> typename QScript::Buffer<T>::iterator QScript::Buffer<T>::end()
|
|---|
| 190 | {
|
|---|
| 191 | return m_data + m_size;
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | template <typename T> typename QScript::Buffer<T>::const_iterator QScript::Buffer<T>::begin() const
|
|---|
| 195 | {
|
|---|
| 196 | return m_data;
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | template <typename T> typename QScript::Buffer<T>::const_iterator QScript::Buffer<T>::end() const
|
|---|
| 200 | {
|
|---|
| 201 | return m_data + m_size;
|
|---|
| 202 | }
|
|---|
| 203 |
|
|---|
| 204 | QT_END_NAMESPACE
|
|---|
| 205 |
|
|---|
| 206 | #endif
|
|---|