From a556b45abf18f1bd509daaf63b66b7d55e9fd291 Mon Sep 17 00:00:00 2001 From: jjesswan Date: Mon, 22 Apr 2024 21:56:26 -0400 Subject: add engine version --- engine-ocean/Game/TypeMap.h | 54 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 engine-ocean/Game/TypeMap.h (limited to 'engine-ocean/Game/TypeMap.h') 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 +#include + +template +class TypeMap { + typedef std::unordered_map 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 + iterator find() { return m_map.find(getTypeId()); } + + template + const_iterator find() const { return m_map.find(getTypeId()); } + + template + bool contains() { return m_map.count(getTypeId()); } + + // associates a value with the type "Key" + template + void put(ValueType &&value){ + m_map[getTypeId()] = std::forward(value); + } + + template + void remove() { m_map.erase(getTypeId()); } + +private: + template + inline static int getTypeId(){ + static const int id = LastTypeId++; + return id; + } + + static std::atomic_int LastTypeId; + InternalMap m_map; +}; + +template +std::atomic_int TypeMap::LastTypeId(0); + +#endif // TYPEMAP_H -- cgit v1.2.3-70-g09d2