Saga
Saga Game Engine
Loading...
Searching...
No Matches
typemap.h
Go to the documentation of this file.
1#pragma once
2
3#include <atomic>
4#include <unordered_map>
5
6// maps types to values
7namespace Saga {
14 template <class DataType>
15 class TypeMap {
16 using Map = std::unordered_map<int, DataType>;
17 public:
18 typedef typename Map::iterator iterator;
19 typedef typename Map::const_iterator const_iterator;
20 typedef typename Map::value_type value_type;
21
28 const_iterator begin() const { return map.begin(); }
29
36 const_iterator end() const { return map.end(); }
37
44 iterator begin() { return map.begin(); }
45
52 iterator end() { return map.end(); }
53
61 template <class KeyType>
62 iterator find() { return map.find(getTypeId<KeyType>()); }
63
71 template <class KeyType>
72 const_iterator find() const { return map.find(getTypeId<KeyType>()); }
73
81 template <class KeyType>
82 bool hasKey() { return find<KeyType>() != end(); }
83
84
93 template <class KeyType>
94 void put(DataType &&data) {
95 map[getTypeId<KeyType>()] = std::forward<DataType>(data);
96 }
97
104 template <class KeyType>
105 inline int getTypeId() {
106 static int id = LastTypeId++;
107 return id;
108 }
109
115 int size() { return map.size(); }
116
117 private:
118 static std::atomic_int LastTypeId;
119 Map map;
120 };
121
122 template <class ValueType>
123 std::atomic_int TypeMap<ValueType>::LastTypeId(0);
124}
Map that has Types as key.
Definition: typemap.h:15
const_iterator begin() const
Return a const iterator to the beginning of this typemap.
Definition: typemap.h:28
void put(DataType &&data)
Place an item inside the map. If the map already contains this KeyType as a key, then replace the map...
Definition: typemap.h:94
const_iterator end() const
Return a const iterator to the end of this typemap.
Definition: typemap.h:36
iterator end()
Return an iterator to the end of this typemap.
Definition: typemap.h:52
Map::iterator iterator
Definition: typemap.h:18
int getTypeId()
Generate a unique ID for a type. Any call to this with the same type will return the same value.
Definition: typemap.h:105
iterator find()
Find a type inside this map.
Definition: typemap.h:62
Map::value_type value_type
Definition: typemap.h:20
iterator begin()
Return an iterator to the beginning of this typemap.
Definition: typemap.h:44
const_iterator find() const
Find a type inside this map.
Definition: typemap.h:72
int size()
Determine the number of mappings this map has.
Definition: typemap.h:115
bool hasKey()
Determine if the map already maps a type to some value.
Definition: typemap.h:82
Map::const_iterator const_iterator
Definition: typemap.h:19
Definition: app.cpp:8