• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef FRUIT_COMPONENT_DEFN_H
18 #define FRUIT_COMPONENT_DEFN_H
19 
20 #include <fruit/component.h>
21 
22 #include <fruit/impl/component_storage/component_storage.h>
23 #include <fruit/impl/injection_errors.h>
24 #include <fruit/impl/component_install_arg_checks.h>
25 
26 #include <memory>
27 
28 namespace fruit {
29 
30 namespace impl {
31 namespace meta {
32 // This is a helper class used in the implementation of Component and PartialComponent.
33 // It's in fruit::impl::meta so that we don't need to qualify everything with fruit::impl::meta.
34 template <typename... PreviousBindings>
35 struct OpForComponent {
36   template <typename Comp>
37   using ConvertTo = Eval<Call(ReverseComposeFunctors(Id<ComponentFunctor(ConvertComponent, Comp)>,
38                                                      ProcessDeferredBindings, Id<ProcessBinding(PreviousBindings)>...),
39                               ConstructComponentImpl())>;
40 
41   template <typename Binding>
42   using AddBinding =
43       Eval<Call(ReverseComposeFunctors(Id<ProcessBinding(Binding)>, Id<ProcessBinding(PreviousBindings)>...),
44                 ConstructComponentImpl())>;
45 };
46 } // namespace meta
47 } // namespace impl
48 
49 template <typename... Params>
50 template <typename... Bindings>
Component(PartialComponent<Bindings...> && partial_component)51 inline Component<Params...>::Component(PartialComponent<Bindings...>&& partial_component) noexcept : storage() {
52 
53   (void)typename fruit::impl::meta::CheckIfError<Comp>::type();
54 
55   using Op = typename fruit::impl::meta::OpForComponent<Bindings...>::template ConvertTo<Comp>;
56   (void)typename fruit::impl::meta::CheckIfError<Op>::type();
57 
58 #if !FRUIT_NO_LOOP_CHECK
59   (void)typename fruit::impl::meta::CheckIfError<
60       fruit::impl::meta::Eval<fruit::impl::meta::CheckNoLoopInDeps(typename Op::Result)>>::type();
61 #endif // !FRUIT_NO_LOOP_CHECK
62 
63   std::size_t num_entries = partial_component.storage.numBindings() + Op().numEntries();
64   fruit::impl::FixedSizeVector<fruit::impl::ComponentStorageEntry> entries(num_entries);
65 
66   Op()(entries);
67 
68   // addBindings may modify the storage member of PartialComponent.
69   // Therefore, it should not be used after this operation.
70   partial_component.storage.addBindings(entries);
71 
72   // TODO: re-enable this check somehow.
73   // component.component.already_converted_to_component = true;
74 
75   FruitAssert(entries.size() == num_entries);
76 
77   storage = fruit::impl::ComponentStorage(std::move(entries));
78 }
79 
createComponent()80 inline PartialComponent<> createComponent() {
81   return {{}};
82 }
83 
84 template <typename... Bindings>
85 template <typename AnnotatedI, typename AnnotatedC>
bind()86 inline PartialComponent<fruit::impl::Bind<AnnotatedI, AnnotatedC>, Bindings...> PartialComponent<Bindings...>::bind() {
87   using Op = OpFor<fruit::impl::Bind<AnnotatedI, AnnotatedC>>;
88   (void)typename fruit::impl::meta::CheckIfError<Op>::type();
89 
90   return {{storage}};
91 }
92 
93 template <typename... Bindings>
94 template <typename AnnotatedSignature>
95 inline PartialComponent<fruit::impl::RegisterConstructor<AnnotatedSignature>, Bindings...>
registerConstructor()96 PartialComponent<Bindings...>::registerConstructor() {
97   using Op = OpFor<fruit::impl::RegisterConstructor<AnnotatedSignature>>;
98   (void)typename fruit::impl::meta::CheckIfError<Op>::type();
99 
100   return {{storage}};
101 }
102 
103 template <typename... Bindings>
104 template <typename C>
105 inline PartialComponent<fruit::impl::BindInstance<C, C>, Bindings...>
bindInstance(C & instance)106 PartialComponent<Bindings...>::bindInstance(C& instance) {
107   using Op = OpFor<fruit::impl::BindInstance<C, C>>;
108   (void)typename fruit::impl::meta::CheckIfError<Op>::type();
109   return {{storage, instance}};
110 }
111 
112 template <typename... Bindings>
113 template <typename C>
114 inline PartialComponent<fruit::impl::BindConstInstance<C, C>, Bindings...>
bindInstance(const C & instance)115 PartialComponent<Bindings...>::bindInstance(const C& instance) {
116   using Op = OpFor<fruit::impl::BindConstInstance<C, C>>;
117   (void)typename fruit::impl::meta::CheckIfError<Op>::type();
118   return {{storage, instance}};
119 }
120 
121 template <typename... Bindings>
122 template <typename AnnotatedC, typename C>
123 inline PartialComponent<fruit::impl::BindInstance<AnnotatedC, C>, Bindings...>
bindInstance(C & instance)124 PartialComponent<Bindings...>::bindInstance(C& instance) {
125   using Op = OpFor<fruit::impl::BindInstance<AnnotatedC, C>>;
126   (void)typename fruit::impl::meta::CheckIfError<Op>::type();
127   return {{storage, instance}};
128 }
129 
130 template <typename... Bindings>
131 template <typename AnnotatedC, typename C>
132 inline PartialComponent<fruit::impl::BindConstInstance<AnnotatedC, C>, Bindings...>
bindInstance(const C & instance)133 PartialComponent<Bindings...>::bindInstance(const C& instance) {
134   using Op = OpFor<fruit::impl::BindConstInstance<AnnotatedC, C>>;
135   (void)typename fruit::impl::meta::CheckIfError<Op>::type();
136   return {{storage, instance}};
137 }
138 
139 template <typename... Bindings>
140 template <typename Lambda>
141 inline PartialComponent<fruit::impl::RegisterProvider<Lambda>, Bindings...>
registerProvider(Lambda)142 PartialComponent<Bindings...>::registerProvider(Lambda) {
143   using Op = OpFor<fruit::impl::RegisterProvider<Lambda>>;
144   (void)typename fruit::impl::meta::CheckIfError<Op>::type();
145   return {{storage}};
146 }
147 
148 template <typename... Bindings>
149 template <typename AnnotatedSignature, typename Lambda>
150 inline PartialComponent<fruit::impl::RegisterProvider<AnnotatedSignature, Lambda>, Bindings...>
registerProvider(Lambda)151 PartialComponent<Bindings...>::registerProvider(Lambda) {
152   using Op = OpFor<fruit::impl::RegisterProvider<AnnotatedSignature, Lambda>>;
153   (void)typename fruit::impl::meta::CheckIfError<Op>::type();
154   return {{storage}};
155 }
156 
157 template <typename... Bindings>
158 template <typename AnnotatedI, typename AnnotatedC>
159 inline PartialComponent<fruit::impl::AddMultibinding<AnnotatedI, AnnotatedC>, Bindings...>
addMultibinding()160 PartialComponent<Bindings...>::addMultibinding() {
161   using Op = OpFor<fruit::impl::AddMultibinding<AnnotatedI, AnnotatedC>>;
162   (void)typename fruit::impl::meta::CheckIfError<Op>::type();
163 
164   return {{storage}};
165 }
166 
167 template <typename... Bindings>
168 template <typename C>
169 inline PartialComponent<fruit::impl::AddInstanceMultibinding<C>, Bindings...>
addInstanceMultibinding(C & instance)170 PartialComponent<Bindings...>::addInstanceMultibinding(C& instance) {
171   using Op = fruit::impl::meta::Eval<fruit::impl::meta::CheckNormalizedTypes(
172       fruit::impl::meta::Vector<fruit::impl::meta::Type<C>>)>;
173   (void)typename fruit::impl::meta::CheckIfError<Op>::type();
174 
175   return {{storage, instance}};
176 }
177 
178 template <typename... Bindings>
179 template <typename AnnotatedC, typename C>
180 inline PartialComponent<fruit::impl::AddInstanceMultibinding<AnnotatedC>, Bindings...>
addInstanceMultibinding(C & instance)181 PartialComponent<Bindings...>::addInstanceMultibinding(C& instance) {
182   using Op = fruit::impl::meta::Eval<fruit::impl::meta::CheckNormalizedTypes(
183       fruit::impl::meta::Vector<fruit::impl::meta::Type<C>>)>;
184   (void)typename fruit::impl::meta::CheckIfError<Op>::type();
185   return {{storage, instance}};
186 }
187 
188 template <typename... Bindings>
189 template <typename C>
190 inline PartialComponent<fruit::impl::AddInstanceVectorMultibindings<C>, Bindings...>
addInstanceMultibindings(std::vector<C> & instances)191 PartialComponent<Bindings...>::addInstanceMultibindings(std::vector<C>& instances) {
192   using Op = fruit::impl::meta::Eval<fruit::impl::meta::CheckNormalizedTypes(
193       fruit::impl::meta::Vector<fruit::impl::meta::Type<C>>)>;
194   (void)typename fruit::impl::meta::CheckIfError<Op>::type();
195   return {{storage, instances}};
196 }
197 
198 template <typename... Bindings>
199 template <typename AnnotatedC, typename C>
200 inline PartialComponent<fruit::impl::AddInstanceVectorMultibindings<AnnotatedC>, Bindings...>
addInstanceMultibindings(std::vector<C> & instances)201 PartialComponent<Bindings...>::addInstanceMultibindings(std::vector<C>& instances) {
202   using Op = fruit::impl::meta::Eval<fruit::impl::meta::CheckNormalizedTypes(
203       fruit::impl::meta::Vector<fruit::impl::meta::Type<C>>)>;
204   (void)typename fruit::impl::meta::CheckIfError<Op>::type();
205 
206   return {{storage, instances}};
207 }
208 
209 template <typename... Bindings>
210 template <typename Lambda>
211 inline PartialComponent<fruit::impl::AddMultibindingProvider<Lambda>, Bindings...>
addMultibindingProvider(Lambda)212 PartialComponent<Bindings...>::addMultibindingProvider(Lambda) {
213   using Op = OpFor<fruit::impl::AddMultibindingProvider<Lambda>>;
214   (void)typename fruit::impl::meta::CheckIfError<Op>::type();
215 
216   return {{storage}};
217 }
218 
219 template <typename... Bindings>
220 template <typename AnnotatedSignature, typename Lambda>
221 inline PartialComponent<fruit::impl::AddMultibindingProvider<AnnotatedSignature, Lambda>, Bindings...>
addMultibindingProvider(Lambda)222 PartialComponent<Bindings...>::addMultibindingProvider(Lambda) {
223   using Op = OpFor<fruit::impl::AddMultibindingProvider<AnnotatedSignature, Lambda>>;
224   (void)typename fruit::impl::meta::CheckIfError<Op>::type();
225 
226   return {{storage}};
227 }
228 
229 template <typename... Bindings>
230 template <typename DecoratedSignature, typename Lambda>
231 inline PartialComponent<fruit::impl::RegisterFactory<DecoratedSignature, Lambda>, Bindings...>
registerFactory(Lambda)232 PartialComponent<Bindings...>::registerFactory(Lambda) {
233   using Op = OpFor<fruit::impl::RegisterFactory<DecoratedSignature, Lambda>>;
234   (void)typename fruit::impl::meta::CheckIfError<Op>::type();
235 
236   return {{storage}};
237 }
238 
239 template <typename... Bindings>
PartialComponent(fruit::impl::PartialComponentStorage<Bindings...> storage)240 inline PartialComponent<Bindings...>::PartialComponent(fruit::impl::PartialComponentStorage<Bindings...> storage)
241     : storage(std::move(storage)) {}
242 
243 template <typename... Bindings>
244 template <typename... OtherComponentParams, typename... FormalArgs, typename... Args>
245 inline PartialComponent<fruit::impl::InstallComponent<fruit::Component<OtherComponentParams...>(FormalArgs...)>,
246                         Bindings...>
install(fruit::Component<OtherComponentParams...> (* getComponent)(FormalArgs...),Args &&...args)247 PartialComponent<Bindings...>::install(fruit::Component<OtherComponentParams...> (*getComponent)(FormalArgs...),
248                                        Args&&... args) {
249   using IntCollector = int[];
250   (void)IntCollector{0, fruit::impl::checkAcceptableComponentInstallArg<FormalArgs>()...};
251 
252   using Op = OpFor<fruit::impl::InstallComponent<fruit::Component<OtherComponentParams...>(FormalArgs...)>>;
253   (void)typename fruit::impl::meta::CheckIfError<Op>::type();
254 
255   std::tuple<FormalArgs...> args_tuple{std::forward<Args>(args)...};
256 
257   return {{storage, getComponent, std::move(args_tuple)}};
258 }
259 
260 template <typename... Bindings>
261 template <typename... ComponentFunctions>
262 inline PartialComponent<fruit::impl::InstallComponentFunctions<ComponentFunctions...>, Bindings...>
installComponentFunctions(ComponentFunctions...componentFunctions)263 PartialComponent<Bindings...>::installComponentFunctions(ComponentFunctions... componentFunctions) {
264 
265   using Op = OpFor<fruit::impl::InstallComponentFunctions<ComponentFunctions...>>;
266   (void)typename fruit::impl::meta::CheckIfError<Op>::type();
267 
268   std::tuple<ComponentFunctions...> component_functions_tuple{std::move(componentFunctions)...};
269 
270   return {{storage, std::move(component_functions_tuple)}};
271 }
272 
273 template <typename... Bindings>
274 template <typename... OtherComponentParams, typename... FormalArgs, typename... Args>
275 inline typename PartialComponent<Bindings...>::template PartialComponentWithReplacementInProgress<
276     fruit::Component<OtherComponentParams...>, FormalArgs...>
replace(fruit::Component<OtherComponentParams...> (* getReplacedComponent)(FormalArgs...),Args &&...args)277 PartialComponent<Bindings...>::replace(fruit::Component<OtherComponentParams...> (*getReplacedComponent)(FormalArgs...),
278                                        Args&&... args) {
279   using IntCollector = int[];
280   (void)IntCollector{0, fruit::impl::checkAcceptableComponentInstallArg<FormalArgs>()...};
281 
282   std::tuple<FormalArgs...> args_tuple{std::forward<Args>(args)...};
283 
284   return {{storage, getReplacedComponent, std::move(args_tuple)}};
285 }
286 
287 template <typename... Bindings>
288 template <typename OtherComponent, typename... GetReplacedComponentFormalArgs>
289 template <typename... GetReplacementComponentFormalArgs, typename... Args>
290 inline PartialComponent<fruit::impl::ReplaceComponent<OtherComponent(GetReplacedComponentFormalArgs...),
291                                                       OtherComponent(GetReplacementComponentFormalArgs...)>,
292                         Bindings...>
293 PartialComponent<Bindings...>::
with(OtherComponent (* getReplacementComponent)(GetReplacementComponentFormalArgs...),Args &&...args)294     PartialComponentWithReplacementInProgress<OtherComponent, GetReplacedComponentFormalArgs...>::with(
295         OtherComponent (*getReplacementComponent)(GetReplacementComponentFormalArgs...), Args&&... args) {
296   using IntCollector = int[];
297   (void)IntCollector{0, fruit::impl::checkAcceptableComponentInstallArg<GetReplacementComponentFormalArgs>()...};
298 
299   std::tuple<GetReplacementComponentFormalArgs...> args_tuple{std::forward<Args>(args)...};
300 
301   return {{storage, getReplacementComponent, std::move(args_tuple)}};
302 }
303 
304 } // namespace fruit
305 
306 #endif // FRUIT_COMPONENT_DEFN_H
307