PageRenderTime 29ms CodeModel.GetById 19ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/src/server/shared/DataStores/DBStorageIterator.h

https://gitlab.com/tkrokli/TrinityCore_434
C Header | 69 lines | 43 code | 10 blank | 16 comment | 8 complexity | 2e8768385c0eec978413a2708aff565f MD5 | raw file
 1/*
 2 * Copyright (C) 2008-2015 TrinityCore <http://www.trinitycore.org/>
 3 *
 4 * This program is free software; you can redistribute it and/or modify it
 5 * under the terms of the GNU General Public License as published by the
 6 * Free Software Foundation; either version 2 of the License, or (at your
 7 * option) any later version.
 8 *
 9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef DBStorageIterator_h__
19#define DBStorageIterator_h__
20
21#include "Define.h"
22#include <iterator>
23
24template<class T>
25class DBStorageIterator : public std::iterator<std::forward_iterator_tag, T>
26{
27public:
28    DBStorageIterator() : _index(nullptr), _pos(0), _end(0) { }
29    DBStorageIterator(T** index, uint32 size, uint32 pos = 0) : _index(index), _pos(pos), _end(size)
30    {
31        if (_pos < _end)
32        {
33            while (_pos < _end && !_index[_pos])
34                ++_pos;
35        }
36    }
37
38    T* operator->() { return _index[_pos]; }
39    T* operator*() { return _index[_pos]; }
40
41    bool operator==(DBStorageIterator const& right) const { /*ASSERT(_index == right._index, "Iterator belongs to a different container")*/ return _pos == right._pos; }
42    bool operator!=(DBStorageIterator const& right) const { return !(*this == right); }
43
44    DBStorageIterator& operator++()
45    {
46        if (_pos < _end)
47        {
48            do
49                ++_pos;
50            while (_pos < _end && !_index[_pos]);
51        }
52
53        return *this;
54    }
55
56    DBStorageIterator operator++(int)
57    {
58        DBStorageIterator tmp = *this;
59        ++*this;
60        return tmp;
61    }
62
63private:
64    T** _index;
65    uint32 _pos;
66    uint32 _end;
67};
68
69#endif // DBStorageIterator_h__