Helios Engine 0.1.0
A modular ECS based data-oriented C++23 game engine
 
Loading...
Searching...
No Matches
helios::app::AccessPolicy Class Reference

#include <access_policy.hpp>

Public Member Functions

constexpr AccessPolicy () noexcept=default
 
constexpr AccessPolicy (const AccessPolicy &)=default
 
constexpr AccessPolicy (AccessPolicy &&) noexcept=default
 
constexpr ~AccessPolicy ()=default
 
constexpr AccessPolicyoperator= (const AccessPolicy &)=default
 
constexpr AccessPolicyoperator= (AccessPolicy &&) noexcept=default
 
template<ecs::ComponentTrait... Components>
requires ((ecs::ComponentTrait<Components> && ...) && utils::UniqueTypes<Components...>)
constexpr auto Query (this auto &&self) -> decltype(std::forward< decltype(self)>(self))
 Declares a query over component types.
 
template<ecs::ResourceTrait... Resources>
requires utils::UniqueTypes<Resources...>
constexpr auto ReadResources (this auto &&self) -> decltype(std::forward< decltype(self)>(self))
 Declares read-only access to resource types.
 
template<ecs::ResourceTrait... Resources>
requires utils::UniqueTypes<Resources...>
constexpr auto WriteResources (this auto &&self) -> decltype(std::forward< decltype(self)>(self))
 Declares write access to resource types.
 
constexpr bool HasQueryConflict (const AccessPolicy &other) const noexcept
 Checks if this policy has query conflict with another policy.
 
constexpr bool HasResourceConflict (const AccessPolicy &other) const noexcept
 Checks if this policy has resource conflict with another policy.
 
constexpr bool ConflictsWith (const AccessPolicy &other) const noexcept
 Checks if this policy conflicts with another policy.
 
constexpr bool HasQueries () const noexcept
 Checks if this policy has any queries.
 
constexpr bool HasResources () const noexcept
 Checks if this policy has any resource access.
 
constexpr auto GetQueries () const noexcept -> std::span< const details::QueryDescriptor >
 Gets all declared query descriptors.
 
constexpr auto GetReadResources () const noexcept -> std::span< const details::ResourceTypeInfo >
 Gets all resource types declared for reading.
 
constexpr auto GetWriteResources () const noexcept -> std::span< const details::ResourceTypeInfo >
 Gets all resource types declared for writing.
 
constexpr bool HasReadComponent (ecs::ComponentTypeId type_id) const noexcept
 Checks if this policy has the specified component type for reading.
 
constexpr bool HasWriteComponent (ecs::ComponentTypeId type_id) const noexcept
 Checks if this policy has the specified component type for writing.
 
constexpr bool HasReadResource (ecs::ResourceTypeId type_id) const noexcept
 Checks if this policy has the specified resource type for reading.
 
constexpr bool HasWriteResource (ecs::ResourceTypeId type_id) const noexcept
 Checks if this policy has the specified resource type for writing.
 

Detailed Description

Constructor & Destructor Documentation

◆ AccessPolicy() [1/3]

constexpr helios::app::AccessPolicy::AccessPolicy ( )
constexprdefaultnoexcept

◆ AccessPolicy() [2/3]

constexpr helios::app::AccessPolicy::AccessPolicy ( const AccessPolicy )
constexprdefault

◆ AccessPolicy() [3/3]

constexpr helios::app::AccessPolicy::AccessPolicy ( AccessPolicy &&  )
constexprdefaultnoexcept

◆ ~AccessPolicy()

constexpr helios::app::AccessPolicy::~AccessPolicy ( )
constexprdefault

Member Function Documentation

◆ ConflictsWith()

constexpr bool helios::app::AccessPolicy::ConflictsWith ( const AccessPolicy other) const
inlineconstexprnoexcept

Checks if this policy conflicts with another policy.

Checks for query and resource conflict.

Parameters
otherOther access policy to check against
Returns
True if policies conflict, false otherwise
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/app/access_policy.hpp.

Definition at line 187 of file access_policy.hpp.

187 {
188 return HasQueryConflict(other) || HasResourceConflict(other);
189 }
constexpr bool HasQueryConflict(const AccessPolicy &other) const noexcept
Checks if this policy has query conflict with another policy.
constexpr bool HasResourceConflict(const AccessPolicy &other) const noexcept
Checks if this policy has resource conflict with another policy.

