entity
Entity/Component System for C++
entity.hpp
Go to the documentation of this file.
1 // ****************************************************************************
2 // entity/entity.hpp
3 //
4 // Represents an entity which is made up of components.
5 // Essentially an ID but not incrementable, etc.
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_ENTITY_H_INCLUDED_
15 #define _ENTITY_ENTITY_H_INCLUDED_
16 
17 #include <boost/operators.hpp>
18 #include <algorithm>
19 #include <functional>
20 #include <memory>
21 
22 #include "entity/config.hpp" // IWYU pragma: keep
23 #include "entity/entity_index.hpp"
24 
25 // ----------------------------------------------------------------------------
26 //
27 namespace entity
28 {
29  // ------------------------------------------------------------------------
30  //
31  class entity : boost::totally_ordered<entity>
32  {
33  public:
34 
36  {
37  return idx_;
38  }
39 
40  bool operator==(entity const& rhs) const
41  {
42  return index() == rhs.index();
43  }
44 
45  bool operator<(entity const& rhs) const
46  {
47  return index() < rhs.index();
48  }
49 
50  private:
51 
52  friend entity make_entity(entity_index_t) BOOST_NOEXCEPT_OR_NOTHROW;
53 
54  // Only make_entity can construct entities.
55  // Can consider impicit conversion here, but
56  // perfer to be conservative at first.
57  explicit entity(entity_index_t idx)
58  : idx_(idx)
59  {}
60 
61  entity_index_t idx_;
62  };
63 
64  inline entity make_entity(entity_index_t idx) BOOST_NOEXCEPT_OR_NOTHROW
65  {
66  return entity(idx);
67  }
68 
69  // ------------------------------------------------------------------------
70  //
71  class unique_entity : boost::totally_ordered<unique_entity>
72  {
73  public:
74 
76  {}
77 
78  entity get() const
79  {
80  return make_entity(*ref_);
81  }
82 
83  bool operator==(unique_entity const& rhs) const
84  {
85  return get() == rhs.get();
86  }
87 
88  bool operator<(unique_entity const& rhs) const
89  {
90  return get() < rhs.get();
91  }
92 
93  void clear()
94  {
95  ref_ = nullptr;
96  }
97 
98  private:
99 
100  friend class entity_pool;
101  friend class shared_entity;
102  friend void swap(unique_entity&, unique_entity&);
103 
104  typedef std::unique_ptr<
105  const entity_index_t,
106  std::function<void(entity_index_t const*)>
107  > const_ref_type;
108 
109  typedef std::unique_ptr<
111  std::function<void(entity_index_t const*)>
112  > ref_type;
113 
114  unique_entity(const_ref_type ref)
115  : ref_(std::move(ref))
116  {}
117 
118  unique_entity(ref_type ref)
119  : ref_(std::move(ref))
120  {}
121 
122  const_ref_type ref_;
123  };
124 
125  inline void swap(unique_entity& a, unique_entity& b)
126  {
127  std::swap(a.ref_, b.ref_);
128  }
129 
130  class shared_entity : boost::totally_ordered<shared_entity>
131  {
132  public:
133 
135  {}
136 
138  : ref_(std::move(ref.ref_))
139  {}
140 
141  entity get() const
142  {
143  return make_entity(*ref_);
144  }
145 
146  bool operator==(shared_entity const& rhs) const
147  {
148  return get() == rhs.get();
149  }
150 
151  bool operator<(shared_entity const& rhs) const
152  {
153  return get() < rhs.get();
154  }
155 
156  void clear()
157  {
158  ref_ = nullptr;
159  }
160 
161  private:
162 
163  friend class entity_pool;
164  friend class weak_entity;
165  friend void swap(shared_entity&, shared_entity&);
166 
167  typedef std::shared_ptr<const entity_index_t> const_ref_type;
168  typedef std::shared_ptr<entity_index_t> ref_type;
169 
170  shared_entity(const_ref_type ref)
171  : ref_(std::move(ref))
172  {}
173 
174  shared_entity(ref_type ref)
175  : ref_(std::move(ref))
176  {}
177 
178  const_ref_type ref_;
179  };
180 
181  inline void swap(shared_entity& a, shared_entity& b)
182  {
183  std::swap(a.ref_, b.ref_);
184  }
185 
186  // ------------------------------------------------------------------------
187  //
188  class weak_entity : boost::totally_ordered<weak_entity>
189  {
190  public:
191 
193  : ref_(ref.ref_)
194  {}
195 
197  {
198  return ref_.lock();
199  }
200 
201  bool operator==(weak_entity const& rhs) const
202  {
203  return ref_.lock() == rhs.ref_.lock();
204  }
205 
206  bool operator<(weak_entity const& rhs) const
207  {
208  return ref_.lock() < rhs.ref_.lock();
209  }
210 
211  private:
212 
213  friend class shared_entity;
214  std::weak_ptr<const entity_index_t> ref_;
215  };
216 }
217 
218 #endif // _ENTITY_ENTITY_H_INCLUDED_
friend void swap(unique_entity &, unique_entity &)
Definition: entity.hpp:125
shared_entity(unique_entity &&ref)
Definition: entity.hpp:137
bool operator==(entity const &rhs) const
Definition: entity.hpp:40
entity get() const
Definition: entity.hpp:78
bool operator<(weak_entity const &rhs) const
Definition: entity.hpp:206
bool operator==(unique_entity const &rhs) const
Definition: entity.hpp:83
void swap(shared_entity &a, shared_entity &b)
Definition: entity.hpp:181
friend void swap(shared_entity &, shared_entity &)
Definition: entity.hpp:181
STL namespace.
bool operator<(unique_entity const &rhs) const
Definition: entity.hpp:88
weak_entity(shared_entity const &ref)
Definition: entity.hpp:192
void swap(unique_entity &a, unique_entity &b)
Definition: entity.hpp:125
bool operator==(weak_entity const &rhs) const
Definition: entity.hpp:201
entity_index_t index() const
Definition: entity.hpp:35
std::size_t entity_index_t
bool operator<(shared_entity const &rhs) const
Definition: entity.hpp:151
entity make_entity(entity_index_t idx) BOOST_NOEXCEPT_OR_NOTHROW
Definition: entity.hpp:64
bool operator==(shared_entity const &rhs) const
Definition: entity.hpp:146
entity get() const
Definition: entity.hpp:141
bool operator<(entity const &rhs) const
Definition: entity.hpp:45
shared_entity lock() const
Definition: entity.hpp:196