| 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 "default-constructor-method.h" |
| 22 | |
| 23 | #include <cassert> |
| 24 | |
| 25 | namespace injeqt { namespace internal { |
| 26 | |
| 27 | default_constructor_method::default_constructor_method() |
| 28 | { |
| 29 | } |
| 30 | |
| 31 | default_constructor_method::default_constructor_method(QMetaMethod meta_method) : |
| 32 | _object_type{meta_method.enclosingMetaObject()}, |
| 33 | _meta_method{std::move(meta_method)} |
| 34 | { |
| 35 | assert(meta_method.methodType() == QMetaMethod::Constructor); |
| 36 | assert(meta_method.parameterCount() == 0); |
| 37 | assert(meta_method.enclosingMetaObject() != nullptr); |
| 38 | } |
| 39 | |
| 40 | bool default_constructor_method::is_empty() const |
| 41 | { |
| 42 | return !_meta_method.isValid(); |
| 43 | } |
| 44 | |
| 45 | const type & default_constructor_method::object_type() const |
| 46 | { |
| 47 | return _object_type; |
| 48 | } |
| 49 | |
| 50 | const QMetaMethod & default_constructor_method::meta_method() const |
| 51 | { |
| 52 | return _meta_method; |
| 53 | } |
| 54 | |
| 55 | std::unique_ptr<QObject> default_constructor_method::invoke() const |
| 56 | { |
| 57 | assert(!is_empty()); |
| 58 | |
| 59 | return std::unique_ptr<QObject>{_meta_method.enclosingMetaObject()->newInstance()}; |
| 60 | } |
| 61 | |
| 62 | bool operator == (const default_constructor_method &x, const default_constructor_method &y) |
| 63 | { |
| 64 | if (x.object_type() != y.object_type()) |
| 65 | return false; |
| 66 | |
| 67 | assert(x.meta_method() == y.meta_method()); |
| 68 | |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | bool operator != (const default_constructor_method &x, const default_constructor_method &y) |
| 73 | { |
| 74 | return !(x == y); |
| 75 | } |
| 76 | |
| 77 | default_constructor_method make_default_constructor_method(const type &t) |
| 78 | { |
| 79 | assert(!t.is_empty()); |
| 80 | assert(!t.is_qobject()); |
| 81 | |
| 82 | auto meta_object = t.meta_object(); |
| 83 | auto constructor_count = meta_object->constructorCount(); |
| 84 | for (decltype(constructor_count) i = 0; i < constructor_count; i++) |
| 85 | { |
| 86 | auto constructor = meta_object->constructor(i); |
| 87 | if (constructor.methodType() != QMetaMethod::Constructor) |
| 88 | continue; |
| 89 | if (constructor.parameterCount() != 0) |
| 90 | continue; |
| 91 | return default_constructor_method{constructor}; |
| 92 | } |
| 93 | |
| 94 | return default_constructor_method{}; |
| 95 | } |
| 96 | |
| 97 | }} |