Helios Engine
A modular ECS based data-oriented C++23 game engine framework
Loading...
Searching...
No Matches
local_param.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <helios/assert.hpp>
6
7#include <concepts>
8#include <functional>
9#include <utility>
10
11namespace helios::ecs {
12
13/**
14 * @brief Read-only or mutable access to a system-local resource.
15 * @details Only one system ever accesses it.
16 * Use `Local<const T>` for read-only access and `Local<T>` for mutable access.
17 * @tparam T Resource type. Qualify with `const` for read-only access
18 */
19template <ResourceTrait T>
20class Local {
21private:
22 using DecayedT = std::remove_const_t<T>;
23
24public:
25 /**
26 * @brief Constructs a local resource.
27 * @param resources Resource manager
28 */
29 explicit constexpr Local(ResourceManager& resources) noexcept
30 : resources_(resources) {}
31 constexpr Local(const Local&) noexcept = default;
32 constexpr Local(Local&&) noexcept = default;
33 constexpr ~Local() noexcept = default;
34
35 constexpr Local& operator=(const Local&) noexcept = default;
36 constexpr Local& operator=(Local&&) noexcept = default;
37
38 /**
39 * @brief Inserts a local resource.
40 * @details Replaces existing resource if present.
41 * @tparam U Resource type
42 * @param value Resource value
43 */
44 template <ResourceTrait U>
45 requires std::same_as<DecayedT, std::remove_cvref_t<U>> &&
46 (!std::is_const_v<T>)
47 void Insert(U&& value) const {
48 return resources_.get().Insert(std::forward<U>(value));
49 }
50
51 /**
52 * @brief Emplaces a local resource.
53 * @details Replaces existing resource if present.
54 * @tparam Args Resource constructor arguments
55 * @param args Resource constructor arguments
56 */
57 template <typename... Args>
58 requires std::constructible_from<DecayedT, Args...> && (!std::is_const_v<T>)
59 void Emplace(Args&&... args) const {
60 return resources_.get().template Emplace<DecayedT>(
61 std::forward<Args>(args)...);
62 }
63
64 [[nodiscard]] T& operator*() const noexcept {
65 return resources_.get().template Get<DecayedT>();
66 }
67
68 [[nodiscard]] T* operator->() const noexcept {
69 return &resources_.get().template Get<DecayedT>();
70 }
71
72 /**
73 * @brief Gets the local resource.
74 * @return Reference to the local resource
75 */
76 [[nodiscard]] T& Get() const noexcept {
77 return resources_.get().template Get<DecayedT>();
78 }
79
80 /**
81 * @brief Gets the local resource manager.
82 * @return Reference to the local resource manager
83 */
84 [[nodiscard]] constexpr ResourceManager& Resources() const noexcept {
85 return resources_.get();
86 }
87
88private:
89 std::reference_wrapper<ResourceManager> resources_;
90};
91
92} // namespace helios::ecs
T * operator->() const noexcept
void Insert(U &&value) const
Inserts a local resource.
constexpr Local(Local &&) noexcept=default
constexpr Local(ResourceManager &resources) noexcept
Constructs a local resource.
T & Get() const noexcept
Gets the local resource.
constexpr Local(const Local &) noexcept=default
constexpr ResourceManager & Resources() const noexcept
Gets the local resource manager.
T & operator*() const noexcept
void Emplace(Args &&... args) const
Emplaces a local resource.
Resource manager for storing and managing resources of various types.
Definition manager.hpp:21
Concept for valid resource types.
Definition resource.hpp:25
STL namespace.