CoriEngine
Loading...
Searching...
No Matches
AutoRegisteringFactory.hpp
Go to the documentation of this file.
1#pragma once
2
3namespace Cori {
4 namespace Core {
5 template <typename BaseType, typename KeyType, typename... CtorArgs>
6 class Factory {
7 public:
8 using SharedCreator = std::function<std::shared_ptr<BaseType>(CtorArgs...)>;
9 using UniqueCreator = std::function<std::unique_ptr<BaseType>(CtorArgs...)>;
10
11 static Factory& Instance() {
12 static Factory factory;
13 return factory;
14 }
15
16 Factory(const Factory&) = delete;
17 Factory& operator=(const Factory&) = delete;
18 Factory(Factory&&) = delete;
20
21 bool RegisterShared(const KeyType& key, SharedCreator creator) {
22
23 if (m_SharedCreators.count(key)) {
24 return false;
25 }
26
27 m_SharedCreators.insert({ key, creator });
28 return true;
29 }
30
31 bool RegisterUnique(const KeyType& key, UniqueCreator creator) {
32 if (m_UniqueCreators.count(key)) {
33 return false;
34 }
35
36 m_UniqueCreators.insert({ key, creator });
37 return true;
38 }
39
40 static std::shared_ptr<BaseType> CreateShared(const KeyType& key, CtorArgs... ctorArgs) {
41 if (!Instance().m_SharedCreators.contains(key)) {
43 return nullptr;
44 }
45 return Instance().m_SharedCreators.at(key)(std::forward<CtorArgs>(ctorArgs)...);
46 }
47
48 static std::unique_ptr<BaseType> CreateUnique(const KeyType& key, CtorArgs... ctorArgs) {
49 if (!Instance().m_UniqueCreators.contains(key)) {
51 return nullptr;
52 }
53 return Instance().m_UniqueCreators.at(key)(std::forward<CtorArgs>(ctorArgs)...);
54 }
55
56 private:
57
58 Factory() = default;
59 ~Factory() = default;
60
61 std::unordered_map<KeyType, SharedCreator> m_SharedCreators;
62 std::unordered_map<KeyType, UniqueCreator> m_UniqueCreators;
63 };
64
65 // CRTP base class for self-registration
66 template <typename BaseType, typename DerivedType, typename KeyType, KeyType KeyValue, typename... CtorArgs>
68 public:
69 RegisterInFactory() = default;
70 virtual ~RegisterInFactory() = default;
71
76
77 private:
78 static std::shared_ptr<BaseType> CreateShared(CtorArgs&&... ctorArgs) {
79 if (DerivedType::PreCreateHook(std::forward<CtorArgs>(ctorArgs)...)) {
80 return std::make_shared<DerivedType>(std::forward<CtorArgs>(ctorArgs)...);
81 }
83 return nullptr;
84 }
85
86 static std::unique_ptr<BaseType> CreateUnique(CtorArgs&&... ctorArgs) {
87 if (DerivedType::PreCreateHook(std::forward<CtorArgs>(ctorArgs)...)) {
88 return std::make_unique<DerivedType>(std::forward<CtorArgs>(ctorArgs)...);
89 }
91 return nullptr;
92 }
93
94 protected:
96
98 };
99 }
100}
101
102#define CORI_REGISTERED_FACTORY_INIT \
103private: \
104 struct StaticInitHelper { \
105 StaticInitHelper() { \
106 (void)RegisterShared; \
107 (void)RegisterUnique; \
108 } \
109 }; \
110 inline static StaticInitHelper s_InitHelper
#define CORI_CLEAN_TYPE_NAME(tn)
#define CORI_CORE_ERROR_TAGGED(...)
Definition Logger.hpp:1039
static std::shared_ptr< BaseType > CreateShared(const KeyType &key, CtorArgs... ctorArgs)
bool RegisterShared(const KeyType &key, SharedCreator creator)
std::function< std::unique_ptr< BaseType >(CtorArgs...)> UniqueCreator
Factory(Factory &&)=delete
bool RegisterUnique(const KeyType &key, UniqueCreator creator)
static std::unique_ptr< BaseType > CreateUnique(const KeyType &key, CtorArgs... ctorArgs)
Factory(const Factory &)=delete
Factory & operator=(Factory &&)=delete
std::function< std::shared_ptr< BaseType >(CtorArgs...)> SharedCreator
Factory & operator=(const Factory &)=delete
virtual ~RegisterInFactory()=default
RegisterInFactory & operator=(const RegisterInFactory &)=delete
RegisterInFactory(RegisterInFactory &&)=delete
RegisterInFactory & operator=(RegisterInFactory &&)=delete
RegisterInFactory(const RegisterInFactory &)=delete
Core systems of the engine are here.
Global engine namespace.
static constexpr char Register[]
Definition Logger.hpp:88
static constexpr char Self[]
Definition Logger.hpp:85
static constexpr char Shared[]
Definition Logger.hpp:89
static constexpr char Unique[]
Definition Logger.hpp:90
static constexpr char Self[]
Definition Logger.hpp:83