| 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 "dependencies.h" |
| 22 | |
| 23 | #include <injeqt/exception/dependency-on-self.h> |
| 24 | #include <injeqt/exception/dependency-on-subtype.h> |
| 25 | #include <injeqt/exception/dependency-on-supertype.h> |
| 26 | #include <injeqt/exception/exception.h> |
| 27 | #include <injeqt/exception/invalid-dependency.h> |
| 28 | #include <injeqt/exception/invalid-setter.h> |
| 29 | #include <injeqt/type.h> |
| 30 | |
| 31 | #include "dependency.h" |
| 32 | #include "interfaces-utils.h" |
| 33 | #include "setter-method.h" |
| 34 | #include "type-relations.h" |
| 35 | |
| 36 | #include <QtCore/QMetaMethod> |
| 37 | #include <algorithm> |
| 38 | #include <cassert> |
| 39 | #include <set> |
| 40 | |
| 41 | namespace injeqt { namespace internal { |
| 42 | |
| 43 | namespace { |
| 44 | |
| 45 | std::vector<setter_method> extract_setters(const types_by_name &known_types, const type &for_type) |
| 46 | { |
| 47 | assert(!for_type.is_empty()); |
| 48 | |
| 49 | auto result = std::vector<setter_method>{}; |
| 50 | |
| 51 | auto meta_object = for_type.meta_object(); |
| 52 | auto method_count = meta_object->methodCount(); |
| 53 | for (decltype(method_count) i = 0; i < method_count; i++) |
| 54 | { |
| 55 | auto maybe_setter = meta_object->method(i); |
| 56 | if (setter_method::is_setter_tag(maybe_setter.tag())) |
| 57 | result.emplace_back(make_setter_method(known_types, maybe_setter)); |
| 58 | } |
| 59 | |
| 60 | return result; |
| 61 | } |
| 62 | |
| 63 | } |
| 64 | |
| 65 | dependencies extract_dependencies(const types_by_name &known_types, const type &for_type) |
| 66 | { |
| 67 | assert(!for_type.is_empty()); |
| 68 | |
| 69 | auto interfaces = extract_interfaces(for_type); |
| 70 | auto setters = extract_setters(known_types, for_type); |
| 71 | for (auto &&setter : setters) |
| 72 | { |
| 73 | auto parameter_type = setter.parameter_type(); |
| 74 | if (parameter_type == for_type) |
| 75 | throw exception::dependency_on_self{}; |
| 76 | if (std::find(std::begin(interfaces), std::end(interfaces), parameter_type) != std::end(interfaces)) |
| 77 | throw exception::dependency_on_supertype{}; |
| 78 | auto parameter_interfaces = extract_interfaces(parameter_type); |
| 79 | if (std::find(std::begin(parameter_interfaces), std::end(parameter_interfaces), for_type) != std::end(parameter_interfaces)) |
| 80 | throw exception::dependency_on_subtype{}; |
| 81 | } |
| 82 | |
| 83 | auto result = std::vector<dependency>{}; |
| 84 | std::transform(std::begin(setters), std::end(setters), std::back_inserter(result), |
| 85 | [](const setter_method &setter){ return dependency{setter}; } |
| 86 | ); |
| 87 | |
| 88 | return dependencies{result}; |
| 89 | } |
| 90 | |
| 91 | }} |