-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
120 lines (103 loc) · 2.9 KB
/
main.cpp
File metadata and controls
120 lines (103 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//
// main.cpp
// IOC
//
// Created by James Mintram on 18/08/2015.
// Copyright © 2015 James Mintram. All rights reserved.
//
#include <iostream>
#include <map>
#include <assert.h>
#define DEFINE_INJECTABLE_SERVICE(ServiceType)\
inline const char* GetServiceName(ServiceType* dontUse) {return #ServiceType;}
#define DEFINE_INJECTABLE_SERVICE_CLASS(ServiceClass, ServiceType)\
inline const char* GetServiceName(ServiceClass* dontUse) {return GetServiceName((ServiceType*)0);}
class ServiceRegistry
{
public:
template<typename ServiceType>
void RegisterService(ServiceType* service)
{
std::string serviceName = GetServiceName((ServiceType*)0);
//Service already registered
assert(services.find(serviceName) == services.end());
services[serviceName] = service;
}
template<typename ServiceType>
ServiceType* GetService()
{
std::string serviceName = GetServiceName((ServiceType*)0);
//Service not registered
assert(services.find(serviceName) != services.end());
return (ServiceType*)services[GetServiceName((ServiceType*)0)];
}
protected:
std::map<std::string, void*> services;
};
template<typename ServiceType>
class Injectable
{
public:
Injectable(ServiceRegistry& registry)
{
service = registry.GetService<ServiceType>();
printf("Service name: %s\n", GetServiceName((ServiceType*)0));
}
ServiceType* Get() {return service;}
ServiceType* operator-> ()
{
return service;
}
protected:
ServiceType* service;
};
class IFacebookService
{
virtual int GetVal() = 0;
};
class IAchievementsService
{
public:
virtual int GetVal() = 0;
};
class AchievementsService
{
public:
AchievementsService(int _val) : val(_val){}
virtual int GetVal() {return val;}
protected:
int val;
};
class FacebookService : public IFacebookService
{
public:
FacebookService(int _val) : val(_val){}
virtual int GetVal() {return val;}
protected:
int val;
};
// Need to be visible wherever Injectables are used
DEFINE_INJECTABLE_SERVICE(IAchievementsService)
DEFINE_INJECTABLE_SERVICE(IFacebookService)
// Only need to be visible where the injectables are
// registered with a Registry
DEFINE_INJECTABLE_SERVICE_CLASS(AchievementsService, IAchievementsService)
DEFINE_INJECTABLE_SERVICE_CLASS(FacebookService, IFacebookService)
class MyGameObject
{
public:
MyGameObject(ServiceRegistry& registry)
: Achievements(registry)
, Facebook(registry)
{}
Injectable<IAchievementsService> Achievements;
Injectable<IFacebookService> Facebook;
};
int main(int argc, const char * argv[]) {
ServiceRegistry registry;
registry.RegisterService(new AchievementsService(12));
registry.RegisterService(new FacebookService(10));
MyGameObject myObject(registry);
printf("Value: %d", myObject.Achievements->GetVal());
return 0;
}