summaryrefslogtreecommitdiff
path: root/engine-ocean/Game/TypeMap.h
diff options
context:
space:
mode:
authorjjesswan <jessica_wan@brown.edu>2024-04-22 21:56:26 -0400
committerjjesswan <jessica_wan@brown.edu>2024-04-22 21:56:26 -0400
commita556b45abf18f1bd509daaf63b66b7d55e9fd291 (patch)
treebc9b8a2d184c12aee236e7f9f276a34b84ca552d /engine-ocean/Game/TypeMap.h
parentcd7c76017a12bb548036571c1ff13e551369d06d (diff)
add engine version
Diffstat (limited to 'engine-ocean/Game/TypeMap.h')
-rw-r--r--engine-ocean/Game/TypeMap.h54
1 files changed, 54 insertions, 0 deletions
diff --git a/engine-ocean/Game/TypeMap.h b/engine-ocean/Game/TypeMap.h
new file mode 100644
index 0000000..add3367
--- /dev/null
+++ b/engine-ocean/Game/TypeMap.h
@@ -0,0 +1,54 @@
+#ifndef TYPEMAP_H
+#define TYPEMAP_H
+#include <unordered_map>
+#include <atomic>
+
+template <class ValueType>
+class TypeMap {
+ typedef std::unordered_map<int, ValueType> InternalMap;
+
+public:
+ typedef typename InternalMap::iterator iterator;
+ typedef typename InternalMap::const_iterator const_iterator;
+ typedef typename InternalMap::value_type value_type;
+
+ const_iterator begin() const { return m_map.begin(); }
+ const_iterator end() const { return m_map.end(); }
+
+ iterator begin() { return m_map.begin(); }
+ iterator end() { return m_map.end(); }
+
+ // finds the value associated with type "Key" in the typemap
+ template <class Key>
+ iterator find() { return m_map.find(getTypeId<Key>()); }
+
+ template <class Key>
+ const_iterator find() const { return m_map.find(getTypeId<Key>()); }
+
+ template <class Key>
+ bool contains() { return m_map.count(getTypeId<Key>()); }
+
+ // associates a value with the type "Key"
+ template <class Key>
+ void put(ValueType &&value){
+ m_map[getTypeId<Key>()] = std::forward<ValueType>(value);
+ }
+
+ template <class Key>
+ void remove() { m_map.erase(getTypeId<Key>()); }
+
+private:
+ template <class Key>
+ inline static int getTypeId(){
+ static const int id = LastTypeId++;
+ return id;
+ }
+
+ static std::atomic_int LastTypeId;
+ InternalMap m_map;
+};
+
+template <class ValueType>
+std::atomic_int TypeMap<ValueType>::LastTypeId(0);
+
+#endif // TYPEMAP_H