forked from Shell4026/ShellEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSObject.cpp
More file actions
172 lines (158 loc) · 4.66 KB
/
Copy pathSObject.cpp
File metadata and controls
172 lines (158 loc) · 4.66 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "PCH.h"
#include "SObject.h"
#include "GarbageCollection.h"
#include "Observer.hpp"
#include "Util.h"
#include <cstring>
#include <stack>
namespace sh::core
{
SH_CORE_API SObject::SObject() :
gc(GarbageCollection::GetInstance()),
bPendingKill(false), bMark(false),
uuid(UUID::Generate())
{
}
SH_CORE_API SObject::SObject(const SObject& other) :
gc(other.gc),
bPendingKill(other.bPendingKill.load(std::memory_order::memory_order_relaxed)),
bMark(other.bMark),
uuid(other.uuid), name(other.name)
{
}
SH_CORE_API SObject::SObject(SObject&& other) noexcept :
gc(other.gc),
bPendingKill(other.bPendingKill.load(std::memory_order::memory_order_relaxed)),
bMark(other.bMark),
uuid(other.uuid), name(std::move(other.name))
{
other.bPendingKill.store(true, std::memory_order_release);
other.bMark = false;
}
SH_CORE_API SObject::~SObject()
{
onDestroy.Notify(this);
SObjectManager::GetInstance()->UnRegisterSObject(this);
}
SH_CORE_API void SObject::RegisterToManager(SObject* ptr)
{
SObjectManager::GetInstance()->RegisterSObject(ptr);
}
auto SObject::operator new(std::size_t size) -> void*
{
return ::operator new(size);
}
void SObject::operator delete(void* ptr)
{
::operator delete(ptr);
}
SH_CORE_API auto SObject::IsPendingKill() const -> bool
{
return bPendingKill.load(std::memory_order::memory_order_acquire);
}
SH_CORE_API auto SObject::IsMark() const -> bool
{
return bMark;
}
SH_CORE_API void SObject::OnPropertyChanged(const reflection::Property& prop)
{
}
SH_CORE_API void SObject::Destroy()
{
bPendingKill.store(true, std::memory_order::memory_order_release);
gc->RemoveRootSet(this);
}
SH_CORE_API void SObject::OnDestroy()
{
}
SH_CORE_API void SObject::SetName(std::string_view name)
{
this->name = name;
}
SH_CORE_API auto SObject::GetName() const -> const std::string&
{
return name;
}
SH_CORE_API void SObject::SetUUID(const UUID& uuid)
{
SObjectManager::GetInstance()->UnRegisterSObject(this);
this->uuid = uuid;
RegisterToManager(this);
}
SH_CORE_API auto SObject::GetUUID() const -> const UUID&
{
return uuid;
}
SH_CORE_API auto SObject::Serialize() const -> core::Json
{
const reflection::STypeInfo* stypeInfo = &GetType();
core::Json mainJson;
mainJson["version"] = 1;
mainJson["type"] = stypeInfo->name;
mainJson["uuid"] = uuid.ToString();
mainJson["name"] = name;
while (stypeInfo)
{
core::Json json{};
for (auto& [name, prop] :stypeInfo->GetProperties())
{
const reflection::TypeInfo& propType = prop.type;
if (propType == core::reflection::GetType<int>())
core::SerializeProperty(json, name, *prop.Get<int>(this));
else if (propType == core::reflection::GetType<float>())
core::SerializeProperty(json, name, *prop.Get<float>(this));
else if (propType == core::reflection::GetType<std::string>())
core::SerializeProperty(json, name, *prop.Get<std::string>(this));
else if (propType == core::reflection::GetType<bool>())
core::SerializeProperty(json, name, *prop.Get<bool>(this));
else if (prop.isSObjectPointer)
{
SObject* ptr = *prop.Get<SObject*>(this);
if (core::IsValid(ptr))
core::SerializeProperty(json, name, ptr);
}
}
if (!json.empty())
mainJson[stypeInfo->name] = json;
stypeInfo = stypeInfo->GetSuper();
}
return mainJson;
}
SH_CORE_API void SObject::Deserialize(const nlohmann::json& json)
{
const reflection::STypeInfo* stypeInfo = &GetType();
if (json["type"].get<std::string>() != stypeInfo->name)
return;
if (json.contains("uuid"))
{
SetUUID(UUID{ json["uuid"].get<std::string>() });
}
if (json.contains("name"))
SetName(json["name"].get<std::string>());
while (stypeInfo)
{
if (!json.contains(stypeInfo->name))
{
stypeInfo = stypeInfo->GetSuper();
continue;
}
core::Json subJson = json[stypeInfo->name];
for (auto& [name, prop] : stypeInfo->GetProperties())
{
const reflection::TypeInfo& propType = prop.type;
if (propType == core::reflection::GetType<int>())
core::DeserializeProperty(subJson, name, *prop.Get<int>(this));
else if (propType == core::reflection::GetType<float>())
core::DeserializeProperty(subJson, name, *prop.Get<float>(this));
else if (propType == core::reflection::GetType<std::string>())
core::DeserializeProperty(subJson, name, *prop.Get<std::string>(this));
else if (propType == core::reflection::GetType<bool>())
core::DeserializeProperty(subJson, name, *prop.Get<bool>(this));
else if (prop.isSObjectPointer)
core::DeserializeProperty(subJson, name, *prop.Get<SObject*>(this));
OnPropertyChanged(prop);
}
stypeInfo = stypeInfo->GetSuper();
}
}
}