xref: /aosp_15_r20/external/google-fruit/include/fruit/macro.h (revision a65addddcf69f38db5b288d787b6b7571a57bb8f)
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_MACRO_H
18*a65addddSAndroid Build Coastguard Worker #define FRUIT_MACRO_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 
25*a65addddSAndroid Build Coastguard Worker /**
26*a65addddSAndroid Build Coastguard Worker  * A convenience macro to define the Inject typedef while declaring/defining the constructor that will be used for
27*a65addddSAndroid Build Coastguard Worker  * injection.
28*a65addddSAndroid Build Coastguard Worker  * It also supports assisted injection and injection of annotated types.
29*a65addddSAndroid Build Coastguard Worker  *
30*a65addddSAndroid Build Coastguard Worker  * Example usage:
31*a65addddSAndroid Build Coastguard Worker  *
32*a65addddSAndroid Build Coastguard Worker  * class MyClass {
33*a65addddSAndroid Build Coastguard Worker  * public:
34*a65addddSAndroid Build Coastguard Worker  *    INJECT(MyClass(Foo* foo, Bar* bar)) {...}
35*a65addddSAndroid Build Coastguard Worker  * };
36*a65addddSAndroid Build Coastguard Worker  *
37*a65addddSAndroid Build Coastguard Worker  * is equivalent to:
38*a65addddSAndroid Build Coastguard Worker  *
39*a65addddSAndroid Build Coastguard Worker  * class MyClass {
40*a65addddSAndroid Build Coastguard Worker  * public:
41*a65addddSAndroid Build Coastguard Worker  *    using Inject = MyClass(Foo*, Bar*);
42*a65addddSAndroid Build Coastguard Worker  *
43*a65addddSAndroid Build Coastguard Worker  *    MyClass(Foo* foo, Bar* y) {...}
44*a65addddSAndroid Build Coastguard Worker  * };
45*a65addddSAndroid Build Coastguard Worker  *
46*a65addddSAndroid Build Coastguard Worker  * Example usage for assisted injection (see PartialComponent::registerFactory):
47*a65addddSAndroid Build Coastguard Worker  *
48*a65addddSAndroid Build Coastguard Worker  * class MyClass {
49*a65addddSAndroid Build Coastguard Worker  * public:
50*a65addddSAndroid Build Coastguard Worker  *    INJECT(MyClass(Foo* foo, ASSISTED(int) n) {...}
51*a65addddSAndroid Build Coastguard Worker  * };
52*a65addddSAndroid Build Coastguard Worker  *
53*a65addddSAndroid Build Coastguard Worker  * is equivalent to:
54*a65addddSAndroid Build Coastguard Worker  *
55*a65addddSAndroid Build Coastguard Worker  * class MyClass {
56*a65addddSAndroid Build Coastguard Worker  * public:
57*a65addddSAndroid Build Coastguard Worker  *    using Inject = MyClass(Foo*, Assisted<int>);
58*a65addddSAndroid Build Coastguard Worker  *
59*a65addddSAndroid Build Coastguard Worker  *    MyClass(Foo* foo, int n) {...}
60*a65addddSAndroid Build Coastguard Worker  * };
61*a65addddSAndroid Build Coastguard Worker  *
62*a65addddSAndroid Build Coastguard Worker  * Example usage for annotated types:
63*a65addddSAndroid Build Coastguard Worker  *
64*a65addddSAndroid Build Coastguard Worker  * class MyClass {
65*a65addddSAndroid Build Coastguard Worker  * public:
66*a65addddSAndroid Build Coastguard Worker  *    INJECT(MyClass(ANNOTATED(SomeAnnotation, Foo*) foo, Bar* bar)) {...}
67*a65addddSAndroid Build Coastguard Worker  * };
68*a65addddSAndroid Build Coastguard Worker  *
69*a65addddSAndroid Build Coastguard Worker  * ASSISTED and ANNOTATED *can* be used together in the same INJECT() annotation, but they can't both be used for a
70*a65addddSAndroid Build Coastguard Worker  * single parameter (as this wouldn't make sense, parameters that use assisted injection are user-supplied, they aren't
71*a65addddSAndroid Build Coastguard Worker  * injected from a binding).
72*a65addddSAndroid Build Coastguard Worker  *
73*a65addddSAndroid Build Coastguard Worker  * NOTE: This can't be used if the constructor is templated (the class can be templated, however), if there are any
74*a65addddSAndroid Build Coastguard Worker  * default arguments or if the constructor is marked `explicit'.
75*a65addddSAndroid Build Coastguard Worker  * In those cases, define the Inject annotation manually or use registerConstructor()/registerFactory() instead.
76*a65addddSAndroid Build Coastguard Worker  *
77*a65addddSAndroid Build Coastguard Worker  * NOTE: ASSISTED takes just one argument, but it's declared as variadic to make sure that the preprocessor doesn't
78*a65addddSAndroid Build Coastguard Worker  * choke on multi-argument templates like the map above, that the processor is unable to parse correctly.
79*a65addddSAndroid Build Coastguard Worker  *
80*a65addddSAndroid Build Coastguard Worker  * NOTE: ASSISTED takes just 2 arguments, but it's declared as variadic to make sure that the preprocessor doesn't choke
81*a65addddSAndroid Build Coastguard Worker  * on multi-argument templates, that the processor is unable to parse correctly.
82*a65addddSAndroid Build Coastguard Worker  *
83*a65addddSAndroid Build Coastguard Worker  * NOTE: In addition to the public Inject typedef, two typedefs (FruitAssistedTypedef and FruitAnnotatedTypedef) will be
84*a65addddSAndroid Build Coastguard Worker  * defined inside the class, make sure you don't define another typedef/field/method with the same name if you use the
85*a65addddSAndroid Build Coastguard Worker  * INJECT macro (unlikely but possible) these typedefs are an implementation detail of Fruit and should not be used.
86*a65addddSAndroid Build Coastguard Worker  *
87*a65addddSAndroid Build Coastguard Worker  * NOTE: The return type (MyClass in this case) should not be annotated. However an annotated
88*a65addddSAndroid Build Coastguard Worker  * MyClass (or MyClass factory) can be injected from any INJECT declaration.
89*a65addddSAndroid Build Coastguard Worker  */
90*a65addddSAndroid Build Coastguard Worker #define INJECT(Signature)                                                                                              \
91*a65addddSAndroid Build Coastguard Worker   using Inject = Signature;                                                                                            \
92*a65addddSAndroid Build Coastguard Worker                                                                                                                        \
93*a65addddSAndroid Build Coastguard Worker   template <typename FruitAssistedDeclarationParam>                                                                    \
94*a65addddSAndroid Build Coastguard Worker   using FruitAssistedTypedef = FruitAssistedDeclarationParam;                                                          \
95*a65addddSAndroid Build Coastguard Worker   template <typename Annotation, typename FruitAnnotatedDeclarationParam>                                              \
96*a65addddSAndroid Build Coastguard Worker   using FruitAnnotatedTypedef = FruitAnnotatedDeclarationParam;                                                        \
97*a65addddSAndroid Build Coastguard Worker                                                                                                                        \
98*a65addddSAndroid Build Coastguard Worker   Signature
99*a65addddSAndroid Build Coastguard Worker 
100*a65addddSAndroid Build Coastguard Worker #define ASSISTED(...) FruitAssistedTypedef<__VA_ARGS__>
101*a65addddSAndroid Build Coastguard Worker #define ANNOTATED(Annotation, ...) FruitAnnotatedTypedef<Annotation, __VA_ARGS__>
102*a65addddSAndroid Build Coastguard Worker 
103*a65addddSAndroid Build Coastguard Worker /**
104*a65addddSAndroid Build Coastguard Worker  * These are intentionally NOT in the fruit namespace, they can't be there for technical reasons.
105*a65addddSAndroid Build Coastguard Worker  *
106*a65addddSAndroid Build Coastguard Worker  * NOTE: don't use these directly, they're only used to implement the INJECT macro.
107*a65addddSAndroid Build Coastguard Worker  * Consider them part of fruit::impl.
108*a65addddSAndroid Build Coastguard Worker  */
109*a65addddSAndroid Build Coastguard Worker template <typename T>
110*a65addddSAndroid Build Coastguard Worker using FruitAssistedTypedef = fruit::Assisted<T>;
111*a65addddSAndroid Build Coastguard Worker template <typename Annotation, typename T>
112*a65addddSAndroid Build Coastguard Worker using FruitAnnotatedTypedef = fruit::Annotated<Annotation, T>;
113*a65addddSAndroid Build Coastguard Worker 
114*a65addddSAndroid Build Coastguard Worker #endif // FRUIT_MACRO_H
115