1 /*
2  * Copyright 2014 Google Inc. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <fruit/fruit.h>
18 
19 #if MULTIPLIER == 1
20 #define REPEAT(X) REPEAT_1(X, _)
21 
22 #elif MULTIPLIER == 2
23 #define REPEAT(X) REPEAT_2(X, _)
24 
25 #elif MULTIPLIER == 4
26 #define REPEAT(X) REPEAT_4(X, _)
27 
28 #elif MULTIPLIER == 8
29 #define REPEAT(X) REPEAT_8(X, _)
30 
31 #elif MULTIPLIER == 16
32 #define REPEAT(X) REPEAT_16(X, _)
33 
34 #elif MULTIPLIER == 32
35 #define REPEAT(X) REPEAT_32(X, _)
36 
37 #elif MULTIPLIER == 64
38 #define REPEAT(X) REPEAT_64(X, _)
39 
40 #elif MULTIPLIER == 128
41 #define REPEAT(X) REPEAT_128(X, _)
42 
43 #elif MULTIPLIER == 256
44 #define REPEAT(X) REPEAT_256(X, _)
45 
46 #elif MULTIPLIER == 512
47 #define REPEAT(X) REPEAT_512(X, _)
48 
49 #elif MULTIPLIER == 1024
50 #define REPEAT(X) REPEAT_1024(X, _)
51 
52 #else
53 #error Multiplier not supported.
54 #endif
55 
56 #define PLACEHOLDER
57 
58 #define EVAL0(...) __VA_ARGS__
59 #define EVAL1(...) EVAL0(EVAL0(EVAL0(EVAL0(__VA_ARGS__))))
60 #define EVAL2(...) EVAL1(EVAL1(EVAL1(EVAL1(__VA_ARGS__))))
61 #define EVAL(...) EVAL2(EVAL2(EVAL2(EVAL2(__VA_ARGS__))))
62 
63 #define META_REPEAT_2(R, X, I) R PLACEHOLDER(X, I##0) R PLACEHOLDER(X, I##1)
64 
65 #define REPEAT_1(X, I) X(I)
66 
67 #define REPEAT_2(X, I) META_REPEAT_2(REPEAT_1, X, I)
68 
69 #define REPEAT_4(X, I) META_REPEAT_2(REPEAT_2, X, I)
70 
71 #define REPEAT_8(X, I) META_REPEAT_2(REPEAT_4, X, I)
72 
73 #define REPEAT_16(X, I) META_REPEAT_2(REPEAT_8, X, I)
74 
75 #define REPEAT_32(X, I) META_REPEAT_2(REPEAT_16, X, I)
76 
77 #define REPEAT_64(X, I) META_REPEAT_2(REPEAT_32, X, I)
78 
79 #define REPEAT_128(X, I) META_REPEAT_2(REPEAT_64, X, I)
80 
81 #define REPEAT_256(X, I) META_REPEAT_2(REPEAT_128, X, I)
82 
83 #define REPEAT_512(X, I) META_REPEAT_2(REPEAT_256, X, I)
84 
85 #define REPEAT_1024(X, I) META_REPEAT_2(REPEAT_512, X, I)
86 
87 using namespace fruit;
88 
89 #define DEFINITIONS(N)                                                                                                 \
90   struct A##N {                                                                                                        \
91     INJECT(A##N()) = default;                                                                                          \
92   };                                                                                                                   \
93                                                                                                                        \
94   struct B##N {};                                                                                                      \
95                                                                                                                        \
96   struct C##N {};                                                                                                      \
97                                                                                                                        \
98   struct I##N {                                                                                                        \
99     virtual void f() = 0;                                                                                              \
100   };                                                                                                                   \
101                                                                                                                        \
102   struct X##N : public I##N {                                                                                          \
103     INJECT(X##N(A##N, B##N*, const C##N&)){};                                                                          \
104                                                                                                                        \
105     virtual void f();                                                                                                  \
106   };                                                                                                                   \
107                                                                                                                        \
108   struct Y##N {};                                                                                                      \
109                                                                                                                        \
110   struct Z##N {};                                                                                                      \
111                                                                                                                        \
112   Component<Required<Y##N>, Z##N> getZ##N##Component();
113 
114 #define REQUIREMENTS(N) C##N,
115 
116 #define PARAMETERS(N) B##N &b##N,
117 
118 #if USE_FRUIT_2_X_SYNTAX
119 #define BINDINGS(N)                                                                                                    \
120   .bind<I##N, X##N>().bindInstance(b##N).install(getZ##N##Component()).registerProvider([]() { return Y##N(); })
121 #else
122 
123 #define BINDINGS(N)                                                                                                    \
124   .bind<I##N, X##N>().bindInstance(b##N).install(getZ##N##Component).registerProvider([]() { return Y##N(); })
125 #endif
126 
EVAL(REPEAT (DEFINITIONS))127 EVAL(REPEAT(DEFINITIONS))
128 
129 Component<Required<EVAL(REPEAT(REQUIREMENTS)) int>> getComponent(EVAL(REPEAT(PARAMETERS)) int) {
130   return createComponent() EVAL(REPEAT(BINDINGS));
131 }
132