co_ecs 0.9.0
Cobalt ECS
Loading...
Searching...
No Matches
access.hpp
Go to the documentation of this file.
1#pragma once
2
4
5namespace co_ecs {
6
15
20public:
24 access_pattern_t() = default;
25
29 access_pattern_t(access_type registry_access) : _registry_access(registry_access) {
30 }
31
37 _component_access.emplace(meta.id, access);
38 }
39
44 [[nodiscard]] auto allows(const access_pattern_t& other) const noexcept -> bool {
45 if ((writes_all() && other.reads_all()) || (reads_all() && !other.reads_all())) {
46 return false;
47 }
48
49 for (auto [component_id, access] : _component_access) {
50 if ((access == access_type::write && other.reads(component_id)) || other.writes(component_id)) {
51 return false;
52 }
53 }
54
55 return true;
56 }
57
61 [[nodiscard]] auto writes_all() const noexcept -> bool {
62 return _registry_access == access_type::write;
63 }
64
68
69 [[nodiscard]] auto reads_all() const noexcept -> bool {
70 return _registry_access != access_type::none;
71 }
72
77 [[nodiscard]] auto writes(component_id_t id) const noexcept -> bool {
78 if (_registry_access == access_type::write) {
79 return true;
80 }
81
82 if (auto iter = _component_access.find(id); iter != _component_access.end()) {
83 auto [_, access] = *iter;
84 if (access == access_type::write) {
85 return true;
86 }
87 }
88
89 return false;
90 }
91
96 [[nodiscard]] auto reads(component_id_t id) const noexcept -> bool {
97 if (_registry_access != access_type::none) {
98 return true;
99 }
100
101 if (auto iter = _component_access.find(id); iter != _component_access.end()) {
102 return true;
103 }
104
105 return false;
106 }
107
113 access_pattern_t res = *this;
114 res &= rhs;
115 return res;
116 }
117
123 _registry_access = std::max(_registry_access, rhs._registry_access);
124
125 for (auto [component_id, access] : rhs._component_access) {
126 _component_access[component_id] = std::max(access, _component_access[component_id]);
127 }
128
129 return *this;
130 }
131
132private:
133 access_type _registry_access{ access_type::none };
134 detail::sparse_map<component_id_t, access_type> _component_access;
135};
136
137} // namespace co_ecs
Class representing an access pattern for components.
Definition access.hpp:19
access_pattern_t(access_type registry_access)
Constructs an access pattern with specified registry access.
Definition access.hpp:29
auto allows(const access_pattern_t &other) const noexcept -> bool
Checks if this access pattern allows another access pattern.
Definition access.hpp:44
auto reads_all() const noexcept -> bool
Checks if this access pattern reads from all components.
Definition access.hpp:69
access_pattern_t()=default
Default constructor.
auto writes_all() const noexcept -> bool
Checks if this access pattern writes to all components.
Definition access.hpp:61
auto writes(component_id_t id) const noexcept -> bool
Checks if this access pattern writes to a specific component.
Definition access.hpp:77
auto operator&(const access_pattern_t &rhs) -> access_pattern_t
Combines two access patterns using the bitwise AND operator.
Definition access.hpp:112
auto operator&=(const access_pattern_t &rhs) -> access_pattern_t &
Combines this access pattern with another using the bitwise AND assignment operator.
Definition access.hpp:122
auto reads(component_id_t id) const noexcept -> bool
Checks if this access pattern reads from a specific component.
Definition access.hpp:96
access_pattern_t(access_type access, component_meta meta)
Constructs an access pattern for a specific component.
Definition access.hpp:36
Definition archetype.hpp:11
detail::type_id< struct _component_family_t, component_id_t > component_id
Type for family used to generated component IDs.
Definition component.hpp:79
std::uint32_t component_id_t
Type for component ID.
Definition component.hpp:73
access_type
Enumeration representing the type of access.
Definition access.hpp:10
@ none
No access.
Definition access.hpp:11
@ write
Write access.
Definition access.hpp:13
@ read
Read access.
Definition access.hpp:12
Component metadata. Stores an ID, size, alignment, destructor, etc.
Definition component.hpp:145
component_id_t id
Definition component.hpp:176