| 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 <memory> |
| 30 | #include <QtCore/QMetaMethod> |
| 31 | |
| 32 | /** |
| 33 | * @file |
| 34 | * @brief Contains classes and functions for representing default constructors. |
| 35 | */ |
| 36 | |
| 37 | class QObject; |
| 38 | |
| 39 | namespace injeqt { namespace internal { |
| 40 | |
| 41 | /** |
| 42 | * @brief Abstraction of default constructor. |
| 43 | * |
| 44 | * This class is used for creating objects of types configured with module::add_type<T>(). |
| 45 | * Default constructor is crated from Qt type QMetaMethod. As Qt only creates QMetaMethod |
| 46 | * objects for slots, signals and all methods marked with Q_INVOKABLE tag - Injeqt |
| 47 | * requires that default constructor is also marked with that tag. It is also not allowed |
| 48 | * to have any parameters without default values. |
| 49 | * |
| 50 | * Example of valid default constructor: |
| 51 | * |
| 52 | * class with_default_constructor : public QObject |
| 53 | * { |
| 54 | * Q_OBJECT |
| 55 | * public: |
| 56 | * Q_INVOKABLE with_default_constructor(QObject * parent = nullptr) {} |
| 57 | * }; |
| 58 | * |
| 59 | * To extract constructor from a type call make_default_constructor_method<T>(). |
| 60 | */ |
| 61 | class INJEQT_INTERNAL_API default_constructor_method final |
| 62 | { |
| 63 | |
| 64 | public: |
| 65 | /** |
| 66 | * @brief Create empty default_constructor_method. |
| 67 | */ |
| 68 | default_constructor_method(); |
| 69 | |
| 70 | /** |
| 71 | * @brief Create object from QMetaMethod definition. |
| 72 | * @param meta_method Qt meta method that should be a default constructor |
| 73 | * @pre meta_method.methodType() == QMetaMethod::Constructor |
| 74 | * @pre meta_method.parameterCount() == 0 |
| 75 | * @pre meta_method.enclosingMetaObject() != nullptr |
| 76 | */ |
| 77 | explicit default_constructor_method(QMetaMethod meta_method); |
| 78 | |
| 79 | /** |
| 80 | * @return true if default_constructor_method is empty and does not represent constructor |
| 81 | */ |
| 82 | bool is_empty() const; |
| 83 | |
| 84 | /** |
| 85 | * @return Type of objects created by default constructor. |
| 86 | */ |
| 87 | const type & object_type() const; |
| 88 | |
| 89 | /** |
| 90 | * @return Qt representation of default constructor. |
| 91 | * |
| 92 | * May return empty value if object is empty. |
| 93 | */ |
| 94 | const QMetaMethod & meta_method() const; |
| 95 | |
| 96 | /** |
| 97 | * @return New instance of type of default constructor. |
| 98 | * @pre !is_empty() |
| 99 | */ |
| 100 | std::unique_ptr<QObject> invoke() const; |
| 101 | |
| 102 | private: |
| 103 | type _object_type; |
| 104 | QMetaMethod _meta_method; |
| 105 | |
| 106 | }; |
| 107 | |
| 108 | INJEQT_INTERNAL_API bool operator == (const default_constructor_method &x, const default_constructor_method &y); |
| 109 | INJEQT_INTERNAL_API bool operator != (const default_constructor_method &x, const default_constructor_method &y); |
| 110 | |
| 111 | /** |
| 112 | * @brief Extract default constructor method from given type T. |
| 113 | * @tparam t type to extract constructor from |
| 114 | * @pre !t.is_empty() && !t.is_qobject() |
| 115 | * |
| 116 | * This method looks for all constructors of type t tagged with Q_INVOKABLE and returns |
| 117 | * default_constructor_method based on one without any parameters or with all parameters |
| 118 | * with default values. If no such constructor is found - empty default_constructor_method |
| 119 | * is returned. |
| 120 | */ |
| 121 | INJEQT_INTERNAL_API default_constructor_method make_default_constructor_method(const type &t); |
| 122 | |
| 123 | }} |