OGLplus  (0.59.0) a C++ wrapper for rendering APIs

component.hpp
Go to the documentation of this file.
1 #ifndef EAGINE_ECS_COMPONENT_HPP
9 #define EAGINE_ECS_COMPONENT_HPP
10 
11 #include "../config/basic.hpp"
12 #include "../flat_map.hpp"
13 #include "../identifier_t.hpp"
14 #include "../optional_ref.hpp"
15 #include <type_traits>
16 
17 namespace eagine::ecs {
18 
19 // component unique identifier
20 using component_uid_t = identifier_t;
21 
22 // entity_data
23 template <typename Derived, bool IsRelation>
24 struct entity_data {
25  static constexpr auto component_uid() noexcept {
26  return Derived::uid();
27  }
28 
29  static constexpr auto is_relation() noexcept {
30  return IsRelation;
31  }
32 };
33 
34 // component - base class
35 template <typename Derived>
36 using component = entity_data<Derived, false>;
37 
38 // relation - base class
39 template <typename Derived>
40 using relation = entity_data<Derived, true>;
41 
42 // component_uid_map
43 template <typename T>
44 class component_uid_map {
45 private:
46  flat_map<component_uid_t, T> _storage{};
47 
48 public:
49  auto find(component_uid_t cid) noexcept -> optional_reference_wrapper<T> {
50  const auto pos{_storage.find(cid)};
51  if(pos != _storage.end()) {
52  return {pos->second};
53  }
54  return {nothing};
55  }
56 
57  auto find(component_uid_t cid) const noexcept
58  -> optional_reference_wrapper<const T> {
59  const auto pos{_storage.find(cid)};
60  if(pos != _storage.end()) {
61  return {pos->second};
62  }
63  return {nothing};
64  }
65 
66  template <typename... Args>
67  auto emplace(component_uid_t cid, Args&&... args) -> bool {
68  return _storage.emplace(cid, std::forward<Args>(args)...).second;
69  }
70 
71  auto erase(component_uid_t cid) {
72  return _storage.erase(cid);
73  }
74 
75  auto operator[](component_uid_t cid) -> T& {
76  return _storage[cid];
77  }
78 };
79 
80 } // namespace eagine::ecs
81 
82 #endif // EAGINE_ECS_COMPONENT_HPP
static auto find(basic_span< T1, P1, S1 > where, basic_span< T2, P2, S2 > what) -> basic_span< T1, P1, S1 >
Finds the position of the last occurrence of what in a span.
Definition: span_algo.hpp:374
std::uint64_t identifier_t
The underlying integer type for eagine::identifier.
Definition: identifier_t.hpp:19
static constexpr nothing_t nothing
Constant of nothing_t type.
Definition: nothing.hpp:30

Copyright © 2015-2021 Matúš Chochlík.
<chochlik -at -gmail.com>
Documentation generated on Tue Apr 13 2021 by Doxygen (version 1.8.17).