Ophidian
 All Classes Namespaces Functions
vector_property.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 SRC_ENTITY_SYSTEM_VECTOR_PROPERTY_H
22 #define SRC_ENTITY_SYSTEM_VECTOR_PROPERTY_H
23 
24 #include <vector>
25 #include "property.h"
26 #include <stdexcept>
27 
28 namespace ophidian{
29 namespace entity_system{
30 
32 
36 template <class T>
37 class vector_property : public property {
38 
39  std::vector<T> m_values;
40 
41  inline void m_range_check(size_t index) const {
42  if(index >= m_values.size())
43  throw std::out_of_range("vector::_M_range_check");
44  }
45 
47 
51  void destroy(entity_index e ) {
52  std::size_t last_index = m_values.size() - 1;
53  m_values[e] = m_values[last_index];
54  m_values.pop_back();
55  }
56 
58 
61  void create( ) {
62  m_values.push_back(T());
63  }
64 
66 
70  void preallocate( std::size_t qnt ) {
71  m_values.reserve(std::max(m_values.capacity(), qnt));
72  }
73 
74 public:
76 
80 
81  }
82  ~vector_property(){
83 
84  }
85 
87 
92  T &operator[](entity_index e) {
93  return m_values[e];
94  }
95 
97 
102  const T &operator[](entity_index e) const {
103  return m_values[e];
104  }
105 
107 
112  T & at(entity_index e) {
113 #ifndef NDEBUG
114  m_range_check(e);
115 #endif
116  return m_values[e];
117  }
118 
120 
125  const T & at(entity_index e) const {
126 #ifndef NDEBUG
127  m_range_check(e);
128 #endif
129  return m_values[e];
130  }
131 
133 
137  typename std::vector<T>::const_iterator begin() const {
138  return m_values.begin();
139  }
140 
142 
146  typename std::vector<T>::const_iterator end() const {
147  return m_values.end();
148  }
149 
150  T* data() {
151  return m_values.data();
152  }
153 
154  const T* data() const {
155  return m_values.data();
156  }
157 };
158 
159 template <>
160 class vector_property<bool> : public property {
161  std::vector<bool> m_values;
162 
163  inline void m_range_check(size_t index) const {
164  if(index >= m_values.size())
165  throw std::out_of_range("vector::_M_range_check");
166  }
167 
168  void destroy( entity_index e ) {
169  std::size_t last_index = m_values.size() - 1;
170  m_values[e] = m_values[last_index];
171  m_values.pop_back();
172  }
173 
174  void create( ) {
175  m_values.push_back(bool());
176  }
177 
178  void preallocate( std::size_t qnt ) {
179  m_values.reserve(std::max(m_values.capacity(), qnt));
180  }
181 
182 public:
183  vector_property() {
184 
185  }
186 
187  vector_property(bool default_value) : m_values(default_value) {
188 
189  }
190 
191  bool operator[](std::size_t entity_index) const {
192  return m_values[entity_index];
193  }
194 
195  std::vector<bool>::reference operator[](std::size_t entity_index) {
196  return m_values[entity_index];
197  }
198 
199  bool at(std::size_t entity_index) const {
200 #ifndef NDEBUG
201  m_range_check(entity_index);
202 #endif
203  return m_values[entity_index];
204  }
205 
206  std::vector<bool>::reference at(std::size_t entity_index) {
207 #ifndef NDEBUG
208  m_range_check(entity_index);
209 #endif
210  return m_values[entity_index];
211  }
212 
213  std::vector<bool>::const_iterator begin() const {
214  return m_values.begin();
215  }
216 
217  std::vector<bool>::const_iterator end() const {
218  return m_values.end();
219  }
220 };
221 
222 } /* namespace entity system */
223 } /* namespace ophidian */
224 
225 
226 #endif //SRC_ENTITY_SYSTEM_VECTOR_PROPERTY_H
T & operator[](entity_index e)
Property getter.
Definition: vector_property.h:92
T & at(entity_index e)
Property getter with range check.
Definition: vector_property.h:112
std::vector< T >::const_iterator begin() const
Begin iterator.
Definition: vector_property.h:137
const T & operator[](entity_index e) const
Constant property getter.
Definition: vector_property.h:102
std::vector< T >::const_iterator end() const
End iterator.
Definition: vector_property.h:146
const T & at(entity_index e) const
Constant property getter with range check.
Definition: vector_property.h:125
Implementation of the vector property class.
Definition: vector_property.h:37
Property class.
Definition: property.h:33
vector_property()
Constructor.
Definition: vector_property.h:79