GPR5100 - Rollback
Loading...
Searching...
No Matches
service_locator.h
1#pragma once
2
3#include <type_traits>
4
5namespace core
6{
12template<class T, class TNull>
14{
15 static_assert(std::is_base_of<T, TNull>::value, "TNull has to be a subtype of T");
16public:
22 static T& get()
23 {
24 return *service_;
25 }
26
31 static void provide(T* service)
32 {
33 if (service == nullptr)
34 {
35 // Revert to null service.
36 service_ = &nullService_;
37 } else
38 {
39 service_ = service;
40 }
41 }
42
43protected:
44 static T* service_;
45 static TNull nullService_;
46};
47
48template<class T, class TNull> TNull Locator<T, TNull>::nullService_;
49template<class T, class TNull> T* Locator<T, TNull>::service_ = &Locator<T, TNull>::nullService_;
50
51
52}
Locator is an utility class based on the Locator design-patter.
Definition: service_locator.h:14
static T & get()
get is a method that gets you the reference to the T class. If a pointer was not provided,...
Definition: service_locator.h:22
static void provide(T *service)
provide is a method that allows to provide a pointer to a class T object.
Definition: service_locator.h:31