/
scene.hpp
372 lines (344 loc) · 10 KB
/
scene.hpp
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#pragma once
#include <array>
#include <cassert>
#include <functional>
#include <memory>
#include <optional>
#include <set>
#include <tuple>
#include <type_traits>
#include <Node.hpp>
#include "packed_scene.hpp"
#include "tree.hpp"
#if !defined(GDN_ASSERT)
#define GDN_ASSERT(x) assert(x)
#endif
namespace gdn {
namespace scene {
using node_deleter_t = std::function<void(godot::Node*)>;
static constexpr auto default_delete = [](godot::Node* node) {
node->free();
};
struct acquire_t {
godot::Node* node;
};
template <typename... Ts>
struct make_scene_t {
std::tuple<Ts...> args;
};
template <typename... Ts>
struct make_node_t {
std::tuple<Ts...> args;
};
template <typename MkNode, typename MkScene>
struct make_t {
node_deleter_t deleter;
MkNode node;
MkScene scene;
};
template <typename NodeType, typename... Ts>
struct base_open_t {
NodeType* node;
make_scene_t<Ts...> args;
};
template <typename... Ts>
struct open_singleton_t {
make_scene_t<Ts...> args;
};
template <typename NodeType, typename... Ts> struct open_t : public base_open_t<NodeType, Ts...> {};
template <typename NodeType, typename... Ts> struct reopen_t : public base_open_t<NodeType, Ts...> {};
inline auto acquire(godot::Node* node) -> acquire_t {
return acquire_t{node};
}
template <typename... Ts>
auto make_scene(Ts... ts) -> make_scene_t<Ts...> {
return make_scene_t<Ts...>{ {std::move(ts)...}};
}
template <typename... Ts>
auto make_node(Ts... ts) -> make_node_t<Ts...> {
return make_node_t<Ts...>{ {std::move(ts)...}};
}
template <typename MakeNode, typename MakeScene>
auto make(node_deleter_t deleter, MakeNode node, MakeScene scene) -> make_t<MakeNode, MakeScene> {
return make_t<MakeNode, MakeScene>{std::move(deleter), std::move(node), std::move(scene)};
}
template <typename MakeNode, typename MakeScene>
auto make(MakeNode node, MakeScene scene) -> make_t<MakeNode, MakeScene> {
return make(default_delete, std::move(node), std::move(scene));
}
template <typename NodeType, typename... Ts>
auto open(NodeType* node, Ts... ts) -> open_t<NodeType, Ts...> {
return open_t<NodeType, Ts...>{ node, make_scene(std::move(ts)...)};
}
template <typename... Ts>
auto open_singleton(Ts... ts) -> open_singleton_t<Ts...> {
return open_singleton_t<Ts...>{ make_scene(std::move(ts)...)};
}
template <typename NodeType, typename... Ts>
auto reopen(NodeType* node, Ts... ts) -> reopen_t<NodeType, Ts...> {
return reopen_t<NodeType, Ts...>{ node, make_scene(std::move(ts)...)};
}
} // scene
template <typename T> struct View;
template <typename T> struct Script;
template <typename T, typename ScriptType, typename NodeType = godot::Node>
struct Scene {
using node_type = NodeType;
using script_type = ScriptType;
struct make{node_type* node; scene::node_deleter_t deleter;};
struct open{node_type* node;};
node_type* const node{nullptr};
Scene(const Scene&) = delete;
Scene(Scene&& rhs) = delete;
auto operator=(const Scene&) -> Scene& = delete;
auto operator=(Scene&& rhs) -> Scene& = delete;
Scene(open o)
: node{o.node}
, root{godot::Object::cast_to<script_type>(o.node)}
, owning_{false}
{
}
Scene(make m)
: node{m.node}
, root{godot::Object::cast_to<script_type>(m.node)}
, owning_{true}
, deleter_{std::move(m.deleter)}
{
}
operator bool() const { return root; }
template <typename Fn, typename... Args>
auto root_invoke(Fn&& fn, Args&&... args) -> void {
if (!root) {
return;
}
std::invoke(std::forward<Fn>(fn), *root, std::forward<Args>(args)...);
}
protected:
script_type* root{nullptr};
private:
// If false, this is a non-owning scene (the node
// won't be freed when the scene is destroyed.)
bool owning_{false};
scene::node_deleter_t deleter_;
friend struct View<T>;
friend struct Script<T>;
};
template <typename UserScene>
struct View {
using node_type = typename UserScene::node_type;
using make_scene = typename UserScene::make;
using open_scene = typename UserScene::open;
View() = default;
// Acquire a view for an already created scene.
View(scene::acquire_t a) {
script_ = &UserScene::acquire(a.node);
ref();
}
template <typename MkNode, typename MkScene>
// Create the node and create the scene for it.
// The created scene owns the node and will free
// it automatically when all views are destroyed.
View(scene::make_t<MkNode, MkScene>&& make) {
auto& node{create(std::move(make.node))};
script_ = &UserScene::acquire(&node);
GDN_ASSERT (!script_->scene);
create(make_scene{&node, std::move(make.deleter)}, std::move(make.scene));
ref();
}
template <typename NodeType, typename... SceneArgs>
// Create the scene for an already existing node.
// The created scene does not own the node.
// The node must not already have a scene associated
// with it.
View(scene::open_t<NodeType, SceneArgs...>&& open) {
script_ = &UserScene::acquire(open.node);
GDN_ASSERT (!script_->scene);
create(open_scene{open.node}, std::move(open.args));
ref();
}
template <typename NodeType, typename... SceneArgs>
// Create the scene for an already existing node.
// The created scene does not own the node.
// If the node already has a scene associated with
// it then it is closed and the new scene takes its
// place.
View(scene::reopen_t<NodeType, SceneArgs...>&& open) {
script_ = &UserScene::acquire(open.node);
if (script_->scene) {
script_->reset();
}
create(open_scene{open.node}, std::move(open.args));
ref();
}
~View() {
unref();
}
View(const View& rhs)
: script_{rhs.script_}
{
ref();
}
View(View&& rhs) noexcept
: script_{rhs.script_}
{
ref();
rhs.unref();
}
View& operator=(const View& rhs) {
unref();
script_ = rhs.script_;
ref();
return *this;
}
View& operator=(View&& rhs) noexcept {
unref();
script_ = rhs.script_;
ref();
rhs.unref();
return *this;
}
auto ref_count() const -> size_t {
if (!script_) {
return 0;
}
return script_->ref_count();
}
auto& scene() { return *script_->scene; }
auto& scene() const { return *script_->scene; }
auto operator->() const -> UserScene* { return script_->scene; }
auto operator*() -> UserScene& { return *script_->scene; }
auto operator*() const -> const UserScene& { return *script_->scene; }
operator bool() const { return bool(script_); }
operator node_type*() const { return script_->scene->node; }
private:
auto ref() -> void {
if (script_) {
script_->ref(this);
}
}
auto unref() -> void {
if (script_) {
script_->unref(this);
script_ = nullptr;
}
}
template <typename Mode, typename... Ts>
// Create the scene in either "Make" mode or "Open" mode
auto create(Mode&& mode, scene::make_scene_t<Ts...>&& make) -> void {
auto fn = [mode, script = script_](auto&&...args) {
script->construct_scene(mode, std::move(args)...);
};
std::apply(std::move(fn), std::move(make.args));
}
template <typename... Ts>
// Crate the node
auto create(scene::make_node_t<Ts...>&& make) const -> node_type& {
auto fn = [](auto&&...args) -> decltype(auto) {
return UserScene::make_node(std::move(args)...);
};
return std::apply(std::move(fn), std::move(make.args));
}
Script<UserScene>* script_{};
friend struct Script<UserScene>;
};
template <typename UserScene>
struct Script {
private:
template <typename T, size_t N>
struct alignas(alignof(T)) aligned_array_t : public std::array<T, N> {};
using storage_t = aligned_array_t<std::byte, sizeof(UserScene)>;
public:
~Script() {
if (scene) {
scene->root = nullptr;
scene->owning_ = false;
}
for (const auto ref : refs_) {
ref->script_ = nullptr;
}
destroy_scene();
}
template <typename Fn, typename... Args>
auto scene_invoke(Fn&& fn, Args&&... args) -> void {
if (!scene) {
return;
}
std::invoke(std::forward<Fn>(fn), *scene, std::forward<Args>(args)...);
}
auto ref_count() const { return refs_.size(); }
UserScene* scene{nullptr};
private:
template <typename... Args>
auto construct_scene(Args&&... args) -> UserScene* {
::new(std::addressof(scene_storage_)) UserScene{std::forward<Args>(args)...};
scene = reinterpret_cast<UserScene*>(std::addressof(scene_storage_));
return scene;
}
auto ref(View<UserScene>* ref) -> void {
GDN_ASSERT (scene);
refs_.insert(ref);
}
auto unref(View<UserScene>* ref) -> void {
GDN_ASSERT (scene);
refs_.erase(ref);
if (refs_.empty()) {
reset();
}
}
auto reset() -> void {
godot::Node* node_to_free{scene->owning_ ? scene->node : nullptr};
if (node_to_free) {
auto deleter{scene->deleter_};
destroy_scene();
deleter(node_to_free);
return;
}
for (const auto ref : refs_) {
ref->script_ = nullptr;
}
destroy_scene();
}
auto destroy_scene() -> void {
if (scene) {
scene->~UserScene();
scene = nullptr;
}
}
std::set<View<UserScene>*> refs_;
storage_t scene_storage_;
friend struct View<UserScene>;
};
template <typename UserScene>
struct SingletonView {
using node_type = typename UserScene::node_type;
SingletonView() = default;
template <typename... Ts>
SingletonView(scene::open_singleton_t<Ts...> open)
: view_{scene::reopen_t<node_type, Ts...>{&node(), std::move(open.args)}}
{
}
auto& scene() { return view_.scene(); }
auto& scene() const { return view_.scene(); }
auto operator*() -> UserScene& { return *view_; }
auto operator*() const -> const UserScene& { return *view_; }
auto operator->() const -> UserScene* { return view_.operator->(); }
operator bool() const { return bool(view_); }
operator gdn::View<UserScene>() const { return view_; }
static auto& node() { return UserScene::singleton(); }
private:
View<UserScene> view_;
};
template <typename UserScene>
auto acquire(godot::Node* node) -> gdn::Script<UserScene>& {
return *reinterpret_cast<gdn::Script<UserScene>*>(godot::Object::cast_to<typename UserScene::script_type>(node));
}
template <typename NodeType, typename Body>
struct ViewWrapper {
ViewWrapper() = default;
template <typename... Args>
ViewWrapper(godot::Node* node, Args&&... args) : body_{std::make_unique<Body>(godot::Object::cast_to<NodeType>(node), std::forward<Args>(args)...)} {}
operator bool() const { return bool(body_); }
auto get_node() const { return body_->node; }
std::unique_ptr<Body> body_;
};
} // gdn