| 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 "factory-method.h" |
| 22 | |
| 23 | #include "interfaces-utils.h" |
| 24 | |
| 25 | #include <cassert> |
| 26 | |
| 27 | namespace injeqt { namespace internal { |
| 28 | |
| 29 | factory_method::factory_method() |
| 30 | { |
| 31 | } |
| 32 | |
| 33 | factory_method::factory_method(type result_type, QMetaMethod meta_method) : |
| 34 | _object_type{meta_method.enclosingMetaObject()}, |
| 35 | _result_type{std::move(result_type)}, |
| 36 | _meta_method{std::move(meta_method)} |
| 37 | { |
| 38 | assert(meta_method.methodType() == QMetaMethod::Method || meta_method.methodType() == QMetaMethod::Slot); |
| 39 | assert(meta_method.parameterCount() == 0); |
| 40 | assert(meta_method.enclosingMetaObject() != nullptr); |
| 41 | assert(!_result_type.is_empty()); |
| 42 | assert(_result_type.name() + "*" == std::string{meta_method.typeName()}); |
| 43 | } |
| 44 | |
| 45 | bool factory_method::is_empty() const |
| 46 | { |
| 47 | return !_meta_method.isValid(); |
| 48 | } |
| 49 | |
| 50 | const type & factory_method::object_type() const |
| 51 | { |
| 52 | return _object_type; |
| 53 | } |
| 54 | |
| 55 | const type & factory_method::result_type() const |
| 56 | { |
| 57 | return _result_type; |
| 58 | } |
| 59 | |
| 60 | const QMetaMethod & factory_method::meta_method() const |
| 61 | { |
| 62 | return _meta_method; |
| 63 | } |
| 64 | |
| 65 | std::unique_ptr<QObject> factory_method::invoke(QObject *on) const |
| 66 | { |
| 67 | assert(!is_empty()); |
| 68 | assert(on != nullptr); |
| 69 | assert(meta_method().enclosingMetaObject() == on->metaObject()); |
| 70 | |
| 71 | QObject *result = nullptr; |
| 72 | _meta_method.invoke(on, QReturnArgument<QObject *>((_result_type.name() + "*").c_str(), result)); // TODO: check for false result |
| 73 | return std::unique_ptr<QObject>{result}; |
| 74 | } |
| 75 | |
| 76 | bool operator == (const factory_method &x, const factory_method &y) |
| 77 | { |
| 78 | if (x.object_type() != y.object_type()) |
| 79 | return false; |
| 80 | |
| 81 | if (x.result_type() != y.result_type()) |
| 82 | return false; |
| 83 | |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | bool operator != (const factory_method &x, const factory_method &y) |
| 88 | { |
| 89 | return !(x == y); |
| 90 | } |
| 91 | |
| 92 | factory_method make_factory_method(const types_by_name &known_types, const type &t, const type &f) |
| 93 | { |
| 94 | assert(!t.is_empty()); |
| 95 | assert(!t.is_qobject()); |
| 96 | assert(!f.is_empty()); |
| 97 | assert(!f.is_qobject()); |
| 98 | |
| 99 | auto meta_object = f.meta_object(); |
| 100 | auto method_count = meta_object->methodCount(); |
| 101 | auto factory_methods = std::vector<factory_method>{}; |
| 102 | |
| 103 | for (decltype(method_count) i = 0; i < method_count; i++) |
| 104 | { |
| 105 | auto method = meta_object->method(i); |
| 106 | if (method.parameterCount() != 0) |
| 107 | continue; |
| 108 | auto return_type = type_by_pointer(known_types, method.typeName()); |
| 109 | if (return_type.is_empty()) |
| 110 | continue; |
| 111 | auto interfaces = extract_interfaces(return_type); |
| 112 | if (interfaces.contains(t)) |
| 113 | factory_methods.emplace_back(return_type, method); |
| 114 | } |
| 115 | |
| 116 | if (factory_methods.size() == 1) |
| 117 | return factory_methods.front(); |
| 118 | else |
| 119 | return factory_method{}; |
| 120 | } |
| 121 | |
| 122 | }} |