| 1 | /* |
| 2 | * %injeqt copyright begin% |
| 3 | * Copyright 2014 Rafał Malinowski ([email protected]) |
| 4 | * %injeqt copyright end% |
| 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation; either |
| 9 | * version 2.1 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | |
| 21 | #pragma once |
| 22 | |
| 23 | #include <injeqt/injeqt.h> |
| 24 | #include <injeqt/type.h> |
| 25 | |
| 26 | #include "implementations.h" |
| 27 | #include "providers.h" |
| 28 | #include "types-by-name.h" |
| 29 | #include "types-model.h" |
| 30 | |
| 31 | #include <vector> |
| 32 | #include <QtCore/QObject> |
| 33 | |
| 34 | /** |
| 35 | * @file |
| 36 | * @brief Contains classes and functions for implementation of injector core. |
| 37 | */ |
| 38 | |
| 39 | namespace injeqt { namespace internal { |
| 40 | |
| 41 | class provided_object; |
| 42 | |
| 43 | /** |
| 44 | * @brief Implementation of injector class. |
| 45 | * @see injector |
| 46 | * @see injector_impl |
| 47 | * |
| 48 | * This class is core of Injeqt library. |
| 49 | * |
| 50 | * Its main purpose is to gather configuration from set of providers and construct objects with |
| 51 | * resolved dependencies. |
| 52 | * |
| 53 | * Injector keeps list of all configured providers and of all already created objects. |
| 54 | * Two methods objects_with(implementations, const type &) and objects_with(implementations, const types &) |
| 55 | * are used to update list of already created objects with new ones. |
| 56 | */ |
| 57 | class INJEQT_API injector_core final |
| 58 | { |
| 59 | |
| 60 | public: |
| 61 | /** |
| 62 | * @brief Create empty injector_core with no configuration. |
| 63 | * |
| 64 | * Injector with empty configuration will not be able to return any objects. Each call |
| 65 | * to get(const type &) will result in type_not_configured |
| 66 | */ |
| 67 | injector_core(); |
| 68 | |
| 69 | /** |
| 70 | * @brief Create injector configured with set of providers. |
| 71 | * @param all_providers set of all providers available to injector |
| 72 | * @see injector::injector(std::vector<std::unique_ptr<module>>) |
| 73 | * @throw ambiguous_types if one or more types in @p providers is ambiguous |
| 74 | * @throw unresolvable_dependencies if a type with unresolvable dependency is found in @p providers |
| 75 | * @throw dependency_on_self when type depends on self |
| 76 | * @throw dependency_on_subtype when type depends on own supertype |
| 77 | * @throw dependency_on_subtype when type depends on own subtype |
| 78 | * @throw invalid_setter if any tagged setter has parameter that is not a QObject-derived pointer |
| 79 | * @throw invalid_setter if any tagged setter has parameter that is a QObject pointer |
| 80 | * @throw invalid_setter if any tagged setter has parameter that is a QObject-derived pointer of not configured type |
| 81 | * @throw invalid_setter if any tagged setter has other number of parameters than one |
| 82 | * |
| 83 | * This constructor creates types_model object to get all required information from providers. This object |
| 84 | * takes ownership of passed providers. |
| 85 | */ |
| 86 | explicit injector_core(types_by_name known_types, std::vector<std::unique_ptr<provider>> &&all_providers); |
| 87 | |
| 88 | injector_core(const injector_core &) = delete; |
| 89 | injector_core(injector_core &&) = default; |
| 90 | |
| 91 | injector_core & operator = (const injector_core &) = delete; |
| 92 | injector_core & operator = (injector_core &&) = default; |
| 93 | |
| 94 | /** |
| 95 | * @brief Destroy injector_core. |
| 96 | * |
| 97 | * TODO: add note about INJEQT_DONE |
| 98 | */ |
| 99 | ~injector_core(); |
| 100 | |
| 101 | /** |
| 102 | * @brief Returns list of all configured types. |
| 103 | * |
| 104 | * Result contians only types explicitely configured in modules, not supertypes of these. It also includes |
| 105 | * results of provided_types() calls from all added parent injectors. |
| 106 | */ |
| 107 | std::vector<type> provided_types() const; |
| 108 | |
| 109 | /** |
| 110 | * @brief Instantiates object of given type @p interface_type |
| 111 | * @param interface_type type of object to instantiate. |
| 112 | * @throw unknown_type if @p interface_type was not configured in injector |
| 113 | * @throw instantiation_failed if instantiation of one of required types failed |
| 114 | * @pre !interface_type.is_empty() |
| 115 | * @pre !interface_type.is_qobject() |
| 116 | * @see injector::get<T>() |
| 117 | */ |
| 118 | void instantiate(const type &interface_type); |
| 119 | |
| 120 | /** |
| 121 | * @brief Instantiate all objects with given @p type_role. |
| 122 | * @throw instantiation_failed if instantiation of one of found types failed |
| 123 | */ |
| 124 | void instantiate_all_with_type_role(const std::string &type_role); |
| 125 | |
| 126 | /** |
| 127 | * @brief Returns pointer to object of given type @p interface_type |
| 128 | * @param interface_type type of object to return. |
| 129 | * @throw unknown_type if @p interface_type was not configured in injector |
| 130 | * @throw instantiation_failed if instantiation of one of required types failed |
| 131 | * @pre !interface_type.is_empty() |
| 132 | * @pre !interface_type.is_qobject() |
| 133 | * @see injector::get<T>() |
| 134 | */ |
| 135 | QObject * get(const type &interface_type); |
| 136 | |
| 137 | /** |
| 138 | * @brief Returns all objects with given @p type_role. |
| 139 | * @throw instantiation_failed if instantiation of one of found types failed |
| 140 | */ |
| 141 | std::vector<QObject *> get_all_with_type_role(const std::string &type_role); |
| 142 | |
| 143 | /** |
| 144 | * @brief Inject dependencies into @p object. |
| 145 | * @param object object to inject dependencies into. |
| 146 | * @throw invalid_setter if any tagged setter has parameter that is not a QObject-derived pointer |
| 147 | * @throw invalid_setter if any tagged setter has parameter that is a QObject pointer |
| 148 | * @throw invalid_setter if any tagged setter has parameter that is a QObject-derived pointer of not configured type |
| 149 | * @throw invalid_setter if any tagged setter has other number of parameters than one |
| 150 | * @pre object != nullptr |
| 151 | * |
| 152 | * This method looks for invokable setters tagged with INJEQT_SET in @p object. If any of setter is not valid |
| 153 | * dependency injector setter or its parameter is of type not configured in injector an exception is thrown. |
| 154 | * If all setters are valid, they are called with proper objects (which may be already available in injector |
| 155 | * or created on demand). |
| 156 | */ |
| 157 | void inject_into(QObject *object); |
| 158 | |
| 159 | private: |
| 160 | types_by_name _known_types; |
| 161 | providers _available_providers; |
| 162 | implementations _objects; |
| 163 | implementations _resolved_objects; |
| 164 | types_model _types_model; |
| 165 | |
| 166 | /** |
| 167 | * @brief Extract all provided types and makes a types_model from them. |
| 168 | * @throw ambiguous_types if one or more types in @p all_providers is ambiguous |
| 169 | * @throw unresolvable_dependencies if a type with unresolvable dependency is found in @p all_providers |
| 170 | * @throw dependency_on_self when type depends on self |
| 171 | * @throw dependency_on_subtype when type depends on own supertype |
| 172 | * @throw dependency_on_subtype when type depends on own subtype |
| 173 | * @throw invalid_setter if any tagged setter has parameter that is not a QObject-derived pointer |
| 174 | * @throw invalid_setter if any tagged setter has parameter that is a QObject pointer |
| 175 | * @throw invalid_setter if any tagged setter has other number of parameters than one |
| 176 | */ |
| 177 | types_model create_types_model() const; |
| 178 | |
| 179 | /** |
| 180 | * @brief Return type that implements @p interface_type. |
| 181 | * @throw unknown_type if @p interface_type does not have corresponding implementation |
| 182 | */ |
| 183 | type implementation_for(const type &interface_type) const; |
| 184 | |
| 185 | /** |
| 186 | * @brief Instantiate class of interface type @p interface_type and makes it available for use. |
| 187 | * @param interface_type type of interface of object to create |
| 188 | * @throw instantiation_failed if instantiation of one of required types failed |
| 189 | * @throw unknown_type if @p interface_type does not have corresponding implementation |
| 190 | * |
| 191 | * Instantiate class of interface type @p interface_type with all of its dependencies, then resolves them and |
| 192 | * calls INJEQT_INIT slots. |
| 193 | */ |
| 194 | void instantiate_interface(const type &interface_type); |
| 195 | |
| 196 | /** |
| 197 | * @brief Instantiate class of type @p implementation_type and makes it available for use. |
| 198 | * @param interface_type type of interface of object to create |
| 199 | * @throw instantiation_failed if instantiation of one of required types failed |
| 200 | * |
| 201 | * Instantiate class of exact type @p implementation_type with all of its dependencies, then resolves them and |
| 202 | * calls INJEQT_INIT slots. |
| 203 | */ |
| 204 | void instantiate_implementation(const type &implementation_type); |
| 205 | |
| 206 | /** |
| 207 | * @brief Return all dependencies for @p implementation_type. |
| 208 | */ |
| 209 | dependencies implementation_type_dependencies(const type &implementation_type) const; |
| 210 | |
| 211 | /** |
| 212 | * @brief Instantiate classes of interface types from @p interface_types and makes them available for use. |
| 213 | * @param interface_types types of interfaces of objects to create |
| 214 | * @throw instantiation_failed if instantiation of one of required types failed |
| 215 | * @pre No class from @p interface_types contains dependency that is not already resolved or not in @p interface_types |
| 216 | * |
| 217 | * Instantiate all classes of interface type @p interface_types without looking for dependencies. |
| 218 | */ |
| 219 | void instantiate_all(const types &interface_types); |
| 220 | |
| 221 | /** |
| 222 | * @brief Instantiate classes that are required before instantiating any of @p types_to_instantiate. |
| 223 | * @param types_to_instantiate types that are checked for list of required types |
| 224 | * @throw instantiation_failed if instantiation of one of required types failed |
| 225 | * |
| 226 | * Instantiate all classes that are returned from @see provider::required_type() methods of any provider |
| 227 | * for these types. |
| 228 | */ |
| 229 | void instantiate_required_types_for(const types &types_to_instantiate); |
| 230 | |
| 231 | /** |
| 232 | * @brief Return list of providers required to instantiate types from @p for_types. |
| 233 | */ |
| 234 | template<typename T> |
| 235 | std::vector<provider *> providers_for(const T &for_types) const |
| 236 | { |
| 237 | auto result = std::vector<provider *>{}; |
| 238 | result.reserve(for_types.size()); |
| 239 | for (auto &&for_type : for_types) |
| 240 | { |
| 241 | auto provider_it = _available_providers.get(for_type); |
| 242 | assert(provider_it != end(_available_providers)); |
| 243 | |
| 244 | result.push_back(provider_it->get()); |
| 245 | } |
| 246 | |
| 247 | return result; |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * @brief Filter list of types from @p to_filter to include only non already instantiated types. |
| 252 | */ |
| 253 | std::vector<type> non_instantiated(const types &to_filter) const; |
| 254 | |
| 255 | /** |
| 256 | * @brief Instantiate types with @p providers. |
| 257 | */ |
| 258 | std::vector<provided_object> provide_objects(const std::vector<provider *> &providers); |
| 259 | |
| 260 | /** |
| 261 | * @brief Return objects that needs resolving from @p provided_objects. |
| 262 | * @see provider::require_resolving() |
| 263 | */ |
| 264 | std::vector<implementation> objects_to_resolve(const std::vector<provided_object> &provided_objects) const; |
| 265 | |
| 266 | /** |
| 267 | * @brief Extract implementations from vector of @p provided_object. |
| 268 | */ |
| 269 | std::vector<implementation> extract_implementations(const std::vector<provided_object> &provided_objects) const; |
| 270 | |
| 271 | /** |
| 272 | * @brief Return objects to store in list of instantiated objects. |
| 273 | * |
| 274 | * Each implementation object is returned with set of @see implementation instances that contains all unique inferfaces |
| 275 | * for that object, so it is later avaialble under all types it implements. |
| 276 | */ |
| 277 | std::vector<implementation> objects_to_store(const std::vector<implementation> &objects) const; |
| 278 | |
| 279 | /** |
| 280 | * @brief Resolve all @p objects dependencies, call all INJEQT_INIT slots and add types to list of resolved objects. |
| 281 | * |
| 282 | * This method assumes that all object dependencies are already instantiated. |
| 283 | */ |
| 284 | void resolve_objects(const std::vector<implementation> &objects); |
| 285 | |
| 286 | /** |
| 287 | * @brief Resolve all @p object dependencies. |
| 288 | * |
| 289 | * This method assumes that all object dependencies are already instantiated. |
| 290 | */ |
| 291 | void resolve_object(const implementation &object) const; |
| 292 | |
| 293 | /** |
| 294 | * @brief Resolve all @p object dependencies with @p object_dependencies. |
| 295 | */ |
| 296 | void resolve_object(const dependencies &object_dependencies, const implementation &object) const; |
| 297 | |
| 298 | /** |
| 299 | * @brief Call all INJEQT_INIT methods on given object in proper order. |
| 300 | */ |
| 301 | void call_init_methods(QObject *object) const; |
| 302 | |
| 303 | /** |
| 304 | * @brief Call all INJEQT_DONE methods on given object in proper order. |
| 305 | */ |
| 306 | void call_done_methods(QObject *object) const; |
| 307 | |
| 308 | }; |
| 309 | |
| 310 | }} |