EnTT: src/entt/locator/locator.hpp Source File

EnTT 3.16.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>
27 class locator final {
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
71 [[nodiscard]] static Service &value() noexcept {
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
124 static node_type handle() noexcept {
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
entt::locator::handle
static node_type handle() noexcept
Returns a handle to the underlying service.
Definition locator.hpp:124
entt::locator::operator=
locator & operator=(const locator &)=delete
Default copy assignment operator, deleted on purpose.
entt::locator::reset
static void reset(Type *elem, Deleter deleter={})
Resets or replaces a service.
Definition locator.hpp:146
entt::locator::emplace
static Service & emplace(std::allocator_arg_t, Allocator alloc, Args &&...args)
Sets or replaces a service using a given allocator.
Definition locator.hpp:115
entt::locator::reset
static void reset(const node_type &other={}) noexcept
Resets or replaces a service.
Definition locator.hpp:134
entt::locator::value
static Service & value() noexcept
Returns a reference to a valid service, if any.
Definition locator.hpp:71
entt::locator::has_value
static bool has_value() noexcept
Checks whether a service locator contains a value.
Definition locator.hpp:58
entt::locator::node_type
service_handle node_type
Service node type.
Definition locator.hpp:37
entt::locator::locator
locator(const locator &)=delete
Default copy constructor, deleted on purpose.
entt::locator::~locator
~locator()=delete
Default destructor, deleted on purpose.
entt::locator::type
Service type
Service type.
Definition locator.hpp:35
entt::locator::value_or
static Service & value_or(Args &&...args)
Returns a service if available or sets it from a fallback type.
Definition locator.hpp:88
entt::locator::locator
locator()=delete
Default constructor, deleted on purpose.
entt::locator::emplace
static Service & emplace(Args &&...args)
Sets or replaces a service.
Definition locator.hpp:100
entt
EnTT default namespace.
Definition dense_map.hpp:22