forked from Shell4026/ShellEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComponentModule.h
More file actions
47 lines (39 loc) · 1.27 KB
/
Copy pathComponentModule.h
File metadata and controls
47 lines (39 loc) · 1.27 KB
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
#pragma once
#include "Export.h"
#include "Component/ComponentType.hpp"
#include "Core/Singleton.hpp"
#include "Core/SContainer.hpp"
#include <string>
#include <string_view>
#include <unordered_map>
#include <memory>
#define REGISTER_COMPONENT(component) \
ComponentModule::GetInstance()->RegisterComponent<component>(#component)
namespace sh::game
{
class ComponentModule : public core::Singleton<ComponentModule>
{
private:
std::unordered_map<std::string, std::unique_ptr<IComponentType>> components;
public:
ComponentModule() = default;
template<typename T>
void RegisterComponent(std::string_view name, std::string&& group);
SH_GAME_API auto GetComponents() -> std::unordered_map<std::string, std::unique_ptr<IComponentType>>&;
SH_GAME_API auto GetComponents() const -> const std::unordered_map<std::string, std::unique_ptr<IComponentType>>&;
SH_GAME_API auto GetComponent(std::string_view name) const -> IComponentType*;
};
template<typename T>
inline void ComponentModule::RegisterComponent(std::string_view name, std::string&& group)
{
if (group.size() == 0)
group = name;
else
{
group.push_back('/');
group += name;
}
std::string newName{ group };
components.insert({ std::move(newName), std::make_unique<ComponentType<T>>(newName) });
}
}