38 lines
846 B
C++
38 lines
846 B
C++
#ifndef SERVICEREGISTRY_H
|
|
#define SERVICEREGISTRY_H
|
|
|
|
#include <unordered_map>
|
|
#include <typeindex>
|
|
#include <typeinfo>
|
|
#include <string>
|
|
|
|
template<typename Interface>
|
|
class ServiceRegistry
|
|
{
|
|
public:
|
|
static ServiceRegistry& instance()
|
|
{
|
|
static ServiceRegistry instance;
|
|
return instance;
|
|
}
|
|
|
|
void registerService(Interface* service) {
|
|
services[typeid(Interface)] = service;
|
|
}
|
|
|
|
Interface* getService() {
|
|
auto it = services.find(typeid(Interface));
|
|
if (it != services.end())
|
|
return static_cast<Interface*>(it->second);
|
|
return nullptr;
|
|
}
|
|
|
|
private:
|
|
ServiceRegistry() {}
|
|
~ServiceRegistry() {}
|
|
|
|
std::unordered_map<std::type_index, Interface*> services;
|
|
};
|
|
|
|
#endif // SERVICEREGISTRY_H
|