1*a65addddSAndroid Build Coastguard Worker /* 2*a65addddSAndroid Build Coastguard Worker * Copyright 2014 Google Inc. All rights reserved. 3*a65addddSAndroid Build Coastguard Worker * 4*a65addddSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*a65addddSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*a65addddSAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*a65addddSAndroid Build Coastguard Worker * 8*a65addddSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*a65addddSAndroid Build Coastguard Worker * 10*a65addddSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*a65addddSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*a65addddSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*a65addddSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*a65addddSAndroid Build Coastguard Worker * limitations under the License. 15*a65addddSAndroid Build Coastguard Worker */ 16*a65addddSAndroid Build Coastguard Worker 17*a65addddSAndroid Build Coastguard Worker #ifndef FRUIT_COMPONENT_H 18*a65addddSAndroid Build Coastguard Worker #define FRUIT_COMPONENT_H 19*a65addddSAndroid Build Coastguard Worker 20*a65addddSAndroid Build Coastguard Worker // This include is not required here, but having it here shortens the include trace in error messages. 21*a65addddSAndroid Build Coastguard Worker #include <fruit/impl/injection_errors.h> 22*a65addddSAndroid Build Coastguard Worker 23*a65addddSAndroid Build Coastguard Worker #include <fruit/fruit_forward_decls.h> 24*a65addddSAndroid Build Coastguard Worker #include <fruit/impl/bindings.h> 25*a65addddSAndroid Build Coastguard Worker #include <fruit/impl/component_functors.defn.h> 26*a65addddSAndroid Build Coastguard Worker #include <fruit/impl/component_storage/component_storage.h> 27*a65addddSAndroid Build Coastguard Worker #include <fruit/impl/component_storage/partial_component_storage.h> 28*a65addddSAndroid Build Coastguard Worker #include <fruit/impl/meta/component.h> 29*a65addddSAndroid Build Coastguard Worker 30*a65addddSAndroid Build Coastguard Worker namespace fruit { 31*a65addddSAndroid Build Coastguard Worker 32*a65addddSAndroid Build Coastguard Worker /** 33*a65addddSAndroid Build Coastguard Worker * A component (aka module) represents a collection of bindings. 34*a65addddSAndroid Build Coastguard Worker * You can think of a Component object as a UML component. 35*a65addddSAndroid Build Coastguard Worker * 36*a65addddSAndroid Build Coastguard Worker * The parameters can be of the form <P...> or <Required<R...>, P...> where: 37*a65addddSAndroid Build Coastguard Worker * * R... are the required types (types required to inject some types in P... but that are not provided by this 38*a65addddSAndroid Build Coastguard Worker * Component), if any 39*a65addddSAndroid Build Coastguard Worker * * P... are the types provided by this Component. 40*a65addddSAndroid Build Coastguard Worker * No type can appear twice, not even once in R and once in P. 41*a65addddSAndroid Build Coastguard Worker * 42*a65addddSAndroid Build Coastguard Worker * See PartialComponent below for how to construct a Component. 43*a65addddSAndroid Build Coastguard Worker */ 44*a65addddSAndroid Build Coastguard Worker template <typename... Params> 45*a65addddSAndroid Build Coastguard Worker class Component { 46*a65addddSAndroid Build Coastguard Worker public: 47*a65addddSAndroid Build Coastguard Worker Component(Component&&) noexcept = default; 48*a65addddSAndroid Build Coastguard Worker 49*a65addddSAndroid Build Coastguard Worker Component& operator=(Component&&) = delete; 50*a65addddSAndroid Build Coastguard Worker Component& operator=(const Component&) = delete; 51*a65addddSAndroid Build Coastguard Worker 52*a65addddSAndroid Build Coastguard Worker /** 53*a65addddSAndroid Build Coastguard Worker * Converts a PartialComponent to an arbitrary Component, auto-injecting the missing types (if 54*a65addddSAndroid Build Coastguard Worker * any). 55*a65addddSAndroid Build Coastguard Worker * This is usually called implicitly when returning a component from a function. See PartialComponent for an example. 56*a65addddSAndroid Build Coastguard Worker */ 57*a65addddSAndroid Build Coastguard Worker template <typename... Bindings> 58*a65addddSAndroid Build Coastguard Worker Component(PartialComponent<Bindings...>&& component) noexcept; // NOLINT(google-explicit-constructor) 59*a65addddSAndroid Build Coastguard Worker 60*a65addddSAndroid Build Coastguard Worker private: 61*a65addddSAndroid Build Coastguard Worker // Do not use. Use fruit::createComponent() instead. 62*a65addddSAndroid Build Coastguard Worker Component() = default; 63*a65addddSAndroid Build Coastguard Worker 64*a65addddSAndroid Build Coastguard Worker template <typename... Types> 65*a65addddSAndroid Build Coastguard Worker friend class Component; 66*a65addddSAndroid Build Coastguard Worker 67*a65addddSAndroid Build Coastguard Worker template <typename... Bindings> 68*a65addddSAndroid Build Coastguard Worker friend class PartialComponent; 69*a65addddSAndroid Build Coastguard Worker 70*a65addddSAndroid Build Coastguard Worker template <typename... OtherParams> 71*a65addddSAndroid Build Coastguard Worker friend class NormalizedComponent; 72*a65addddSAndroid Build Coastguard Worker 73*a65addddSAndroid Build Coastguard Worker template <typename... OtherParams> 74*a65addddSAndroid Build Coastguard Worker friend class Injector; 75*a65addddSAndroid Build Coastguard Worker 76*a65addddSAndroid Build Coastguard Worker template <typename... Bindings> 77*a65addddSAndroid Build Coastguard Worker friend class fruit::impl::PartialComponentStorage; 78*a65addddSAndroid Build Coastguard Worker 79*a65addddSAndroid Build Coastguard Worker template <typename Component, typename... Args> 80*a65addddSAndroid Build Coastguard Worker friend class fruit::impl::LazyComponentImpl; 81*a65addddSAndroid Build Coastguard Worker 82*a65addddSAndroid Build Coastguard Worker friend struct fruit::impl::ComponentStorageEntry::LazyComponentWithNoArgs; 83*a65addddSAndroid Build Coastguard Worker 84*a65addddSAndroid Build Coastguard Worker template <typename Component, typename... Args> 85*a65addddSAndroid Build Coastguard Worker friend class fruit::impl::ComponentInterfaceImpl; 86*a65addddSAndroid Build Coastguard Worker 87*a65addddSAndroid Build Coastguard Worker fruit::impl::ComponentStorage storage; 88*a65addddSAndroid Build Coastguard Worker 89*a65addddSAndroid Build Coastguard Worker using Comp = fruit::impl::meta::Eval<fruit::impl::meta::ConstructComponentImpl(fruit::impl::meta::Type<Params>...)>; 90*a65addddSAndroid Build Coastguard Worker 91*a65addddSAndroid Build Coastguard Worker using Check1 = typename fruit::impl::meta::CheckIfError<Comp>::type; 92*a65addddSAndroid Build Coastguard Worker // Force instantiation of Check1. 93*a65addddSAndroid Build Coastguard Worker static_assert(true || sizeof(Check1), ""); 94*a65addddSAndroid Build Coastguard Worker }; 95*a65addddSAndroid Build Coastguard Worker 96*a65addddSAndroid Build Coastguard Worker /** 97*a65addddSAndroid Build Coastguard Worker * Constructs an empty component. 98*a65addddSAndroid Build Coastguard Worker * 99*a65addddSAndroid Build Coastguard Worker * Example usage: 100*a65addddSAndroid Build Coastguard Worker * 101*a65addddSAndroid Build Coastguard Worker * fruit::Component<Foo> getFooComponent() { 102*a65addddSAndroid Build Coastguard Worker * return fruit::createComponent() 103*a65addddSAndroid Build Coastguard Worker * .install(getComponent1) 104*a65addddSAndroid Build Coastguard Worker * .install(getComponent2) 105*a65addddSAndroid Build Coastguard Worker * .bind<Foo, FooImpl>(); 106*a65addddSAndroid Build Coastguard Worker * } 107*a65addddSAndroid Build Coastguard Worker * 108*a65addddSAndroid Build Coastguard Worker * Since types are auto-injected when needed, just converting this to the desired component can suffice in some cases, 109*a65addddSAndroid Build Coastguard Worker * e.g.: 110*a65addddSAndroid Build Coastguard Worker * 111*a65addddSAndroid Build Coastguard Worker * fruit::Component<Foo> getFooComponent() { 112*a65addddSAndroid Build Coastguard Worker * return fruit::createComponent(); 113*a65addddSAndroid Build Coastguard Worker * } 114*a65addddSAndroid Build Coastguard Worker * 115*a65addddSAndroid Build Coastguard Worker * That works if Foo has an Inject typedef or a constructor wrapped in INJECT. 116*a65addddSAndroid Build Coastguard Worker */ 117*a65addddSAndroid Build Coastguard Worker PartialComponent<> createComponent(); 118*a65addddSAndroid Build Coastguard Worker 119*a65addddSAndroid Build Coastguard Worker /** 120*a65addddSAndroid Build Coastguard Worker * A partially constructed component. 121*a65addddSAndroid Build Coastguard Worker * 122*a65addddSAndroid Build Coastguard Worker * Client code should never write `PartialComponent'; always start the construction of a component with 123*a65addddSAndroid Build Coastguard Worker * fruit::createComponent(), and end it by casting the PartialComponent to the desired Component (often done implicitly 124*a65addddSAndroid Build Coastguard Worker * by returning a PartialComponent from a function that has Component<...> as the return type). 125*a65addddSAndroid Build Coastguard Worker * 126*a65addddSAndroid Build Coastguard Worker * The template parameter is used to propagate information about bound types, it is purely an implementation detail; 127*a65addddSAndroid Build Coastguard Worker * users of the Fruit library can pretend that this class is not templated, in no case a specific template parameter is 128*a65addddSAndroid Build Coastguard Worker * required. All methods of this class return a PartialComponent, which allows to chain method invocations without 129*a65addddSAndroid Build Coastguard Worker * declaring a variable of type PartialComponent. 130*a65addddSAndroid Build Coastguard Worker * 131*a65addddSAndroid Build Coastguard Worker * Example usage: 132*a65addddSAndroid Build Coastguard Worker * 133*a65addddSAndroid Build Coastguard Worker * fruit::Component<Foo> getFooComponent() { 134*a65addddSAndroid Build Coastguard Worker * return fruit::createComponent() 135*a65addddSAndroid Build Coastguard Worker * .install(getComponent1) 136*a65addddSAndroid Build Coastguard Worker * .install(getComponent2) 137*a65addddSAndroid Build Coastguard Worker * .bind<Foo, FooImpl>(); 138*a65addddSAndroid Build Coastguard Worker * } 139*a65addddSAndroid Build Coastguard Worker * 140*a65addddSAndroid Build Coastguard Worker * Note that no variable of type PartialComponent has been declared; this class should only be used for temporary 141*a65addddSAndroid Build Coastguard Worker * values. 142*a65addddSAndroid Build Coastguard Worker */ 143*a65addddSAndroid Build Coastguard Worker template <typename... Bindings> 144*a65addddSAndroid Build Coastguard Worker class PartialComponent { 145*a65addddSAndroid Build Coastguard Worker public: 146*a65addddSAndroid Build Coastguard Worker PartialComponent& operator=(PartialComponent&&) = delete; 147*a65addddSAndroid Build Coastguard Worker PartialComponent& operator=(const PartialComponent&) = delete; 148*a65addddSAndroid Build Coastguard Worker 149*a65addddSAndroid Build Coastguard Worker /** 150*a65addddSAndroid Build Coastguard Worker * This tells Fruit that "the implementation of I is C". 151*a65addddSAndroid Build Coastguard Worker * I must be a base class of C, and it's typically (but not necessarily) an abstract class. 152*a65addddSAndroid Build Coastguard Worker * C is typically a concrete class, but it doesn't have to be: for example, if A inherits from B and B inherits from C 153*a65addddSAndroid Build Coastguard Worker * you can specify bind<C, B>() and bind<B, A>(). 154*a65addddSAndroid Build Coastguard Worker * 155*a65addddSAndroid Build Coastguard Worker * The most common use of this is to bind the type I to the type C, for example: 156*a65addddSAndroid Build Coastguard Worker * 157*a65addddSAndroid Build Coastguard Worker * class MyInterface { 158*a65addddSAndroid Build Coastguard Worker * public: 159*a65addddSAndroid Build Coastguard Worker * virtual void foo() = 0; 160*a65addddSAndroid Build Coastguard Worker * }; 161*a65addddSAndroid Build Coastguard Worker * 162*a65addddSAndroid Build Coastguard Worker * class MyImplementation { 163*a65addddSAndroid Build Coastguard Worker * public: 164*a65addddSAndroid Build Coastguard Worker * INJECT(MyImplementation()) {} 165*a65addddSAndroid Build Coastguard Worker * 166*a65addddSAndroid Build Coastguard Worker * void foo() { 167*a65addddSAndroid Build Coastguard Worker * ... 168*a65addddSAndroid Build Coastguard Worker * } 169*a65addddSAndroid Build Coastguard Worker * }; 170*a65addddSAndroid Build Coastguard Worker * 171*a65addddSAndroid Build Coastguard Worker * fruit::Component<MyInterface> getMyInterfaceComponent() { 172*a65addddSAndroid Build Coastguard Worker * return fruit::createComponent() 173*a65addddSAndroid Build Coastguard Worker * .bind<MyInterface, MyImplementation>(); 174*a65addddSAndroid Build Coastguard Worker * } 175*a65addddSAndroid Build Coastguard Worker * 176*a65addddSAndroid Build Coastguard Worker * The implementation class can be bound in any way, it doesn't need to be bound using constructor injection as above 177*a65addddSAndroid Build Coastguard Worker * (although that's a very common use case). 178*a65addddSAndroid Build Coastguard Worker * 179*a65addddSAndroid Build Coastguard Worker * You can bind an interface to a type bound using a constant binding (see the bindInstance method that takes a const& 180*a65addddSAndroid Build Coastguard Worker * for more details), however the interface will then also be considered bound with a constant binding, and you must 181*a65addddSAndroid Build Coastguard Worker * declare that in the returned Component's type. For example: 182*a65addddSAndroid Build Coastguard Worker * 183*a65addddSAndroid Build Coastguard Worker * fruit::Component<const MyInterface> getMyInterfaceComponent() { 184*a65addddSAndroid Build Coastguard Worker * static const MyImplementation my_implementation = MyImplementation(); 185*a65addddSAndroid Build Coastguard Worker * return fruit::createComponent() 186*a65addddSAndroid Build Coastguard Worker * .bindInstance(my_implementation) 187*a65addddSAndroid Build Coastguard Worker * .bind<MyInterface, MyImplementation>(); 188*a65addddSAndroid Build Coastguard Worker * } 189*a65addddSAndroid Build Coastguard Worker * 190*a65addddSAndroid Build Coastguard Worker * In addition to binding the type I to the type C, a `bind()` can also be used to bind a 191*a65addddSAndroid Build Coastguard Worker * std::function<std::unique_ptr<I>(Args...)> to a std::function<std::unique_ptr<C>(Args...)> or a 192*a65addddSAndroid Build Coastguard Worker * std::function<C(Args...)>. For example: 193*a65addddSAndroid Build Coastguard Worker * 194*a65addddSAndroid Build Coastguard Worker * fruit::Component<std::function<std::unique_ptr<MyInterface>(int)>> getIFactoryComponent() { 195*a65addddSAndroid Build Coastguard Worker * static const std::function<MyImplementation(int)> cFactory = [](int n) { return MyImplementation(n); }; 196*a65addddSAndroid Build Coastguard Worker * return fruit::createComponent() 197*a65addddSAndroid Build Coastguard Worker * .bind<MyInterface, MyImplementation>() 198*a65addddSAndroid Build Coastguard Worker * .bindInstance(cFactory); 199*a65addddSAndroid Build Coastguard Worker * } 200*a65addddSAndroid Build Coastguard Worker * 201*a65addddSAndroid Build Coastguard Worker * Or alternatively you can do the same using constructor injection: 202*a65addddSAndroid Build Coastguard Worker * 203*a65addddSAndroid Build Coastguard Worker * class MyImplementation { 204*a65addddSAndroid Build Coastguard Worker * public: 205*a65addddSAndroid Build Coastguard Worker * INJECT(MyImplementation(ASSISTED(int) n)) 206*a65addddSAndroid Build Coastguard Worker * : n(n) { 207*a65addddSAndroid Build Coastguard Worker * } 208*a65addddSAndroid Build Coastguard Worker * ... 209*a65addddSAndroid Build Coastguard Worker * }; 210*a65addddSAndroid Build Coastguard Worker * 211*a65addddSAndroid Build Coastguard Worker * fruit::Component<std::function<std::unique_ptr<MyInterface>(int)>> getIFactoryComponent() { 212*a65addddSAndroid Build Coastguard Worker * return fruit::createComponent() 213*a65addddSAndroid Build Coastguard Worker * .bind<MyInterface, MyImplementation>(); 214*a65addddSAndroid Build Coastguard Worker * } 215*a65addddSAndroid Build Coastguard Worker * 216*a65addddSAndroid Build Coastguard Worker * Fruit determines the actual binding(s) to generate based on the types you declare as provided in the returned 217*a65addddSAndroid Build Coastguard Worker * Component<> type (e.g. in the last example std::function<std::unique_ptr<MyInterface>(int)> is declared as provided 218*a65addddSAndroid Build Coastguard Worker * so Fruit will generate a factory binding instead of a normal MyInterface->MyImplementation binding. 219*a65addddSAndroid Build Coastguard Worker * 220*a65addddSAndroid Build Coastguard Worker * Fruit can also detect types that are needed for constructor/provider/factory bindings, and it will then use that 221*a65addddSAndroid Build Coastguard Worker * information to determine the desired binding. For example: 222*a65addddSAndroid Build Coastguard Worker * 223*a65addddSAndroid Build Coastguard Worker * class MyImplementation { 224*a65addddSAndroid Build Coastguard Worker * public: 225*a65addddSAndroid Build Coastguard Worker * INJECT(MyImplementation(ASSISTED(int) n)) 226*a65addddSAndroid Build Coastguard Worker * : n(n) { 227*a65addddSAndroid Build Coastguard Worker * } 228*a65addddSAndroid Build Coastguard Worker * ... 229*a65addddSAndroid Build Coastguard Worker * }; 230*a65addddSAndroid Build Coastguard Worker * 231*a65addddSAndroid Build Coastguard Worker * class Foo { 232*a65addddSAndroid Build Coastguard Worker * private: 233*a65addddSAndroid Build Coastguard Worker * std::function<std::unique_ptr<MyInterface>(int)> factory; 234*a65addddSAndroid Build Coastguard Worker * public: 235*a65addddSAndroid Build Coastguard Worker * INJECT(Foo(std::function<std::unique_ptr<MyInterface>(int)> factory)) 236*a65addddSAndroid Build Coastguard Worker * : factory(factory) { 237*a65addddSAndroid Build Coastguard Worker * } 238*a65addddSAndroid Build Coastguard Worker * }; 239*a65addddSAndroid Build Coastguard Worker * 240*a65addddSAndroid Build Coastguard Worker * fruit::Component<Foo> getIFactoryComponent() { 241*a65addddSAndroid Build Coastguard Worker * return fruit::createComponent() 242*a65addddSAndroid Build Coastguard Worker * // We want to bind Foo, which requires std::function<std::unique_ptr<MyInterface>(int)>. 243*a65addddSAndroid Build Coastguard Worker * // So std::function<std::unique_ptr<MyInterface>(int)> will be bound to 244*a65addddSAndroid Build Coastguard Worker * // std::function<std::unique_ptr<MyImplementation>(int)>, and that will be bound using constructor injection. 245*a65addddSAndroid Build Coastguard Worker * .bind<MyInterface, MyImplementation>(); 246*a65addddSAndroid Build Coastguard Worker * } 247*a65addddSAndroid Build Coastguard Worker * 248*a65addddSAndroid Build Coastguard Worker * All these cases support annotated injection, just wrap I and/or C in fruit::Annotated<> if desired. For example: 249*a65addddSAndroid Build Coastguard Worker * 250*a65addddSAndroid Build Coastguard Worker * struct MyAnnotation{}; 251*a65addddSAndroid Build Coastguard Worker * 252*a65addddSAndroid Build Coastguard Worker * fruit::Component<fruit::Annotated<MyAnnotation, MyInterface>> getMyInterfaceComponent() { 253*a65addddSAndroid Build Coastguard Worker * return fruit::createComponent() 254*a65addddSAndroid Build Coastguard Worker * .bind<fruit::Annotated<MyAnnotation, MyInterface>, MyImplementation>(); 255*a65addddSAndroid Build Coastguard Worker * } 256*a65addddSAndroid Build Coastguard Worker */ 257*a65addddSAndroid Build Coastguard Worker template <typename I, typename C> 258*a65addddSAndroid Build Coastguard Worker PartialComponent<fruit::impl::Bind<I, C>, Bindings...> bind(); 259*a65addddSAndroid Build Coastguard Worker 260*a65addddSAndroid Build Coastguard Worker /** 261*a65addddSAndroid Build Coastguard Worker * Registers Signature as the constructor signature to use to inject a type. 262*a65addddSAndroid Build Coastguard Worker * 263*a65addddSAndroid Build Coastguard Worker * Example usage: 264*a65addddSAndroid Build Coastguard Worker * 265*a65addddSAndroid Build Coastguard Worker * fruit::createComponent() 266*a65addddSAndroid Build Coastguard Worker * .registerConstructor<Foo(Bar*,Baz*)>() // Registers the constructor Foo::Foo(Bar*,Baz*) 267*a65addddSAndroid Build Coastguard Worker * 268*a65addddSAndroid Build Coastguard Worker * It's usually more convenient to use an INJECT macro or Inject typedef instead, e.g.: 269*a65addddSAndroid Build Coastguard Worker * 270*a65addddSAndroid Build Coastguard Worker * class Foo { 271*a65addddSAndroid Build Coastguard Worker * public: 272*a65addddSAndroid Build Coastguard Worker * // This also declares the constructor 273*a65addddSAndroid Build Coastguard Worker * INJECT(Foo(Bar* bar, Baz* baz)); 274*a65addddSAndroid Build Coastguard Worker * ... 275*a65addddSAndroid Build Coastguard Worker * }; 276*a65addddSAndroid Build Coastguard Worker * 277*a65addddSAndroid Build Coastguard Worker * or (equivalent): 278*a65addddSAndroid Build Coastguard Worker * 279*a65addddSAndroid Build Coastguard Worker * class Foo { 280*a65addddSAndroid Build Coastguard Worker * public: 281*a65addddSAndroid Build Coastguard Worker * using Inject = Foo(Bar*, Baz*); 282*a65addddSAndroid Build Coastguard Worker * Foo(Bar* bar, Baz* baz); 283*a65addddSAndroid Build Coastguard Worker * ... 284*a65addddSAndroid Build Coastguard Worker * }; 285*a65addddSAndroid Build Coastguard Worker * 286*a65addddSAndroid Build Coastguard Worker * Use registerConstructor() when you want to inject the class C in different ways in different components (just make 287*a65addddSAndroid Build Coastguard Worker * sure those don't end up in the same injector, or use annotated injection to prevent the bindings from clashing), 288*a65addddSAndroid Build Coastguard Worker * or when C is a third-party class that can't be modified. 289*a65addddSAndroid Build Coastguard Worker * 290*a65addddSAndroid Build Coastguard Worker * This supports annotated injection, just wrap the desired types (return type and/or argument types of the signature) 291*a65addddSAndroid Build Coastguard Worker * with fruit::Annotated<> if desired. For example: 292*a65addddSAndroid Build Coastguard Worker * 293*a65addddSAndroid Build Coastguard Worker * struct Annotation1 {}; 294*a65addddSAndroid Build Coastguard Worker * struct Annotation2 {}; 295*a65addddSAndroid Build Coastguard Worker * 296*a65addddSAndroid Build Coastguard Worker * struct Foo { 297*a65addddSAndroid Build Coastguard Worker * Foo(Bar* bar) {...} 298*a65addddSAndroid Build Coastguard Worker * }; 299*a65addddSAndroid Build Coastguard Worker * 300*a65addddSAndroid Build Coastguard Worker * fruit::Component<fruit::Annotated<Annotation1, Bar>> getBarComponent() {...} 301*a65addddSAndroid Build Coastguard Worker * 302*a65addddSAndroid Build Coastguard Worker * fruit::Component<fruit::Annotated<Annotation2, Foo>> getFooComponent() { 303*a65addddSAndroid Build Coastguard Worker * return fruit::createComponent() 304*a65addddSAndroid Build Coastguard Worker * .install(getBarComponent) 305*a65addddSAndroid Build Coastguard Worker * .registerConstructor<fruit::Annotated<Annotation2, Foo>(fruit::Annotated<Annotation1, Bar*>)>(); 306*a65addddSAndroid Build Coastguard Worker * } 307*a65addddSAndroid Build Coastguard Worker * 308*a65addddSAndroid Build Coastguard Worker * This does *not* support assisted injection, for that you should use registerFactory() instead. 309*a65addddSAndroid Build Coastguard Worker * 310*a65addddSAndroid Build Coastguard Worker * The allowed argument types in the signature are, for any class (or fundamental) type C: 311*a65addddSAndroid Build Coastguard Worker * 312*a65addddSAndroid Build Coastguard Worker * C 313*a65addddSAndroid Build Coastguard Worker * C* 314*a65addddSAndroid Build Coastguard Worker * C& 315*a65addddSAndroid Build Coastguard Worker * const C* 316*a65addddSAndroid Build Coastguard Worker * const C& 317*a65addddSAndroid Build Coastguard Worker * shared_ptr<C> 318*a65addddSAndroid Build Coastguard Worker * Provider<C> 319*a65addddSAndroid Build Coastguard Worker * Provider<const C> 320*a65addddSAndroid Build Coastguard Worker * Annotated<Annotation, C> (for any type `Annotation') 321*a65addddSAndroid Build Coastguard Worker * Annotated<Annotation, C*> (for any type `Annotation') 322*a65addddSAndroid Build Coastguard Worker * Annotated<Annotation, C&> (for any type `Annotation') 323*a65addddSAndroid Build Coastguard Worker * Annotated<Annotation, const C*> (for any type `Annotation') 324*a65addddSAndroid Build Coastguard Worker * Annotated<Annotation, const C&> (for any type `Annotation') 325*a65addddSAndroid Build Coastguard Worker * Annotated<Annotation, shared_ptr<C>> (for any type `Annotation') 326*a65addddSAndroid Build Coastguard Worker * Annotated<Annotation, Provider<C>> (for any type `Annotation') 327*a65addddSAndroid Build Coastguard Worker * Annotated<Annotation, Provider<const C>> (for any type `Annotation') 328*a65addddSAndroid Build Coastguard Worker */ 329*a65addddSAndroid Build Coastguard Worker template <typename Signature> 330*a65addddSAndroid Build Coastguard Worker PartialComponent<fruit::impl::RegisterConstructor<Signature>, Bindings...> registerConstructor(); 331*a65addddSAndroid Build Coastguard Worker 332*a65addddSAndroid Build Coastguard Worker /** 333*a65addddSAndroid Build Coastguard Worker * Use this method to bind the type C to a specific instance. 334*a65addddSAndroid Build Coastguard Worker * The caller must ensure that the provided reference is valid for the entire lifetime of the component and of any 335*a65addddSAndroid Build Coastguard Worker * components or injectors that install this component; the caller must also ensure that the object is destroyed after 336*a65addddSAndroid Build Coastguard Worker * the last components/injectors using it are destroyed. 337*a65addddSAndroid Build Coastguard Worker * 338*a65addddSAndroid Build Coastguard Worker * Example usage: 339*a65addddSAndroid Build Coastguard Worker * 340*a65addddSAndroid Build Coastguard Worker * Component<Request> getRequestComponent(Request* request) { 341*a65addddSAndroid Build Coastguard Worker * return fruit::createComponent() 342*a65addddSAndroid Build Coastguard Worker * .bindInstance(*request); 343*a65addddSAndroid Build Coastguard Worker * } 344*a65addddSAndroid Build Coastguard Worker * 345*a65addddSAndroid Build Coastguard Worker * NormalizedComponent<...> normalizedComponent = ...; 346*a65addddSAndroid Build Coastguard Worker * Request request; 347*a65addddSAndroid Build Coastguard Worker * Injector<...> injector(normalizedComponent, 348*a65addddSAndroid Build Coastguard Worker * getRequestComponent, 349*a65addddSAndroid Build Coastguard Worker * request)); 350*a65addddSAndroid Build Coastguard Worker * 351*a65addddSAndroid Build Coastguard Worker * This should be used sparingly (you should let Fruit handle the object lifetime when possible), but in some cases it 352*a65addddSAndroid Build Coastguard Worker * is necessary; for example, if a web server creates an injector to handle each request, this method can be used to 353*a65addddSAndroid Build Coastguard Worker * inject the request itself as in the example above (see the Server page in the Fruit tutorial for more details). 354*a65addddSAndroid Build Coastguard Worker * 355*a65addddSAndroid Build Coastguard Worker * It's also possible to bind constants, see the documentation of the bindInstance() method taking a const& for 356*a65addddSAndroid Build Coastguard Worker * details. 357*a65addddSAndroid Build Coastguard Worker */ 358*a65addddSAndroid Build Coastguard Worker template <typename C> 359*a65addddSAndroid Build Coastguard Worker PartialComponent<fruit::impl::BindInstance<C, C>, Bindings...> bindInstance(C& instance); 360*a65addddSAndroid Build Coastguard Worker 361*a65addddSAndroid Build Coastguard Worker /** 362*a65addddSAndroid Build Coastguard Worker * Similar to the previous, but binds a const&. Note that the reference must still outlive the component/injector 363*a65addddSAndroid Build Coastguard Worker * as in the non-const case. 364*a65addddSAndroid Build Coastguard Worker * When using this method, you must declare that the type is constant in the Component type. For example: 365*a65addddSAndroid Build Coastguard Worker * 366*a65addddSAndroid Build Coastguard Worker * Component<const MyExpensiveClass> getMyExpensiveClassComponent() { 367*a65addddSAndroid Build Coastguard Worker * static const MyExpensiveClass my_expensive_class = createMyExpensiveClass(); 368*a65addddSAndroid Build Coastguard Worker * return fruit::createComponent() 369*a65addddSAndroid Build Coastguard Worker * .bindInstance(my_expensive_class); 370*a65addddSAndroid Build Coastguard Worker * } 371*a65addddSAndroid Build Coastguard Worker * 372*a65addddSAndroid Build Coastguard Worker * Constant bindings can be used as other bindings, except that you can only inject the constant type (e.g. as a 373*a65addddSAndroid Build Coastguard Worker * constructor parameter) as: 374*a65addddSAndroid Build Coastguard Worker * 375*a65addddSAndroid Build Coastguard Worker * C 376*a65addddSAndroid Build Coastguard Worker * const C* 377*a65addddSAndroid Build Coastguard Worker * const C& 378*a65addddSAndroid Build Coastguard Worker * Provider<const C> 379*a65addddSAndroid Build Coastguard Worker * Annotated<Annotation, C> (for any type `Annotation') 380*a65addddSAndroid Build Coastguard Worker * Annotated<Annotation, const C*> (for any type `Annotation') 381*a65addddSAndroid Build Coastguard Worker * Annotated<Annotation, const C&> (for any type `Annotation') 382*a65addddSAndroid Build Coastguard Worker * Annotated<Annotation, Provider<const C>> (for any type `Annotation') 383*a65addddSAndroid Build Coastguard Worker * 384*a65addddSAndroid Build Coastguard Worker * While you can't inject it as: 385*a65addddSAndroid Build Coastguard Worker * 386*a65addddSAndroid Build Coastguard Worker * C* 387*a65addddSAndroid Build Coastguard Worker * C& 388*a65addddSAndroid Build Coastguard Worker * shared_ptr<C> 389*a65addddSAndroid Build Coastguard Worker * Provider<C> 390*a65addddSAndroid Build Coastguard Worker * Annotated<Annotation, C*> (for any type `Annotation') 391*a65addddSAndroid Build Coastguard Worker * Annotated<Annotation, C&> (for any type `Annotation') 392*a65addddSAndroid Build Coastguard Worker * Annotated<Annotation, shared_ptr<C>> (for any type `Annotation') 393*a65addddSAndroid Build Coastguard Worker * Annotated<Annotation, Provider<C>> (for any type `Annotation') 394*a65addddSAndroid Build Coastguard Worker */ 395*a65addddSAndroid Build Coastguard Worker template <typename C> 396*a65addddSAndroid Build Coastguard Worker PartialComponent<fruit::impl::BindConstInstance<C, C>, Bindings...> bindInstance(const C& instance); 397*a65addddSAndroid Build Coastguard Worker 398*a65addddSAndroid Build Coastguard Worker /** 399*a65addddSAndroid Build Coastguard Worker * This is deleted to catch cases where the instance would likely be destroyed before the component/injectors. 400*a65addddSAndroid Build Coastguard Worker */ 401*a65addddSAndroid Build Coastguard Worker template <typename C> 402*a65addddSAndroid Build Coastguard Worker PartialComponent<fruit::impl::BindConstInstance<C, C>, Bindings...> bindInstance(C&&) = delete; 403*a65addddSAndroid Build Coastguard Worker 404*a65addddSAndroid Build Coastguard Worker /** 405*a65addddSAndroid Build Coastguard Worker * Similar to the first version of bindInstance(), but allows to specify an annotated type that 406*a65addddSAndroid Build Coastguard Worker * will be bound to the specified value. 407*a65addddSAndroid Build Coastguard Worker * For example, to bind an instance to the type fruit::Annotated<Hostname, std::string>, you can use: 408*a65addddSAndroid Build Coastguard Worker * 409*a65addddSAndroid Build Coastguard Worker * fruit::Component<fruit::Annotated<Hostname, std::string>> getHostnameComponent(std::string* hostname) { 410*a65addddSAndroid Build Coastguard Worker * fruit::createComponent() 411*a65addddSAndroid Build Coastguard Worker * .bindInstance<fruit::Annotated<Hostname, std::string>>(*hostname); 412*a65addddSAndroid Build Coastguard Worker * } 413*a65addddSAndroid Build Coastguard Worker */ 414*a65addddSAndroid Build Coastguard Worker template <typename AnnotatedType, typename C> 415*a65addddSAndroid Build Coastguard Worker PartialComponent<fruit::impl::BindInstance<AnnotatedType, C>, Bindings...> bindInstance(C& instance); 416*a65addddSAndroid Build Coastguard Worker 417*a65addddSAndroid Build Coastguard Worker /** 418*a65addddSAndroid Build Coastguard Worker * Similar to the previous, but binds a const&. Example usage: 419*a65addddSAndroid Build Coastguard Worker * 420*a65addddSAndroid Build Coastguard Worker * fruit::Component<fruit::Annotated<Hostname, const std::string>> getHostnameComponent() { 421*a65addddSAndroid Build Coastguard Worker * static const std::string hostname = determineHostname(); 422*a65addddSAndroid Build Coastguard Worker * fruit::createComponent() 423*a65addddSAndroid Build Coastguard Worker * .bindInstance<fruit::Annotated<Hostname, std::string>>(hostname); 424*a65addddSAndroid Build Coastguard Worker * } 425*a65addddSAndroid Build Coastguard Worker * 426*a65addddSAndroid Build Coastguard Worker * See the documentation for the bindInstance() overload that takes a non-annotated const& for more details. 427*a65addddSAndroid Build Coastguard Worker */ 428*a65addddSAndroid Build Coastguard Worker template <typename AnnotatedType, typename C> 429*a65addddSAndroid Build Coastguard Worker PartialComponent<fruit::impl::BindConstInstance<AnnotatedType, C>, Bindings...> bindInstance(const C& instance); 430*a65addddSAndroid Build Coastguard Worker 431*a65addddSAndroid Build Coastguard Worker /** 432*a65addddSAndroid Build Coastguard Worker * This is deleted to catch cases where the instance would likely be destroyed before the component/injectors. 433*a65addddSAndroid Build Coastguard Worker */ 434*a65addddSAndroid Build Coastguard Worker template <typename AnnotatedType, typename C> 435*a65addddSAndroid Build Coastguard Worker PartialComponent<fruit::impl::BindConstInstance<AnnotatedType, C>, Bindings...> bindInstance(C&& instance); 436*a65addddSAndroid Build Coastguard Worker 437*a65addddSAndroid Build Coastguard Worker /** 438*a65addddSAndroid Build Coastguard Worker * Registers `provider' as a provider of C, where provider is a lambda with no captures returning either C or C* 439*a65addddSAndroid Build Coastguard Worker * (prefer returning a C by value instead of allocating a C using `new C', to avoid the allocation). 440*a65addddSAndroid Build Coastguard Worker * 441*a65addddSAndroid Build Coastguard Worker * When injecting a C, the arguments of the provider will be injected and the provider will then be called to create 442*a65addddSAndroid Build Coastguard Worker * the C instance, that will then be stored in the injector. 443*a65addddSAndroid Build Coastguard Worker * 444*a65addddSAndroid Build Coastguard Worker * If `provider' returns a pointer, it must be non-null; otherwise the program will abort. 445*a65addddSAndroid Build Coastguard Worker * 446*a65addddSAndroid Build Coastguard Worker * Example: 447*a65addddSAndroid Build Coastguard Worker * 448*a65addddSAndroid Build Coastguard Worker * fruit::Component<Foo> getFooComponent() { 449*a65addddSAndroid Build Coastguard Worker * return fruit::createComponent() 450*a65addddSAndroid Build Coastguard Worker * .install(getBarComponent) 451*a65addddSAndroid Build Coastguard Worker * .install(getBazComponent) 452*a65addddSAndroid Build Coastguard Worker * .registerProvider([](Bar* bar, Baz* baz) { 453*a65addddSAndroid Build Coastguard Worker * Foo foo(bar, baz); 454*a65addddSAndroid Build Coastguard Worker * foo.initialize(); 455*a65addddSAndroid Build Coastguard Worker * return foo; 456*a65addddSAndroid Build Coastguard Worker * }); 457*a65addddSAndroid Build Coastguard Worker * } 458*a65addddSAndroid Build Coastguard Worker * 459*a65addddSAndroid Build Coastguard Worker * As in the previous example, it's not necessary to specify the type parameter, it will be inferred by the compiler. 460*a65addddSAndroid Build Coastguard Worker * 461*a65addddSAndroid Build Coastguard Worker * registerProvider() can't be called with a plain function, but you can write a lambda that wraps the function to 462*a65addddSAndroid Build Coastguard Worker * achieve the same result. 463*a65addddSAndroid Build Coastguard Worker * 464*a65addddSAndroid Build Coastguard Worker * Registering stateful functors (including lambdas with captures) is NOT supported. 465*a65addddSAndroid Build Coastguard Worker * However, you can write something like: 466*a65addddSAndroid Build Coastguard Worker * 467*a65addddSAndroid Build Coastguard Worker * struct Functor { 468*a65addddSAndroid Build Coastguard Worker * Functor(int n) {...} 469*a65addddSAndroid Build Coastguard Worker * MyClass operator()(Foo* foo) const {...} 470*a65addddSAndroid Build Coastguard Worker * }; 471*a65addddSAndroid Build Coastguard Worker * 472*a65addddSAndroid Build Coastguard Worker * Component<MyClass> getMyClassComponent() { 473*a65addddSAndroid Build Coastguard Worker * static const Functor aFunctor(42); 474*a65addddSAndroid Build Coastguard Worker * return fruit::createComponent() 475*a65addddSAndroid Build Coastguard Worker * .install(getFooComponent) 476*a65addddSAndroid Build Coastguard Worker * .bindInstance(aFunctor) 477*a65addddSAndroid Build Coastguard Worker * .registerProvider([](const Functor& functor, Foo* foo) { return functor(foo); }); 478*a65addddSAndroid Build Coastguard Worker * } 479*a65addddSAndroid Build Coastguard Worker */ 480*a65addddSAndroid Build Coastguard Worker template <typename Lambda> 481*a65addddSAndroid Build Coastguard Worker PartialComponent<fruit::impl::RegisterProvider<Lambda>, Bindings...> registerProvider(Lambda lambda); 482*a65addddSAndroid Build Coastguard Worker 483*a65addddSAndroid Build Coastguard Worker /** 484*a65addddSAndroid Build Coastguard Worker * Similar to the previous version of registerProvider(), but allows to specify an annotated type 485*a65addddSAndroid Build Coastguard Worker * for the provider. This allows to inject annotated types in the parameters and/or bind the 486*a65addddSAndroid Build Coastguard Worker * provider to an annotated type. For example: 487*a65addddSAndroid Build Coastguard Worker * 488*a65addddSAndroid Build Coastguard Worker * struct MyAnnotation1 {}; 489*a65addddSAndroid Build Coastguard Worker * struct MyAnnotation2 {}; 490*a65addddSAndroid Build Coastguard Worker * 491*a65addddSAndroid Build Coastguard Worker * Component<fruit::Annotated<Annotation1, Bar>> getBarComponent() {...} 492*a65addddSAndroid Build Coastguard Worker * Component<Baz> getBazComponent() {...} 493*a65addddSAndroid Build Coastguard Worker * 494*a65addddSAndroid Build Coastguard Worker * fruit::Component<fruit::Annotated<Annotation2, Foo>> getFooComponent() { 495*a65addddSAndroid Build Coastguard Worker * return fruit::createComponent() 496*a65addddSAndroid Build Coastguard Worker * .install(getBarComponent) 497*a65addddSAndroid Build Coastguard Worker * .install(getBazComponent) 498*a65addddSAndroid Build Coastguard Worker * .registerProvider< 499*a65addddSAndroid Build Coastguard Worker * fruit::Annotated<Annotation2, Foo>( 500*a65addddSAndroid Build Coastguard Worker * fruit::Annotated<Annotation1, Bar*>, 501*a65addddSAndroid Build Coastguard Worker * Baz*) 502*a65addddSAndroid Build Coastguard Worker * >([](Bar* bar, Baz* baz) { 503*a65addddSAndroid Build Coastguard Worker * Foo foo(bar, baz); 504*a65addddSAndroid Build Coastguard Worker * foo.initialize(); 505*a65addddSAndroid Build Coastguard Worker * return foo; 506*a65addddSAndroid Build Coastguard Worker * }); 507*a65addddSAndroid Build Coastguard Worker * } 508*a65addddSAndroid Build Coastguard Worker */ 509*a65addddSAndroid Build Coastguard Worker template <typename AnnotatedSignature, typename Lambda> 510*a65addddSAndroid Build Coastguard Worker PartialComponent<fruit::impl::RegisterProvider<AnnotatedSignature, Lambda>, Bindings...> 511*a65addddSAndroid Build Coastguard Worker registerProvider(Lambda lambda); 512*a65addddSAndroid Build Coastguard Worker 513*a65addddSAndroid Build Coastguard Worker /** 514*a65addddSAndroid Build Coastguard Worker * Similar to bind<I, C>(), but adds a multibinding instead. 515*a65addddSAndroid Build Coastguard Worker * 516*a65addddSAndroid Build Coastguard Worker * Multibindings are independent from bindings; creating a binding with bind doesn't count as a multibinding, and 517*a65addddSAndroid Build Coastguard Worker * adding a multibinding doesn't allow to inject the type (it only allows to retrieve multibindings through the 518*a65addddSAndroid Build Coastguard Worker * getMultibindings method of the injector). 519*a65addddSAndroid Build Coastguard Worker * 520*a65addddSAndroid Build Coastguard Worker * Unlike bindings, where adding a the same binding twice is allowed (and ignored), adding the same multibinding 521*a65addddSAndroid Build Coastguard Worker * multiple times will result in the creation of multiple "equivalent" instances, that will all be returned by 522*a65addddSAndroid Build Coastguard Worker * getMultibindings(). 523*a65addddSAndroid Build Coastguard Worker * 524*a65addddSAndroid Build Coastguard Worker * Another difference compared with normal bindings is that this can't be used to bind a 525*a65addddSAndroid Build Coastguard Worker * std::function<std::unique_ptr<I>(Args...)> to a std::function<std::unique_ptr<C>(Args...)> or a 526*a65addddSAndroid Build Coastguard Worker * std::function<C(Args...)>. 527*a65addddSAndroid Build Coastguard Worker * 528*a65addddSAndroid Build Coastguard Worker * As bind(), this supports annotated injection, just wrap I and/or C in fruit::Annotated<> if desired. See the 529*a65addddSAndroid Build Coastguard Worker * documentation of bind() for more details. 530*a65addddSAndroid Build Coastguard Worker */ 531*a65addddSAndroid Build Coastguard Worker template <typename I, typename C> 532*a65addddSAndroid Build Coastguard Worker PartialComponent<fruit::impl::AddMultibinding<I, C>, Bindings...> addMultibinding(); 533*a65addddSAndroid Build Coastguard Worker 534*a65addddSAndroid Build Coastguard Worker /** 535*a65addddSAndroid Build Coastguard Worker * Similar to bindInstance(), but adds a multibinding instead. 536*a65addddSAndroid Build Coastguard Worker * 537*a65addddSAndroid Build Coastguard Worker * Multibindings are independent from bindings; creating a binding with bindInstance doesn't count as a 538*a65addddSAndroid Build Coastguard Worker * multibinding, and adding a multibinding doesn't allow to inject the type (it only allows to retrieve 539*a65addddSAndroid Build Coastguard Worker * multibindings through the getMultibindings method of the injector). 540*a65addddSAndroid Build Coastguard Worker * 541*a65addddSAndroid Build Coastguard Worker * Unlike bindings, where adding a the same binding twice is allowed (and ignored), adding several multibindings for 542*a65addddSAndroid Build Coastguard Worker * the same instance will result in duplicated values in the result of getMultibindings. 543*a65addddSAndroid Build Coastguard Worker * 544*a65addddSAndroid Build Coastguard Worker * Another difference compared to bindInstance() is that you can't use this to bind a const& (note that there is no 545*a65addddSAndroid Build Coastguard Worker * overload of this method that takes a const&). 546*a65addddSAndroid Build Coastguard Worker * 547*a65addddSAndroid Build Coastguard Worker * This method adds a multibinding for C. If the object implements an interface I and you want to add a multibinding 548*a65addddSAndroid Build Coastguard Worker * for that interface instead, you must cast the object to I& before passing it to this method. 549*a65addddSAndroid Build Coastguard Worker * 550*a65addddSAndroid Build Coastguard Worker * Note that this takes the instance by reference, not by value; it must remain valid for the entire lifetime of this 551*a65addddSAndroid Build Coastguard Worker * component and of any injectors created from this component. 552*a65addddSAndroid Build Coastguard Worker * 553*a65addddSAndroid Build Coastguard Worker * Example use: 554*a65addddSAndroid Build Coastguard Worker * 555*a65addddSAndroid Build Coastguard Worker * class MyClass { 556*a65addddSAndroid Build Coastguard Worker * ... 557*a65addddSAndroid Build Coastguard Worker * }; 558*a65addddSAndroid Build Coastguard Worker * 559*a65addddSAndroid Build Coastguard Worker * fruit::Component<> getMyComponent() { 560*a65addddSAndroid Build Coastguard Worker * static MyClass x = MyClass(...); 561*a65addddSAndroid Build Coastguard Worker * static MyClass y = MyClass(...); 562*a65addddSAndroid Build Coastguard Worker * return fruit::createComponent() 563*a65addddSAndroid Build Coastguard Worker * .addInstanceMultibinding(x) 564*a65addddSAndroid Build Coastguard Worker * .addInstanceMultibinding(y); 565*a65addddSAndroid Build Coastguard Worker * } 566*a65addddSAndroid Build Coastguard Worker * 567*a65addddSAndroid Build Coastguard Worker * fruit::Injector<> injector(getMyComponent); 568*a65addddSAndroid Build Coastguard Worker * // This vector contains {&x, &y}. 569*a65addddSAndroid Build Coastguard Worker * const std::vector<MyClass*>& objects = injector.getMultibindings<MyClass>(); 570*a65addddSAndroid Build Coastguard Worker */ 571*a65addddSAndroid Build Coastguard Worker template <typename C> 572*a65addddSAndroid Build Coastguard Worker PartialComponent<fruit::impl::AddInstanceMultibinding<C>, Bindings...> addInstanceMultibinding(C& instance); 573*a65addddSAndroid Build Coastguard Worker 574*a65addddSAndroid Build Coastguard Worker /** 575*a65addddSAndroid Build Coastguard Worker * Similar to the previous version of addInstanceMultibinding(), but allows to specify an 576*a65addddSAndroid Build Coastguard Worker * annotated type. 577*a65addddSAndroid Build Coastguard Worker * Example use: 578*a65addddSAndroid Build Coastguard Worker * 579*a65addddSAndroid Build Coastguard Worker * struct MyAnnotation {}; 580*a65addddSAndroid Build Coastguard Worker * 581*a65addddSAndroid Build Coastguard Worker * class MyClass { 582*a65addddSAndroid Build Coastguard Worker * ... 583*a65addddSAndroid Build Coastguard Worker * }; 584*a65addddSAndroid Build Coastguard Worker * 585*a65addddSAndroid Build Coastguard Worker * fruit::Component<> getMyComponent() { 586*a65addddSAndroid Build Coastguard Worker * static MyClass x = MyClass(...); 587*a65addddSAndroid Build Coastguard Worker * static MyClass y = MyClass(...); 588*a65addddSAndroid Build Coastguard Worker * return fruit::createComponent() 589*a65addddSAndroid Build Coastguard Worker * .addInstanceMultibinding<fruit::Annotated<MyAnnotation, MyClass>>(x) 590*a65addddSAndroid Build Coastguard Worker * .addInstanceMultibinding<fruit::Annotated<MyAnnotation, MyClass>>(y); 591*a65addddSAndroid Build Coastguard Worker * } 592*a65addddSAndroid Build Coastguard Worker * 593*a65addddSAndroid Build Coastguard Worker * fruit::Injector<> injector(getMyComponent); 594*a65addddSAndroid Build Coastguard Worker * // This vector contains {&x, &y}. 595*a65addddSAndroid Build Coastguard Worker * const std::vector<MyClass*>& objects = injector.getMultibindings<fruit::Annotated<MyAnnotation, MyClass>>(); 596*a65addddSAndroid Build Coastguard Worker */ 597*a65addddSAndroid Build Coastguard Worker template <typename AnnotatedC, typename C> 598*a65addddSAndroid Build Coastguard Worker PartialComponent<fruit::impl::AddInstanceMultibinding<AnnotatedC>, Bindings...> addInstanceMultibinding(C& instance); 599*a65addddSAndroid Build Coastguard Worker 600*a65addddSAndroid Build Coastguard Worker /** 601*a65addddSAndroid Build Coastguard Worker * Equivalent to calling addInstanceMultibinding() for each elements of `instances'. 602*a65addddSAndroid Build Coastguard Worker * See the documentation of addInstanceMultibinding() for more details. 603*a65addddSAndroid Build Coastguard Worker * 604*a65addddSAndroid Build Coastguard Worker * Note that this takes the vector by reference, not by value; the vector (and its elements) must remain valid for the 605*a65addddSAndroid Build Coastguard Worker * entire lifetime of this component and of any injectors created from this component. 606*a65addddSAndroid Build Coastguard Worker * 607*a65addddSAndroid Build Coastguard Worker * Example use: 608*a65addddSAndroid Build Coastguard Worker * 609*a65addddSAndroid Build Coastguard Worker * class MyClass { 610*a65addddSAndroid Build Coastguard Worker * ... 611*a65addddSAndroid Build Coastguard Worker * }; 612*a65addddSAndroid Build Coastguard Worker * 613*a65addddSAndroid Build Coastguard Worker * fruit::Component<> getMyComponent() { 614*a65addddSAndroid Build Coastguard Worker * static MyClass x = MyClass(...); 615*a65addddSAndroid Build Coastguard Worker * static std::vector<MyClass> other_objects{MyClass(...), MyClass(...)}; 616*a65addddSAndroid Build Coastguard Worker * return fruit::createComponent() 617*a65addddSAndroid Build Coastguard Worker * .addInstanceMultibinding(x) 618*a65addddSAndroid Build Coastguard Worker * .addInstanceMultibindings(other_objects); 619*a65addddSAndroid Build Coastguard Worker * } 620*a65addddSAndroid Build Coastguard Worker * 621*a65addddSAndroid Build Coastguard Worker * fruit::Injector<> injector(getMyComponent); 622*a65addddSAndroid Build Coastguard Worker * // This vector contains {&x, &(other_objects[0]), &(other_objects[1])}. 623*a65addddSAndroid Build Coastguard Worker * const std::vector<MyClass*>& objects = injector.getMultibindings<MyClass>(); 624*a65addddSAndroid Build Coastguard Worker */ 625*a65addddSAndroid Build Coastguard Worker template <typename C> 626*a65addddSAndroid Build Coastguard Worker PartialComponent<fruit::impl::AddInstanceVectorMultibindings<C>, Bindings...> 627*a65addddSAndroid Build Coastguard Worker addInstanceMultibindings(std::vector<C>& instances); 628*a65addddSAndroid Build Coastguard Worker 629*a65addddSAndroid Build Coastguard Worker /** 630*a65addddSAndroid Build Coastguard Worker * Similar to the previous version of addInstanceMultibindings(), but it allows to specify an annotated type. 631*a65addddSAndroid Build Coastguard Worker * 632*a65addddSAndroid Build Coastguard Worker * Example use: 633*a65addddSAndroid Build Coastguard Worker * 634*a65addddSAndroid Build Coastguard Worker * class MyClass { 635*a65addddSAndroid Build Coastguard Worker * ... 636*a65addddSAndroid Build Coastguard Worker * }; 637*a65addddSAndroid Build Coastguard Worker * 638*a65addddSAndroid Build Coastguard Worker * fruit::Component<> getMyComponent() { 639*a65addddSAndroid Build Coastguard Worker * static MyClass x = MyClass(...); 640*a65addddSAndroid Build Coastguard Worker * static std::vector<MyClass> other_objects{MyClass(...), MyClass(...)}; 641*a65addddSAndroid Build Coastguard Worker * return fruit::createComponent() 642*a65addddSAndroid Build Coastguard Worker * .addInstanceMultibinding<fruit::Annotated<MyAnnotation, MyClass>>(x) 643*a65addddSAndroid Build Coastguard Worker * .addInstanceMultibindings<fruit::Annotated<MyAnnotation, MyClass>>(other_objects); 644*a65addddSAndroid Build Coastguard Worker * } 645*a65addddSAndroid Build Coastguard Worker * 646*a65addddSAndroid Build Coastguard Worker * fruit::Injector<> injector(getMyComponent); 647*a65addddSAndroid Build Coastguard Worker * // This vector contains {&x, &(other_objects[0]), &(other_objects[1])}. 648*a65addddSAndroid Build Coastguard Worker * const std::vector<MyClass*>& objects = injector.getMultibindings<fruit::Annotated<MyAnnotation, MyClass>>(); 649*a65addddSAndroid Build Coastguard Worker */ 650*a65addddSAndroid Build Coastguard Worker template <typename AnnotatedC, typename C> 651*a65addddSAndroid Build Coastguard Worker PartialComponent<fruit::impl::AddInstanceVectorMultibindings<AnnotatedC>, Bindings...> 652*a65addddSAndroid Build Coastguard Worker addInstanceMultibindings(std::vector<C>& instances); 653*a65addddSAndroid Build Coastguard Worker 654*a65addddSAndroid Build Coastguard Worker /** 655*a65addddSAndroid Build Coastguard Worker * Similar to registerProvider, but adds a multibinding instead. 656*a65addddSAndroid Build Coastguard Worker * 657*a65addddSAndroid Build Coastguard Worker * Multibindings are independent from bindings; creating a binding with registerProvider doesn't count as a 658*a65addddSAndroid Build Coastguard Worker * multibinding, and adding a multibinding doesn't allow to inject the type (it only allows to retrieve multibindings 659*a65addddSAndroid Build Coastguard Worker * through the getMultibindings method of the injector). 660*a65addddSAndroid Build Coastguard Worker * 661*a65addddSAndroid Build Coastguard Worker * Unlike bindings, where adding a the same binding twice is allowed (and ignored), adding the same multibinding 662*a65addddSAndroid Build Coastguard Worker * provider multiple times will result in the creation of multiple "equivalent" instances, that will all be returned 663*a65addddSAndroid Build Coastguard Worker * by getMultibindings. 664*a65addddSAndroid Build Coastguard Worker * It is good practice to add the multibindings in a component that is "close" to the injector in the get*Component 665*a65addddSAndroid Build Coastguard Worker * call chain, to avoid adding the same multibinding more than once. 666*a65addddSAndroid Build Coastguard Worker * 667*a65addddSAndroid Build Coastguard Worker * Example use: 668*a65addddSAndroid Build Coastguard Worker * 669*a65addddSAndroid Build Coastguard Worker * class MyClass { 670*a65addddSAndroid Build Coastguard Worker * public: 671*a65addddSAndroid Build Coastguard Worker * MyClass(int n) {...} 672*a65addddSAndroid Build Coastguard Worker * }; 673*a65addddSAndroid Build Coastguard Worker * 674*a65addddSAndroid Build Coastguard Worker * fruit::Component<> getMyComponent() { 675*a65addddSAndroid Build Coastguard Worker * return fruit::createComponent() 676*a65addddSAndroid Build Coastguard Worker * .addMultibindingProvider([]() { return MyClass(10); }) 677*a65addddSAndroid Build Coastguard Worker * .addMultibindingProvider([]() { return MyClass(10); }) 678*a65addddSAndroid Build Coastguard Worker * .addMultibindingProvider([]() { return MyClass(20); }); 679*a65addddSAndroid Build Coastguard Worker * } 680*a65addddSAndroid Build Coastguard Worker * 681*a65addddSAndroid Build Coastguard Worker * fruit::Injector<> injector(getMyComponent); 682*a65addddSAndroid Build Coastguard Worker * // This vector contains {&x, &y, &z} where x and y are MyClass objects constructed with 10 and z is a MyClass 683*a65addddSAndroid Build Coastguard Worker * // object constructed with 20. 684*a65addddSAndroid Build Coastguard Worker * const std::vector<MyClass*>& objects = injector.getMultibindings<MyClass>(); 685*a65addddSAndroid Build Coastguard Worker * 686*a65addddSAndroid Build Coastguard Worker * Note that this method adds a multibinding for the type returned by the provider. If the returned object implements 687*a65addddSAndroid Build Coastguard Worker * an interface I and you want to add a multibinding for that interface instead, you should cast the pointer to I* 688*a65addddSAndroid Build Coastguard Worker * before returning it. 689*a65addddSAndroid Build Coastguard Worker */ 690*a65addddSAndroid Build Coastguard Worker template <typename Lambda> 691*a65addddSAndroid Build Coastguard Worker PartialComponent<fruit::impl::AddMultibindingProvider<Lambda>, Bindings...> addMultibindingProvider(Lambda lambda); 692*a65addddSAndroid Build Coastguard Worker 693*a65addddSAndroid Build Coastguard Worker /** 694*a65addddSAndroid Build Coastguard Worker * Similar to the previous version of addMultibindingProvider(), but allows to specify an annotated type 695*a65addddSAndroid Build Coastguard Worker * for the provider. This allows to inject annotated types in the parameters and/or bind the 696*a65addddSAndroid Build Coastguard Worker * provider to an annotated type. 697*a65addddSAndroid Build Coastguard Worker * 698*a65addddSAndroid Build Coastguard Worker * Example use: 699*a65addddSAndroid Build Coastguard Worker * 700*a65addddSAndroid Build Coastguard Worker * struct MyAnnotation1 {}; 701*a65addddSAndroid Build Coastguard Worker * struct MyAnnotation2 {}; 702*a65addddSAndroid Build Coastguard Worker * 703*a65addddSAndroid Build Coastguard Worker * Component<fruit::Annotated<Annotation1, Bar>> getBarComponent() {...} 704*a65addddSAndroid Build Coastguard Worker * Component<Baz> getBazComponent() {...} 705*a65addddSAndroid Build Coastguard Worker * 706*a65addddSAndroid Build Coastguard Worker * fruit::Component<> getFooComponent() { 707*a65addddSAndroid Build Coastguard Worker * return fruit::createComponent() 708*a65addddSAndroid Build Coastguard Worker * .install(getBarComponent) 709*a65addddSAndroid Build Coastguard Worker * .install(getBazComponent) 710*a65addddSAndroid Build Coastguard Worker * .registerMultibindingProvider< 711*a65addddSAndroid Build Coastguard Worker * fruit::Annotated<Annotation2, Foo>( 712*a65addddSAndroid Build Coastguard Worker * fruit::Annotated<Annotation1, Bar*>, 713*a65addddSAndroid Build Coastguard Worker * Baz*) 714*a65addddSAndroid Build Coastguard Worker * >([](Bar* bar, Baz* baz) { 715*a65addddSAndroid Build Coastguard Worker * Foo foo(bar, baz); 716*a65addddSAndroid Build Coastguard Worker * foo.initialize(); 717*a65addddSAndroid Build Coastguard Worker * return foo; 718*a65addddSAndroid Build Coastguard Worker * }); 719*a65addddSAndroid Build Coastguard Worker * } 720*a65addddSAndroid Build Coastguard Worker * 721*a65addddSAndroid Build Coastguard Worker * 722*a65addddSAndroid Build Coastguard Worker * fruit::Injector<> injector(getFooComponent); 723*a65addddSAndroid Build Coastguard Worker * // This vector contains {&x} where x is an instance of Foo constructed using the lambda above, with injected 724*a65addddSAndroid Build Coastguard Worker * // instances of Bar and Baz. 725*a65addddSAndroid Build Coastguard Worker * const std::vector<MyClass*>& objects = injector.getMultibindings<fruit::Annotated<Annotation2, Foo>>(); 726*a65addddSAndroid Build Coastguard Worker */ 727*a65addddSAndroid Build Coastguard Worker template <typename AnnotatedSignature, typename Lambda> 728*a65addddSAndroid Build Coastguard Worker PartialComponent<fruit::impl::AddMultibindingProvider<AnnotatedSignature, Lambda>, Bindings...> 729*a65addddSAndroid Build Coastguard Worker addMultibindingProvider(Lambda lambda); 730*a65addddSAndroid Build Coastguard Worker 731*a65addddSAndroid Build Coastguard Worker /** 732*a65addddSAndroid Build Coastguard Worker * Registers `factory' as a factory of C, where `factory' is a lambda with no captures returning C. 733*a65addddSAndroid Build Coastguard Worker * This is typically used for assisted injection (but it can also be used if no parameters are assisted). 734*a65addddSAndroid Build Coastguard Worker * 735*a65addddSAndroid Build Coastguard Worker * C can be any class (or fundamental) type. If C is std::unique_ptr<T>, the factory together with a bind<I,C> in the 736*a65addddSAndroid Build Coastguard Worker * same component will automatically bind the corresponding std::function that returns a std::unique_ptr<I>. 737*a65addddSAndroid Build Coastguard Worker * See the documentation of bind() for more details. 738*a65addddSAndroid Build Coastguard Worker * 739*a65addddSAndroid Build Coastguard Worker * The returned type can't be a pointer type. If you don't want to return it by value, you should return a 740*a65addddSAndroid Build Coastguard Worker * std::unique_ptr instead. 741*a65addddSAndroid Build Coastguard Worker * 742*a65addddSAndroid Build Coastguard Worker * Example: 743*a65addddSAndroid Build Coastguard Worker * 744*a65addddSAndroid Build Coastguard Worker * Component<std::function<std::unique_ptr<MyClass>(int)>> getMyClassComponent() { 745*a65addddSAndroid Build Coastguard Worker * fruit::createComponent() 746*a65addddSAndroid Build Coastguard Worker * .install(getFooComponent) 747*a65addddSAndroid Build Coastguard Worker * .registerFactory<std::unique_ptr<MyClass>(Foo*, fruit::Assisted<int>)>( 748*a65addddSAndroid Build Coastguard Worker * [](Foo* foo, int n) { 749*a65addddSAndroid Build Coastguard Worker * return std::unique_ptr<MyClass>(new MyClass(foo, n)); 750*a65addddSAndroid Build Coastguard Worker * }); 751*a65addddSAndroid Build Coastguard Worker * } 752*a65addddSAndroid Build Coastguard Worker * 753*a65addddSAndroid Build Coastguard Worker * Injector<std::function<std::unique_ptr<MyClass>(int)>> injector(getMyClassComponent); 754*a65addddSAndroid Build Coastguard Worker * 755*a65addddSAndroid Build Coastguard Worker * std::function<std::unique_ptr<MyClass>(int)> factory(injector); 756*a65addddSAndroid Build Coastguard Worker * std::unique_ptr<MyClass> x = factory(42); 757*a65addddSAndroid Build Coastguard Worker * 758*a65addddSAndroid Build Coastguard Worker * The parameters marked as Assisted will become parameters of the std::function (in the same order), while the others 759*a65addddSAndroid Build Coastguard Worker * (e.g. Foo in the example above) will be injected. 760*a65addddSAndroid Build Coastguard Worker * 761*a65addddSAndroid Build Coastguard Worker * Unlike registerProvider(), where the signature is inferred, for this method the signature (including any Assisted 762*a65addddSAndroid Build Coastguard Worker * annotations) must be specified explicitly, while the second template parameter is inferred. 763*a65addddSAndroid Build Coastguard Worker * 764*a65addddSAndroid Build Coastguard Worker * If the only thing that the factory does is to call new and the constructor of the class, it's usually more 765*a65addddSAndroid Build Coastguard Worker * convenient to use an Inject typedef or INJECT macro instead, e.g. the following are equivalent to the above: 766*a65addddSAndroid Build Coastguard Worker * 767*a65addddSAndroid Build Coastguard Worker * class MyClass { 768*a65addddSAndroid Build Coastguard Worker * public: 769*a65addddSAndroid Build Coastguard Worker * using Inject = MyClass(Foo*, Assisted<int>); 770*a65addddSAndroid Build Coastguard Worker * 771*a65addddSAndroid Build Coastguard Worker * MyClass(Foo* foo, int n) {...} 772*a65addddSAndroid Build Coastguard Worker * }; 773*a65addddSAndroid Build Coastguard Worker * 774*a65addddSAndroid Build Coastguard Worker * or: 775*a65addddSAndroid Build Coastguard Worker * 776*a65addddSAndroid Build Coastguard Worker * class MyClass { 777*a65addddSAndroid Build Coastguard Worker * public: 778*a65addddSAndroid Build Coastguard Worker * INJECT(MyClass(Foo* foo, ASSISTED(int) n)) {...} 779*a65addddSAndroid Build Coastguard Worker * }; 780*a65addddSAndroid Build Coastguard Worker * 781*a65addddSAndroid Build Coastguard Worker * Use registerFactory() when you want to inject the class in different ways in different components (just make sure 782*a65addddSAndroid Build Coastguard Worker * those don't end up in the same injector), or when MyClass is a third-party class that can't be modified. 783*a65addddSAndroid Build Coastguard Worker * 784*a65addddSAndroid Build Coastguard Worker * registerFactory() can't be called with a plain function, but you can write a lambda that wraps the function to 785*a65addddSAndroid Build Coastguard Worker * achieve the same result. 786*a65addddSAndroid Build Coastguard Worker * 787*a65addddSAndroid Build Coastguard Worker * Registering stateful functors (including lambdas with captures) is NOT supported. 788*a65addddSAndroid Build Coastguard Worker * However, you can write something like: 789*a65addddSAndroid Build Coastguard Worker * 790*a65addddSAndroid Build Coastguard Worker * struct Functor { 791*a65addddSAndroid Build Coastguard Worker * Functor(float x) {...} 792*a65addddSAndroid Build Coastguard Worker * std::unique_ptr<MyClass> operator()(Foo* foo, int n) {...} 793*a65addddSAndroid Build Coastguard Worker * }; 794*a65addddSAndroid Build Coastguard Worker * 795*a65addddSAndroid Build Coastguard Worker * Component<std::function<std::unique_ptr<MyClass>(int)>> getMyClassComponent() { 796*a65addddSAndroid Build Coastguard Worker * static const Functor aFunctor(42.0); 797*a65addddSAndroid Build Coastguard Worker * return fruit::createComponent() 798*a65addddSAndroid Build Coastguard Worker * ... // Bind Foo 799*a65addddSAndroid Build Coastguard Worker * .bindInstance(aFunctor) 800*a65addddSAndroid Build Coastguard Worker * .registerFactory< 801*a65addddSAndroid Build Coastguard Worker * std::unique_ptr<MyClass>( 802*a65addddSAndroid Build Coastguard Worker * Functor functor, 803*a65addddSAndroid Build Coastguard Worker * Foo*, 804*a65addddSAndroid Build Coastguard Worker * Assisted<int>) 805*a65addddSAndroid Build Coastguard Worker * >([](Functor functor, Foo* foo, int n) { 806*a65addddSAndroid Build Coastguard Worker * return functor(foo, n); 807*a65addddSAndroid Build Coastguard Worker * }); 808*a65addddSAndroid Build Coastguard Worker * } 809*a65addddSAndroid Build Coastguard Worker */ 810*a65addddSAndroid Build Coastguard Worker template <typename DecoratedSignature, typename Factory> 811*a65addddSAndroid Build Coastguard Worker PartialComponent<fruit::impl::RegisterFactory<DecoratedSignature, Factory>, Bindings...> 812*a65addddSAndroid Build Coastguard Worker registerFactory(Factory factory); 813*a65addddSAndroid Build Coastguard Worker 814*a65addddSAndroid Build Coastguard Worker /** 815*a65addddSAndroid Build Coastguard Worker * Adds the bindings (and multibindings) in the Component obtained by calling fun(args...) to the current component. 816*a65addddSAndroid Build Coastguard Worker * 817*a65addddSAndroid Build Coastguard Worker * For example, these component functions: 818*a65addddSAndroid Build Coastguard Worker * fruit::Component<Foo> getComponent1(); 819*a65addddSAndroid Build Coastguard Worker * fruit::Component<Bar> getComponent2(int n, std::string s); 820*a65addddSAndroid Build Coastguard Worker * 821*a65addddSAndroid Build Coastguard Worker * can be combined as: 822*a65addddSAndroid Build Coastguard Worker * 823*a65addddSAndroid Build Coastguard Worker * fruit::Component<Foo, Bar> getFooBarComponent() { 824*a65addddSAndroid Build Coastguard Worker * return fruit::createComponent() 825*a65addddSAndroid Build Coastguard Worker * .install(getComponent1) 826*a65addddSAndroid Build Coastguard Worker * .install(getComponent2, 5, std::string("Hello")); 827*a65addddSAndroid Build Coastguard Worker * } 828*a65addddSAndroid Build Coastguard Worker * 829*a65addddSAndroid Build Coastguard Worker * If any `args` are provided, they must be: 830*a65addddSAndroid Build Coastguard Worker * - Copy-constructible 831*a65addddSAndroid Build Coastguard Worker * - Move-constructible 832*a65addddSAndroid Build Coastguard Worker * - Assignable 833*a65addddSAndroid Build Coastguard Worker * - Move-assignable 834*a65addddSAndroid Build Coastguard Worker * - Equality comparable (i.e., operator== must be defined for two values of that type) 835*a65addddSAndroid Build Coastguard Worker * - Hashable (i.e., std::hash must be defined for values of that type) 836*a65addddSAndroid Build Coastguard Worker * 837*a65addddSAndroid Build Coastguard Worker * Note that this only applies to `args`. E.g. in the example above `int` and `std::string` must satisfy this 838*a65addddSAndroid Build Coastguard Worker * requirement (and they do), but `Foo` and `Bar` don't need to. 839*a65addddSAndroid Build Coastguard Worker * 840*a65addddSAndroid Build Coastguard Worker * Args and FormalArgs (if any) must be the same types; or to be precise, each type in Args must be convertible into 841*a65addddSAndroid Build Coastguard Worker * the corresponding type in FormalArgs. 842*a65addddSAndroid Build Coastguard Worker * 843*a65addddSAndroid Build Coastguard Worker * A lambda with no captures can also be used as the first argument, for example: 844*a65addddSAndroid Build Coastguard Worker * 845*a65addddSAndroid Build Coastguard Worker * fruit::Component<Foo, Bar> getFooBarComponent() { 846*a65addddSAndroid Build Coastguard Worker * return fruit::createComponent() 847*a65addddSAndroid Build Coastguard Worker * .install([]() { return getComponent1(); }) 848*a65addddSAndroid Build Coastguard Worker * .install([](int n) { return getComponent2(n, std::string("Hello")); }, 5); 849*a65addddSAndroid Build Coastguard Worker * } 850*a65addddSAndroid Build Coastguard Worker * 851*a65addddSAndroid Build Coastguard Worker * These two install() calls are equivalent to the previous ones. 852*a65addddSAndroid Build Coastguard Worker * 853*a65addddSAndroid Build Coastguard Worker * As in the example, the template parameters for this method will be inferred by the compiler, it's not necessary to 854*a65addddSAndroid Build Coastguard Worker * specify them explicitly. 855*a65addddSAndroid Build Coastguard Worker * 856*a65addddSAndroid Build Coastguard Worker * Fruit automatically de-duplicates install() calls, so they're effectively memoized (within each injector). 857*a65addddSAndroid Build Coastguard Worker * For example, in this code: 858*a65addddSAndroid Build Coastguard Worker * 859*a65addddSAndroid Build Coastguard Worker * fruit::Component<Foo> getFooComponent() {...} 860*a65addddSAndroid Build Coastguard Worker * 861*a65addddSAndroid Build Coastguard Worker * fruit::Component<Bar> getBarComponent() { 862*a65addddSAndroid Build Coastguard Worker * return fruit::createComponent() 863*a65addddSAndroid Build Coastguard Worker * .install(getFooComponent) 864*a65addddSAndroid Build Coastguard Worker * .bind<Bar, BarImpl>(); 865*a65addddSAndroid Build Coastguard Worker * } 866*a65addddSAndroid Build Coastguard Worker * 867*a65addddSAndroid Build Coastguard Worker * fruit::Component<Baz> getBazComponent() { 868*a65addddSAndroid Build Coastguard Worker * return fruit::createComponent() 869*a65addddSAndroid Build Coastguard Worker * .install(getFooComponent) 870*a65addddSAndroid Build Coastguard Worker * .bind<Baz, BazImpl>(); 871*a65addddSAndroid Build Coastguard Worker * } 872*a65addddSAndroid Build Coastguard Worker * 873*a65addddSAndroid Build Coastguard Worker * fruit::Component<Bar, Baz> getBarBazComponent() { 874*a65addddSAndroid Build Coastguard Worker * return fruit::createComponent() 875*a65addddSAndroid Build Coastguard Worker * .install(getBarComponent) 876*a65addddSAndroid Build Coastguard Worker * .install(getBazComponent); 877*a65addddSAndroid Build Coastguard Worker * } 878*a65addddSAndroid Build Coastguard Worker * 879*a65addddSAndroid Build Coastguard Worker * fruit::Injector<Bar, Baz> injector(getBarBazComponent); 880*a65addddSAndroid Build Coastguard Worker * 881*a65addddSAndroid Build Coastguard Worker * 882*a65addddSAndroid Build Coastguard Worker * getFooComponent() will only be called once. 883*a65addddSAndroid Build Coastguard Worker * For Component functions with arguments, only one call will be done for each set of arguments, but multiple calls 884*a65addddSAndroid Build Coastguard Worker * might be made if multiple sets of arguments are used. 885*a65addddSAndroid Build Coastguard Worker * 886*a65addddSAndroid Build Coastguard Worker * If you actually want a Component function to be called/installed multiple times (e.g. if it binds a multibinding 887*a65addddSAndroid Build Coastguard Worker * and you actually want multiple multibindings to be bound) you can add a dummy argument and specify different values 888*a65addddSAndroid Build Coastguard Worker * for that argument when installing the component. 889*a65addddSAndroid Build Coastguard Worker */ 890*a65addddSAndroid Build Coastguard Worker template <typename... OtherComponentParams, typename... FormalArgs, typename... Args> 891*a65addddSAndroid Build Coastguard Worker PartialComponent<fruit::impl::InstallComponent<fruit::Component<OtherComponentParams...>(FormalArgs...)>, Bindings...> 892*a65addddSAndroid Build Coastguard Worker install(fruit::Component<OtherComponentParams...> (*)(FormalArgs...), Args&&... args); 893*a65addddSAndroid Build Coastguard Worker 894*a65addddSAndroid Build Coastguard Worker /** 895*a65addddSAndroid Build Coastguard Worker * Similar to install(), but allows to install a variable number of component functions instead of just 1. This 896*a65addddSAndroid Build Coastguard Worker * additional flexibility is sometimes useful in templated `get*Component` functions and for other advanced use-cases. 897*a65addddSAndroid Build Coastguard Worker * 898*a65addddSAndroid Build Coastguard Worker * To use this method, wrap each get*Component function with its args in a fruit::ComponentFunction<...> object (using 899*a65addddSAndroid Build Coastguard Worker * the helper function fruit::componentFunction), then pass all the fruit::ComponentFunction<...> objects (which can 900*a65addddSAndroid Build Coastguard Worker * potentially have different params) to this method. 901*a65addddSAndroid Build Coastguard Worker * 902*a65addddSAndroid Build Coastguard Worker * For example: 903*a65addddSAndroid Build Coastguard Worker * 904*a65addddSAndroid Build Coastguard Worker * fruit::Component<Foo, Bar> getBarBazComponent() { 905*a65addddSAndroid Build Coastguard Worker * return fruit::createComponent() 906*a65addddSAndroid Build Coastguard Worker * .installComponentFunctions( 907*a65addddSAndroid Build Coastguard Worker * fruit::componentFunction(getFooComponent, a, b, c), 908*a65addddSAndroid Build Coastguard Worker * fruit::componentFunction(getBarComponent, x, y)); 909*a65addddSAndroid Build Coastguard Worker * } 910*a65addddSAndroid Build Coastguard Worker * 911*a65addddSAndroid Build Coastguard Worker * Is equivalent to: 912*a65addddSAndroid Build Coastguard Worker * 913*a65addddSAndroid Build Coastguard Worker * fruit::Component<Foo, Bar> getBarBazComponent() { 914*a65addddSAndroid Build Coastguard Worker * return fruit::createComponent() 915*a65addddSAndroid Build Coastguard Worker * .install(getFooComponent, a, b, c) 916*a65addddSAndroid Build Coastguard Worker * .install(getBarComponent, x, y); 917*a65addddSAndroid Build Coastguard Worker * } 918*a65addddSAndroid Build Coastguard Worker * 919*a65addddSAndroid Build Coastguard Worker * This is a simple example to show the idea, however in a simple case like this it's easier to just use install(). 920*a65addddSAndroid Build Coastguard Worker */ 921*a65addddSAndroid Build Coastguard Worker template <typename... ComponentFunctions> 922*a65addddSAndroid Build Coastguard Worker PartialComponent<fruit::impl::InstallComponentFunctions<ComponentFunctions...>, Bindings...> 923*a65addddSAndroid Build Coastguard Worker installComponentFunctions(ComponentFunctions... componentFunctions); 924*a65addddSAndroid Build Coastguard Worker 925*a65addddSAndroid Build Coastguard Worker /** 926*a65addddSAndroid Build Coastguard Worker * This class is returned by PartialComponent::replace, see the documentation of that method for more information. 927*a65addddSAndroid Build Coastguard Worker */ 928*a65addddSAndroid Build Coastguard Worker template <typename ReplacedComponent, typename... GetReplacedComponentFormalArgs> 929*a65addddSAndroid Build Coastguard Worker class PartialComponentWithReplacementInProgress { 930*a65addddSAndroid Build Coastguard Worker private: 931*a65addddSAndroid Build Coastguard Worker using storage_t = fruit::impl::PartialComponentStorage< 932*a65addddSAndroid Build Coastguard Worker fruit::impl::PartialReplaceComponent<ReplacedComponent(GetReplacedComponentFormalArgs...)>, Bindings...>; 933*a65addddSAndroid Build Coastguard Worker 934*a65addddSAndroid Build Coastguard Worker public: 935*a65addddSAndroid Build Coastguard Worker template <typename... FormalArgs, typename... Args> 936*a65addddSAndroid Build Coastguard Worker PartialComponent<fruit::impl::ReplaceComponent<ReplacedComponent(GetReplacedComponentFormalArgs...), 937*a65addddSAndroid Build Coastguard Worker ReplacedComponent(FormalArgs...)>, 938*a65addddSAndroid Build Coastguard Worker Bindings...> 939*a65addddSAndroid Build Coastguard Worker with(ReplacedComponent (*)(FormalArgs...), Args&&... args); 940*a65addddSAndroid Build Coastguard Worker PartialComponentWithReplacementInProgress(storage_t storage)941*a65addddSAndroid Build Coastguard Worker PartialComponentWithReplacementInProgress(storage_t storage) // NOLINT(google-explicit-constructor) 942*a65addddSAndroid Build Coastguard Worker : storage(storage) {} 943*a65addddSAndroid Build Coastguard Worker 944*a65addddSAndroid Build Coastguard Worker private: 945*a65addddSAndroid Build Coastguard Worker storage_t storage; 946*a65addddSAndroid Build Coastguard Worker 947*a65addddSAndroid Build Coastguard Worker PartialComponentWithReplacementInProgress() = delete; 948*a65addddSAndroid Build Coastguard Worker }; 949*a65addddSAndroid Build Coastguard Worker 950*a65addddSAndroid Build Coastguard Worker /** 951*a65addddSAndroid Build Coastguard Worker * This allows to replace an installed Component with another one. This is useful for testing. 952*a65addddSAndroid Build Coastguard Worker * For example, if you have these components: 953*a65addddSAndroid Build Coastguard Worker * 954*a65addddSAndroid Build Coastguard Worker * fruit::Component<MyDependency> getDependencyComponent() {...} 955*a65addddSAndroid Build Coastguard Worker * 956*a65addddSAndroid Build Coastguard Worker * fruit::Component<Foo> getFooComponent() { 957*a65addddSAndroid Build Coastguard Worker * return fruit::createComponent() 958*a65addddSAndroid Build Coastguard Worker * .install(getDependencyComponent) 959*a65addddSAndroid Build Coastguard Worker * .bind<Foo, FooImpl>(); 960*a65addddSAndroid Build Coastguard Worker * } 961*a65addddSAndroid Build Coastguard Worker * 962*a65addddSAndroid Build Coastguard Worker * fruit::Component<Bar> getBarComponent() { 963*a65addddSAndroid Build Coastguard Worker * return fruit::createComponent() 964*a65addddSAndroid Build Coastguard Worker * .install(getFooComponent) 965*a65addddSAndroid Build Coastguard Worker * .bind<Bar, BarImpl>(); 966*a65addddSAndroid Build Coastguard Worker * } 967*a65addddSAndroid Build Coastguard Worker * 968*a65addddSAndroid Build Coastguard Worker * When you test Bar, you might want to replace getDependencyComponent with a component that binds a fake 969*a65addddSAndroid Build Coastguard Worker * MyDependency: 970*a65addddSAndroid Build Coastguard Worker * 971*a65addddSAndroid Build Coastguard Worker * fruit::Component<MyDependency> getFakeDependencyComponent() {...} 972*a65addddSAndroid Build Coastguard Worker * 973*a65addddSAndroid Build Coastguard Worker * To do so, you can define a component like: 974*a65addddSAndroid Build Coastguard Worker * 975*a65addddSAndroid Build Coastguard Worker * fruit::Component<Bar> getBarComponentWithFakeDependency() { 976*a65addddSAndroid Build Coastguard Worker * return fruit::createComponent() 977*a65addddSAndroid Build Coastguard Worker * .replace(getDependencyComponent).with(getFakeDependencyComponent) 978*a65addddSAndroid Build Coastguard Worker * .install(getBarComponent); 979*a65addddSAndroid Build Coastguard Worker * } 980*a65addddSAndroid Build Coastguard Worker * 981*a65addddSAndroid Build Coastguard Worker * This component is equivalent to: 982*a65addddSAndroid Build Coastguard Worker * 983*a65addddSAndroid Build Coastguard Worker * fruit::Component<Bar> getBarComponentWithFakeDependency() { 984*a65addddSAndroid Build Coastguard Worker * return fruit::createComponent() 985*a65addddSAndroid Build Coastguard Worker * .install(getFakeDependencyComponent) 986*a65addddSAndroid Build Coastguard Worker * .bind<Foo, FooImpl>() 987*a65addddSAndroid Build Coastguard Worker * .bind<Bar, BarImpl>(); 988*a65addddSAndroid Build Coastguard Worker * } 989*a65addddSAndroid Build Coastguard Worker * 990*a65addddSAndroid Build Coastguard Worker * However this way you don't need to duplicate the bindings for Foo and Bar, and you don't even need to include them 991*a65addddSAndroid Build Coastguard Worker * in the translation unit (i.e., cc/cpp file) that defines getBarComponentWithFakeDependency(). 992*a65addddSAndroid Build Coastguard Worker * In codebases with many layers, this can save a lot of duplication. 993*a65addddSAndroid Build Coastguard Worker * 994*a65addddSAndroid Build Coastguard Worker * Note that the .replace(...).with(...) must appear *before* installing the component to which it's applied to; 995*a65addddSAndroid Build Coastguard Worker * e.g., in the example above note how we install getBarComponent after the replacement in 996*a65addddSAndroid Build Coastguard Worker * getBarComponentWithFakeDependency. 997*a65addddSAndroid Build Coastguard Worker * If you add a replacement after the replaced component has been installed, Fruit will report an error at run-time. 998*a65addddSAndroid Build Coastguard Worker * 999*a65addddSAndroid Build Coastguard Worker * In the example above, the replaced and replacement component functions had no arguments, however it's also possible 1000*a65addddSAndroid Build Coastguard Worker * to replace component functions with args. The arguments of the replaced and replacement component functions are 1001*a65addddSAndroid Build Coastguard Worker * independent; for example .replace(getDependencyComponentWithArgs, 15).with(myFakeComponentWithNoArgs) is allowed 1002*a65addddSAndroid Build Coastguard Worker * and it would replace all install(getDependencyComponentWithArgs, 15) calls with install(myFakeComponentWithNoArgs). 1003*a65addddSAndroid Build Coastguard Worker * 1004*a65addddSAndroid Build Coastguard Worker * The component types returned by the replaced and replacement components must be the same. For example, this is NOT 1005*a65addddSAndroid Build Coastguard Worker * allowed: 1006*a65addddSAndroid Build Coastguard Worker * 1007*a65addddSAndroid Build Coastguard Worker * fruit::Component<MyDependency, SomethingElse> getFakeDependencyComponentWithSomethingElse() {...} 1008*a65addddSAndroid Build Coastguard Worker * 1009*a65addddSAndroid Build Coastguard Worker * fruit::Component<Bar> getBarComponentWithFakeDependency() { 1010*a65addddSAndroid Build Coastguard Worker * return fruit::createComponent() 1011*a65addddSAndroid Build Coastguard Worker * .replace(getDependencyComponent).with(getFakeDependencyComponentWithSomethingElse) // error! 1012*a65addddSAndroid Build Coastguard Worker * .install(getBarComponent); 1013*a65addddSAndroid Build Coastguard Worker * } 1014*a65addddSAndroid Build Coastguard Worker * 1015*a65addddSAndroid Build Coastguard Worker * But replacing a replaced component is allowed: 1016*a65addddSAndroid Build Coastguard Worker * 1017*a65addddSAndroid Build Coastguard Worker * fruit::Component<MyDependency> getOtherFakeDependencyComponent() {...} 1018*a65addddSAndroid Build Coastguard Worker * 1019*a65addddSAndroid Build Coastguard Worker * fruit::Component<Bar> getBarComponentWithOtherFakeDependency() { 1020*a65addddSAndroid Build Coastguard Worker * return fruit::createComponent() 1021*a65addddSAndroid Build Coastguard Worker * // The two replacements can appear in any order, but they must both be before the install(). 1022*a65addddSAndroid Build Coastguard Worker * .replace(getFakeDependencyComponent).with(getOtherFakeDependencyComponent) 1023*a65addddSAndroid Build Coastguard Worker * .replace(getDependencyComponent).with(getFakeDependencyComponent) 1024*a65addddSAndroid Build Coastguard Worker * .install(getBarComponent); 1025*a65addddSAndroid Build Coastguard Worker * } 1026*a65addddSAndroid Build Coastguard Worker * 1027*a65addddSAndroid Build Coastguard Worker * Of course this is a simple example, in the real world the replacements and the install would probably come from 1028*a65addddSAndroid Build Coastguard Worker * other components. 1029*a65addddSAndroid Build Coastguard Worker * 1030*a65addddSAndroid Build Coastguard Worker * And note that you can also replace components that define replacements, for example: 1031*a65addddSAndroid Build Coastguard Worker * 1032*a65addddSAndroid Build Coastguard Worker * fruit::Component<> getFakeDependencyReplacementComponent() { 1033*a65addddSAndroid Build Coastguard Worker * return fruit::createComponent() 1034*a65addddSAndroid Build Coastguard Worker * .replace(getDependencyComponent).with(getFakeDependencyComponentWithSomethingElse); 1035*a65addddSAndroid Build Coastguard Worker * } 1036*a65addddSAndroid Build Coastguard Worker * 1037*a65addddSAndroid Build Coastguard Worker * fruit::Component<...> getComponent() { 1038*a65addddSAndroid Build Coastguard Worker * return fruit::createComponent() 1039*a65addddSAndroid Build Coastguard Worker * .replace(getFakeDependencyReplacementComponent).with(...) 1040*a65addddSAndroid Build Coastguard Worker * .install(...); 1041*a65addddSAndroid Build Coastguard Worker * } 1042*a65addddSAndroid Build Coastguard Worker * 1043*a65addddSAndroid Build Coastguard Worker * Replacements are only installed if the replaced component is installed, otherwise they are ignored. 1044*a65addddSAndroid Build Coastguard Worker * In the first example above, if getFooComponent didn't install getDependencyComponent, when a test creates an 1045*a65addddSAndroid Build Coastguard Worker * injector for getBarComponentWithFakeDependency it would not install getFakeDependencyComponent. 1046*a65addddSAndroid Build Coastguard Worker */ 1047*a65addddSAndroid Build Coastguard Worker template <typename... OtherComponentParams, typename... FormalArgs, typename... Args> 1048*a65addddSAndroid Build Coastguard Worker typename PartialComponent<Bindings...>::template PartialComponentWithReplacementInProgress< 1049*a65addddSAndroid Build Coastguard Worker fruit::Component<OtherComponentParams...>, FormalArgs...> 1050*a65addddSAndroid Build Coastguard Worker replace(fruit::Component<OtherComponentParams...> (*)(FormalArgs...), Args&&... args); 1051*a65addddSAndroid Build Coastguard Worker 1052*a65addddSAndroid Build Coastguard Worker ~PartialComponent() = default; 1053*a65addddSAndroid Build Coastguard Worker 1054*a65addddSAndroid Build Coastguard Worker // Do not use. Use fruit::createComponent() instead. 1055*a65addddSAndroid Build Coastguard Worker PartialComponent() = delete; 1056*a65addddSAndroid Build Coastguard Worker 1057*a65addddSAndroid Build Coastguard Worker // Do not use. Only use PartialComponent for temporaries, and then convert it to a Component. 1058*a65addddSAndroid Build Coastguard Worker PartialComponent(const PartialComponent&) = delete; 1059*a65addddSAndroid Build Coastguard Worker PartialComponent(PartialComponent&&) = delete; 1060*a65addddSAndroid Build Coastguard Worker 1061*a65addddSAndroid Build Coastguard Worker private: 1062*a65addddSAndroid Build Coastguard Worker template <typename... OtherBindings> 1063*a65addddSAndroid Build Coastguard Worker friend class PartialComponent; 1064*a65addddSAndroid Build Coastguard Worker 1065*a65addddSAndroid Build Coastguard Worker template <typename... Types> 1066*a65addddSAndroid Build Coastguard Worker friend class Component; 1067*a65addddSAndroid Build Coastguard Worker 1068*a65addddSAndroid Build Coastguard Worker fruit::impl::PartialComponentStorage<Bindings...> storage; 1069*a65addddSAndroid Build Coastguard Worker 1070*a65addddSAndroid Build Coastguard Worker PartialComponent(fruit::impl::PartialComponentStorage<Bindings...> storage); // NOLINT(google-explicit-constructor) 1071*a65addddSAndroid Build Coastguard Worker 1072*a65addddSAndroid Build Coastguard Worker template <typename NewBinding> 1073*a65addddSAndroid Build Coastguard Worker using OpFor = typename fruit::impl::meta::OpForComponent<Bindings...>::template AddBinding<NewBinding>; 1074*a65addddSAndroid Build Coastguard Worker 1075*a65addddSAndroid Build Coastguard Worker friend PartialComponent<> createComponent(); 1076*a65addddSAndroid Build Coastguard Worker }; 1077*a65addddSAndroid Build Coastguard Worker 1078*a65addddSAndroid Build Coastguard Worker } // namespace fruit 1079*a65addddSAndroid Build Coastguard Worker 1080*a65addddSAndroid Build Coastguard Worker #include <fruit/impl/component.defn.h> 1081*a65addddSAndroid Build Coastguard Worker 1082*a65addddSAndroid Build Coastguard Worker #endif // FRUIT_COMPONENT_H 1083