◆ GetQueries()

constexpr auto helios::app::AccessPolicy::GetQueries ( ) const -> std::span<const details::QueryDescriptor>
inlineconstexprnoexcept

Gets all declared query descriptors.

Returns
Span of query descriptors
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/app/access_policy.hpp.

Definition at line 209 of file access_policy.hpp.

209 {
210 return queries_;
211 }

◆ GetReadResources()

constexpr auto helios::app::AccessPolicy::GetReadResources ( ) const -> std::span<const details::ResourceTypeInfo>
inlineconstexprnoexcept

Gets all resource types declared for reading.

Returns
Span of resource type info (sorted)
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/app/access_policy.hpp.

Definition at line 217 of file access_policy.hpp.

217 {
218 return read_resources_;
219 }

◆ GetWriteResources()

constexpr auto helios::app::AccessPolicy::GetWriteResources ( ) const -> std::span<const details::ResourceTypeInfo>
inlineconstexprnoexcept

Gets all resource types declared for writing.

Returns
Span of resource type info (sorted)
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/app/access_policy.hpp.

Definition at line 225 of file access_policy.hpp.

225 {
226 return write_resources_;
227 }

◆ HasQueries()

constexpr bool helios::app::AccessPolicy::HasQueries ( ) const
inlineconstexprnoexcept

Checks if this policy has any queries.

Returns
True if any queries are declared, false otherwise
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/app/access_policy.hpp.

Definition at line 195 of file access_policy.hpp.

195{ return !queries_.empty(); }

◆ HasQueryConflict()

constexpr bool helios::app::AccessPolicy::HasQueryConflict ( const AccessPolicy other) const
constexprnoexcept

Checks if this policy has query conflict with another policy.

Two policies conflict if:

  • One reads from a component that the other writes to
  • Both write to the same component
  • Both access the same resource with at least one write access
    Parameters
    otherOther access policy to check against
    Returns
    True if policies have a conflict, false otherwise
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/app/access_policy.hpp.

Definition at line 380 of file access_policy.hpp.

380 {
381 if (!HasQueries() || !other.HasQueries()) {
382 return false;
383 }
384
385 // Check component conflicts using sorted-range intersection: O(n + m)
386 // Conflict cases:
387 // - my.write intersects other.write (write-write)
388 // - my.write intersects other.read (write-read)
389 // - my.read intersects other.write (read-write)
390 for (const auto& my_query : queries_) {
391 for (const auto& other_query : other.queries_) {
392 // write-write
393 if (details::HasIntersection(my_query.write_components, other_query.write_components)) {
394 return true;
395 }
396
397 // my write vs other read
398 if (details::HasIntersection(my_query.write_components, other_query.read_components)) {
399 return true;
400 }
401
402 // my read vs other write
403 if (details::HasIntersection(my_query.read_components, other_query.write_components)) {
404 return true;
405 }
406 }
407 }
408
409 return false;
410}
constexpr bool HasQueries() const noexcept
Checks if this policy has any queries.
constexpr auto Query(this auto &&self) -> decltype(std::forward< decltype(self)>(self))
Declares a query over component types.
constexpr bool HasIntersection(std::span< const ComponentTypeInfo > lhs, std::span< const ComponentTypeInfo > rhs) noexcept
Checks if two sorted ranges have any common elements.

◆ HasReadComponent()

constexpr bool helios::app::AccessPolicy::HasReadComponent ( ecs::ComponentTypeId  type_id) const
inlineconstexprnoexcept

Checks if this policy has the specified component type for reading.

Parameters
type_idComponent type ID to check
Returns
True if component is declared for reading in any query
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/app/access_policy.hpp.

Definition at line 234 of file access_policy.hpp.

234 {
235 for (const auto& query : queries_) {
236 auto matches_id = [type_id](const details::ComponentTypeInfo& info) { return info.type_id == type_id; };
237 if (std::ranges::any_of(query.read_components, matches_id)) {
238 return true;
239 }
240 }
241 return false;
242 }

◆ HasReadResource()

constexpr bool helios::app::AccessPolicy::HasReadResource ( ecs::ResourceTypeId  type_id) const
inlineconstexprnoexcept

Checks if this policy has the specified resource type for reading.

