/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. #ifndef DBStorageIterator_h__
  18. #define DBStorageIterator_h__
  19. #include "Define.h"
  20. #include <iterator>
  21. template<class T>
  22. class DBStorageIterator : public std::iterator<std::forward_iterator_tag, T>
  23. {
  24. public:
  25. DBStorageIterator() : _index(nullptr), _pos(0), _end(0) { }
  26. DBStorageIterator(T** index, uint32 size, uint32 pos = 0) : _index(index), _pos(pos), _end(size)
  27. {
  28. if (_pos < _end)
  29. {
  30. while (_pos < _end && !_index[_pos])
  31. ++_pos;
  32. }
  33. }
  34. T* operator->() { return _index[_pos]; }
  35. T* operator*() { return _index[_pos]; }
  36. bool operator==(DBStorageIterator const& right) const { /*ASSERT(_index == right._index, "Iterator belongs to a different container")*/ return _pos == right._pos; }
  37. bool operator!=(DBStorageIterator const& right) const { return !(*this == right); }
  38. DBStorageIterator& operator++()
  39. {
  40. if (_pos < _end)
  41. {
  42. do
  43. ++_pos;
  44. while (_pos < _end && !_index[_pos]);
  45. }
  46. return *this;
  47. }
  48. DBStorageIterator operator++(int)
  49. {
  50. DBStorageIterator tmp = *this;
  51. ++*this;
  52. return tmp;
  53. }
  54. private:
  55. T** _index;
  56. uint32 _pos;
  57. uint32 _end;
  58. };
  59. #endif // DBStorageIterator_h__