Consolix
Loading...
Searching...
No Matches
consolix::ServiceLocator Class Reference

A universal service locator for managing shared resources. More...

#include <ServiceLocator.hpp>

Public Member Functions

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.
 

Static Public Member Functions

static ServiceLocatorget_instance ()
 Retrieves the singleton instance of the ServiceLocator.
 

Private Member Functions

 ServiceLocator ()=default
 
 ~ServiceLocator ()=default
 
 ServiceLocator (const ServiceLocator &)=delete
 
ServiceLocatoroperator= (const ServiceLocator &)=delete
 
 ServiceLocator (ServiceLocator &&)=delete
 
ServiceLocatoroperator= (ServiceLocator &&)=delete
 

Private Attributes

std::unordered_map< std::type_index, std::shared_ptr< void > > m_services
 Registered services.
 
std::shared_mutex m_mutex
 Mutex for thread-safe access.
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ ServiceLocator() [1/3]

consolix::ServiceLocator::ServiceLocator ( )
privatedefault

◆ ~ServiceLocator()

consolix::ServiceLocator::~ServiceLocator ( )
privatedefault

◆ ServiceLocator() [2/3]

consolix::ServiceLocator::ServiceLocator ( const ServiceLocator & )
privatedelete

◆ ServiceLocator() [3/3]

consolix::ServiceLocator::ServiceLocator ( ServiceLocator && )
privatedelete

Member Function Documentation

◆ clear_all()

void consolix::ServiceLocator::clear_all ( )
inline

Clears all registered resources.

Definition at line 103 of file ServiceLocator.hpp.

◆ get_instance()

static ServiceLocator & consolix::ServiceLocator::get_instance ( )
inlinestatic

Retrieves the singleton instance of the ServiceLocator.

Returns
Reference to the ServiceLocator instance.

Definition at line 33 of file ServiceLocator.hpp.

◆ get_service()

template<typename T>
T & consolix::ServiceLocator::get_service ( )
inline

Retrieves a resource from the locator.

Template Parameters
TThe 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
TThe type of the resource.
Returns
true if the resource is registered, false otherwise.

Definition at line 96 of file ServiceLocator.hpp.

◆ operator=() [1/2]

ServiceLocator & consolix::ServiceLocator::operator= ( const ServiceLocator & )
privatedelete

◆ operator=() [2/2]

ServiceLocator & consolix::ServiceLocator::operator= ( ServiceLocator && )
privatedelete

◆ register_service() [1/2]

template<typename T>
void consolix::ServiceLocator::register_service ( )
inline

Registers a resource with default construction.

Template Parameters
TThe 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
TThe type of the resource.
Parameters
creatorA function to create the resource (optional).
Exceptions
`std::runtime_error`if the resource is already registered.

Definition at line 43 of file ServiceLocator.hpp.

Member Data Documentation

◆ m_mutex

std::shared_mutex consolix::ServiceLocator::m_mutex
private

Mutex for thread-safe access.

Definition at line 112 of file ServiceLocator.hpp.

◆ m_services

std::unordered_map< std::type_index, std::shared_ptr<void> > consolix::ServiceLocator::m_services
private

Registered services.

Definition at line 111 of file ServiceLocator.hpp.


The documentation for this class was generated from the following file: