| 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 <injeqt/injeqt.h> |
| 24 | #include <injeqt/type.h> |
| 25 | |
| 26 | #include "internal.h" |
| 27 | |
| 28 | /** |
| 29 | * @file |
| 30 | * @brief Contains classes and functions for defining implementation of type. |
| 31 | */ |
| 32 | |
| 33 | class QObject; |
| 34 | |
| 35 | namespace injeqt { namespace internal { |
| 36 | |
| 37 | /** |
| 38 | * @brief Connects type with object that implements it. |
| 39 | * |
| 40 | * This class is used to connect type with object that implements that type. |
| 41 | * |
| 42 | * This class is mostly used in internal code to store objects in cache and to pass |
| 43 | * them as resolved dependencies. The only place where this class is created from outside |
| 44 | * data is module::add_ready_object<T>(QObject *) where a validation is used to ensure |
| 45 | * that user passed valid data. |
| 46 | */ |
| 47 | class INJEQT_INTERNAL_API implementation final |
| 48 | { |
| 49 | |
| 50 | public: |
| 51 | /** |
| 52 | * @brief Create new instance. |
| 53 | * @param interface_type type that object implements |
| 54 | * @param object object that should implement interface_type |
| 55 | * @pre !interface_type.is_empty() |
| 56 | * @pre !interface_type.is_qobject() |
| 57 | * @pre object != nullptr |
| 58 | * @pre object->metaObject() != nullptr |
| 59 | * @pre extract_interfaces(type{object->metaObject()}).contains(interface_type) |
| 60 | */ |
| 61 | explicit implementation(type interface_type, QObject *object); |
| 62 | |
| 63 | const type & interface_type() const; |
| 64 | QObject * object() const; |
| 65 | |
| 66 | private: |
| 67 | type _interface_type; |
| 68 | QObject *_object; |
| 69 | |
| 70 | }; |
| 71 | |
| 72 | INJEQT_INTERNAL_API bool operator == (const implementation &x, const implementation &y); |
| 73 | INJEQT_INTERNAL_API bool operator != (const implementation &x, const implementation &y); |
| 74 | INJEQT_INTERNAL_API bool operator < (const implementation &x, const implementation &y); |
| 75 | INJEQT_INTERNAL_API bool operator > (const implementation &x, const implementation &y); |
| 76 | INJEQT_INTERNAL_API bool operator <= (const implementation &x, const implementation &y); |
| 77 | INJEQT_INTERNAL_API bool operator >= (const implementation &x, const implementation &y); |
| 78 | |
| 79 | /** |
| 80 | * @brief Check if @p interface_type and @p object met implementation preconditions and return new instance if true |
| 81 | * @param interface_type type that object implements |
| 82 | * @param object object that should implement interface_type |
| 83 | * @throw empty_type when passed type @p interface_type is an empty type |
| 84 | * @throw qobject_type when passed type @p interface_type represents QObject |
| 85 | * @throw invalid_qobject when passed @p object is nullptr or does not have valid QMetaObject |
| 86 | * @throw interface_not_implemented when passed @p object does not implements type @p interface_type |
| 87 | */ |
| 88 | INJEQT_INTERNAL_API implementation make_implementation(type interface_type, QObject *object); |
| 89 | |
| 90 | }} |