Parameters
type_idResource type ID to check
Returns
True if resource is declared for reading
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/app/access_policy.hpp, and /home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/app/system_context.hpp.

Definition at line 264 of file access_policy.hpp.

264 {
265 auto matches_id = [type_id](const details::ResourceTypeInfo& info) { return info.type_id == type_id; };
266 return std::ranges::any_of(read_resources_, matches_id);
267 }

◆ HasResourceConflict()

constexpr bool helios::app::AccessPolicy::HasResourceConflict ( const AccessPolicy other) const
constexprnoexcept

Checks if this policy has resource conflict with another policy.

Two policies conflict if both access the same resource with at least one write access.

Parameters
otherOther access policy to check against
Returns
True if policies have a conflict, false otherwise
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/app/access_policy.hpp.

Definition at line 411 of file access_policy.hpp.

411 {
412 if (!HasResources() || !other.HasResources()) {
413 return false;
414 }
415
416 // Write-write conflicts
417 if (!write_resources_.empty() && !other.write_resources_.empty() &&
418 details::HasIntersectionBinarySearch(write_resources_, other.write_resources_)) {
419 return true;
420 }
421
422 // Write-read conflicts (my write vs other read)
423 if (!write_resources_.empty() && !other.read_resources_.empty() &&
424 details::HasIntersectionBinarySearch(write_resources_, other.read_resources_)) {
425 return true;
426 }
427
428 // Read-write conflicts (my read vs other write)
429 if (!read_resources_.empty() && !other.write_resources_.empty() &&
430 details::HasIntersectionBinarySearch(read_resources_, other.write_resources_)) {
431 return true;
432 }
433
434 return false;
435}
constexpr bool HasResources() const noexcept
Checks if this policy has any resource access.
constexpr bool HasIntersectionBinarySearch(std::span< const ResourceTypeInfo > lhs, std::span< const ResourceTypeInfo > rhs) noexcept
Checks if any element from one range exists in another sorted range.

◆ HasResources()

constexpr bool helios::app::AccessPolicy::HasResources ( ) const
inlineconstexprnoexcept

Checks if this policy has any resource access.

Returns
True if any resources are declared, false otherwise
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/app/access_policy.hpp.

Definition at line 201 of file access_policy.hpp.

201 {
202 return !read_resources_.empty() || !write_resources_.empty();
203 }

◆ HasWriteComponent()

constexpr bool helios::app::AccessPolicy::HasWriteComponent ( ecs::ComponentTypeId  type_id) const
inlineconstexprnoexcept

Checks if this policy has the specified component type for writing.

Parameters
type_idComponent type ID to check
Returns
True if component is declared for writing in any query
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/app/access_policy.hpp.

Definition at line 249 of file access_policy.hpp.

249 {
250 for (const auto& query : queries_) {
251 auto matches_id = [type_id](const details::ComponentTypeInfo& info) { return info.type_id == type_id; };
252 if (std::ranges::any_of(query.write_components, matches_id)) {
253 return true;
254 }
255 }
256 return false;
257 }

◆ HasWriteResource()

constexpr bool helios::app::AccessPolicy::HasWriteResource ( ecs::ResourceTypeId  type_id) const
inlineconstexprnoexcept

Checks if this policy has the specified resource type for writing.

Parameters
type_idResource type ID to check
Returns
True if resource is declared for writing
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/app/access_policy.hpp, and /home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/app/system_context.hpp.

Definition at line 274 of file access_policy.hpp.

274 {
275 auto matches_id = [type_id](const details::ResourceTypeInfo& info) { return info.type_id == type_id; };
276 return std::ranges::any_of(write_resources_, matches_id);
277 }

◆ operator=() [1/2]

◆ operator=() [2/2]

◆ Query()

template<ecs::ComponentTrait... Components>
requires ((ecs::ComponentTrait<Components> && ...) && utils::UniqueTypes<Components...>)
constexpr auto helios::app::AccessPolicy::Query ( this auto &&  self) -> decltype(std::forward<decltype(self)>(self))
constexpr

Declares a query over component types.

Adds a query specification to the policy. Multiple queries can be declared. Component access is classified per-type into read-only or writable sets. Component types are automatically sorted for efficient conflict detection.

Template Parameters
ComponentsComponent access types (e.g., const Position&, Velocity&)
Returns
Updated AccessPolicy for method chaining
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/app/access_policy.hpp, and /home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/ecs/system.hpp.

