-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
94 lines (73 loc) · 2.76 KB
/
test.cpp
File metadata and controls
94 lines (73 loc) · 2.76 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
#include "container.h"
// Just a plain pod with trivial constructor
struct Pod
{
int x = 0;
};
// Pod with a constructor that takes an argument
struct PodWithInt
{
int x;
PodWithInt (int n) : x { n }
{
}
};
// The constructor here takes the container.
// This allows to manually "inject" the dependencies into the class.
// Of course it is not the true DI but it is as close as we can get.
struct PodWithContainer
{
Pod &pod;
PodWithInt &pod_with_int;
PodWithContainer (maxy::control::Container &c) :
pod { c.get<Pod> () },
pod_with_int { c.get<PodWithInt> () }
{
}
};
// Same as above but with additional arguments to the constructor
struct PodWithContainerAndInt
{
PodWithInt pod_with_int;
PodWithInt &pod_with_int_ref;
PodWithContainerAndInt (maxy::control::Container &c, int n) :
pod_with_int { n },
pod_with_int_ref { c.get<PodWithInt> () }
{
}
};
int main ()
{
std::cout << "Testing\n" << std::boolalpha;
maxy::control::Container c;
// Register some classes
c.add<Pod> ();
c.add<PodWithInt> (666);
c.add<PodWithContainer> ();
c.add<PodWithContainerAndInt> (1234);
// Get an instance from the container
auto &pod = c.get<Pod> ();
std::cout << "Pod.x is 0 : " << (pod.x == 0) << "\n";
// The container holds singletons so pod and pod2 point to the same instance!
auto &pod2 = c.get<Pod> ();
// So this changes pod as well
pod2.x = 555;
std::cout << "Pod.x changed to 555 : " << (pod.x == 555) << "\n";
// Get a couple more instances from the container
auto & pod_with_int = c.get<PodWithInt> ();
auto & pod_with_container = c.get<PodWithContainer> ();
std::cout << "pod_with_container.pod.x = 555 : " << (pod_with_container.pod.x == 555)
<< ", pod_with_container.pod_with_int.x = 666 : " << (pod_with_container.pod_with_int.x == 666) << "\n";
// Now modify instances retrieved earlier
pod.x = 333;
pod_with_int.x = 888;
// The dependencies inside the pod_with_container are references to container-stored singletons too, so they were just modified.
std::cout << "pod_with_container.pod.x = 333 : " << (pod_with_container.pod.x == 333)
<< ", pod_with_container.pod_with_int.x = 888 : " << (pod_with_container.pod_with_int.x == 888) << "\n";
auto pod_with_container_and_int = c.get<PodWithContainerAndInt> ();
// pod_with_container_and_int.pod_with_int is not a reference and therefore it retains the value provided at the time of registration.
// However its 'pod_with_int_ref' property is a reference to the global singleton so its value is now changed as expected.
std::cout << "pod_with_container_and_int.pod_with_int.x = 1234 : " << (pod_with_container_and_int.pod_with_int.x == 1234)
<< ", pod_with_container_and_int.pod_with_int_ref.x = 888 : " << (pod_with_container_and_int.pod_with_int_ref.x == 888) << "\n";
std::cout << "Done\n";
}