| 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 "factory-method.h" |
| 26 | #include "internal.h" |
| 27 | #include "provider.h" |
| 28 | |
| 29 | /** |
| 30 | * @file |
| 31 | * @brief Contains classes and functions for representing provider working on factory method. |
| 32 | */ |
| 33 | |
| 34 | namespace injeqt { namespace internal { |
| 35 | |
| 36 | /** |
| 37 | * @brief Provider that returns object created by factory. |
| 38 | * |
| 39 | * This provider implementation will return object using factory method. Its provided_type() returns |
| 40 | * type of factory method return type and its required_types() returns type of object of factory method. |
| 41 | * On first call of provide(injector_core &) it will ask injector_core for object of type |
| 42 | * factory_method::object_method() to be able to invoke this method on that object. |
| 43 | * |
| 44 | * Once created, object will be stored inside and return on subsequents calls to provide(injector_core &). |
| 45 | * This provider has ownershipd over created object and will destroy it at own destruction. |
| 46 | */ |
| 47 | class INJEQT_INTERNAL_API provider_by_factory final : public provider |
| 48 | { |
| 49 | |
| 50 | public: |
| 51 | /** |
| 52 | * @brief Create provider instance with factory method to call. |
| 53 | * @param factory factory method used to create object |
| 54 | */ |
| 55 | explicit provider_by_factory(factory_method factory); |
| 56 | virtual ~provider_by_factory(); |
| 57 | |
| 58 | provider_by_factory(provider_by_factory &&x) = delete; |
| 59 | provider_by_factory & operator = (provider_by_factory &&x) = delete; |
| 60 | |
| 61 | /** |
| 62 | * @return factory_method::retrun_type() of object passed to construtor |
| 63 | */ |
| 64 | virtual const type & provided_type() const override; |
| 65 | |
| 66 | /** |
| 67 | * @return object created by factory method |
| 68 | * @post result != nullptr |
| 69 | * @post implements(type{result->metaObject()}, provided_type()) |
| 70 | * @throw instantiation_failed if instantiation of provided type failed |
| 71 | * |
| 72 | * If object was not yet created the object of type factory_method::object_type() is requested |
| 73 | * from @p i and the factory method is called on it to get object and stoe it in internal cache, |
| 74 | * Then object from cache is returned. |
| 75 | */ |
| 76 | virtual QObject * provide(injector_core &i) override; |
| 77 | |
| 78 | /** |
| 79 | * @return factory_method::object_type() from object passed to constructor |
| 80 | * |
| 81 | * Factory object must be present before factory method can be invoked. |
| 82 | */ |
| 83 | virtual types required_types() const override; |
| 84 | |
| 85 | /** |
| 86 | * @return false |
| 87 | * |
| 88 | * Objects creaded by factory should be ready to use and do not require dependency resolving. |
| 89 | */ |
| 90 | virtual bool require_resolving() const override; |
| 91 | |
| 92 | /** |
| 93 | * @return factory method object passed in constructor |
| 94 | */ |
| 95 | const factory_method & factory() const; |
| 96 | |
| 97 | private: |
| 98 | factory_method _factory; |
| 99 | std::unique_ptr<QObject> _object; |
| 100 | |
| 101 | }; |
| 102 | |
| 103 | }} |