Definition at line 293 of file access_policy.hpp.

293 {
294 details::QueryDescriptor query;
295
296 // Populate read vs write component lists based on constness of the parameter type.
297 // For component parameter T:
298 // - If T is const (after removing reference), treat as read-only.
299 // - Otherwise treat as writable.
300 (
301 [&query]() {
302 using Component = std::remove_cvref_t<Components>;
304 constexpr std::string_view name = ecs::ComponentNameOf<Component>();
305 if constexpr (ecs::TagComponentTrait<Component>) {
306 return;
307 }
308 // Check if the original Components type (before removing cvref) is const
309 if constexpr (std::is_const_v<std::remove_reference_t<Components>>) {
310 query.read_components.push_back({id, name});
311 } else {
312 query.write_components.push_back({id, name});
313 }
314 }(),
315 ...);
316
317 // Sort component type lists for efficient conflict detection
318 auto cmp = [](const details::ComponentTypeInfo& lhs, const details::ComponentTypeInfo& rhs) {
319 return lhs.type_id < rhs.type_id;
320 };
321 std::ranges::sort(query.read_components, cmp);
322 std::ranges::sort(query.write_components, cmp);
323
324 self.queries_.push_back(std::move(query));
325 return std::forward<decltype(self)>(self);
326}
size_t ComponentTypeId
Type ID for components.
BasicQuery< World, Allocator, Components... > Query
Type alias for query with mutable world access.
Definition query.hpp:2481

◆ ReadResources()

template<ecs::ResourceTrait... Resources>
requires utils::UniqueTypes<Resources...>
constexpr auto helios::app::AccessPolicy::ReadResources ( this auto &&  self) -> decltype(std::forward<decltype(self)>(self))
constexpr

Declares read-only access to resource types.

Adds resource types that the system will read but not modify. Resources are kept sorted for efficient conflict detection.

Note
If resource is thread-safe it will be ignored.
Template Parameters
ResourcesResource types to read
Returns
Updated AccessPolicy for method chaining
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/app/access_policy.hpp.

Definition at line 330 of file access_policy.hpp.

330 {
331 // Insert each resource that is not thread-safe while maintaining sorted order
332 (
333 [&self]() {
335 if constexpr (Resources::ThreadSafe()) {
337 "'{}' resource was declared in AccessPolicy::ReadResources, but will be ignored since it is "
338 "thread-safe.",
340 } else {
341 self.InsertSorted(self.read_resources_,
342 {ecs::ResourceTypeIdOf<Resources>(), ecs::ResourceNameOf<Resources>()});
343 }
344 } else {
345 self.InsertSorted(self.read_resources_,
346 {ecs::ResourceTypeIdOf<Resources>(), ecs::ResourceNameOf<Resources>()});
347 }
348 }(),
349 ...);
350
351 return std::forward<decltype(self)>(self);
352}
#define HELIOS_INFO(...)
Definition logger.hpp:685

◆ WriteResources()

template<ecs::ResourceTrait... Resources>
requires utils::UniqueTypes<Resources...>
constexpr auto helios::app::AccessPolicy::WriteResources ( this auto &&  self) -> decltype(std::forward<decltype(self)>(self))
constexpr

Declares write access to resource types.

Adds resource types that the system will modify. Resources are kept sorted for efficient conflict detection.

Note
If resource is thread-safe it will be ignored.
Template Parameters
ResourcesResource types to write
Returns
Updated AccessPolicy for method chaining
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/app/access_policy.hpp.

Definition at line 356 of file access_policy.hpp.

356 {
357 // Insert each resource that is not thread-safe while maintaining sorted order
358 (
359 [&self]() {
361 if constexpr (Resources::ThreadSafe()) {
363 "'{}' resource was declared in AccessPolicy::WriteResources, but will be ignored since it is "
364 "thread-safe.",
366 } else {
367 self.InsertSorted(self.write_resources_,
368 {ecs::ResourceTypeIdOf<Resources>(), ecs::ResourceNameOf<Resources>()});
369 }
370 } else {
371 self.InsertSorted(self.write_resources_,
372 {ecs::ResourceTypeIdOf<Resources>(), ecs::ResourceNameOf<Resources>()});
373 }
374 }(),
375 ...);
376
377 return std::forward<decltype(self)>(self);
378}