EnTT 3.14.0
Loading...
Searching...
No Matches
locator.hpp
1#ifndef ENTT_LOCATOR_LOCATOR_HPP
2#define ENTT_LOCATOR_LOCATOR_HPP
3
4#include <memory>
5#include <utility>
6#include "../config/config.h"
7
8namespace entt {
9
26template<typename Service>
28 class service_handle {
29 friend class locator<Service>;
30 std::shared_ptr<Service> value{};
31 };
32
33public:
35 using type = Service;
37 using node_type = service_handle;
38
40 locator() = delete;
41
43 locator(const locator &) = delete;
44
46 ~locator() = delete;
47
52 locator &operator=(const locator &) = delete;
53
58 [[nodiscard]] static bool has_value() noexcept {
59 return (service != nullptr);
60 }
61
72 ENTT_ASSERT(has_value(), "Service not available");
73 return *service;
74 }
75
87 template<typename Type = Service, typename... Args>
88 [[nodiscard]] static Service &value_or(Args &&...args) {
89 return service ? *service : emplace<Type>(std::forward<Args>(args)...);
90 }
91
99 template<typename Type = Service, typename... Args>
100 static Service &emplace(Args &&...args) {
101 service = std::make_shared<Type>(std::forward<Args>(args)...);
102 return *service;
103 }
104
114 template<typename Type = Service, typename Allocator, typename... Args>
115 static Service &emplace(std::allocator_arg_t, Allocator alloc, Args &&...args) {
116 service = std::allocate_shared<Type>(alloc, std::forward<Args>(args)...);
117 return *service;
118 }
119
125 node_type node{};
126 node.value = service;
127 return node;
128 }
129
134 static void reset(const node_type &other = {}) noexcept {
135 service = other.value;
136 }
137
145 template<typename Type, typename Deleter = std::default_delete<Type>>
146 static void reset(Type *elem, Deleter deleter = {}) {
147 service = std::shared_ptr<Service>{elem, std::move(deleter)};
148 }
149
150private:
151 // std::shared_ptr because of its type erased allocator which is useful here
152 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
153 inline static std::shared_ptr<Service> service{};
154};
155
156} // namespace entt
157
158#endif
Service locator, nothing more.
Definition locator.hpp:27
static node_type handle() noexcept
Returns a handle to the underlying service.
Definition locator.hpp:124
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:146
static Service & emplace(std::allocator_arg_t, Allocator alloc, Args &&...args)
Sets or replaces a service using a given allocator.
Definition locator.hpp:115
static void reset(const node_type &other={}) noexcept
Resets or replaces a service.
Definition locator.hpp:134
static Service & value() noexcept
Returns a reference to a valid service, if any.
Definition locator.hpp:71
static bool has_value() noexcept
Checks whether a service locator contains a value.
Definition locator.hpp:58
service_handle node_type
Service node type.
Definition locator.hpp:37
locator(const locator &)=delete
Default copy constructor, deleted on purpose.
~locator()=delete
Default destructor, deleted on purpose.
Service type
Service type.
Definition locator.hpp:35
static Service & value_or(Args &&...args)
Returns a service if available or sets it from a fallback type.
Definition locator.hpp:88
locator()=delete
Default constructor, deleted on purpose.
static Service & emplace(Args &&...args)
Sets or replaces a service.
Definition locator.hpp:100
EnTT default namespace.
Definition dense_map.hpp:22
constexpr Type make_obj_using_allocator(const Allocator &allocator, Args &&...args)
Uses-allocator construction utility (waiting for C++20).
Definition memory.hpp:219