| 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 "required-to-satisfy.h" |
| 22 | |
| 23 | #include "dependencies.h" |
| 24 | #include "implemented-by.h" |
| 25 | #include "interfaces-utils.h" |
| 26 | |
| 27 | #include <cassert> |
| 28 | #include <set> |
| 29 | |
| 30 | namespace injeqt { namespace internal { |
| 31 | |
| 32 | types required_to_satisfy(const dependencies &dependencies_to_satisfy, const types_model &model, const implementations &objects) |
| 33 | { |
| 34 | assert(model.get_unresolvable_dependencies().empty()); |
| 35 | |
| 36 | auto result = std::vector<type>{}; |
| 37 | auto ready = std::set<type>{}; |
| 38 | std::transform(std::begin(objects), std::end(objects), std::inserter(ready, std::end(ready)), |
| 39 | [](const implementation &i){ return i.interface_type(); }); |
| 40 | |
| 41 | auto interfaces_to_check = std::vector<type>{}; |
| 42 | std::transform(std::begin(dependencies_to_satisfy), std::end(dependencies_to_satisfy), std::back_inserter(interfaces_to_check), |
| 43 | [](const dependency &d){ return d.required_type(); }); |
| 44 | |
| 45 | while (!interfaces_to_check.empty()) |
| 46 | { |
| 47 | auto current_interface_type = interfaces_to_check.back(); |
| 48 | interfaces_to_check.pop_back(); |
| 49 | |
| 50 | auto current_implementation_type_it = model.available_types().get(current_interface_type); |
| 51 | if (current_implementation_type_it == std::end(model.available_types())) |
| 52 | continue; |
| 53 | |
| 54 | auto current_implementation_type = current_implementation_type_it->implementation_type(); |
| 55 | if (ready.find(current_implementation_type) != std::end(ready)) |
| 56 | continue; |
| 57 | ready.insert(current_implementation_type); |
| 58 | result.push_back(current_implementation_type); |
| 59 | |
| 60 | if (model.mapped_dependencies().contains_key(current_implementation_type)) |
| 61 | { |
| 62 | auto dependencies = model.mapped_dependencies().get(current_implementation_type)->dependency_list(); |
| 63 | std::transform(std::begin(dependencies), std::end(dependencies), std::back_inserter(interfaces_to_check), |
| 64 | [](const dependency &d){ return d.required_type(); }); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | return types{result}; |
| 69 | } |
| 70 | |
| 71 | }} |