galaxy 1.0.0
Real-Time C++23 Game Programming Framework. Built on data-driven design principles and agile software engineering.
Loading...
Searching...
No Matches
SystemFactory.hpp
Go to the documentation of this file.
1
7
8#ifndef GALAXY_META_SYSTEMFACTORY_HPP_
9#define GALAXY_META_SYSTEMFACTORY_HPP_
10
11#include <ankerl/unordered_dense.h>
12
13#include "galaxy/math/FNV1a.hpp"
17
18namespace galaxy
19{
23 using SystemStack = std::vector<std::shared_ptr<System>>;
24
28 class SystemFactory final
29 {
30 public:
34 SystemFactory() noexcept = default;
35
39 ~SystemFactory() noexcept = default;
40
48 template<meta::valid_component _System>
49 void register_system(const std::string& name);
50
57 void create_system(const std::string& name, SystemStack& stack);
58
59 private:
63 ankerl::unordered_dense::map<std::uint64_t, std::move_only_function<void(SystemStack&)>> m_factory;
64 };
65
66 template<meta::valid_component _System>
67 inline void SystemFactory::register_system(const std::string& name)
68 {
69 const auto hash = math::fnv1a(name.c_str());
70 if (!m_factory.contains(hash))
71 {
72 m_factory.emplace(hash, [name](SystemStack& stack) {
73 for (auto&& sys : stack)
74 {
75 if (sys->id() == name)
76 {
77 GALAXY_LOG(GALAXY_WARN, "Tried to create a create a duplicate system '{0}'.", name);
78 return;
79 }
80 }
81
82 auto ptr = std::make_shared<_System>(name);
83 stack.push_back(std::static_pointer_cast<System>(ptr));
84 });
85 }
86 else
87 {
88 GALAXY_LOG(GALAXY_WARN, "Tried to create a duplicate register system '{0}'.", name);
89 }
90 }
91} // namespace galaxy
92
93#endif
#define GALAXY_LOG(level, msg,...)
Definition Log.hpp:28
#define GALAXY_WARN
Definition Log.hpp:23
Meta factory for creating systems.
SystemFactory() noexcept=default
Constructor.
ankerl::unordered_dense::map< std::uint64_t, std::move_only_function< void(SystemStack &)> > m_factory
Lets us construct a system class from a string representation.
void create_system(const std::string &name, SystemStack &stack)
Create a system using the factory.
void register_system(const std::string &name)
Registers a system into the engine.
constexpr bits fnv1a(const char *const str, const bits value=fnv_1a_params< bits >::offset) noexcept
Convert string to hash.
Definition FNV1a.hpp:64
Animated.cpp galaxy.
Definition Animated.cpp:16
std::vector< std::shared_ptr< System > > SystemStack
System stack typedef.
STL namespace.