| 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 "internal.h" |
| 24 | |
| 25 | #include <injeqt/exception/exception.h> |
| 26 | #include <injeqt/injeqt.h> |
| 27 | #include <injeqt/type.h> |
| 28 | |
| 29 | #include <QtCore/QMetaMethod> |
| 30 | #include <string> |
| 31 | |
| 32 | /** |
| 33 | * @file |
| 34 | * @brief Contains classes and functions for representing setter methods. |
| 35 | */ |
| 36 | |
| 37 | namespace injeqt { namespace internal { |
| 38 | |
| 39 | /** |
| 40 | * @brief Abstraction of setter method. |
| 41 | * |
| 42 | * This class is used for defining setter based dependencies and for invoking these setter methods |
| 43 | * to resolve dependencies. Setter method is crated from Qt type QMetaMethod. As Qt only creates QMetaMethod |
| 44 | * objects for slots, signals and all methods marked with Q_INVOKABLE tag and Injeqt requires |
| 45 | * all its setter methods to be tagged with INJEQT_SET - setter method must be a slot. This method should return |
| 46 | * void is allowed to have only one parameter of pointer to type inherited from QObject. This type must |
| 47 | * be also configured in one of module to be able to be resolved. |
| 48 | * |
| 49 | * Example of valid setter method: |
| 50 | * |
| 51 | * class set_object : public QObject |
| 52 | * { |
| 53 | * Q_OBJECT |
| 54 | * }; |
| 55 | * |
| 56 | * class with_setter : public QObject |
| 57 | * { |
| 58 | * Q_OBJECT |
| 59 | * public slots: |
| 60 | * INJEQT_SET void setter(set_object *obj) { ... } |
| 61 | * }; |
| 62 | * |
| 63 | * Object with setter method must not take ownership of passed object. |
| 64 | */ |
| 65 | class INJEQT_INTERNAL_API action_method final |
| 66 | { |
| 67 | |
| 68 | public: |
| 69 | static bool is_action_init_tag(const std::string &tag); |
| 70 | static bool is_action_done_tag(const std::string &tag); |
| 71 | |
| 72 | static bool validate_action_method(const QMetaMethod &meta_method); |
| 73 | |
| 74 | /** |
| 75 | * @brief Create empty action_method. |
| 76 | */ |
| 77 | action_method(); |
| 78 | |
| 79 | /** |
| 80 | * @brief Create object from QMetaMethod definition. |
| 81 | * @param parameter_type Type of parameter of setter @p meta_method |
| 82 | * @param meta_method Qt meta method that should be a setter method |
| 83 | * @note Qt QMetaType system limitations with plugins disallow use of QMetaType to retreive parameter type from QMetaMethod. |
| 84 | * @pre meta_method.methodType() == QMetaMethod::Slot |
| 85 | * @pre meta_method.parameterCount() == 1 |
| 86 | * @pre meta_method.enclosingMetaObject() != nullptr |
| 87 | * @pre !parameter_type.is_empty() |
| 88 | * @pre parameter_type.name() + "*" == std::string{meta_method.parameterTypes()[0].data()} |
| 89 | */ |
| 90 | explicit action_method(QMetaMethod meta_method); |
| 91 | |
| 92 | /** |
| 93 | * @return true if action_method is empty and does not represent valie setter method |
| 94 | */ |
| 95 | bool is_empty() const; |
| 96 | |
| 97 | /** |
| 98 | * @return Type of objects that owns this setter method. |
| 99 | * |
| 100 | * May return invalid type if QMetaMethod passed in constructor was invalid. |
| 101 | */ |
| 102 | const type & object_type() const; |
| 103 | |
| 104 | /** |
| 105 | * @param on object to call this method on |
| 106 | * @param parameter parmeter to be passed in invocation |
| 107 | * @return true if invoke was successfull |
| 108 | * @pre !is_empty() |
| 109 | * @pre on != nullptr |
| 110 | * @pre type{on->metaObject()} == object_type() |
| 111 | * @pre parameter != nullptr |
| 112 | * @pre !type{parameter->metaObject()}.is_empty() |
| 113 | * @pre implements(type{parameter->metaObject()}, _parameter_type) |
| 114 | * |
| 115 | * This method can be only called on valid objects with @p on parameter being |
| 116 | * the same type as object_type() returns and @p parameter of type that implements parameter_type(). |
| 117 | * |
| 118 | * Calling this on invalid object with result in undefined behavior. |
| 119 | */ |
| 120 | bool invoke(QObject *on) const; |
| 121 | |
| 122 | private: |
| 123 | type _object_type; |
| 124 | QMetaMethod _meta_method; |
| 125 | |
| 126 | }; |
| 127 | |
| 128 | INJEQT_INTERNAL_API action_method make_action_method(const QMetaMethod &meta_method); |
| 129 | std::vector<action_method> extract_actions(const std::string &action_tag, const type &for_type); |
| 130 | |
| 131 | }} |