-
Notifications
You must be signed in to change notification settings - Fork 135
/
annotations.cpp
129 lines (113 loc) · 2.61 KB
/
annotations.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//
// Copyright (c) 2012-2020 Kris Jusiak (kris at jusiak dot net)
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
//<-
#include <cassert>
//->
#include <boost/di.hpp>
namespace di = boost::di;
auto int_1 = [] {};
struct int_2_t {
} int_2;
class annotations1 {
public:
/*<<Constructor with named parameters of the same `int` type>>*/
BOOST_DI_INJECT(annotations1, (named = int_1) int i1, (named = int_2) int i2, int i3) : i1(i1), i2(i2), i3(i3) {
assert(i1 == 42);
assert(i2 == 87);
assert(i3 == 123);
}
private:
int i1 = 0;
int i2 = 0;
int i3 = 0;
};
class annotations2 {
public:
/*<<Constructor with named parameters of the same `int` type>>*/
BOOST_DI_INJECT(annotations2, (named = int_1) int i1, (named = int_2) int i2, int i3);
private:
int i1 = 0;
int i2 = 0;
int i3 = 0;
};
annotations2::annotations2(int i1, int i2, int i3) {
assert(i1 == 42);
assert(i2 == 87);
assert(i3 == 123);
}
//<-
#if !defined(__MSVC__)
#if defined(__CLANG__)
#pragma clang diagnostic ignored "-Wgnu-string-literal-operator-template"
#endif
template <char...>
struct string {};
#if (__GNUC__ >= 9)
#pragma GCC diagnostic push
#pragma GCC diagnostic warning "-Wpedantic"
#endif
template <class T, T... Chars>
constexpr auto operator""_s() {
return string<Chars...>{};
}
#if (__GNUC__ >= 9)
#pragma GCC diagnostic pop
#endif
#endif
//->
//<-
#if !defined(__MSVC__)
//->
class annotations3 {
public:
/*<<Constructor with named parameters of the same `int` type>>*/
BOOST_DI_INJECT(annotations3, (named = "int1"_s) int i1, (named = "int2"_s) int i2, int i3) : i1(i1), i2(i2), i3(i3) {
assert(i1 == 42);
assert(i2 == 87);
assert(i3 == 123);
}
private:
int i1 = 0;
int i2 = 0;
int i3 = 0;
};
//<-
#endif
//->
int main() {
{
/*<<make injector and bind named parameters>>*/
// clang-format off
auto injector = di::make_injector(
di::bind<int>().named(int_1).to(42)
, di::bind<int>().named(int_2).to(87)
, di::bind<int>().to(123)
);
// clang-format on
/*<<create `annotations`>>*/
injector.create<annotations1>();
injector.create<annotations2>();
}
//<-
#if !defined(__MSVC__)
//->
{
/*<<make injector and bind named parameters>>*/
// clang-format off
auto injector = di::make_injector(
di::bind<int>().named("int1"_s).to(42)
, di::bind<int>().named("int2"_s).to(87)
, di::bind<int>().to(123)
);
// clang-format on
/*<<create `annotations`>>*/
injector.create<annotations3>();
}
//<-
#endif
//->
}