1 /*
2  * %injeqt copyright begin%
3  * Copyright 2015 Rafał Malinowski (rafal.przemyslaw.malinowski@gmail.com)
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 "injector-impl.h"
26 #include "provider.h"
27 
28 /**
29  * @file
30  * @brief Contains classes and functions for representing provider that uses different injector.
31  */
32 
33 namespace injeqt { namespace internal {
34 
35 /**
36  * @brief Provider that returns object from other (parent) injector.
37  *
38  * This provider implementation will return object from different (parent) injector.
39  * It is basically a proxy. It can be used to implement injectors inside plugins that
40  * use main application injector for getting global services. It can be also used to manager
41  * scopes of object - by creating separate subinjectors for each context.
42  */
43 class provider_by_parent_injector final : public provider
44 {
45 
46 public:
47 	/**
48 	 * @param parent_injector injector used to get object
49 	 * @param provided_type type to provide
50 	 * @pre parent_injector != nullptr
51 	 */
52 	explicit provider_by_parent_injector(injector_impl *parent_injector, type provided_type);
~provider_by_parent_injector()53 	virtual ~provider_by_parent_injector() {}
54 
55 	/**
56 	 * @return provided_type value passed to constructor
57 	 */
58 	virtual const type & provided_type() const override;
59 
60 	/**
61 	 * @return object returned by parent_injector
62 	 * @post result != nullptr
63 	 * @post implements(type{result->metaObject()}, provided_type())
64 	 * @throw instantiation_failed if instantiation of provided type failed
65 	 *
66 	 * If object was not yet created the constructor() method is called and object is stored
67 	 * in internal cache. Then object from cache is returned.
68 	 */
69 	virtual QObject * provide(injector_core &i) override;
70 
71 	/**
72 	 * @return empty set of object - this provider does not require another object to instantiate
73 	 */
required_types()74 	virtual types required_types() const override { return types{}; }
75 
76 	/**
77 	 * @return false
78 	 *
79 	 * Parent injector takes care of it.
80 	 */
81 	virtual bool require_resolving() const override;
82 
83 private:
84 	injector_impl *_parent_injector;
85 	type _provided_type;
86 
87 };
88 
89 }}
90