| 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 "setter-method.h" |
| 22 | |
| 23 | #include <injeqt/exception/invalid-setter.h> |
| 24 | #include <injeqt/type.h> |
| 25 | |
| 26 | #include "interfaces-utils.h" |
| 27 | |
| 28 | #include <cassert> |
| 29 | |
| 30 | namespace injeqt { namespace internal { |
| 31 | |
| 32 | bool setter_method::is_setter_tag(const std::string &tag) |
| 33 | { |
| 34 | return tag == "INJEQT_SET" || tag == "INJEQT_SETTER"; |
| 35 | } |
| 36 | |
| 37 | bool setter_method::validate_setter_method(type parameter_type, const QMetaMethod &meta_method) |
| 38 | { |
| 39 | auto meta_object = meta_method.enclosingMetaObject(); |
| 40 | if (!meta_object) |
| 41 | throw exception::invalid_setter{std::string{"setter does not have enclosing meta object: "} + "?::" + meta_method.methodSignature().data()}; |
| 42 | if (meta_method.methodType() == QMetaMethod::Signal) |
| 43 | throw exception::invalid_setter{std::string{"setter is signal: "} + meta_object->className() + "::" + meta_method.methodSignature().data()}; |
| 44 | if (meta_method.methodType() == QMetaMethod::Constructor) |
| 45 | throw exception::invalid_setter{std::string{"setter is constructor: "} + meta_object->className() + "::" + meta_method.methodSignature().data()}; |
| 46 | if (!is_setter_tag(meta_method.tag())) |
| 47 | throw exception::invalid_setter{std::string{"setter does not have valid tag: "} + meta_object->className() + "::" + meta_method.methodSignature().data()}; |
| 48 | if (meta_method.parameterCount() != 1) |
| 49 | throw exception::invalid_setter{std::string{"invalid parameter count: "} + meta_object->className() + "::" + meta_method.methodSignature().data()}; |
| 50 | if (parameter_type.is_empty() || parameter_type.is_qobject()) |
| 51 | throw exception::invalid_setter{std::string{"invalid parameter (empty): "} + meta_object->className() + "::" + meta_method.methodSignature().data()}; |
| 52 | if (parameter_type.is_empty()) |
| 53 | throw exception::invalid_setter{std::string{"invalid parameter (qobject): "} + meta_object->className() + "::" + meta_method.methodSignature().data()}; |
| 54 | if (parameter_type.name() + "*" != std::string{meta_method.parameterTypes()[0].data()}) |
| 55 | throw exception::invalid_setter{std::string{"invalid parameter (type): "} + meta_object->className() + "::" + meta_method.methodSignature().data()}; |
| 56 | return true; |
| 57 | } |
| 58 | |
| 59 | setter_method::setter_method() |
| 60 | { |
| 61 | } |
| 62 | |
| 63 | setter_method::setter_method(type parameter_type, QMetaMethod meta_method) : |
| 64 | _object_type{meta_method.enclosingMetaObject()}, |
| 65 | _parameter_type{std::move(parameter_type)}, |
| 66 | _meta_method{std::move(meta_method)} |
| 67 | { |
| 68 | assert(validate_setter_method(parameter_type, meta_method)); |
| 69 | } |
| 70 | |
| 71 | bool setter_method::is_empty() const |
| 72 | { |
| 73 | return !_meta_method.isValid(); |
| 74 | } |
| 75 | |
| 76 | const type & setter_method::object_type() const |
| 77 | { |
| 78 | return _object_type; |
| 79 | } |
| 80 | |
| 81 | const type & setter_method::parameter_type() const |
| 82 | { |
| 83 | return _parameter_type; |
| 84 | } |
| 85 | |
| 86 | const QMetaMethod & setter_method::meta_method() const |
| 87 | { |
| 88 | return _meta_method; |
| 89 | } |
| 90 | |
| 91 | std::string setter_method::signature() const |
| 92 | { |
| 93 | return _meta_method.methodSignature().data(); |
| 94 | } |
| 95 | |
| 96 | bool setter_method::invoke(QObject *on, QObject *parameter) const |
| 97 | { |
| 98 | assert(!is_empty()); |
| 99 | assert(on != nullptr); |
| 100 | assert(implements(type{on->metaObject()}, _object_type)); |
| 101 | assert(parameter != nullptr); |
| 102 | assert(!type{parameter->metaObject()}.is_empty()); |
| 103 | assert(implements(type{parameter->metaObject()}, _parameter_type)); |
| 104 | |
| 105 | return _meta_method.invoke(on, Q_ARG(QObject *, parameter)); |
| 106 | } |
| 107 | |
| 108 | bool operator == (const setter_method &x, const setter_method &y) |
| 109 | { |
| 110 | if (x.object_type() != y.object_type()) |
| 111 | return false; |
| 112 | |
| 113 | if (x.parameter_type() != y.parameter_type()) |
| 114 | return false; |
| 115 | |
| 116 | if (x.meta_method() != y.meta_method()) |
| 117 | return false; |
| 118 | |
| 119 | return true; |
| 120 | } |
| 121 | |
| 122 | bool operator != (const setter_method &x, const setter_method &y) |
| 123 | { |
| 124 | return !(x == y); |
| 125 | } |
| 126 | |
| 127 | bool operator < (const setter_method &x, const setter_method &y) |
| 128 | { |
| 129 | if (x.object_type() < y.object_type()) |
| 130 | return true; |
| 131 | |
| 132 | if (x.object_type() > y.object_type()) |
| 133 | return false; |
| 134 | |
| 135 | if (x.parameter_type() < y.parameter_type()) |
| 136 | return true; |
| 137 | |
| 138 | if (x.parameter_type() > y.parameter_type()) |
| 139 | return false; |
| 140 | |
| 141 | return x.meta_method().methodSignature() < y.meta_method().methodSignature(); |
| 142 | } |
| 143 | |
| 144 | bool operator > (const setter_method &x, const setter_method &y) |
| 145 | { |
| 146 | return y < x; |
| 147 | } |
| 148 | |
| 149 | bool operator <= (const setter_method &x, const setter_method &y) |
| 150 | { |
| 151 | return !(y < x); |
| 152 | } |
| 153 | |
| 154 | bool operator >= (const setter_method &x, const setter_method &y) |
| 155 | { |
| 156 | return !(x < y); |
| 157 | } |
| 158 | |
| 159 | setter_method make_setter_method(const types_by_name &known_types, const QMetaMethod &meta_method) |
| 160 | { |
| 161 | auto parameter_type = meta_method.parameterCount() == 1 |
| 162 | ? type_by_pointer(known_types, meta_method.parameterTypes()[0].data()) |
| 163 | : type{nullptr}; |
| 164 | setter_method::validate_setter_method(parameter_type, meta_method); |
| 165 | |
| 166 | return setter_method{parameter_type, meta_method}; |
| 167 | } |
| 168 | |
| 169 | }} |