| 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 "implementation.h" |
| 22 | |
| 23 | #include <injeqt/exception/empty-type.h> |
| 24 | #include <injeqt/exception/interface-not-implemented.h> |
| 25 | #include <injeqt/exception/invalid-qobject.h> |
| 26 | #include <injeqt/exception/qobject-type.h> |
| 27 | |
| 28 | #include "interfaces-utils.h" |
| 29 | |
| 30 | #include <QtCore/QObject> |
| 31 | #include <cassert> |
| 32 | |
| 33 | namespace injeqt { namespace internal { |
| 34 | |
| 35 | implementation::implementation(type interface_type, QObject *object) : |
| 36 | _interface_type{std::move(interface_type)}, |
| 37 | _object{object} |
| 38 | { |
| 39 | assert(!_interface_type.is_empty()); |
| 40 | assert(!_interface_type.is_qobject()); |
| 41 | assert(_object != nullptr); |
| 42 | assert(_object->metaObject() != nullptr); |
| 43 | assert(extract_interfaces(type{_object->metaObject()}).contains(_interface_type)); |
| 44 | } |
| 45 | |
| 46 | const type & implementation::interface_type() const |
| 47 | { |
| 48 | return _interface_type; |
| 49 | } |
| 50 | |
| 51 | QObject * implementation::object() const |
| 52 | { |
| 53 | return _object; |
| 54 | } |
| 55 | |
| 56 | bool operator == (const implementation &x, const implementation &y) |
| 57 | { |
| 58 | if (x.interface_type() != y.interface_type()) |
| 59 | return false; |
| 60 | |
| 61 | if (x.object() != y.object()) |
| 62 | return false; |
| 63 | |
| 64 | return true; |
| 65 | } |
| 66 | |
| 67 | bool operator != (const implementation &x, const implementation &y) |
| 68 | { |
| 69 | return !(x == y); |
| 70 | } |
| 71 | |
| 72 | bool operator < (const implementation &x, const implementation &y) |
| 73 | { |
| 74 | if (x.interface_type() < y.interface_type()) |
| 75 | return true; |
| 76 | if (x.interface_type() > y.interface_type()) |
| 77 | return false; |
| 78 | |
| 79 | return x.object() < y.object(); |
| 80 | } |
| 81 | |
| 82 | bool operator > (const implementation &x, const implementation &y) |
| 83 | { |
| 84 | return y < x; |
| 85 | } |
| 86 | |
| 87 | bool operator <= (const implementation &x, const implementation &y) |
| 88 | { |
| 89 | return !(y < x); |
| 90 | } |
| 91 | |
| 92 | bool operator >= (const implementation &x, const implementation &y) |
| 93 | { |
| 94 | return !(x < y); |
| 95 | } |
| 96 | |
| 97 | implementation make_implementation(type interface_type, QObject *object) |
| 98 | { |
| 99 | if (interface_type.is_empty()) |
| 100 | throw exception::empty_type{}; |
| 101 | if (interface_type.is_qobject()) |
| 102 | throw exception::qobject_type(); |
| 103 | if (!object || !object->metaObject()) |
| 104 | throw exception::invalid_qobject{}; |
| 105 | if (!implements(type{object->metaObject()}, interface_type)) |
| 106 | throw exception::interface_not_implemented{type{object->metaObject()}.name() + ": " + interface_type.name()}; |
| 107 | |
| 108 | return implementation{interface_type, object}; |
| 109 | } |
| 110 | |
| 111 | }} |