handles.h 6.54 KB
Newer Older
Jochen Becher's avatar
Jochen Becher committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
/***************************************************************************
**
** Copyright (C) 2015 Jochen Becher
** Contact: http://www.qt.io/licensing
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company.  For licensing terms and
** conditions see http://www.qt.io/terms-conditions.  For further information
** use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

#ifndef QMT_HANDLES_H
#define QMT_HANDLES_H

#include "handle.h"
#include "qmt/infrastructure/qmtassert.h"

#include <QList>
#include <QHash>


namespace qmt {

template<typename T>
class Handles
{
public:

48
    typedef QList<Handle<T> > value_type;
Jochen Becher's avatar
Jochen Becher committed
49

50
    typedef typename value_type::iterator iterator;
Jochen Becher's avatar
Jochen Becher committed
51

52
    typedef typename value_type::const_iterator const_iterator;
Jochen Becher's avatar
Jochen Becher committed
53 54 55

public:

56
    explicit Handles(bool takeOwnership = false) : m_takeOwnership(takeOwnership) { }
Jochen Becher's avatar
Jochen Becher committed
57 58

    Handles(const Handles<T> &rhs)
59 60
        : m_handleList(rhs.m_handleList),
          m_takeOwnership(false)
Jochen Becher's avatar
Jochen Becher committed
61 62 63
    {
    }

64
    Handles(const Handles<T> &rhs, bool takeOwnership)
65
        : m_handleList(rhs.m_handleList),
66
          m_takeOwnership(takeOwnership)
Jochen Becher's avatar
Jochen Becher committed
67
    {
68 69
        if (m_takeOwnership && rhs.m_takeOwnership) {
            const_cast<Handles<T> &>(rhs).m_handleList.clear();
Jochen Becher's avatar
Jochen Becher committed
70 71 72 73 74 75 76 77 78 79 80 81 82
        }
    }

    ~Handles()
    {
        reset();
    }

public:

    Handles<T> operator=(const Handles<T> &rhs)
    {
        if (this != &rhs) {
83 84 85
            m_handleList = rhs.m_handleList;
            if (m_takeOwnership && rhs.m_takeOwnership) {
                const_cast<Handles<T> &>(rhs).m_handleList.clear();
Jochen Becher's avatar
Jochen Becher committed
86 87 88 89 90 91 92
            }
        }
        return *this;
    }

public:

93
    bool takesOwnership() const { return m_takeOwnership; }
Jochen Becher's avatar
Jochen Becher committed
94

95
    bool isEmpty() const { return m_handleList.empty(); }
Jochen Becher's avatar
Jochen Becher committed
96

97
    int size() const { return m_handleList.size(); }
Jochen Becher's avatar
Jochen Becher committed
98 99 100

    bool contains(const Uid &uid) const
    {
101
        foreach (const Handle<T> &handle, m_handleList) {
102
            if (handle.uid() == uid) {
Jochen Becher's avatar
Jochen Becher committed
103 104 105 106 107 108 109 110 111
                return true;
            }
        }
        return false;
    }

    bool contains(const T *t) const
    {
        QMT_CHECK(t);
112
        return contains(t->uid());
Jochen Becher's avatar
Jochen Becher committed
113 114 115 116
    }

    T *find(const Uid &uid) const
    {
117
        foreach (const Handle<T> &handle, m_handleList) {
118 119
            if (handle.uid() == uid) {
                return handle.target();
Jochen Becher's avatar
Jochen Becher committed
120 121 122 123 124 125 126
            }
        }
        return 0;
    }

    T *at(int index) const
    {
127
        QMT_CHECK(index >= 0 && index < m_handleList.size());
128
        return m_handleList.at(index).target();
Jochen Becher's avatar
Jochen Becher committed
129 130 131 132
    }

    T *at(int index)
    {
133 134
        QMT_CHECK(index >= 0 && index < m_handleList.size());
        return m_handleList.at(index);
Jochen Becher's avatar
Jochen Becher committed
135 136 137 138 139
    }

    int indexOf(const Uid &uid) const
    {
        int index = 0;
140
        foreach (const Handle<T> &handle, m_handleList) {
141
            if (handle.uid() == uid) {
Jochen Becher's avatar
Jochen Becher committed
142 143 144 145 146 147 148 149 150 151
                return index;
            }
            ++index;
        }
        return -1;
    }

    int indexOf(const T *t) const
    {
        QMT_CHECK(t);
152
        return indexOf(t->uid());
Jochen Becher's avatar
Jochen Becher committed
153 154 155 156
    }

public:

157
    const value_type &get() const { return m_handleList; }
Jochen Becher's avatar
Jochen Becher committed
158

159
    value_type take()
Jochen Becher's avatar
Jochen Becher committed
160
    {
161
        value_type handles = m_handleList;
162
        m_handleList.clear();
Jochen Becher's avatar
Jochen Becher committed
163 164 165
        return handles;
    }

166
    void set(const value_type &handles) {
Jochen Becher's avatar
Jochen Becher committed
167
        reset();
168
        m_handleList = handles;
Jochen Becher's avatar
Jochen Becher committed
169 170 171 172
    }

    void reset()
    {
173 174
        if (m_takeOwnership) {
            foreach (const Handle<T> &handle, m_handleList) {
175
                delete handle.target();
Jochen Becher's avatar
Jochen Becher committed
176 177
            }
        }
178
        m_handleList.clear();
Jochen Becher's avatar
Jochen Becher committed
179 180 181
    }

public:
182
    iterator begin() { return m_handleList.begin(); }
Jochen Becher's avatar
Jochen Becher committed
183

184
    iterator end() { return m_handleList.end(); }
Jochen Becher's avatar
Jochen Becher committed
185

186
    const_iterator begin() const { return m_handleList.begin(); }
Jochen Becher's avatar
Jochen Becher committed
187

188
    const_iterator end() const { return m_handleList.end(); }
Jochen Becher's avatar
Jochen Becher committed
189 190 191 192 193 194

public:

    void add(const Uid &uid)
    {
        QMT_CHECK(uid.isValid());
195
        m_handleList.append(Handle<T>(uid));
Jochen Becher's avatar
Jochen Becher committed
196 197 198 199 200
    }

    void add(T *t)
    {
        QMT_CHECK(t);
201
        m_handleList.append(Handle<T>(t));
Jochen Becher's avatar
Jochen Becher committed
202 203
    }

204
    void insert(int beforeIndex, const Uid &uid)
Jochen Becher's avatar
Jochen Becher committed
205
    {
206
        QMT_CHECK(beforeIndex >= 0 && beforeIndex <= m_handleList.size());
Jochen Becher's avatar
Jochen Becher committed
207
        QMT_CHECK(uid.isValid());
208
        m_handleList.insert(beforeIndex, Handle<T>(uid));
Jochen Becher's avatar
Jochen Becher committed
209 210
    }

211
    void insert(int beforeIndex, T *t)
Jochen Becher's avatar
Jochen Becher committed
212
    {
213
        QMT_CHECK(beforeIndex >= 0 && beforeIndex <= m_handleList.size());
Jochen Becher's avatar
Jochen Becher committed
214
        QMT_CHECK(t);
215
        m_handleList.insert(beforeIndex, Handle<T>(t));
Jochen Becher's avatar
Jochen Becher committed
216 217 218 219 220
    }

    void remove(int index)
    {
        QMT_CHECK(index >= 0 && index < size());
221
        if (m_takeOwnership) {
222
            T *t = m_handleList.at(index).target();
223
            m_handleList.removeAt(index);
Jochen Becher's avatar
Jochen Becher committed
224 225
            delete t;
        } else {
226
            m_handleList.removeAt(index);
Jochen Becher's avatar
Jochen Becher committed
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
        }
    }

    void remove(const Uid &uid)
    {
        remove(indexOf(uid));
    }

    void remove(T *t)
    {
        QMT_CHECK(t);
        remove(indexOf(t));
    }

    T * take(int index)
    {
        QMT_CHECK(index >= 0 && index < size());
244
        T *t = m_handleList.at(index).target();
245
        m_handleList.removeAt(index);
Jochen Becher's avatar
Jochen Becher committed
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
        return t;
    }

    T *take(const Uid &uid)
    {
        return take(indexOf(uid));
    }

    T *take(T *t)
    {
        QMT_CHECK(t);
        return take(indexOf(t));
    }

private:

262
    value_type m_handleList;
Jochen Becher's avatar
Jochen Becher committed
263

264
    bool m_takeOwnership;
Jochen Becher's avatar
Jochen Becher committed
265 266 267 268 269 270 271 272 273 274 275 276 277 278
};

template<typename T>
bool operator==(const Handles<T> &lhs, const Handles<T> &rhs)
{
    return lhs.get() == rhs.get();
}

template<typename T>
bool operator!=(const Handles<T> &lhs, const Handles<T> &rhs) { return !(lhs == rhs); }

}

#endif // QMT_HANDLES_H