| 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-impl.h" |
| 22 | |
| 23 | #include <injeqt/exception/unknown-type.h> |
| 24 | #include <injeqt/module.h> |
| 25 | |
| 26 | #include "containers.h" |
| 27 | #include "interfaces-utils.h" |
| 28 | #include "provider-by-default-constructor.h" |
| 29 | #include "provider-by-parent-injector-configuration.h" |
| 30 | #include "provider-ready.h" |
| 31 | #include "provider.h" |
| 32 | #include "module-impl.h" |
| 33 | #include "required-to-satisfy.h" |
| 34 | #include "resolve-dependencies.h" |
| 35 | #include "resolved-dependency.h" |
| 36 | |
| 37 | #include <cassert> |
| 38 | |
| 39 | namespace injeqt { namespace internal { |
| 40 | |
| 41 | injector_impl::injector_impl() |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | injector_impl::injector_impl(std::vector<std::unique_ptr<module>> modules) : |
| 46 | // modules are only stored because these can own objects used by injector |
| 47 | _modules{std::move(modules)} |
| 48 | { |
| 49 | init(std::vector<injector_impl *>{}); |
| 50 | } |
| 51 | |
| 52 | injector_impl::injector_impl(std::vector<injector_impl *> super_injectors, std::vector<std::unique_ptr<module>> modules) : |
| 53 | // modules are only stored because these can own objects used by injector |
| 54 | _modules{std::move(modules)} |
| 55 | { |
| 56 | init(super_injectors); |
| 57 | } |
| 58 | |
| 59 | void injector_impl::init(std::vector<injector_impl *> super_injectors) |
| 60 | { |
| 61 | auto extract_provider_configurations_lambda = [](const std::unique_ptr<module> &m){ return m->_pimpl->provider_configurations(); }; |
| 62 | auto extract_provider_configurations = std::function<std::vector<std::shared_ptr<provider_configuration>>(const std::unique_ptr<module> &)>{extract_provider_configurations_lambda}; |
| 63 | auto provider_configurations = extract(_modules, extract_provider_configurations); |
| 64 | |
| 65 | for (auto &&super_injector : super_injectors) |
| 66 | for (auto &&provided_type : super_injector->provided_types()) |
| 67 | provider_configurations.push_back(std::make_shared<provider_by_parent_injector_configuration>(super_injector, provided_type)); |
| 68 | |
| 69 | auto extract_types_lamdba = [](const std::shared_ptr<provider_configuration> &pc){ |
| 70 | auto result = std::vector<type>{}; |
| 71 | for (auto &&t : pc->types()) |
| 72 | { |
| 73 | auto interfaces = extract_interfaces(t); |
| 74 | std::copy(std::begin(interfaces), std::end(interfaces), std::back_inserter(result)); |
| 75 | } |
| 76 | return result; |
| 77 | }; |
| 78 | auto extract_types = std::function<std::vector<type>(const std::shared_ptr<provider_configuration> &)>{extract_types_lamdba}; |
| 79 | auto known_types = types_by_name{extract(provider_configurations, extract_types)}; |
| 80 | |
| 81 | auto create_provider_lambda = [&known_types](const std::shared_ptr<provider_configuration> &pc){ return pc->create_provider(known_types); }; |
| 82 | auto create_provider = std::function<std::unique_ptr<provider>(std::shared_ptr<provider_configuration>)>{create_provider_lambda}; |
| 83 | auto providers = transform(provider_configurations, create_provider); |
| 84 | |
| 85 | _core = injector_core{known_types, std::move(providers)}; |
| 86 | } |
| 87 | |
| 88 | std::vector<type> injector_impl::provided_types() const |
| 89 | { |
| 90 | return _core.provided_types(); |
| 91 | } |
| 92 | |
| 93 | void injector_impl::instantiate(const type &interface_type) |
| 94 | { |
| 95 | assert(!interface_type.is_empty()); |
| 96 | assert(!interface_type.is_qobject()); |
| 97 | |
| 98 | _core.instantiate(interface_type); |
| 99 | } |
| 100 | |
| 101 | void injector_impl::instantiate_all_with_type_role(const std::string &type_role) |
| 102 | { |
| 103 | _core.instantiate_all_with_type_role(type_role); |
| 104 | } |
| 105 | |
| 106 | QObject * injector_impl::get(const type &interface_type) |
| 107 | { |
| 108 | assert(!interface_type.is_empty()); |
| 109 | assert(!interface_type.is_qobject()); |
| 110 | |
| 111 | return _core.get(interface_type); |
| 112 | } |
| 113 | |
| 114 | std::vector<QObject *> injector_impl::get_all_with_type_role(const std::string &type_role) |
| 115 | { |
| 116 | return _core.get_all_with_type_role(type_role); |
| 117 | } |
| 118 | |
| 119 | void injector_impl::inject_into(QObject *object) |
| 120 | { |
| 121 | assert(object); |
| 122 | |
| 123 | _core.inject_into(object); |
| 124 | } |
| 125 | |
| 126 | }} |