entity
Entity/Component System for C++
entity_range.hpp
Go to the documentation of this file.
1 // ****************************************************************************
2 // entity/entity_iterator.h
3 //
4 // Iterates over a list of entities and a zipped component
5 // pool as a tuple.
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_ENTITYITERATOR_H_INCLUDED_
15 #define _ENTITY_ENTITYITERATOR_H_INCLUDED_
16 
17 #include <boost/iterator/iterator_facade.hpp>
18 
19 #include "entity/config.hpp" // IWYU pragma: keep
20 #include "entity/entity.hpp"
22 
23 namespace boost {
24 namespace iterators {
25 struct forward_traversal_tag;
26 } // namespace iterators
27 } // namespace boost
28 
29 // ----------------------------------------------------------------------------
30 //
31 namespace entity
32 {
33  // ------------------------------------------------------------------------
34  //
35  template<typename EntityListIterator, typename ZippedComponentWindows>
37  : public boost::iterator_facade<
38  entity_incremental_iterator<EntityListIterator, ZippedComponentWindows>
39  , ZippedComponentWindows
40  , boost::forward_traversal_tag
41  , ZippedComponentWindows
42  >
43  {
44  public:
45 
46  entity_incremental_iterator(EntityListIterator begin, EntityListIterator end, ZippedComponentWindows windows)
47  : entity_iter_(begin)
48  , end_(end)
49  , windows_(windows)
50  {}
51 
53  {
54  return *entity_iter_;
55  }
56 
57  private:
58 
60 
61  void increment()
62  {
63  ++entity_iter_;
64  if(entity_iter_ != end_)
65  {
66  windows_.increment(*entity_iter_);
67  }
68  }
69 
70  bool equal(entity_incremental_iterator const& other) const
71  {
72  return entity_iter_ == other.entity_iter_;
73  }
74 
75  ZippedComponentWindows dereference() const
76  {
77  return windows_;
78  }
79 
80  EntityListIterator entity_iter_;
81  EntityListIterator end_;
82  ZippedComponentWindows windows_;
83 
84  };
85 
86  // ------------------------------------------------------------------------
87  //
88  template<typename EntityListIterator, typename ZippedComponentWindows>
90  : public boost::iterator_facade<
91  entity_skipping_iterator<EntityListIterator, ZippedComponentWindows>
92  , ZippedComponentWindows
93  , boost::forward_traversal_tag
94  , ZippedComponentWindows
95  >
96  {
97  public:
98 
99  entity_skipping_iterator(EntityListIterator begin, EntityListIterator end, ZippedComponentWindows windows)
100  : entity_iter_(begin)
101  , end_(end)
102  , windows_(windows)
103  {
104  if(entity_iter_ != end_)
105  {
106  windows_.advance(*entity_iter_);
107  }
108  }
109 
111  {
112  return *entity_iter_;
113  }
114 
115  private:
116 
118 
119  void increment()
120  {
121  ++entity_iter_;
122  if(entity_iter_ != end_)
123  {
124  windows_.advance(*entity_iter_);
125  }
126  }
127 
128  bool equal(entity_skipping_iterator const& other) const
129  {
130  return entity_iter_ == other.entity_iter_;
131  }
132 
133  ZippedComponentWindows dereference() const
134  {
135  return windows_;
136  }
137 
138  EntityListIterator entity_iter_;
139  EntityListIterator end_;
140  ZippedComponentWindows windows_;
141 
142  };
143 
144  // ------------------------------------------------------------------------
145  //
146  template<typename Iterator, typename EndIterator = Iterator>
148  {
149  public:
150 
151  entity_range(Iterator begin, EndIterator end)
152  : begin_(std::move(begin))
153  , end_(std::move(end))
154  {}
155 
156  Iterator begin()
157  {
158  return begin_;
159  }
160 
161  EndIterator end()
162  {
163  return end_;
164  }
165 
166  private:
167 
168  Iterator begin_;
169  EndIterator end_;
170  };
171 
172  template<typename Iterator, typename EndIterator>
174  {
175  return r.begin();
176  }
177 
178  template<typename Iterator, typename EndIterator>
180  {
181  return r.end();
182  }
183 
184  // ------------------------------------------------------------------------
185  //
186  template<typename EntityList, typename ZippedComponentWindows>
187  auto make_entity_begin(iterator_traits::is_skipping_tag, EntityList const& entities, ZippedComponentWindows windows) -> entity_skipping_iterator<
188  decltype(begin(entities)),
189  ZippedComponentWindows>
190  {
192  decltype(begin(entities)),
193  ZippedComponentWindows
194  >(begin(entities), end(entities), std::move(windows));
195  }
196 
197  template<typename EntityList, typename ZippedComponentWindows>
198  auto make_entity_end(iterator_traits::is_skipping_tag, EntityList const& entities, ZippedComponentWindows windows) -> entity_skipping_iterator<
199  decltype(end(entities)),
200  ZippedComponentWindows>
201  {
203  decltype(begin(entities)),
204  ZippedComponentWindows
205  >(end(entities), end(entities), std::move(windows));
206  }
207 
208  // --------------------------------------------------------------------
209  //
210  template<typename EntityList, typename ZippedComponentWindows>
211  auto make_entity_begin(iterator_traits::is_incremental_tag, EntityList const& entities, ZippedComponentWindows windows) -> entity_incremental_iterator<
212  decltype(entities.begin()),
213  ZippedComponentWindows>
214  {
216  decltype(entities.begin()),
217  ZippedComponentWindows
218  >(entities.begin(), entities.end(), std::move(windows));
219  }
220 
221  template<typename EntityList, typename ZippedComponentWindows>
222  auto make_entity_end(iterator_traits::is_incremental_tag, EntityList& entities, ZippedComponentWindows windows) -> entity_incremental_iterator<
223  decltype(entities.end()),
224  ZippedComponentWindows>
225  {
227  decltype(entities.begin()),
228  ZippedComponentWindows
229  >(entities.end(), entities.end(), std::move(windows));
230  }
231 
232  // --------------------------------------------------------------------
233  //
234  template<typename EntityList, typename ZippedComponentWindows>
235  auto make_entity_begin(EntityList const& list, ZippedComponentWindows windows) -> decltype(make_entity_begin(iterator_traits::entity_list_is_incremental<EntityList>(), list, windows))
236  {
237  return make_entity_begin(
239  list,
240  std::move(windows)
241  );
242  }
243 
244  template<typename EntityList, typename ZippedComponentWindows>
245  auto make_entity_end(EntityList const& list, ZippedComponentWindows windows) -> decltype(make_entity_end(iterator_traits::entity_list_is_incremental<EntityList>(), list, windows))
246  {
247  return make_entity_end(
249  list,
250  std::move(windows)
251  );
252  }
253 
254  // --------------------------------------------------------------------
255  //
256  template<typename EntityList, typename ZippedComponentWindows>
258  {
259  return entity_range<
260  decltype(make_entity_begin(list, windows)),
261  decltype(make_entity_end(list, windows))>(
262  make_entity_begin(list, windows),
263  make_entity_end(list, windows)
264  )
265  ;
266  }
267 }
268 
269 #endif // _ENTITY_ENTITYITERATOR_H_INCLUDED_
entity_pool::iterator end(entity_pool const &p)
auto make_entity_begin(iterator_traits::is_skipping_tag, EntityList const &entities, ZippedComponentWindows windows) -> entity_skipping_iterator< decltype(begin(entities)), ZippedComponentWindows >
STL namespace.
auto make_entity_end(iterator_traits::is_skipping_tag, EntityList const &entities, ZippedComponentWindows windows) -> entity_skipping_iterator< decltype(end(entities)), ZippedComponentWindows >
entity_range(Iterator begin, EndIterator end)
entity_pool::iterator begin(entity_pool const &p)
auto make_entity_range(EntityList const &list, ZippedComponentWindows windows) -> entity_range< decltype(make_entity_begin(list, windows)), decltype(make_entity_end(list, windows))>
friend class boost::iterator_core_access
entity_skipping_iterator(EntityListIterator begin, EntityListIterator end, ZippedComponentWindows windows)
friend class boost::iterator_core_access
entity_incremental_iterator(EntityListIterator begin, EntityListIterator end, ZippedComponentWindows windows)