entity
Entity/Component System for C++
creation_queue.hpp
Go to the documentation of this file.
1 // ****************************************************************************
2 // entity/component/creation_queue.h
3 //
4 // Represents a way to queue component creation 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 #pragma once
15 #ifndef _ENTITY_COMPONENT_CREATIONQUEUE_H_INCLUDED_
16 #define _ENTITY_COMPONENT_CREATIONQUEUE_H_INCLUDED_
17 
18 #include "entity/config.hpp" // IWYU pragma: keep
19 #include "entity/entity.hpp"
20 #include <algorithm>
21 #include <vector>
22 #include <utility>
23 
24 // ----------------------------------------------------------------------------
25 //
26 namespace entity { namespace component
27 {
28  template<typename ComponentPool>
30  {
31  public:
32 
33  typedef typename ComponentPool::type type;
34 
35  creation_queue(ComponentPool& p)
36  : pool_(p)
37  {}
38 
40  {
41  flush();
42  }
43 
44  #if ENTITY_SUPPORT_VARIADICS
45  template<typename... Args>
46  void push(weak_entity e, Args&&... args)
47  {
48  created_.push_back(std::make_pair(e, type(std::forward<Args>(args)...)));
49  }
50  #else
51  void push(weak_entity e, type&& original)
52  {
53  created_.push_back(std::make_pair(e, std::move(original)));
54  }
55  #endif
56 
57  void flush()
58  {
59  std::sort(created_.begin(), created_.end());
60  pool_.create_range(created_.begin(), created_.end());
61  clear();
62  }
63 
64  void clear()
65  {
66  created_.clear();
67  }
68 
69  private:
70 
71  // No copying.
73  creation_queue operator=(creation_queue);
74 
75  std::vector<std::pair<weak_entity, type>> created_;
76  ComponentPool& pool_;
77  };
78 } } // namespace entity { namespace component
79 
80 #endif // _ENTITY_COMPONENT_CREATIONQUEUE_H_INCLUDED_
void push(weak_entity e, Args &&...args)