| 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 | #include "injector-core.h" |
| 22 | |
| 23 | #include <injeqt/exception/ambiguous-types.h> |
| 24 | #include <injeqt/exception/unavailable-required-types.h> |
| 25 | #include <injeqt/exception/unknown-type.h> |
| 26 | #include <injeqt/module.h> |
| 27 | |
| 28 | #include "action-method.h" |
| 29 | #include "containers.h" |
| 30 | #include "interfaces-utils.h" |
| 31 | #include "provided-object.h" |
| 32 | #include "provider-by-default-constructor.h" |
| 33 | #include "provider-ready.h" |
| 34 | #include "provider.h" |
| 35 | #include "module-impl.h" |
| 36 | #include "required-to-satisfy.h" |
| 37 | #include "resolve-dependencies.h" |
| 38 | #include "resolved-dependency.h" |
| 39 | #include "type-role.h" |
| 40 | |
| 41 | #include <cassert> |
| 42 | |
| 43 | namespace injeqt { namespace internal { |
| 44 | |
| 45 | injector_core::injector_core() |
| 46 | { |
| 47 | } |
| 48 | |
| 49 | injector_core::injector_core(types_by_name known_types, std::vector<std::unique_ptr<provider>> &&all_providers) : |
| 50 | _known_types{std::move(known_types)} |
| 51 | { |
| 52 | auto all_providers_size = all_providers.size(); |
| 53 | _available_providers = providers{std::move(all_providers)}; |
| 54 | |
| 55 | // some types were removed, because of duplication |
| 56 | if (_available_providers.size() != all_providers_size) |
| 57 | throw exception::ambiguous_types{}; // TODO: find a way to extract type names |
| 58 | |
| 59 | _types_model = create_types_model(); |
| 60 | |
| 61 | auto required_types = std::vector<type>{}; |
| 62 | for (auto &&p : _available_providers) |
| 63 | for (auto &&r : p->required_types()) |
| 64 | required_types.push_back(r); |
| 65 | |
| 66 | auto unavailable_required_types = match(types{required_types}, _types_model.available_types()).unmatched_1; |
| 67 | if (!unavailable_required_types.empty()) |
| 68 | { |
| 69 | auto message = std::string{}; |
| 70 | for (auto &&t : unavailable_required_types) |
| 71 | { |
| 72 | message.append(t.name()); |
| 73 | message.append("\n"); |
| 74 | } |
| 75 | throw exception::unavailable_required_types{message}; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | injector_core::~injector_core() |
| 80 | { |
| 81 | for (auto &&resolved_object : _resolved_objects) |
| 82 | call_done_methods(resolved_object.object()); |
| 83 | } |
| 84 | |
| 85 | types_model injector_core::create_types_model() const |
| 86 | { |
| 87 | auto all_types = std::vector<type>{}; |
| 88 | auto need_dependencies = std::vector<type>{}; |
| 89 | for (auto &&p : _available_providers) |
| 90 | { |
| 91 | all_types.push_back(p->provided_type()); |
| 92 | if (p->require_resolving()) |
| 93 | { |
| 94 | auto interfaces = extract_interfaces(p->provided_type()); |
| 95 | std::copy(std::begin(interfaces), std::end(interfaces), std::back_inserter(need_dependencies)); |
| 96 | } |
| 97 | } |
| 98 | return make_types_model(_known_types, all_types, need_dependencies); |
| 99 | } |
| 100 | |
| 101 | std::vector<type> injector_core::provided_types() const |
| 102 | { |
| 103 | auto result = std::vector<type>{}; |
| 104 | std::transform(std::begin(_available_providers), std::end(_available_providers), std::back_inserter(result), type_from_provider); |
| 105 | return result; |
| 106 | } |
| 107 | |
| 108 | void injector_core::instantiate(const type &interface_type) |
| 109 | { |
| 110 | assert(!interface_type.is_empty()); |
| 111 | assert(!interface_type.is_qobject()); |
| 112 | |
| 113 | auto object_it = _objects.get(interface_type); |
| 114 | if (object_it == end(_objects)) |
| 115 | instantiate_interface(interface_type); |
| 116 | } |
| 117 | |
| 118 | void injector_core::instantiate_all_with_type_role(const std::string &type_role) |
| 119 | { |
| 120 | for (auto &&provider : _available_providers) |
| 121 | { |
| 122 | auto type = provider->provided_type(); |
| 123 | if (has_type_role(type, type_role)) |
| 124 | instantiate_interface(type); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | QObject * injector_core::get(const type &interface_type) |
| 129 | { |
| 130 | assert(!interface_type.is_empty()); |
| 131 | assert(!interface_type.is_qobject()); |
| 132 | |
| 133 | instantiate(interface_type); |
| 134 | return _objects.get(interface_type)->object(); |
| 135 | } |
| 136 | |
| 137 | std::vector<QObject *> injector_core::get_all_with_type_role(const std::string &type_role) |
| 138 | { |
| 139 | auto result = std::vector<QObject *>{}; |
| 140 | for (auto &&provider : _available_providers) |
| 141 | { |
| 142 | auto type = provider->provided_type(); |
| 143 | if (has_type_role(type, type_role)) |
| 144 | result.push_back(get(type)); |
| 145 | } |
| 146 | |
| 147 | return result; |
| 148 | } |
| 149 | |
| 150 | void injector_core::instantiate_interface(const type &interface_type) |
| 151 | { |
| 152 | assert(!interface_type.is_empty()); |
| 153 | assert(!interface_type.is_qobject()); |
| 154 | |
| 155 | instantiate_implementation(implementation_for(interface_type)); |
| 156 | } |
| 157 | |
| 158 | type injector_core::implementation_for(const type &interface_type) const |
| 159 | { |
| 160 | assert(!interface_type.is_empty()); |
| 161 | assert(!interface_type.is_qobject()); |
| 162 | |
| 163 | auto implementation_type_it = _types_model.available_types().get(interface_type); |
| 164 | if (implementation_type_it == end(_types_model.available_types())) |
| 165 | throw exception::unknown_type{interface_type.name()}; |
| 166 | return implementation_type_it->implementation_type(); |
| 167 | } |
| 168 | |
| 169 | void injector_core::instantiate_implementation(const type &implementation_type) |
| 170 | { |
| 171 | assert(!implementation_type.is_empty()); |
| 172 | assert(!implementation_type.is_qobject()); |
| 173 | |
| 174 | auto types_to_instantiate = required_to_satisfy(implementation_type_dependencies(implementation_type), _types_model, _objects); |
| 175 | types_to_instantiate.add(implementation_type); |
| 176 | instantiate_all(types_to_instantiate); |
| 177 | } |
| 178 | |
| 179 | dependencies injector_core::implementation_type_dependencies(const type &implementation_type) const |
| 180 | { |
| 181 | assert(!implementation_type.is_empty()); |
| 182 | assert(!implementation_type.is_qobject()); |
| 183 | |
| 184 | return _types_model.mapped_dependencies().contains_key(implementation_type) |
| 185 | ? _types_model.mapped_dependencies().get(implementation_type)->dependency_list() |
| 186 | : dependencies{}; |
| 187 | } |
| 188 | |
| 189 | void injector_core::instantiate_all(const types &interface_types) |
| 190 | { |
| 191 | instantiate_required_types_for(interface_types); |
| 192 | |
| 193 | auto provided_objects = provide_objects(providers_for(non_instantiated(interface_types))); |
| 194 | _objects.merge(implementations{objects_to_store(extract_implementations(provided_objects))}); |
| 195 | resolve_objects(objects_to_resolve(provided_objects)); |
| 196 | } |
| 197 | |
| 198 | void injector_core::instantiate_required_types_for(const types &types_to_instantiate) |
| 199 | { |
| 200 | for (auto &&provider : providers_for(types_to_instantiate)) |
| 201 | for (auto &&required_type : provider->required_types()) |
| 202 | instantiate_interface(required_type); |
| 203 | } |
| 204 | |
| 205 | std::vector<type> injector_core::non_instantiated(const types &to_filter) const |
| 206 | { |
| 207 | auto result = std::vector<type>{}; |
| 208 | result.reserve(to_filter.size()); |
| 209 | for (auto &&type : to_filter) |
| 210 | if (_objects.get(type) == end(_objects)) |
| 211 | result.push_back(type); |
| 212 | return result; |
| 213 | } |
| 214 | |
| 215 | std::vector<provided_object> injector_core::provide_objects(const std::vector<provider *> &providers) |
| 216 | { |
| 217 | auto result = std::vector<provided_object>{}; |
| 218 | result.reserve(providers.size()); |
| 219 | for (auto &&provider : providers) |
| 220 | { |
| 221 | auto instance = provider->provide(*this); |
| 222 | auto i = make_implementation(provider->provided_type(), instance); |
| 223 | result.push_back(provided_object{provider, i}); |
| 224 | } |
| 225 | return result; |
| 226 | } |
| 227 | |
| 228 | std::vector<implementation> injector_core::objects_to_resolve(const std::vector<provided_object> &provided_objects) const |
| 229 | { |
| 230 | auto result = std::vector<implementation>{}; |
| 231 | result.reserve(provided_objects.size()); |
| 232 | for (auto &&provided_object : provided_objects) |
| 233 | if (provided_object.provided_by()->require_resolving()) |
| 234 | result.push_back(provided_object.object()); |
| 235 | return result; |
| 236 | } |
| 237 | |
| 238 | std::vector<implementation> injector_core::extract_implementations(const std::vector<provided_object> &provided_objects) const |
| 239 | { |
| 240 | auto result = std::vector<implementation>{}; |
| 241 | result.reserve(provided_objects.size()); |
| 242 | for (auto &&provided_object : provided_objects) |
| 243 | result.push_back(provided_object.object()); |
| 244 | return result; |
| 245 | } |
| 246 | |
| 247 | std::vector<implementation> injector_core::objects_to_store(const std::vector<implementation> &objects) const |
| 248 | { |
| 249 | auto result = std::vector<implementation>{}; |
| 250 | for (auto &&object : objects) |
| 251 | { |
| 252 | auto interfaces = extract_interfaces(object.interface_type()); |
| 253 | auto matched = match(interfaces, _types_model.available_types()).matched; |
| 254 | for (auto &&m : matched) |
| 255 | { |
| 256 | auto i = implementation{m.first, object.object()}; // no need to check preconditions again with make_implementation |
| 257 | result.emplace_back(i); |
| 258 | } |
| 259 | } |
| 260 | return result; |
| 261 | } |
| 262 | |
| 263 | void injector_core::resolve_objects(const std::vector<implementation> &objects) |
| 264 | { |
| 265 | for (auto &&object : objects) |
| 266 | resolve_object(object); |
| 267 | for (auto &&object : objects) |
| 268 | call_init_methods(object.object()); |
| 269 | _resolved_objects.merge(implementations{objects}); |
| 270 | } |
| 271 | |
| 272 | void injector_core::resolve_object(const implementation &object) const |
| 273 | { |
| 274 | auto object_dependencies = implementation_type_dependencies(object.interface_type()); |
| 275 | resolve_object(object_dependencies, object); |
| 276 | } |
| 277 | |
| 278 | void injector_core::resolve_object(const dependencies &object_dependencies, const implementation &object) const |
| 279 | { |
| 280 | auto resolved_dependencies = resolve_dependencies(object_dependencies, _objects); |
| 281 | assert(resolved_dependencies.unresolved.empty()); |
| 282 | |
| 283 | for (auto &&resolved : resolved_dependencies.resolved) |
| 284 | { |
| 285 | assert(implements(object.interface_type(), resolved.setter().object_type())); |
| 286 | resolved.apply_on(object.object()); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | void injector_core::inject_into(QObject *object) |
| 291 | { |
| 292 | auto object_implementation = implementation{type{object->metaObject()}, object}; |
| 293 | auto dependencies = extract_dependencies(_known_types, object_implementation.interface_type()); |
| 294 | auto types_to_instantiate = required_to_satisfy(dependencies, _types_model, _objects); |
| 295 | instantiate_all(types_to_instantiate); |
| 296 | resolve_object(dependencies, object_implementation); |
| 297 | call_init_methods(object); |
| 298 | } |
| 299 | |
| 300 | void injector_core::call_init_methods(QObject *object) const |
| 301 | { |
| 302 | for (auto action : extract_actions("INJEQT_INIT", type{object->metaObject()})) |
| 303 | action.invoke(object); |
| 304 | } |
| 305 | |
| 306 | void injector_core::call_done_methods(QObject *object) const |
| 307 | { |
| 308 | auto done_actions = extract_actions("INJEQT_DONE", type{object->metaObject()}); |
| 309 | for (auto i = done_actions.rbegin(), e = done_actions.rend(); i != e; ++i) |
| 310 | i->invoke(object); |
| 311 | } |
| 312 | |
| 313 | }} |