summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/data-index.h
diff options
context:
space:
mode:
authorPete Hurst <pete@streamuniverse.tv>2013-06-25 06:53:34 +0100
committerPete Hurst <pete@streamuniverse.tv>2013-06-25 23:39:51 +0100
commit3273a34d9857ded372bb5dd24d49dd0d8c6da27e (patch)
tree7b3f8169f96dc70fe7354501850e4dbc78b0f34c /crawl-ref/source/data-index.h
parent358542fc0760670d8f5c98483adaa849c4782441 (diff)
downloadcrawl-ref-3273a34d9857ded372bb5dd24d49dd0d8c6da27e.tar.gz
crawl-ref-3273a34d9857ded372bb5dd24d49dd0d8c6da27e.zip
A data indexer template
This takes some coding out of creating an enum-indexed data list (existing examples would include lists of player status data, spell data, etc.)
Diffstat (limited to 'crawl-ref/source/data-index.h')
-rw-r--r--crawl-ref/source/data-index.h50
1 files changed, 50 insertions, 0 deletions
diff --git a/crawl-ref/source/data-index.h b/crawl-ref/source/data-index.h
new file mode 100644
index 0000000000..fc84cb4d9c
--- /dev/null
+++ b/crawl-ref/source/data-index.h
@@ -0,0 +1,50 @@
+/**
+ * @file
+ * @brief Functions for managing indexed tables of data attached to enums
+**/
+
+#ifndef DATAINDEX_H
+#define DATAINDEX_H
+
+template <typename TKey, typename TVal, int NMax>
+class data_index
+{
+public:
+ data_index(const TVal* _pop) : pop(_pop), index_created(false)
+ { }
+
+ const TVal* operator[](int i)
+ {
+ check_index();
+ return (&pop[index[i]]);
+ }
+
+ bool contains(TKey key)
+ {
+ check_index();
+ return (index[key] >= 0);
+ }
+
+protected:
+ const TVal* pop;
+ bool index_created;
+ int index[NMax];
+ virtual TKey map(const TVal* val) = 0;
+
+ void check_index()
+ {
+ if (!index_created)
+ {
+ memset(index, -1, sizeof(index));
+ int i = 0;
+ for (const TVal* p = pop; map(p); p++)
+ {
+ index[map(p)] = i;
+ i++;
+ }
+ index_created = true;
+ }
+ }
+};
+
+#endif