aboutsummaryrefslogtreecommitdiff
path: root/src/server/shared
diff options
context:
space:
mode:
authorShauren <shauren.trinity@gmail.com>2015-02-22 12:44:24 +0100
committerShauren <shauren.trinity@gmail.com>2015-02-22 12:44:24 +0100
commit40b6736f69403b5fabee5ef39fa114aa9b301136 (patch)
treea9e5f3fb5b3422f8e0928ef6ba23e90d73aed6df /src/server/shared
parent8214a14ef889d91c561861c8e6189782be493d86 (diff)
Core/DataStores: Added iterator class for DBCStorage and DB2Storage
Diffstat (limited to 'src/server/shared')
-rw-r--r--src/server/shared/DataStores/DB2Store.h6
-rw-r--r--src/server/shared/DataStores/DBCStore.h9
-rw-r--r--src/server/shared/DataStores/DBStorageIterator.h46
3 files changed, 60 insertions, 1 deletions
diff --git a/src/server/shared/DataStores/DB2Store.h b/src/server/shared/DataStores/DB2Store.h
index c45a25689cc..98ee059fd8a 100644
--- a/src/server/shared/DataStores/DB2Store.h
+++ b/src/server/shared/DataStores/DB2Store.h
@@ -20,6 +20,7 @@
#include "Common.h"
#include "DB2StorageLoader.h"
+#include "DBStorageIterator.h"
#include "ByteBuffer.h"
/// Interface class for common access
@@ -45,6 +46,8 @@ class DB2Storage : public DB2StorageBase
{
typedef std::list<char*> StringPoolList;
public:
+ typedef DBStorageIterator<T> iterator;
+
DB2Storage(char const* f, int32 preparedStmtIndex = -1)
: _indexTableSize(0), _fieldCount(0), _format(f), _dataTable(nullptr), _dataTableEx(nullptr), _hotfixStatement(preparedStmtIndex)
{
@@ -197,6 +200,9 @@ public:
_indexTableSize = 0;
}
+ iterator begin() { return iterator(_indexTable.AsT, _indexTableSize); }
+ iterator end() { return iterator(_indexTable.AsT, _indexTableSize, _indexTableSize); }
+
private:
uint32 _indexTableSize;
uint32 _fieldCount;
diff --git a/src/server/shared/DataStores/DBCStore.h b/src/server/shared/DataStores/DBCStore.h
index d9cd8472fa7..b988bbd018c 100644
--- a/src/server/shared/DataStores/DBCStore.h
+++ b/src/server/shared/DataStores/DBCStore.h
@@ -20,6 +20,7 @@
#define DBCSTORE_H
#include "DBCFileLoader.h"
+#include "DBStorageIterator.h"
#include "Log.h"
#include "Field.h"
#include "DatabaseWorkerPool.h"
@@ -72,8 +73,11 @@ private:
template<class T>
class DBCStorage
{
- typedef std::list<char*> StringPoolList;
+ typedef std::list<char*> StringPoolList;
+
public:
+ typedef DBStorageIterator<T> iterator;
+
explicit DBCStorage(char const* f)
: fmt(f), nCount(0), fieldCount(0), dataTable(NULL)
{
@@ -296,6 +300,9 @@ class DBCStorage
nCount = 0;
}
+ iterator begin() { return iterator(indexTable.asT, nCount); }
+ iterator end() { return iterator(indexTable.asT, nCount, nCount); }
+
private:
char const* fmt;
uint32 nCount;
diff --git a/src/server/shared/DataStores/DBStorageIterator.h b/src/server/shared/DataStores/DBStorageIterator.h
new file mode 100644
index 00000000000..0bd7e7dcaa9
--- /dev/null
+++ b/src/server/shared/DataStores/DBStorageIterator.h
@@ -0,0 +1,46 @@
+/*
+ * Copyright (C) 2008-2015 TrinityCore <http://www.trinitycore.org/>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef DBStorageIterator_h__
+#define DBStorageIterator_h__
+
+#include "Define.h"
+#include <iterator>
+
+template<class T>
+class DBStorageIterator : public std::iterator<std::forward_iterator_tag, T>
+{
+public:
+ DBStorageIterator() : _index(nullptr), _pos(0), _end(0) { }
+ DBStorageIterator(T** index, uint32 size, uint32 pos = 0) : _index(index), _pos(pos), _end(size) { while (_pos < _end && !_index[++_pos]); }
+
+ T* operator->() { return _index[_pos]; }
+ T* operator*() { return _index[_pos]; }
+
+ bool operator==(DBStorageIterator const& right) const { /*ASSERT(_index == right._index, "Iterator belongs to a different container")*/ return _pos == right._pos; }
+ bool operator!=(DBStorageIterator const& right) const { return !(*this == right); }
+
+ DBStorageIterator& operator++() { while (_pos < _end && !_index[++_pos]); return *this; }
+ DBStorageIterator operator++(int) { DBStorageIterator tmp = *this; ++*this; return tmp; }
+
+private:
+ T** _index;
+ uint32 _pos;
+ uint32 _end;
+};
+
+#endif // DBStorageIterator_h__