Marco Poletti | cf798fa | 2014-06-21 15:05:15 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Google Inc. All rights reserved. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef FRUIT_MACRO_H |
| 18 | #define FRUIT_MACRO_H |
| 19 | |
Marco Poletti | a5289cf | 2015-03-15 10:42:03 +0000 | [diff] [blame] | 20 | // This include is not required here, but having it here shortens the include trace in error messages. |
| 21 | #include "impl/injection_errors.h" |
| 22 | |
Marco Poletti | 97e3623 | 2014-08-24 10:55:28 +0200 | [diff] [blame] | 23 | #include "fruit_forward_decls.h" |
| 24 | |
Marco Poletti | cf798fa | 2014-06-21 15:05:15 +0100 | [diff] [blame] | 25 | /** |
| 26 | * A convenience macro to define the Inject typedef while declaring/defining |
| 27 | * the constructor that will be used for injection. |
Marco Poletti | 31f638a | 2015-05-10 21:21:25 +0100 | [diff] [blame] | 28 | * It also supports assisted injection and injection of annotated types. |
Marco Poletti | cf798fa | 2014-06-21 15:05:15 +0100 | [diff] [blame] | 29 | * |
| 30 | * Example usage: |
| 31 | * |
Marco Poletti | a1e9958 | 2014-11-09 10:00:47 +0000 | [diff] [blame] | 32 | * class MyClass { |
Marco Poletti | cf798fa | 2014-06-21 15:05:15 +0100 | [diff] [blame] | 33 | * public: |
Marco Poletti | a1e9958 | 2014-11-09 10:00:47 +0000 | [diff] [blame] | 34 | * INJECT(MyClass(Foo* foo, Bar* bar)) {...} |
Marco Poletti | cf798fa | 2014-06-21 15:05:15 +0100 | [diff] [blame] | 35 | * }; |
| 36 | * |
| 37 | * is equivalent to: |
| 38 | * |
Marco Poletti | a1e9958 | 2014-11-09 10:00:47 +0000 | [diff] [blame] | 39 | * class MyClass { |
Marco Poletti | cf798fa | 2014-06-21 15:05:15 +0100 | [diff] [blame] | 40 | * public: |
Marco Poletti | a1e9958 | 2014-11-09 10:00:47 +0000 | [diff] [blame] | 41 | * using Inject = MyClass(Foo*, Bar*>); |
Marco Poletti | cf798fa | 2014-06-21 15:05:15 +0100 | [diff] [blame] | 42 | * |
Marco Poletti | a1e9958 | 2014-11-09 10:00:47 +0000 | [diff] [blame] | 43 | * MyClass(Foo* foo, Bar* y) {...} |
| 44 | * }; |
| 45 | * |
| 46 | * Example usage for assisted injection (see PartialComponent::registerFactory): |
| 47 | * |
| 48 | * class MyClass { |
| 49 | * public: |
| 50 | * INJECT(MyClass(Foo* foo, ASSISTED(int) n) {...} |
| 51 | * }; |
| 52 | * |
| 53 | * is equivalent to: |
| 54 | * |
| 55 | * class MyClass { |
| 56 | * public: |
| 57 | * using Inject = MyClass(Foo*, Assisted<int>); |
| 58 | * |
| 59 | * MyClass(Foo* foo, int n) {...} |
Marco Poletti | cf798fa | 2014-06-21 15:05:15 +0100 | [diff] [blame] | 60 | * }; |
| 61 | * |
Marco Poletti | 31f638a | 2015-05-10 21:21:25 +0100 | [diff] [blame] | 62 | * Example usage for annotated types (both the result type and parameters can be annotated |
| 63 | * independently): |
| 64 | * |
| 65 | * class MyClass { |
| 66 | * public: |
| 67 | * INJECT(ANNOTATED(MyAnnotation, MyClass)(ANNOTATED(SomeOtherAnnotation, Foo*) foo, Bar* bar)) {...} |
| 68 | * }; |
| 69 | * |
| 70 | * ASSISTED and ANNOTATED *can* be used together in the same INJECT() annotation, but they can't both be used for a single |
| 71 | * parameter (as this wouldn't make sense, parameters that use assisted injection are user-supplied, they aren't injected |
| 72 | * from a binding). |
| 73 | * |
Marco Poletti | c7b0410 | 2014-07-27 11:59:16 +0100 | [diff] [blame] | 74 | * NOTE: This can't be used if the constructor is templated (the class can be templated, however), if there are any default |
| 75 | * arguments or if the constructor is marked `explicit'. |
Marco Poletti | a185d5e | 2014-11-08 09:00:35 +0000 | [diff] [blame] | 76 | * In those cases, define the Inject annotation manually or use registerConstructor()/registerFactory() instead. |
Marco Poletti | cf798fa | 2014-06-21 15:05:15 +0100 | [diff] [blame] | 77 | * |
Marco Poletti | a185d5e | 2014-11-08 09:00:35 +0000 | [diff] [blame] | 78 | * NOTE: ASSISTED takes just one argument, but it's declared as variadic to make sure that the preprocessor doesn't choke on |
| 79 | * multi-argument templates like the map above, that the processor is unable to parse correctly. |
Marco Poletti | cf798fa | 2014-06-21 15:05:15 +0100 | [diff] [blame] | 80 | * |
Marco Poletti | 31f638a | 2015-05-10 21:21:25 +0100 | [diff] [blame] | 81 | * NOTE: ASSISTED takes just 2 arguments, but it's declared as variadic to make sure that the preprocessor doesn't choke on |
| 82 | * multi-argument templates, that the processor is unable to parse correctly. |
| 83 | * |
| 84 | * NOTE: In addition to the public Inject typedef, two private typedefs (FruitAssistedTypedef and FruitAnnotatedTypedef) will be defined inside the class, |
Marco Poletti | a185d5e | 2014-11-08 09:00:35 +0000 | [diff] [blame] | 85 | * make sure you don't define another typedef/field/method with the same name if you use the INJECT macro (unlikely but possible). |
Marco Poletti | cf798fa | 2014-06-21 15:05:15 +0100 | [diff] [blame] | 86 | */ |
| 87 | #define INJECT(Signature) \ |
| 88 | using Inject = Signature; \ |
| 89 | private: \ |
| 90 | template <typename FruitAssistedDeclarationParam> \ |
Marco Poletti | 31f638a | 2015-05-10 21:21:25 +0100 | [diff] [blame] | 91 | using FruitAssistedTypedef = FruitAssistedDeclarationParam; \ |
| 92 | template <typename FruitAnnotatedDeclarationParam> \ |
| 93 | using FruitAnnotatedTypedef = FruitAnnotatedDeclarationParam; \ |
Marco Poletti | cf798fa | 2014-06-21 15:05:15 +0100 | [diff] [blame] | 94 | public: \ |
| 95 | Signature |
| 96 | |
Marco Poletti | 31f638a | 2015-05-10 21:21:25 +0100 | [diff] [blame] | 97 | #define ASSISTED(...) FruitAssistedTypedef<__VA_ARGS__> |
| 98 | #define ANNOTATED(Annotation, ...) FruitAnnotatedTypedef<Annotation, __VA_ARGS__> |
Marco Poletti | cf798fa | 2014-06-21 15:05:15 +0100 | [diff] [blame] | 99 | |
Marco Poletti | 5bc9876 | 2014-07-27 11:02:44 +0100 | [diff] [blame] | 100 | /** |
Marco Poletti | 31f638a | 2015-05-10 21:21:25 +0100 | [diff] [blame] | 101 | * These are intentionally NOT in the fruit namespace, they can't be there for technical reasons. |
Marco Poletti | cf798fa | 2014-06-21 15:05:15 +0100 | [diff] [blame] | 102 | * |
Marco Poletti | 31f638a | 2015-05-10 21:21:25 +0100 | [diff] [blame] | 103 | * NOTE: don't use these directly, they're only used to implement the INJECT macro. |
| 104 | * Consider them part of fruit::impl. |
Marco Poletti | cf798fa | 2014-06-21 15:05:15 +0100 | [diff] [blame] | 105 | */ |
| 106 | template <typename T> |
Marco Poletti | 31f638a | 2015-05-10 21:21:25 +0100 | [diff] [blame] | 107 | using FruitAssistedTypedef = fruit::Assisted<T>; |
| 108 | template <typename Annotation, typename T> |
| 109 | using FruitAnnotatedTypedef = fruit::Annotated<Annotation, T>; |
Marco Poletti | cf798fa | 2014-06-21 15:05:15 +0100 | [diff] [blame] | 110 | |
Marco Poletti | e0808b5 | 2014-08-23 16:59:13 +0200 | [diff] [blame] | 111 | #endif // FRUIT_MACRO_H |