Ophidian
 All Classes Namespaces Functions
entity_system.h
1 /*
2  * Copyright 2016 Ophidian
3 Licensed to the Apache Software Foundation (ASF) under one
4 or more contributor license agreements. See the NOTICE file
5 distributed with this work for additional information
6 regarding copyright ownership. The ASF licenses this file
7 to you under the Apache License, Version 2.0 (the
8 "License"); you may not use this file except in compliance
9 with the License. You may obtain a copy of the License at
10 
11  http://www.apache.org/licenses/LICENSE-2.0
12 
13 Unless required by applicable law or agreed to in writing,
14 software distributed under the License is distributed on an
15 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 KIND, either express or implied. See the License for the
17 specific language governing permissions and limitations
18 under the License.
19  */
20 
21 #ifndef OPHIDIAN_SRC_ENTITY_SYSTEM_ENTITY_SYSTEM_H
22 #define OPHIDIAN_SRC_ENTITY_SYSTEM_ENTITY_SYSTEM_H
23 
24 #include "entity.h"
25 #include <vector>
26 #include <unordered_set>
27 #include <assert.h>
28 
29 namespace ophidian{
30 namespace entity_system{
31 
32 using entities_storage = typename std::vector<entity>;
33 using entities_index_storage = typename std::vector<entity_index>;
34 class property;
35 
37 /*
38  * This class describes an entity system, which stores all its entities and pointers to the properties associated to them.
39  */
41  entities_storage m_entities;
42  entities_index_storage m_id_to_index;
43 
44  std::unordered_set<property *> m_associated_properties;
45 
46  entity m_next;
47  std::size_t m_last_prealloc_qnt;
48 
49  inline void m_live_entity_check(entity_index e_index) const {
50  if(e_index == invalid_entity_index)
51  throw std::out_of_range("lookup::_invalid_entity");
52  }
53 
54 public:
55 
57 
60  entity_system() : m_next(0), m_last_prealloc_qnt(0){
61 
62  }
63 
65 
69  entity_system(std::size_t qnt) : m_entities(qnt), m_id_to_index(qnt), m_next(0), m_last_prealloc_qnt(0){
70 
71  }
72 
73  ~entity_system(){
74 
75  }
76 
78 
82  std::size_t size() const { return m_entities.size(); }
83 
85 
89  bool empty() const { return m_entities.empty(); }
90 
92 
97  entities_storage::const_iterator begin() const { return m_entities.begin(); }
98 
100 
104  entities_storage::const_iterator end() const { return m_entities.end(); }
105 
107 
111  const entities_storage & entities() const { return m_entities; }
112 
114 
119  void destroy( entity e );
120 
122 
127  entity create();
128 
130 
134  void preallocate(std::size_t qnt);
135 
137 
142 
144 
149  inline entity_index lookup(entity e) const {
150  entity_index e_index = m_id_to_index.at(e);
151  m_live_entity_check(e_index);
152  return e_index;
153  }
154 };
155 
156 } /* namespace entity system */
157 } /* namespace ophidian */
158 
159 #endif //OPHIDIAN_SRC_ENTITY_SYSTEM_ENTITY_SYSTEM_H
entity_system(std::size_t qnt)
Constructor.
Definition: entity_system.h:69
void preallocate(std::size_t qnt)
Preallocate method.
Definition: entity_system.cpp:53
bool empty() const
Returns if the system is empty.
Definition: entity_system.h:89
const entities_storage & entities() const
Entities getter.
Definition: entity_system.h:111
entity_system()
Constructor.
Definition: entity_system.h:60
entities_storage::const_iterator begin() const
Begin iterator.
Definition: entity_system.h:97
void destroy(entity e)
Destroy an entity.
Definition: entity_system.cpp:27
entity create()
Creates an entity.
Definition: entity_system.cpp:42
Property class.
Definition: property.h:33
entity_system class.
Definition: entity_system.h:40
entities_storage::const_iterator end() const
End iterator.
Definition: entity_system.h:104
std::size_t size() const
Returns the size of the system.
Definition: entity_system.h:82
entity_index lookup(entity e) const
Gets the index of an entity.
Definition: entity_system.h:149
void register_property(property *property)
Registers property.
Definition: entity_system.cpp:61