-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBindableList.h
77 lines (65 loc) · 2.32 KB
/
BindableList.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#pragma once
#include "Includes.h"
#include "Bindable.h"
#include <memory>
#include <type_traits>
#include <unordered_map>
#include "ErrorMacros.h"
class BindableList
{
public:
template <class T, class ...Params>
static std::shared_ptr<T> GetBindable(GFX& gfx, Params&& ...params)
{
static_assert(std::is_base_of<Bindable, T>::value, "passed bindable must be delivered from Bindable class");
return m_Get().m_GetBindable<T>(gfx, std::forward<Params>(params)...);
}
template <class T, class ...Params>
static std::shared_ptr<T> GetBindableWithoutCreation(GFX& gfx, Params&& ...params)
{
static_assert(std::is_base_of<Bindable, T>::value, "passed bindable must be delivered from Bindable class");
return m_Get().m_GetBindableWithoutCreation<T>(gfx, std::forward<Params>(params)...);
}
template <class T>
static void PushBindable(std::shared_ptr<T> bind, const char* uid)
{
static_assert(std::is_base_of<Bindable, T>::value, "passed bindable must be delivered from Bindable class");
return m_Get().m_PushBindable(bind, uid);
}
private:
template <class T, class ...Params>
std::shared_ptr<T> m_GetBindable(GFX &gfx, Params&& ...params)
{
const auto uid = typeid(T).name() + std::string("@") + T::GetStaticUID(std::forward<Params>(params)...);
const auto pBind = m_bindables.find(uid);
if (pBind == m_bindables.end())
{
std::shared_ptr<T> bind = std::make_shared<T>(gfx, std::forward<Params>(params)...);
m_bindables[uid] = bind;
return bind;
}
return std::static_pointer_cast<T, Bindable>(pBind->second);
}
template <class T, class ...Params>
std::shared_ptr<T> m_GetBindableWithoutCreation(GFX &gfx, Params&& ...params)
{
const auto uid = typeid(T).name() + std::string("@") + T::GetStaticUID(std::forward<Params>(params)...);
const auto pBind = m_bindables.find(uid);
if (pBind == m_bindables.end())
THROW_INTERNAL_ERROR("Bindable could not be found. Used no creation option.");
return std::static_pointer_cast<T, Bindable>(pBind->second);
}
template <class T>
void m_PushBindable(std::shared_ptr<T> bind, const char* uid)
{
std::string finalUid = typeid(T).name() + std::string("@") + uid;
m_bindables[finalUid] = bind;
}
static BindableList& m_Get()
{
static BindableList list;
return list;
}
private:
std::unordered_map<std::string, std::shared_ptr<Bindable>> m_bindables;
};