A universal service locator for managing shared resources.
More...
#include <ServiceLocator.hpp>
|
template<typename T> |
void | register_service (std::function< std::shared_ptr< T >()> creator) |
| Registers a resource or service.
|
|
template<typename T> |
void | register_service () |
| Registers a resource with default construction.
|
|
template<typename T> |
T & | get_service () |
| Retrieves a resource from the locator.
|
|
template<typename T> |
bool | has_service () |
| Checks if a resource is registered.
|
|
void | clear_all () |
| Clears all registered resources.
|
|
|
std::unordered_map< std::type_index, std::shared_ptr< void > > | m_services |
| Registered services.
|
|
std::shared_mutex | m_mutex |
| Mutex for thread-safe access.
|
|
A universal service locator for managing shared resources.
The ServiceLocator
class implements a singleton pattern to provide a centralized way of registering and accessing shared resources or services.
Definition at line 28 of file ServiceLocator.hpp.
◆ ServiceLocator() [1/3]
consolix::ServiceLocator::ServiceLocator |
( |
| ) |
|
|
privatedefault |
◆ ~ServiceLocator()
consolix::ServiceLocator::~ServiceLocator |
( |
| ) |
|
|
privatedefault |
◆ ServiceLocator() [2/3]
◆ ServiceLocator() [3/3]
◆ clear_all()
void consolix::ServiceLocator::clear_all |
( |
| ) |
|
|
inline |
◆ get_instance()
◆ get_service()
template<typename T>
T & consolix::ServiceLocator::get_service |
( |
| ) |
|
|
inline |
Retrieves a resource from the locator.
- Template Parameters
-
T | The type of the resource. |
- Returns
- Reference to the resource.
- Exceptions
-
`std::runtime_error` | if the resource is not registered. |
Definition at line 79 of file ServiceLocator.hpp.
◆ has_service()
template<typename T>
bool consolix::ServiceLocator::has_service |
( |
| ) |
|
|
inline |
Checks if a resource is registered.
- Template Parameters
-
T | The type of the resource. |
- Returns
true
if the resource is registered, false
otherwise.
Definition at line 96 of file ServiceLocator.hpp.
◆ operator=() [1/2]
◆ operator=() [2/2]
◆ register_service() [1/2]
template<typename T>
void consolix::ServiceLocator::register_service |
( |
| ) |
|
|
inline |
Registers a resource with default construction.
- Template Parameters
-
T | The type of the resource. |
- Exceptions
-
`std::runtime_error` | if the resource is already registered. |
Definition at line 62 of file ServiceLocator.hpp.
◆ register_service() [2/2]
template<typename T>
void consolix::ServiceLocator::register_service |
( |
std::function< std::shared_ptr< T >()> | creator | ) |
|
|
inline |
Registers a resource or service.
- Template Parameters
-
T | The type of the resource. |
- Parameters
-
creator | A function to create the resource (optional). |
- Exceptions
-
`std::runtime_error` | if the resource is already registered. |
Definition at line 43 of file ServiceLocator.hpp.
◆ m_mutex
std::shared_mutex consolix::ServiceLocator::m_mutex |
|
private |
◆ m_services
std::unordered_map< std::type_index, std::shared_ptr<void> > consolix::ServiceLocator::m_services |
|
private |
The documentation for this class was generated from the following file: