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. |
Marco Poletti | f289510 | 2016-01-30 13:38:37 +0000 | [diff] [blame] | 21 | #include <fruit/impl/injection_errors.h> |
Marco Poletti | a5289cf | 2015-03-15 10:42:03 +0000 | [diff] [blame] | 22 | |
Marco Poletti | f289510 | 2016-01-30 13:38:37 +0000 | [diff] [blame] | 23 | #include <fruit/fruit_forward_decls.h> |
Marco Poletti | 97e3623 | 2014-08-24 10:55:28 +0200 | [diff] [blame] | 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 | c75d608 | 2016-09-25 08:10:36 +0100 | [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 | 169e50f | 2016-02-21 17:48:46 +0000 | [diff] [blame] | 62 | * Example usage for annotated types: |
Marco Poletti | 31f638a | 2015-05-10 21:21:25 +0100 | [diff] [blame] | 63 | * |
| 64 | * class MyClass { |
| 65 | * public: |
Marco Poletti | 169e50f | 2016-02-21 17:48:46 +0000 | [diff] [blame] | 66 | * INJECT(MyClass(ANNOTATED(SomeAnnotation, Foo*) foo, Bar* bar)) {...} |
Marco Poletti | 31f638a | 2015-05-10 21:21:25 +0100 | [diff] [blame] | 67 | * }; |
| 68 | * |
| 69 | * ASSISTED and ANNOTATED *can* be used together in the same INJECT() annotation, but they can't both be used for a single |
| 70 | * parameter (as this wouldn't make sense, parameters that use assisted injection are user-supplied, they aren't injected |
| 71 | * from a binding). |
| 72 | * |
Marco Poletti | c7b0410 | 2014-07-27 11:59:16 +0100 | [diff] [blame] | 73 | * NOTE: This can't be used if the constructor is templated (the class can be templated, however), if there are any default |
| 74 | * arguments or if the constructor is marked `explicit'. |
Marco Poletti | a185d5e | 2014-11-08 09:00:35 +0000 | [diff] [blame] | 75 | * 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] | 76 | * |
Marco Poletti | a185d5e | 2014-11-08 09:00:35 +0000 | [diff] [blame] | 77 | * NOTE: ASSISTED takes just one argument, but it's declared as variadic to make sure that the preprocessor doesn't choke on |
| 78 | * 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] | 79 | * |
Marco Poletti | 31f638a | 2015-05-10 21:21:25 +0100 | [diff] [blame] | 80 | * NOTE: ASSISTED takes just 2 arguments, but it's declared as variadic to make sure that the preprocessor doesn't choke on |
| 81 | * multi-argument templates, that the processor is unable to parse correctly. |
| 82 | * |
Marco Poletti | 1c7e821 | 2016-11-12 11:04:45 +0000 | [diff] [blame] | 83 | * NOTE: In addition to the public Inject typedef, two typedefs (FruitAssistedTypedef and FruitAnnotatedTypedef) will be defined inside the class, |
| 84 | * make sure you don't define another typedef/field/method with the same name if you use the INJECT macro (unlikely but possible) these typedefs |
| 85 | * are an implementation detail of Fruit and should not be used. |
Marco Poletti | 169e50f | 2016-02-21 17:48:46 +0000 | [diff] [blame] | 86 | * |
| 87 | * NOTE: The return type (MyClass in this case) should not be annotated. However an annotated |
| 88 | * MyClass (or MyClass factory) can be injected from any INJECT declaration. |
Marco Poletti | cf798fa | 2014-06-21 15:05:15 +0100 | [diff] [blame] | 89 | */ |
| 90 | #define INJECT(Signature) \ |
| 91 | using Inject = Signature; \ |
Marco Poletti | 1c7e821 | 2016-11-12 11:04:45 +0000 | [diff] [blame] | 92 | \ |
Marco Poletti | cf798fa | 2014-06-21 15:05:15 +0100 | [diff] [blame] | 93 | template <typename FruitAssistedDeclarationParam> \ |
Marco Poletti | 31f638a | 2015-05-10 21:21:25 +0100 | [diff] [blame] | 94 | using FruitAssistedTypedef = FruitAssistedDeclarationParam; \ |
Marco Poletti | e464713 | 2015-05-31 14:35:27 +0100 | [diff] [blame] | 95 | template <typename Annotation, typename FruitAnnotatedDeclarationParam> \ |
Marco Poletti | 31f638a | 2015-05-10 21:21:25 +0100 | [diff] [blame] | 96 | using FruitAnnotatedTypedef = FruitAnnotatedDeclarationParam; \ |
Marco Poletti | 1c7e821 | 2016-11-12 11:04:45 +0000 | [diff] [blame] | 97 | \ |
Marco Poletti | cf798fa | 2014-06-21 15:05:15 +0100 | [diff] [blame] | 98 | Signature |
| 99 | |
Marco Poletti | 31f638a | 2015-05-10 21:21:25 +0100 | [diff] [blame] | 100 | #define ASSISTED(...) FruitAssistedTypedef<__VA_ARGS__> |
| 101 | #define ANNOTATED(Annotation, ...) FruitAnnotatedTypedef<Annotation, __VA_ARGS__> |
Marco Poletti | cf798fa | 2014-06-21 15:05:15 +0100 | [diff] [blame] | 102 | |
Marco Poletti | 5bc9876 | 2014-07-27 11:02:44 +0100 | [diff] [blame] | 103 | /** |
Marco Poletti | 31f638a | 2015-05-10 21:21:25 +0100 | [diff] [blame] | 104 | * 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] | 105 | * |
Marco Poletti | 31f638a | 2015-05-10 21:21:25 +0100 | [diff] [blame] | 106 | * NOTE: don't use these directly, they're only used to implement the INJECT macro. |
| 107 | * Consider them part of fruit::impl. |
Marco Poletti | cf798fa | 2014-06-21 15:05:15 +0100 | [diff] [blame] | 108 | */ |
| 109 | template <typename T> |
Marco Poletti | 31f638a | 2015-05-10 21:21:25 +0100 | [diff] [blame] | 110 | using FruitAssistedTypedef = fruit::Assisted<T>; |
| 111 | template <typename Annotation, typename T> |
| 112 | using FruitAnnotatedTypedef = fruit::Annotated<Annotation, T>; |
Marco Poletti | cf798fa | 2014-06-21 15:05:15 +0100 | [diff] [blame] | 113 | |
Marco Poletti | e0808b5 | 2014-08-23 16:59:13 +0200 | [diff] [blame] | 114 | #endif // FRUIT_MACRO_H |