| 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 | |
| 25 | #include "types.h" |
| 26 | |
| 27 | /** |
| 28 | * @file |
| 29 | * @brief Contains classes and functions for representing providers of object. |
| 30 | */ |
| 31 | |
| 32 | class QObject; |
| 33 | |
| 34 | namespace injeqt { namespace internal { |
| 35 | |
| 36 | class injector_core; |
| 37 | |
| 38 | /** |
| 39 | * @brief Abstract provider of objects. |
| 40 | * |
| 41 | * Providers are used in injector to create objects of configured types. Objects |
| 42 | * can be created in different ways so this is an abstract class with a few |
| 43 | * mplementations. |
| 44 | * |
| 45 | * Provider can require objects of other types to be already available in injector |
| 46 | * using required_types() and then provide an object with its provide() object. |
| 47 | * Provider may or may no take ownership of a provided object - look for concrete |
| 48 | * types for more information. |
| 49 | * |
| 50 | * Provider return objects that may or may not require dependency resolving. |
| 51 | * It can be checked with require_resolving() method. |
| 52 | */ |
| 53 | class provider |
| 54 | { |
| 55 | |
| 56 | public: |
| 57 | explicit provider() {} |
| 58 | virtual ~provider() {} |
| 59 | |
| 60 | /** |
| 61 | * @return type of object that this provider provides. |
| 62 | */ |
| 63 | virtual const type & provided_type() const = 0; |
| 64 | |
| 65 | /** |
| 66 | * @return provided object |
| 67 | * @param i injector_core that requests object |
| 68 | * @post result != nullptr |
| 69 | * @post implements(type{result->metaObject()}, provided_type()) |
| 70 | * @throw instantiation_failed if instantiation of provided type failed |
| 71 | * |
| 72 | * Provider can require some types with required_types() to be already available |
| 73 | * in injector_core. Provider may or may no take ownership of a provided object - |
| 74 | * look for concrete types for more information. |
| 75 | */ |
| 76 | virtual QObject * provide(injector_core &i) = 0; |
| 77 | |
| 78 | /** |
| 79 | * @return list of types that must be instantiated in injector_core before calling provide(injector_core) method |
| 80 | * |
| 81 | * Return value of this method must be the same for whole lifetime of this object. |
| 82 | */ |
| 83 | virtual types required_types() const = 0; |
| 84 | |
| 85 | /** |
| 86 | * @return true, if provided object require resolving of dependencies |
| 87 | */ |
| 88 | virtual bool require_resolving() const = 0; |
| 89 | |
| 90 | }; |
| 91 | |
| 92 | }} |