diff options
Diffstat (limited to 'engine-ocean/Game/TypeMap.h')
-rw-r--r-- | engine-ocean/Game/TypeMap.h | 54 |
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 |