Simulant  21.12-574
A portable game engine for Windows, OSX, Linux, Dreamcast, and PSP
stage_node_manager.h
1 #pragma once
2 
3 #include "../generic/containers/polylist.h"
4 
5 #include "actor.h"
6 #include "camera.h"
7 #include "geom.h"
8 #include "light.h"
9 #include "particle_system.h"
10 #include "sprite.h"
11 #include "ui/button.h"
12 #include "ui/image.h"
13 #include "ui/label.h"
14 #include "ui/progress_bar.h"
15 #include "ui/frame.h"
16 #include "ui/keyboard.h"
17 #include "ui/text_entry.h"
18 
19 #define STAGE_NODE_MANAGER_DEBUG 0
20 
21 namespace smlt {
22 
23 
24 template<typename PoolType, typename IDType, typename T, typename ...Subtypes>
26 public:
27  typedef StageNodeManager<PoolType, IDType, T, Subtypes...> this_type;
28 
29  StageNodeManager(PoolType* pool):
30  pool_(pool) {}
31 
32  ~StageNodeManager() {
33  clear();
34  }
35 
36  const PoolType* pool() const {
37  return pool_;
38  }
39 
40  template<typename Derived, typename... Args>
41  Derived* make_as(Args&&... args) {
42  auto pair = pool_->template create<Derived>(
43  std::forward<Args>(args)...
44  );
45 
46  Derived* derived = pair.first;
47  assert(derived);
48 
49  derived->_overwrite_id(IDType(pair.second));
50  derived->_bind_id_pointer(derived);
51 
52  if(!derived->init()) {
53  derived->clean_up();
54  pool_->erase(pool_->find(pair.second));
56  }
57 
58  objects_.push_back(derived);
59 
60  return derived;
61  }
62 
63  template<typename... Args>
64  T* make(Args&&... args) {
65  return make_as<T>(std::forward<Args>(args)...);
66  }
67 
68  bool contains(const IDType& id) const {
69  StageNode* node = (*pool_)[id.value()];
70  return bool(dynamic_cast<T*>(node));
71  }
72 
73  T* get(const IDType& id) const {
74  StageNode* node = (*pool_)[id.value()];
75  T* result = dynamic_cast<T*>(node);
76 #if STAGE_NODE_MANAGER_DEBUG
77  assert((node && result) || (!node && !result));
78 #endif
79  return result;
80  }
81 
82  T* clone(const IDType& id, this_type* target_manager=nullptr) {
83  T* source = get(id);
84  return target_manager->make(*source);
85  }
86 
87  bool destroy(const IDType& id) {
88  auto it = pool_->find(id.value());
89  if(it != pool_->end()) {
90  /* Ensure we fire the destroyed signal */
91  if(!(*it)->destroyed_) {
92  (*it)->signal_destroyed()();
93  (*it)->destroyed_ = true;
94  }
95 
96  queued_for_destruction_.insert(id);
97  return true;
98  } else {
99  return false;
100  }
101  }
102 
103  void clear() {
104  destroy_all();
105  clean_up();
106  }
107 
108  void destroy_all() {
109  destroy_all_next_clean_ = true;
110  }
111 
112  bool destroy_immediately(const IDType& id) {
113  auto it = pool_->find(id.value());
114  if(it != pool_->end()) {
115  StageNode* node = *it;
116 
117  /* Ensure we fire the destroyed signal */
118  if(!(*it)->destroyed_) {
119  (*it)->signal_destroyed()();
120  (*it)->destroyed_ = true;
121  }
122 
123  assert(dynamic_cast<T*>(node));
124  T* a = (T*) node;
125  assert((node && a) || (!node && !a));
126 
127  a->clean_up();
128 
129  objects_.remove(a);
130  pool_->erase(it);
131  queued_for_destruction_.erase(id);
132  return true;
133  }
134  return false;
135  }
136 
137  void clean_up() {
138  if(destroy_all_next_clean_) {
139  destroy_all_next_clean_ = false;
140  for(auto ptr: objects_) {
141  ptr->clean_up();
142  pool_->erase(pool_->find(ptr->id().value()));
143  }
144  objects_.clear();
145  } else {
146  auto queued = queued_for_destruction_;
147  for(auto i: queued) {
148  destroy_immediately(i);
149  }
150  }
151  queued_for_destruction_.clear();
152  }
153 
154  std::size_t size() const {
155  return objects_.size();
156  }
157 
158  typename std::list<T*>::iterator begin() {
159  return objects_.begin();
160  }
161 
162  typename std::list<T*>::iterator end() {
163  return objects_.end();
164  }
165 
166 private:
167  PoolType* pool_ = nullptr;
168  std::list<T*> objects_;
169  std::set<IDType> queued_for_destruction_;
170  bool destroy_all_next_clean_ = false;
171 };
172 
173 }
smlt
Definition: animation.cpp:25
smlt::InstanceInitializationError
Definition: managed.h:29
smlt::StageNodeManager
Definition: stage_node_manager.h:25
smlt::StageNode
Definition: stage_node.h:62