Antkeeper  0.0.1
brep-attribute.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Christopher J. Howard
3  *
4  * This file is part of Antkeeper source code.
5  *
6  * Antkeeper source code is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Antkeeper source code is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef ANTKEEPER_GEOM_BREP_ATTRIBUTE_HPP
21 #define ANTKEEPER_GEOM_BREP_ATTRIBUTE_HPP
22 
24 #include <memory>
25 #include <vector>
26 
27 namespace geom {
28 
33 {
34 public:
36  [[nodiscard]] inline constexpr hash::fnv1a32_t name() const noexcept
37  {
38  return m_name;
39  }
40 
41 protected:
42  inline explicit constexpr brep_attribute_base(hash::fnv1a32_t name) noexcept:
43  m_name(name)
44  {}
45 
46 private:
47  friend class brep_attribute_map;
48  template <class T>
49  friend class brep_element_container;
50 
56  virtual void erase(std::size_t i) = 0;
57 
61  virtual void emplace_back() = 0;
62 
64  [[nodiscard]] virtual std::unique_ptr<brep_attribute_base> clone() const = 0;
65 
66  hash::fnv1a32_t m_name;
67 };
68 
74 template <class T>
76 {
77 public:
78  using value_type = T;
80  using const_reference = const value_type&;
81  using pointer = value_type*;
82  using const_pointer = const value_type*;
83  using iterator = std::vector<value_type>::iterator;
84  using const_iterator = std::vector<value_type>::const_iterator;
85  using reverse_iterator = std::vector<value_type>::reverse_iterator;
86  using const_reverse_iterator = std::vector<value_type>::const_reverse_iterator;
87 
94  brep_attribute(hash::fnv1a32_t name, std::size_t element_count):
96  m_values(element_count)
97  {}
98 
101 
110  [[nodiscard]] inline constexpr const_reference operator[](std::size_t i) const
111  {
112  return m_values[i];
113  }
114  [[nodiscard]] inline constexpr reference operator[](std::size_t i)
115  {
116  return m_values[i];
117  }
119 
122  [[nodiscard]] inline constexpr const_reference front() const
123  {
124  return m_values.front();
125  }
126  [[nodiscard]] inline constexpr reference front()
127  {
128  return m_values.front();
129  }
131 
134  [[nodiscard]] inline constexpr const_reference back() const
135  {
136  return m_values.back();
137  }
138  [[nodiscard]] inline constexpr reference back()
139  {
140  return m_values.back();
141  }
143 
146  [[nodiscard]] inline constexpr const value_type* data() const noexcept
147  {
148  return m_values.data();
149  }
150  [[nodiscard]] inline constexpr value_type* data() noexcept
151  {
152  return m_values.data();
153  }
155 
159 
162  [[nodiscard]] inline constexpr const_iterator begin() const noexcept
163  {
164  return m_values.begin();
165  }
166  [[nodiscard]] inline constexpr iterator begin() noexcept
167  {
168  return m_values.begin();
169  }
170  [[nodiscard]] inline constexpr const_iterator cbegin() const noexcept
171  {
172  return m_values.begin();
173  }
175 
178  [[nodiscard]] inline constexpr const_iterator end() const noexcept
179  {
180  return m_values.end();
181  }
182  [[nodiscard]] inline constexpr iterator end() noexcept
183  {
184  return m_values.end();
185  }
186  [[nodiscard]] inline constexpr const_iterator cend() const noexcept
187  {
188  return m_values.end();
189  }
191 
194  [[nodiscard]] inline constexpr const_reverse_iterator rbegin() const noexcept
195  {
196  return m_values.rbegin();
197  }
198  [[nodiscard]] inline constexpr reverse_iterator rbegin() noexcept
199  {
200  return m_values.rbegin();
201  }
202  [[nodiscard]] inline constexpr const_reverse_iterator crbegin() const noexcept
203  {
204  return m_values.rbegin();
205  }
207 
210  [[nodiscard]] inline constexpr const_reverse_iterator rend() const noexcept
211  {
212  return m_values.rend();
213  }
214  [[nodiscard]] inline constexpr reverse_iterator rend() noexcept
215  {
216  return m_values.rend();
217  }
218  [[nodiscard]] inline constexpr const_reverse_iterator crend() const noexcept
219  {
220  return m_values.rend();
221  }
223 
227 
229  [[nodiscard]] inline constexpr bool empty() const noexcept
230  {
231  return m_values.empty();
232  }
233 
235  [[nodiscard]] inline constexpr std::size_t size() const noexcept
236  {
237  return m_values.size();
238  }
239 
241 
242 private:
243  void erase(std::size_t i) override
244  {
245  m_values[i] = std::move(m_values.back());
246  m_values.pop_back();
247  }
248 
249  void emplace_back() override
250  {
251  m_values.emplace_back();
252  }
253 
254  [[nodiscard]] std::unique_ptr<brep_attribute_base> clone() const override
255  {
256  auto copy = std::make_unique<brep_attribute<T>>(name(), 0);
257  copy->m_values = m_values;
258  return copy;
259  }
260 
261  std::vector<value_type> m_values;
262 };
263 
264 } // namespace geom
265 
266 #endif // ANTKEEPER_GEOM_BREP_ATTRIBUTE_HPP
Abstract base class for B-rep element attributes.
constexpr brep_attribute_base(hash::fnv1a32_t name) noexcept
constexpr hash::fnv1a32_t name() const noexcept
Returns the name of the attribute.
Maps names to B-rep attributes.
Per-element B-rep data.
constexpr const_reverse_iterator rend() const noexcept
Returns a reverse iterator to the attribute value of the element following the last element of the re...
constexpr reverse_iterator rbegin() noexcept
Returns a reverse iterator to the attribute value of the first element of the reversed container.
std::vector< value_type >::reverse_iterator reverse_iterator
constexpr iterator begin() noexcept
Returns an iterator to the attribute value of the first element.
constexpr const_iterator cend() const noexcept
Returns an iterator to the attribute value of the element following the last element.
constexpr iterator end() noexcept
Returns an iterator to the attribute value of the element following the last element.
std::vector< value_type >::const_iterator const_iterator
const value_type * const_pointer
constexpr const_iterator end() const noexcept
Returns an iterator to the attribute value of the element following the last element.
constexpr const_reference back() const
Returns a reference to the attribute value of the last element.
constexpr const_reference front() const
Returns a reference to the attribute value of the first element.
const value_type & const_reference
constexpr const_reverse_iterator rbegin() const noexcept
Returns a reverse iterator to the attribute value of the first element of the reversed container.
brep_attribute(hash::fnv1a32_t name, std::size_t element_count)
Constructs an attribute.
constexpr const_reference operator[](std::size_t i) const
Returns a reference to the attribute value of an element.
constexpr reverse_iterator rend() noexcept
Returns a reverse iterator to the attribute value of the element following the last element of the re...
std::vector< value_type >::iterator iterator
constexpr bool empty() const noexcept
Returns true if the container is empty, false otherwise.
constexpr reference operator[](std::size_t i)
Returns a reference to the attribute value of an element.
constexpr const_iterator begin() const noexcept
Returns an iterator to the attribute value of the first element.
constexpr std::size_t size() const noexcept
Returns the number of attribute values in the container.
constexpr const value_type * data() const noexcept
Returns a pointer to the underlying array serving as attribute value storage.
std::vector< value_type >::const_reverse_iterator const_reverse_iterator
constexpr value_type * data() noexcept
Returns a pointer to the underlying array serving as attribute value storage.
constexpr const_reverse_iterator crbegin() const noexcept
Returns a reverse iterator to the attribute value of the first element of the reversed container.
constexpr reference back()
Returns a reference to the attribute value of the last element.
constexpr reference front()
Returns a reference to the attribute value of the first element.
constexpr const_iterator cbegin() const noexcept
Returns an iterator to the attribute value of the first element.
constexpr const_reverse_iterator crend() const noexcept
Returns a reverse iterator to the attribute value of the element following the last element of the re...
Container for B-rep elements.
void clone(entity::registry &registry, entity::id source, entity::id destination)
Clones all the components of an entity.
Definition: clone.cpp:24
Geometric algorithms.
32-bit FNV-1a hash value.
Definition: fnv1a.hpp:117