entity
Entity/Component System for C++
destruction_queue.hpp
Go to the documentation of this file.
1 // ****************************************************************************
2 // entity/component/destruction_queue.h
3 //
4 // Represents a way to queue component destruction in order to
5 // reduce creation complexity from O(m*nlog(n)) to O(m+n)
6 //
7 // Copyright Chris Glover 2014-2015
8 //
9 // Distributed under the Boost Software License, Version 1.0.
10 // See accompanying file LICENSE_1_0.txt or copy at
11 // http://www.boost.org/LICENSE_1_0.txt
12 //
13 // ****************************************************************************
14 #ifndef _ENTITY_COMPONENT_DESTRUCTIONQUEUE_H_INCLUDED_
15 #define _ENTITY_COMPONENT_DESTRUCTIONQUEUE_H_INCLUDED_
16 
17 #include <algorithm>
18 #include <functional>
19 #include <vector>
20 
21 #include "entity/config.hpp" // IWYU pragma: keep
22 #include "entity/entity.hpp"
23 
24 // ----------------------------------------------------------------------------
25 //
26 namespace entity { namespace component
27 {
28  template<typename ComponentPool>
29  class destruction_queue
30  {
31  public:
32 
33  typedef typename ComponentPool::type type;
34 
35  destruction_queue(ComponentPool& p)
36  : pool_(p)
37  {}
38 
40  {
41  flush();
42  }
43 
44  void push(weak_entity e)
45  {
46  destroyed_.push_back(e);
47  }
48 
49  void flush()
50  {
51  std::sort(destroyed_.begin(), destroyed_.end(), std::greater<weak_entity>());
52  pool_.destroy_range(destroyed_.begin(), destroyed_.end());
53  clear();
54  }
55 
56  void clear()
57  {
58  destroyed_.clear();
59  }
60 
61  private:
62 
63  // No copying.
66 
67  std::vector<weak_entity> destroyed_;
68  ComponentPool& pool_;
69  };
70 } } // namespace entity { namespace component {
71 
72 #endif // _ENTITY_COMPONENT_DESTRUCTIONQUEUE_H_INCLUDED_