-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathComponentModule.h
More file actions
68 lines (60 loc) · 2 KB
/
Copy pathComponentModule.h
File metadata and controls
68 lines (60 loc) · 2 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#pragma once
#include "Export.h"
#include "Component/ComponentType.hpp"
#include "Core/Singleton.hpp"
#include "Core/SContainer.hpp"
#include "Core/Reflection.hpp"
#include "Core/Logger.h"
#include <string>
#include <string_view>
#include <unordered_map>
#include <memory>
#include <vector>
#define REGISTER_COMPONENT(component) \
ComponentModule::GetInstance()->RegisterComponent<component>(#component)
namespace sh::game
{
class ComponentModule : public core::Singleton<ComponentModule>
{
friend core::Singleton<ComponentModule>;
public:
struct ComponentInfo
{
std::string name;
const core::reflection::STypeInfo& type;
std::unique_ptr<IComponentType> componentType;
};
private:
std::unordered_map<std::string, std::unique_ptr<IComponentType>> components;
std::vector<ComponentInfo> waitingComponents;
protected:
ComponentModule() = default;
public:
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*;
SH_GAME_API void RegisterWaitingComponents();
SH_GAME_API auto GetWaitingComponents() const -> const std::vector<ComponentInfo>&;
SH_GAME_API void DestroyComponent(const std::string& name);
SH_GAME_API void DestroyComponent(const std::string& name, const std::string& group);
};
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{ std::move(group) };
if (GetComponent(newName) == nullptr)
{
SH_INFO_FORMAT("Register component: {}", newName);
waitingComponents.push_back({ newName, T::GetStaticType(), std::make_unique<ComponentType<T>>(newName) });
}
}
}