1#ifndef ENTT_LOCATOR_LOCATOR_HPP
2#define ENTT_LOCATOR_LOCATOR_HPP
4#include "../config/config.h"
5#include "../stl/concepts.hpp"
6#include "../stl/memory.hpp"
7#include "../stl/utility.hpp"
27template<
typename Service>
29 class service_handle {
31 stl::shared_ptr<Service>
value{};
60 return (service !=
nullptr);
72 [[nodiscard]]
static Service &
value() noexcept {
73 ENTT_ASSERT(
has_value(),
"Service not available");
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)...);
101 template<stl::derived_from<Service> Type = Service,
typename... Args>
102 requires stl::constructible_from<Type, Args...>
104 service = stl::make_shared<Type>(stl::forward<Args>(args)...);
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)...);
129 node.value = service;
138 service = other.value;
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)};
156 inline static stl::shared_ptr<Service> service{};
static node_type handle() noexcept
Returns a handle to the underlying service.
locator & operator=(const locator &)=delete
Default copy assignment operator, deleted on purpose.
static void reset(Type *elem, Deleter deleter={})
Resets or replaces a service.
static Service & emplace(Args &&...args)
Sets or replaces a service.
static void reset(const node_type &other={}) noexcept
Resets or replaces a service.
static Service & value() noexcept
Returns a reference to a valid service, if any.
static Service & emplace(stl::allocator_arg_t, auto alloc, Args &&...args)
Sets or replaces a service using a given allocator.
static bool has_value() noexcept
Checks whether a service locator contains a value.
service_handle node_type
Service node type.
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.
~locator()=delete
Default destructor, deleted on purpose.
Service type
Service type.
locator()=delete
Default constructor, deleted on purpose.