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"
16
17namespace galaxy
18{
19 namespace systems
20 {
21 class System;
22 } // namespace systems
23
24 namespace meta
25 {
26 using SystemStack = std::vector<std::unique_ptr<systems::System>>;
27
31 class SystemFactory final
32 {
33 public:
37 SystemFactory() noexcept = default;
38
42 ~SystemFactory() noexcept = default;
43
51 template<valid_component System>
52 void register_system(const std::string& name);
53
60 void create_system(const std::string& name, SystemStack& stack);
61
62 private:
66 ankerl::unordered_dense::map<std::uint64_t, std::move_only_function<void(SystemStack&)>> m_factory;
67 };
68
69 template<valid_component System>
70 inline void SystemFactory::register_system(const std::string& name)
71 {
72 const auto hash = math::fnv1a(name.c_str());
73 if (!m_factory.contains(hash))
74 {
75 m_factory.emplace(hash, [](SystemStack& stack) {
76 for (auto&& sys : stack)
77 {
78 if (sys->id() == name)
79 {
80 GALAXY_LOG(GALAXY_WARNING, "Tried to create a create a duplicate system '{0}'.", name);
81 return;
82 }
83 }
84
85 stack.emplace_back(std::move(std::make_unique<System>()));
86 });
87 }
88 else
89 {
90 GALAXY_LOG(GALAXY_WARNING, "Tried to create a duplicate register system '{0}'.", name);
91 }
92 }
93 } // namespace meta
94} // namespace galaxy
95
96#endif
#define GALAXY_WARNING
Definition Log.hpp:24
#define GALAXY_LOG(level, msg,...)
Definition Log.hpp:29
Meta factory for creating system objects.
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 definition.
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.
Makes sure a type is a valid component.
Definition Concepts.hpp:87
constexpr bits fnv1a(const char *const str, const bits value=fnv_1a_params< bits >::offset) noexcept
Convert string to hash.
Definition FNV1a.hpp:64
std::vector< std::unique_ptr< systems::System > > SystemStack
Timer.hpp galaxy.
Definition Async.hpp:17
STL namespace.