PixelBullet  0.0.1
A C++ game engine
Loading...
Searching...
No Matches
TypeInfo.hpp
1#pragma once
2
3#include <cstddef>
4#include <mutex>
5#include <typeindex>
6#include <unordered_map>
7
8namespace PixelBullet
9{
10 using TypeID = std::size_t;
11
12 template <typename Base>
14 {
15 public:
16 TypeInfo() = delete;
17
19 template <typename K>
20 static TypeID Get() noexcept
21 {
22 static_assert(std::is_convertible_v<K*, Base*>, "K must be derived from Base");
23 const std::type_index key(typeid(K));
24 {
25 std::scoped_lock lock(s_Mutex);
26 auto it = s_Map.find(key);
27 if (it != s_Map.end())
28 {
29 return it->second;
30 }
31 const TypeID id = s_NextID++;
32 s_Map.emplace(key, id);
33 return id;
34 }
35 }
36
37 private:
38 static inline std::mutex s_Mutex;
39 static inline std::unordered_map<std::type_index, TypeID> s_Map;
40 static inline TypeID s_NextID = 0;
41 };
42} // namespace PixelBullet
Definition TypeInfo.hpp:14
static TypeID Get() noexcept
Returns a unique ID for type K (which must be derived from Base)
Definition TypeInfo.hpp:20