-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathTestDependencyWithVariant.cxx
60 lines (45 loc) · 1.34 KB
/
TestDependencyWithVariant.cxx
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
/*
* TestDependencyWithVariant.cxx
*
* Created on: Oct 23, 2021
* Author: <a href="mailto:[email protected]">Damir Ljubic</a>
*/
#include "ClientWithVariant.hxx"
#include "TestCases.hxx"
#include "TestDependencyWithVariant.hxx"
namespace test::di::visitor
{
/*
* Function object, with overloaded set
* that covers all the dependencies, and will be used
* primarily with the std::visit call - visitor pattern
*/
struct ServiceVisitor
{
void operator()(ConsoleService& s) const
{
s.provide(std::string("Divna"));
}
void operator()(LocatorService& s) const
{
s.coordinate(11.23f, 32.18f);
}
};
int test()
{
ConsoleService service1;
LocatorService service2("GLONASS");
Client<ConsoleService, LocatorService> client ({service1, service2});
// Similar as with std::get<std::size_t>: index of service
client.call(ServiceVisitor{}, 0);
client.call(ServiceVisitor{}, 1);
// Similar as with std::get<T>: type of service
client.call<LocatorService>([](LocatorService& locator)
{
locator.coordinate(157.83f, 46.99f);
});
// To visit all services at once
client.callAll(ServiceVisitor{});
return 0;
}
}