-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
108 lines (85 loc) · 2.39 KB
/
main.cpp
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
#include <concepts>
#include <string>
#include <iostream>
using namespace std;
template<typename T>
concept LoggerServiceInterface = requires(T logger, const string& str) {
logger.info(str);
logger.warn(str);
logger.error(str);
};
template<typename T>
concept LoggerServiceProvider = requires(T provider){
{provider.logger} -> LoggerServiceInterface;
};
struct LoggerServiceImpl {
void info(const string& msg){cout << "[INFO]" << msg << endl;}
void warn(const string& msg){cout << "[WARN]" << msg << endl;}
void error(const string& msg){cout << "[ERROR]" << msg << endl;}
};
template<typename T>
concept DataPersistenceInterface = requires(T persistence) {
persistence.write();
persistence.read();
persistence.remove();
};
template<typename T>
concept DataPersistenceProvider = requires(T provider){
{provider.persistence} -> DataPersistenceInterface;
};
struct DataPersistenceImpl {
void write(){}
void read(){}
void remove(){}
};
template<typename T>
concept CalculatorInterface = requires(T calculator) {
calculator.add();
calculator.subtract();
calculator.reset();
};
template<typename T>
concept CalculatorProvider = requires(T provider){
{provider.calculator} -> CalculatorInterface;
};
struct CalculatorImpl {
void add(){}
void subtract(){}
void reset(){}
};
template<typename T>
concept ServiceProviderInterface =
LoggerServiceProvider<T> &&
DataPersistenceProvider<T> &&
CalculatorProvider<T>;
void all(ServiceProviderInterface auto provider) {
provider.logger.info("Hello Logger DI: all");
}
template<typename T>
concept TwoProvider = LoggerServiceProvider<T> && DataPersistenceProvider<T>;
void two(TwoProvider auto provider) {
provider.logger.info("Hello Logger DI: two");
}
void onlyLogger(LoggerServiceProvider auto provider) {
provider.logger.info("Hello Logger DI: onlyLogger");
}
void onlyPersist(DataPersistenceProvider auto provider) {
provider.persistence.write();
}
void onlyCalculator(CalculatorProvider auto provider) {
provider.persistence.write();
}
struct ServiceProviderImpl{
LoggerServiceImpl logger;
DataPersistenceImpl persistence;
CalculatorImpl calculator;
};
int main() {
auto provider = ServiceProviderImpl{};
all(provider);
two(provider);
onlyLogger(provider);
onlyPersist(provider);
onlyCalculator(provider);
return 0;
}