Simulant  21.12-194
A portable game engine for Windows, OSX, Linux, Dreamcast, and PSP
manual_object.h
1 #pragma once
2 
3 #include "../signals/signal.h"
4 
5 namespace smlt {
6 
7 typedef sig::signal<void ()> DestroyedSignal;
8 
10  // Fired when destroy() is called
11  DEFINE_SIGNAL(DestroyedSignal, signal_destroyed);
12 
13  template<typename PolyType, typename IDType, typename T, typename ...SubTypes>
14  friend class StageNodeManager;
15 
16 protected:
17  bool destroyed_ = false;
18 
19 public:
20  virtual ~DestroyableObject() {}
21 
22  virtual void destroy() = 0;
23  virtual void destroy_immediately() = 0;
24 
25  bool is_destroyed() const {
26  return destroyed_;
27  }
28 };
29 
30 class Stage;
31 
32 template<typename T, typename Owner>
33 class TypedDestroyableObject : public virtual DestroyableObject {
34 public:
35  friend Owner;
36 
37  TypedDestroyableObject(Owner* owner):
38  owner_(owner) {}
39 
40  virtual ~TypedDestroyableObject() {
41  if(!destroyed_) {
42  signal_destroyed()();
43  }
44  }
45 
46  void destroy() override {
47  if(!destroyed_) {
48  signal_destroyed()();
49  destroyed_ = true;
50  owner_->destroy_object((T*) this);
51  }
52  }
53 
54  void destroy_immediately() override {
55  if(!destroyed_) {
56  signal_destroyed()();
57  destroyed_ = true;
58  owner_->destroy_object_immediately((T*) this);
59  }
60  }
61 
62 private:
63  Owner* owner_ = nullptr;
64 };
65 
66 }
smlt::Stage
Definition: stage.h:92
smlt
Definition: animation.cpp:25
smlt::TypedDestroyableObject
Definition: manual_object.h:33
smlt::StageNodeManager
Definition: stage_node_manager.h:24
smlt::DestroyableObject
Definition: manual_object.h:9
smlt::sig::signal< void()>