1#ifndef ENTT_LOCATOR_LOCATOR_HPP
2#define ENTT_LOCATOR_LOCATOR_HPP
6#include "../config/config.h"
26template<
typename Service>
28 class service_handle {
30 std::shared_ptr<Service> value{};
49 return (service !=
nullptr);
61 [[nodiscard]]
static Service &
value() noexcept {
62 ENTT_ASSERT(
has_value(),
"Service not available");
77 template<
typename Type = Service,
typename... Args>
78 [[nodiscard]]
static Service &
value_or(Args &&...args) {
79 return service ? *service : emplace<Type>(std::forward<Args>(args)...);
89 template<
typename Type = Service,
typename... Args>
91 service = std::make_shared<Type>(std::forward<Args>(args)...);
104 template<
typename Type = Service,
typename Allocator,
typename... Args>
105 static Service &
emplace(std::allocator_arg_t, Allocator alloc, Args &&...args) {
106 service = std::allocate_shared<Type>(alloc, std::forward<Args>(args)...);
116 node.value = service;
125 service = other.value;
135 template<
typename Type,
typename Deleter = std::default_delete<Type>>
136 static void reset(Type *elem, Deleter deleter = {}) {
137 service = std::shared_ptr<Service>{elem, std::move(deleter)};
142 inline static std::shared_ptr<Service> service{};
Service locator, nothing more.
static node_type handle() noexcept
Returns a handle to the underlying service.
static void reset(Type *elem, Deleter deleter={})
Resets or replaces a service.
static Service & emplace(std::allocator_arg_t, Allocator alloc, Args &&...args)
Sets or replaces a service using a given allocator.
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 bool has_value() noexcept
Checks whether a service locator contains a value.
service_handle node_type
Service node type.
~locator()=delete
Default destructor, deleted on purpose.
Service type
Service type.
static Service & value_or(Args &&...args)
Returns a service if available or sets it from a fallback type.
locator()=delete
Default constructor, deleted on purpose.
static Service & emplace(Args &&...args)
Sets or replaces a service.