Vulkan Schnee 0.0.1
High-performance rendering engine
Loading...
Searching...
No Matches
Factory.h
Go to the documentation of this file.
1// Factory.h
2#pragma once
3#include <json.hpp>
4#include <map>
5#include <memory>
6#include <typeindex>
7
8namespace EngineCore {
9 class Serializable; // Forward declaration
10
11 // Forward declare Registrar as a template
12 template<typename T>
13 struct Registrar;
14
15 class Factory {
16 public:
17 using CreateFunction = std::function<std::unique_ptr<Serializable>()>;
18 using JsonFactory = std::function<nlohmann::json()>;
19
20 static void registerType(const std::string& typeName, CreateFunction creator, JsonFactory jsonFactory) {
21 getCreators()[typeName] = creator;
22 getJsonFactories()[typeName] = jsonFactory;
23 }
24
25 // Templated create function
26 template<typename T = Serializable>
27 static std::unique_ptr<T> create(const std::string& typeName) {
28 static_assert(std::is_base_of<Serializable, T>::value, "T must be derived from Serializable");
29
30 auto it = getCreators().find(typeName);
31 if (it != getCreators().end()) {
32 // Create a unique_ptr to Serializable
33 std::unique_ptr<Serializable> basePtr = it->second();
34 if constexpr (std::is_same_v<T, Serializable>) {
35 // If T is Serializable, return as is
36 return std::unique_ptr<T>(basePtr.release());
37 } else {
38 // Attempt to dynamic_cast to T
39 T* derivedPtr = dynamic_cast<T*>(basePtr.get());
40 if (derivedPtr) {
41 // Release ownership from basePtr and assign to derivedPtr
42 basePtr.release();
43 return std::unique_ptr<T>(derivedPtr);
44 } else {
45 // Casting failed
46 return nullptr;
47 }
48 }
49 }
50 return nullptr;
51 }
52
53 static nlohmann::json createJson(const std::string& typeName) {
54 auto it = getJsonFactories().find(typeName);
55 if (it != getJsonFactories().end()) {
56 return it->second();
57 }
58 return nlohmann::json();
59 }
60
61 static const std::vector<std::string>& getRegisteredTypes() {
62 return getTypesList();
63 }
64
65 private:
66 // Static accessor methods for the containers
67 static std::map<std::string, CreateFunction>& getCreators() {
68 static std::map<std::string, CreateFunction> creators;
69 return creators;
70 }
71
72 static std::map<std::string, JsonFactory>& getJsonFactories() {
73 static std::map<std::string, JsonFactory> jsonFactories;
74 return jsonFactories;
75 }
76
77 static std::vector<std::string>& getTypesList() {
78 static std::vector<std::string> registeredTypes;
79 return registeredTypes;
80 }
81
82 // Static method to add type to registered types list
83 static void addRegisteredType(const std::string& typeName) {
84 getTypesList().push_back(typeName);
85 }
86
87 template<typename T>
88 friend class Registrar;
89 };
90
91 // Helper template to get demangled type name
92 template<typename T>
93 std::string getTypeName() {
94 return typeid(T).name();
95 }
96}
static std::unique_ptr< T > create(const std::string &typeName)
Definition Factory.h:27
static void registerType(const std::string &typeName, CreateFunction creator, JsonFactory jsonFactory)
Definition Factory.h:20
static void addRegisteredType(const std::string &typeName)
Definition Factory.h:83
static const std::vector< std::string > & getRegisteredTypes()
Definition Factory.h:61
friend class Registrar
Definition Factory.h:88
static std::map< std::string, JsonFactory > & getJsonFactories()
Definition Factory.h:72
static std::map< std::string, CreateFunction > & getCreators()
Definition Factory.h:67
std::function< std::unique_ptr< Serializable >()> CreateFunction
Definition Factory.h:17
std::function< nlohmann::json()> JsonFactory
Definition Factory.h:18
static nlohmann::json createJson(const std::string &typeName)
Definition Factory.h:53
static std::vector< std::string > & getTypesList()
Definition Factory.h:77
Log category system implementation.
std::string getTypeName()
Definition Factory.h:93