TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
DBStorageIterator.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2016 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 
24 template<class T>
25 class DBStorageIterator : public std::iterator<std::forward_iterator_tag, T>
26 {
27 public:
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 
45  {
46  if (_pos < _end)
47  {
48  do
49  ++_pos;
50  while (_pos < _end && !_index[_pos]);
51  }
52 
53  return *this;
54  }
55 
57  {
58  DBStorageIterator tmp = *this;
59  ++*this;
60  return tmp;
61  }
62 
63 private:
64  T** _index;
67 };
68 
69 #endif // DBStorageIterator_h__
T ** _index
Definition: DBStorageIterator.h:64
DBStorageIterator operator++(int)
Definition: DBStorageIterator.h:56
bool operator==(DBStorageIterator const &right) const
Definition: DBStorageIterator.h:41
DBStorageIterator()
Definition: DBStorageIterator.h:28
T * operator*()
Definition: DBStorageIterator.h:39
bool operator!=(DBStorageIterator const &right) const
Definition: DBStorageIterator.h:42
Definition: DBStorageIterator.h:25
DBStorageIterator & operator++()
Definition: DBStorageIterator.h:44
uint32_t uint32
Definition: Define.h:150
uint32 _pos
Definition: DBStorageIterator.h:65
T * operator->()
Definition: DBStorageIterator.h:38
uint32 _end
Definition: DBStorageIterator.h:66
DBStorageIterator(T **index, uint32 size, uint32 pos=0)
Definition: DBStorageIterator.h:29