| 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 | #pragma once |
| 22 | |
| 23 | #include <injeqt/exception/exception.h> |
| 24 | #include <injeqt/injeqt.h> |
| 25 | #include <injeqt/type.h> |
| 26 | |
| 27 | #include "internal.h" |
| 28 | #include "types-by-name.h" |
| 29 | |
| 30 | #include <memory> |
| 31 | #include <QtCore/QMetaMethod> |
| 32 | |
| 33 | /** |
| 34 | * @file |
| 35 | * @brief Contains classes and functions for representing factory methods. |
| 36 | */ |
| 37 | |
| 38 | class QObject; |
| 39 | |
| 40 | namespace injeqt { namespace internal { |
| 41 | |
| 42 | /** |
| 43 | * @brief Abstraction of factory method. |
| 44 | * |
| 45 | * This class is used for creating objects of types configured with module::add_factory<T, F>(). |
| 46 | * Factory method is created from Qt type QMetaMethod. As Qt only creates QMetaMethod |
| 47 | * objects for slots, signals and all methods marked with Q_INVOKABLE tag - Injeqt |
| 48 | * requires that factory method is marked with that tag or is a slot. This method must return |
| 49 | * pointer of type inherited from QObject and is not allowed only to have parameters with |
| 50 | * default values. |
| 51 | * |
| 52 | * Example of valid factory method: |
| 53 | * |
| 54 | * class created_object : public QObject |
| 55 | * { |
| 56 | * Q_OBJECT |
| 57 | * }; |
| 58 | * |
| 59 | * class with_factory_method : public QObject |
| 60 | * { |
| 61 | * Q_OBJECT |
| 62 | * public: |
| 63 | * Q_INVOKABLE created_object * create() { ... } |
| 64 | * }; |
| 65 | * |
| 66 | * To extract constructor from a type call make_factory_method<T, F>(). |
| 67 | * For above example a call to make_factory_method<with_factory_method, created_object>() |
| 68 | * would return valid object. |
| 69 | * |
| 70 | * Note that in Injeqt factory should not own created object - invoking |
| 71 | * injector own lifetime of it. Method invoke(QObject *) call factory method |
| 72 | * and immediately wraps returned QObject * in a std::unique_ptr that it later |
| 73 | * returns. |
| 74 | */ |
| 75 | class INJEQT_INTERNAL_API factory_method final |
| 76 | { |
| 77 | |
| 78 | public: |
| 79 | /** |
| 80 | * @brief Create empty factory_method. |
| 81 | */ |
| 82 | factory_method(); |
| 83 | |
| 84 | /** |
| 85 | * @brief Create object from QMetaMethod definition. |
| 86 | * @param parameter_type Type of retrun value of @p meta_method |
| 87 | * @param meta_method Qt meta method that should be a factory method |
| 88 | * @note Qt QMetaType system limitations with plugins disallow use of QMetaType to retreive return type from QMetaMethod |
| 89 | * @pre meta_method.methodType() == QMetaMethod::Method || meta_method.methodType() == QMetaMethod::Slot |
| 90 | * @pre meta_method.parameterCount() == 0 |
| 91 | * @pre meta_method.enclosingMetaObject() != nullptr |
| 92 | * @pre !result_type.is_empty() |
| 93 | * @pre result_type.name() + "*" == std::string{result_type.typeName()} |
| 94 | */ |
| 95 | explicit factory_method(type result_type, QMetaMethod meta_method); |
| 96 | |
| 97 | /** |
| 98 | * @return true if factory_method is empty and does not represent method |
| 99 | */ |
| 100 | bool is_empty() const; |
| 101 | |
| 102 | /** |
| 103 | * @return Type of objects that owns this factory method. |
| 104 | */ |
| 105 | const type & object_type() const; |
| 106 | |
| 107 | /** |
| 108 | * @return Type of objects created by factory method. |
| 109 | */ |
| 110 | const type & result_type() const; |
| 111 | |
| 112 | /** |
| 113 | * @return Qt representation of factory method. |
| 114 | * |
| 115 | * May return empty value if is_empty() |
| 116 | */ |
| 117 | const QMetaMethod & meta_method() const; |
| 118 | |
| 119 | /** |
| 120 | * @param on object to call this method on |
| 121 | * @return Result on factory method called on given object. |
| 122 | * @pre !is_empty() |
| 123 | * @pre on != nullptr |
| 124 | * @pre meta_method().enclosingMetaObject() is equal to @p on type |
| 125 | * |
| 126 | * This method can be only called on valid objects with @p on parameter being |
| 127 | * the same type as object_type() returns. Invalid invocation with result |
| 128 | * in undefined behavior. |
| 129 | */ |
| 130 | std::unique_ptr<QObject> invoke(QObject *on) const; |
| 131 | |
| 132 | private: |
| 133 | type _object_type; |
| 134 | type _result_type; |
| 135 | QMetaMethod _meta_method; |
| 136 | |
| 137 | }; |
| 138 | |
| 139 | INJEQT_INTERNAL_API bool operator == (const factory_method &x, const factory_method &y); |
| 140 | INJEQT_INTERNAL_API bool operator != (const factory_method &x, const factory_method &y); |
| 141 | |
| 142 | /** |
| 143 | * @brief Extract factory method from given type F that returns pointers to T. |
| 144 | * @tparam t factory method must return T * |
| 145 | * @tparam f type to extract constructor from |
| 146 | * @pre !t.is_empty() && !t.is_qobject() |
| 147 | * @pre !f.is_empty() && !f.is_qobject() |
| 148 | * |
| 149 | * This function looks for all methods of type F that are tagged with Q_INVOKABLE, does not |
| 150 | * accepts argumetns and returns T* or pointer to type derived from T. If only one such method |
| 151 | * is found it is wrapped in factory_method type and returned. In other cases an empty |
| 152 | * factory_method is returned. |
| 153 | */ |
| 154 | INJEQT_INTERNAL_API factory_method make_factory_method(const types_by_name &known_types, const type &t, const type &f); |
| 155 | |
| 156 | }} |