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_BINDINGS_H 18 #define FRUIT_BINDINGS_H 19 20 #include <fruit/impl/meta/metaprogramming.h> 21 22 namespace fruit { 23 namespace impl { 24 25 // The types here represent individual entries added in a PartialComponent. 26 27 /** 28 * Binds the base class I to the implementation C. 29 * I must be a base class of C. I=C is not allowed. 30 * I and/or C may be annotated using fruit::Annotated<>. 31 */ 32 template <typename I, typename C> 33 struct Bind {}; 34 35 /** 36 * Registers Signature as the constructor signature to use to inject a type. 37 * Signature must be a valid signature and its return type must be constructible with those argument 38 * types. 39 * The arguments and the return type can be annotated using fruit::Annotated<>. 40 */ 41 template <typename Signature> 42 struct RegisterConstructor {}; 43 44 /** 45 * Binds an instance (i.e., object) to the type C. 46 * AnnotatedC may be annotated using fruit::Annotated<>. 47 * NOTE: for this binding, the runtime binding is added in advance. 48 */ 49 template <typename AnnotatedC, typename C> 50 struct BindInstance {}; 51 52 /** 53 * A variant of BindInstance that binds a constant reference. 54 */ 55 template <typename AnnotatedC, typename C> 56 struct BindConstInstance {}; 57 58 template <typename... Params> 59 struct RegisterProvider; 60 61 /** 62 * Registers `provider' as a provider of C, where provider is a lambda with no captures returning 63 * either C or C*. 64 */ 65 template <typename Lambda> 66 struct RegisterProvider<Lambda> {}; 67 68 /** 69 * Registers `provider' as a provider of C, where provider is a lambda with no captures returning 70 * either C or C*. Lambda must have the signature AnnotatedSignature (ignoring annotations). 71 */ 72 template <typename AnnotatedSignature, typename Lambda> 73 struct RegisterProvider<Lambda, AnnotatedSignature> {}; 74 75 /** 76 * Adds a multibinding for an instance (as a C&). 77 */ 78 template <typename C> 79 struct AddInstanceMultibinding {}; 80 81 /** 82 * Adds multibindings for a vector of instances (as a std::vector<C>&). 83 */ 84 template <typename C> 85 struct AddInstanceVectorMultibindings {}; 86 87 /** 88 * Similar to Bind<I, C>, but adds a multibinding instead. 89 */ 90 template <typename I, typename C> 91 struct AddMultibinding {}; 92 93 template <typename... Params> 94 struct AddMultibindingProvider; 95 96 /** 97 * Similar to RegisterProvider, but adds a multibinding instead. 98 */ 99 template <typename Lambda> 100 struct AddMultibindingProvider<Lambda> {}; 101 102 /** 103 * Similar to RegisterProvider, but adds a multibinding instead. 104 * Lambda must have the signature AnnotatedSignature (ignoring annotations). 105 */ 106 template <typename AnnotatedSignature, typename Lambda> 107 struct AddMultibindingProvider<AnnotatedSignature, Lambda> {}; 108 109 /** 110 * Registers `Lambda' as a factory of C, where `Lambda' is a lambda with no captures returning C. 111 * Lambda must have signature DecoratedSignature (ignoring any fruit::Annotated<> and 112 * fruit::Assisted<>). 113 * Lambda must return a C by value, or a std::unique_ptr<C>. 114 */ 115 template <typename DecoratedSignature, typename Lambda> 116 struct RegisterFactory {}; 117 118 /** 119 * Adds the bindings (and multibindings) in `component' to the current component. 120 * OtherComponent must be of the form Component<...>. 121 * NOTE: for this binding, the runtime binding is added in advance. 122 */ 123 template <typename GetComponentFunction> 124 struct InstallComponent {}; 125 126 /** 127 * Installs all the specified ComponentFunction objects. 128 */ 129 template <typename... ComponentFunctions> 130 struct InstallComponentFunctions {}; 131 132 /** 133 * An in-progress ReplaceComponent operation, where we don't have all the required information yet. 134 */ 135 template <typename GetReplacedComponent> 136 struct PartialReplaceComponent {}; 137 138 /** 139 * Replaces install()s for a component with install()s for another one. 140 * The two Get*Component function signatures must return the same Component<...> type. 141 */ 142 template <typename GetReplacedComponent, typename GetReplacementComponent> 143 struct ReplaceComponent {}; 144 145 } // namespace impl 146 } // namespace fruit 147 148 #endif // FRUIT_BINDINGS_H 149