Flecs v3.2
A fast entity component system (ECS) for C & C++
Loading...
Searching...
No Matches
impl.hpp
Go to the documentation of this file.
1
6#pragma once
7
8namespace flecs {
9
10struct snapshot final {
11 explicit snapshot(const world& world)
12 : m_world( world )
13 , m_snapshot( nullptr ) { }
14
15 snapshot(const snapshot& obj)
16 : m_world( obj.m_world )
17 {
18 ecs_iter_t it = ecs_snapshot_iter(obj.m_snapshot);
19 m_snapshot = ecs_snapshot_take_w_iter(&it);
20 }
21
22 snapshot(snapshot&& obj) noexcept
23 : m_world(obj.m_world)
24 , m_snapshot(obj.m_snapshot)
25 {
26 obj.m_snapshot = nullptr;
27 }
28
29 snapshot& operator=(const snapshot& obj) {
30 ecs_assert(m_world.c_ptr() == obj.m_world.c_ptr(), ECS_INVALID_PARAMETER, NULL);
31 ecs_iter_t it = ecs_snapshot_iter(obj.m_snapshot);
32 m_snapshot = ecs_snapshot_take_w_iter(&it);
33 return *this;
34 }
35
36 snapshot& operator=(snapshot&& obj) noexcept {
37 ecs_assert(m_world.c_ptr() == obj.m_world.c_ptr(), ECS_INVALID_PARAMETER, NULL);
38 m_snapshot = obj.m_snapshot;
39 obj.m_snapshot = nullptr;
40 return *this;
41 }
42
43 void take() {
44 if (m_snapshot) {
45 ecs_snapshot_free(m_snapshot);
46 }
47
48 m_snapshot = ecs_snapshot_take(m_world.c_ptr());
49 }
50
51 template <typename F>
52 void take(const F& f) {
53 if (m_snapshot) {
54 ecs_snapshot_free(m_snapshot);
55 }
56
57 ecs_iter_t it = ecs_filter_iter(m_world, f.c_ptr());
58
59 m_snapshot = ecs_snapshot_take_w_iter(&it);
60 }
61
62 void restore() {
63 if (m_snapshot) {
64 ecs_snapshot_restore(m_world.c_ptr(), m_snapshot);
65 m_snapshot = nullptr;
66 }
67 }
68
69 ~snapshot() {
70 if (m_snapshot) {
71 ecs_snapshot_free(m_snapshot);
72 }
73 }
74
75 snapshot_t* c_ptr() const {
76 return m_snapshot;
77 }
78
79private:
80 const world& m_world;
81 snapshot_t *m_snapshot;
82};
83
84// Snapshot mixin implementation
85template <typename... Args>
86inline flecs::snapshot world::snapshot(Args &&... args) const {
87 return flecs::snapshot(*this, FLECS_FWD(args)...);
88}
89
90}
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:351
FLECS_API void ecs_snapshot_free(ecs_snapshot_t *snapshot)
Free snapshot resources.
FLECS_API ecs_iter_t ecs_snapshot_iter(ecs_snapshot_t *snapshot)
Obtain iterator to snapshot data.
FLECS_API void ecs_snapshot_restore(ecs_world_t *world, ecs_snapshot_t *snapshot)
Restore a snapshot.
FLECS_API ecs_snapshot_t * ecs_snapshot_take_w_iter(ecs_iter_t *iter)
Create a filtered snapshot.
FLECS_API ecs_snapshot_t * ecs_snapshot_take(ecs_world_t *world)
Create a snapshot.
flecs::snapshot snapshot(Args &&... args) const
Create a snapshot.
ecs_iter_t ecs_filter_iter(const ecs_world_t *world, const ecs_filter_t *filter)
Return a filter iterator.
The world.
Definition world.hpp:132
world_t * c_ptr() const
Obtain pointer to C world object.
Definition world.hpp:200