EnTT 4.0.0
Loading...
Searching...
No Matches
locator.hpp
1#ifndef ENTT_LOCATOR_LOCATOR_HPP
2#define ENTT_LOCATOR_LOCATOR_HPP
3
4#include "../config/config.h"
5#include "../stl/concepts.hpp"
6#include "../stl/memory.hpp"
7#include "../stl/utility.hpp"
8
9namespace entt {
10
27template<typename Service>
28class locator final {
29 class service_handle {
30 friend class locator<Service>;
31 stl::shared_ptr<Service> value{};
32 };
33
34public:
36 using type = Service;
38 using node_type = service_handle;
39
41 locator() = delete;
42
44 locator(const locator &) = delete;
45
47 ~locator() = delete;
48
53 locator &operator=(const locator &) = delete;
54
59 [[nodiscard]] static bool has_value() noexcept {
60 return (service != nullptr);
61 }
62
72 [[nodiscard]] static Service &value() noexcept {
73 ENTT_ASSERT(has_value(), "Service not available");
74 return *service;
75 }
76
88 template<stl::derived_from<Service> Type = Service, typename... Args>
89 requires stl::constructible_from<Type, Args...>
90 [[nodiscard]] static Service &value_or(Args &&...args) {
91 return service ? *service : emplace<Type>(stl::forward<Args>(args)...);
92 }
93
101 template<stl::derived_from<Service> Type = Service, typename... Args>
102 requires stl::constructible_from<Type, Args...>
103 static Service &emplace(Args &&...args) {
104 service = stl::make_shared<Type>(stl::forward<Args>(args)...);
105 return *service;
106 }
107
116 template<stl::derived_from<Service> Type = Service, typename... Args>
117 requires stl::constructible_from<Type, Args...>
118 static Service &emplace(stl::allocator_arg_t, auto alloc, Args &&...args) {
119 service = stl::allocate_shared<Type>(alloc, stl::forward<Args>(args)...);
120 return *service;
121 }
122
127 static node_type handle() noexcept {
128 node_type node{};
129 node.value = service;
130 return node;
131 }
132
137 static void reset(const node_type &other = {}) noexcept {
138 service = other.value;
139 }
140
148 template<stl::derived_from<Service> Type, typename Deleter = stl::default_delete<Type>>
149 static void reset(Type *elem, Deleter deleter = {}) {
150 service = stl::shared_ptr<Service>{elem, stl::move(deleter)};
151 }
152
153private:
154 // stl::shared_ptr because of its type erased allocator which is useful here
155 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
156 inline static stl::shared_ptr<Service> service{};
157};
158
159} // namespace entt
160
161#endif
static node_type handle() noexcept
Returns a handle to the underlying service.
Definition locator.hpp:127
locator & operator=(const locator &)=delete
Default copy assignment operator, deleted on purpose.
static void reset(Type *elem, Deleter deleter={})
Resets or replaces a service.
Definition locator.hpp:149
static Service & emplace(Args &&...args)
Sets or replaces a service.
Definition locator.hpp:103
static void reset(const node_type &other={}) noexcept
Resets or replaces a service.
Definition locator.hpp:137
static Service & value() noexcept
Returns a reference to a valid service, if any.
Definition locator.hpp:72
static Service & emplace(stl::allocator_arg_t, auto alloc, Args &&...args)
Sets or replaces a service using a given allocator.
Definition locator.hpp:118
static bool has_value() noexcept
Checks whether a service locator contains a value.
Definition locator.hpp:59
service_handle node_type
Service node type.
Definition locator.hpp:38
locator(const locator &)=delete
Default copy constructor, deleted on purpose.
static Service & value_or(Args &&...args)
Returns a service if available or sets it from a fallback type.
Definition locator.hpp:90
~locator()=delete
Default destructor, deleted on purpose.
Service type
Service type.
Definition locator.hpp:36
locator()=delete
Default constructor, deleted on purpose.
EnTT default namespace.
Definition dense_map.hpp:25