Debian Codegraph @

src/internal/injector-core.h

raw symbols SCIP
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>cxx . . $ `<file>/include/injeqt/injeqt.h`/
24 #include <injeqt/type.h>cxx . . $ `<file>/include/injeqt/type.h`/
25
26 #include "implementations.h"cxx . . $ `<file>/src/internal/implementations.h`/
27 #include "providers.h"cxx . . $ `<file>/src/internal/providers.h`/
28 #include "types-by-name.h"cxx . . $ `<file>/src/internal/types-by-name.h`/
29 #include "types-model.h"cxx . . $ `<file>/src/internal/types-model.h`/
30
31 #include <vector>
32 #include <QtCore/QObject>
33
34 /**
35 * @file
36 * @brief Contains classes and functions for implementation of injector core.
37 */
38
39 namespace injeqtcxx . . $ injeqt/ { namespace internalcxx . . $ injeqt/internal/ {
40
41 class provided_objectcxx . . $ injeqt/internal/provided_object#;
42
43 /**
44 * @brief Implementation of injector class.
45 * @see injector
46 * @see injector_impl
47 *
48 * This class is core of Injeqt library.
49 *
50 * Its main purpose is to gather configuration from set of providers and construct objects with
51 * resolved dependencies.
52 *
53 * Injector keeps list of all configured providers and of all already created objects.
54 * Two methods objects_with(implementations, const type &) and objects_with(implementations, const types &)
55 * are used to update list of already created objects with new ones.
56 */
57 class INJEQT_APIcxx . . $ `include/injeqt/injeqt.h:28:9`! injector_corecxx . . $ injeqt/internal/injector_core# final
58 {
59
60 public:
61 /**
62 * @brief Create empty injector_core with no configuration.
63 *
64 * Injector with empty configuration will not be able to return any objects. Each call
65 * to get(const type &) will result in type_not_configured
66 */
67 injector_corecxx . . $ injeqt/internal/injector_core#injector_core(49f6e7a06ebc5aa8).();
68
69 /**
70 * @brief Create injector configured with set of providers.
71 * @param all_providers set of all providers available to injector
72 * @see injector::injector(std::vector<std::unique_ptr<module>>)
73 * @throw ambiguous_types if one or more types in @p providers is ambiguous
74 * @throw unresolvable_dependencies if a type with unresolvable dependency is found in @p providers
75 * @throw dependency_on_self when type depends on self
76 * @throw dependency_on_subtype when type depends on own supertype
77 * @throw dependency_on_subtype when type depends on own subtype
78 * @throw invalid_setter if any tagged setter has parameter that is not a QObject-derived pointer
79 * @throw invalid_setter if any tagged setter has parameter that is a QObject pointer
80 * @throw invalid_setter if any tagged setter has parameter that is a QObject-derived pointer of not configured type
81 * @throw invalid_setter if any tagged setter has other number of parameters than one
82 *
83 * This constructor creates types_model object to get all required information from providers. This object
84 * takes ownership of passed providers.
85 */
86 explicit injector_corecxx . . $ injeqt/internal/injector_core#injector_core(a8e0597472603795).(types_by_namecxx . . $ injeqt/internal/types_by_name# known_types, stdcxx . . $ std/::vectorcxx . . $ std/vector#<stdcxx . . $ std/::unique_ptrcxx . . $ std/unique_ptr#<providercxx . . $ injeqt/internal/provider#>> &&all_providers);
87
88 injector_corecxx . . $ injeqt/internal/injector_core#injector_core(4591ce5a287c83c8).(const injector_corecxx . . $ injeqt/internal/injector_core# &) = delete;
89 injector_corecxx . . $ injeqt/internal/injector_core#injector_core(528a6fa5a342ea28).(injector_corecxx . . $ injeqt/internal/injector_core# &&) = default;
90
91 injector_corecxx . . $ injeqt/internal/injector_core# & operatorcxx . . $ injeqt/internal/injector_core#`operator=`(8c43630f214c4ba0). = (const injector_corecxx . . $ injeqt/internal/injector_core# &) = delete;
92 injector_corecxx . . $ injeqt/internal/injector_core# & operatorcxx . . $ injeqt/internal/injector_core#`operator=`(b162674904b101d8). = (injector_corecxx . . $ injeqt/internal/injector_core# &&) = default;
93
94 /**
95 * @brief Destroy injector_core.
96 *
97 * TODO: add note about INJEQT_DONE
98 */
99 ~cxx . . $ injeqt/internal/injector_core#`~injector_core`(49f6e7a06ebc5aa8).injector_corecxx . . $ injeqt/internal/injector_core#();
100
101 /**
102 * @brief Returns list of all configured types.
103 *
104 * Result contians only types explicitely configured in modules, not supertypes of these. It also includes
105 * results of provided_types() calls from all added parent injectors.
106 */
107 stdcxx . . $ std/::vectorcxx . . $ std/vector#<typecxx . . $ injeqt/v1/type#> provided_typescxx . . $ injeqt/internal/injector_core#provided_types(cf3a392ed5d6b92b).() const;
108
109 /**
110 * @brief Instantiates object of given type @p interface_type
111 * @param interface_type type of object to instantiate.
112 * @throw unknown_type if @p interface_type was not configured in injector
113 * @throw instantiation_failed if instantiation of one of required types failed
114 * @pre !interface_type.is_empty()
115 * @pre !interface_type.is_qobject()
116 * @see injector::get<T>()
117 */
118 void instantiatecxx . . $ injeqt/internal/injector_core#instantiate(ebaf86d70144d0a8).(const typecxx . . $ injeqt/v1/type# &interface_type);
119
120 /**
121 * @brief Instantiate all objects with given @p type_role.
122 * @throw instantiation_failed if instantiation of one of found types failed
123 */
124 void instantiate_all_with_type_rolecxx . . $ injeqt/internal/injector_core#instantiate_all_with_type_role(9c546c94e5fcce4e).(const stdcxx . . $ std/::stringcxx . . $ std/string# &type_role);
125
126 /**
127 * @brief Returns pointer to object of given type @p interface_type
128 * @param interface_type type of object to return.
129 * @throw unknown_type if @p interface_type was not configured in injector
130 * @throw instantiation_failed if instantiation of one of required types failed
131 * @pre !interface_type.is_empty()
132 * @pre !interface_type.is_qobject()
133 * @see injector::get<T>()
134 */
135 QObjectcxx . . $ QObject# * getcxx . . $ injeqt/internal/injector_core#get(e1ed43d1af855660).(const typecxx . . $ injeqt/v1/type# &interface_type);
136
137 /**
138 * @brief Returns all objects with given @p type_role.
139 * @throw instantiation_failed if instantiation of one of found types failed
140 */
141 stdcxx . . $ std/::vectorcxx . . $ std/vector#<QObjectcxx . . $ QObject# *> get_all_with_type_rolecxx . . $ injeqt/internal/injector_core#get_all_with_type_role(60702980c580d8f6).(const stdcxx . . $ std/::stringcxx . . $ std/string# &type_role);
142
143 /**
144 * @brief Inject dependencies into @p object.
145 * @param object object to inject dependencies into.
146 * @throw invalid_setter if any tagged setter has parameter that is not a QObject-derived pointer
147 * @throw invalid_setter if any tagged setter has parameter that is a QObject pointer
148 * @throw invalid_setter if any tagged setter has parameter that is a QObject-derived pointer of not configured type
149 * @throw invalid_setter if any tagged setter has other number of parameters than one
150 * @pre object != nullptr
151 *
152 * This method looks for invokable setters tagged with INJEQT_SET in @p object. If any of setter is not valid
153 * dependency injector setter or its parameter is of type not configured in injector an exception is thrown.
154 * If all setters are valid, they are called with proper objects (which may be already available in injector
155 * or created on demand).
156 */
157 void inject_intocxx . . $ injeqt/internal/injector_core#inject_into(749de1b6ae65bddd).(QObjectcxx . . $ QObject# *object);
158
159 private:
160 types_by_namecxx . . $ injeqt/internal/types_by_name# _known_typescxx . . $ injeqt/internal/injector_core#_known_types.;
161 providerscxx . . $ injeqt/internal/providers# _available_providerscxx . . $ injeqt/internal/injector_core#_available_providers.;
162 implementationscxx . . $ injeqt/internal/implementations# _objectscxx . . $ injeqt/internal/injector_core#_objects.;
163 implementationscxx . . $ injeqt/internal/implementations# _resolved_objectscxx . . $ injeqt/internal/injector_core#_resolved_objects.;
164 types_modelcxx . . $ injeqt/internal/types_model# _types_modelcxx . . $ injeqt/internal/injector_core#_types_model.;
165
166 /**
167 * @brief Extract all provided types and makes a types_model from them.
168 * @throw ambiguous_types if one or more types in @p all_providers is ambiguous
169 * @throw unresolvable_dependencies if a type with unresolvable dependency is found in @p all_providers
170 * @throw dependency_on_self when type depends on self
171 * @throw dependency_on_subtype when type depends on own supertype
172 * @throw dependency_on_subtype when type depends on own subtype
173 * @throw invalid_setter if any tagged setter has parameter that is not a QObject-derived pointer
174 * @throw invalid_setter if any tagged setter has parameter that is a QObject pointer
175 * @throw invalid_setter if any tagged setter has other number of parameters than one
176 */
177 types_modelcxx . . $ injeqt/internal/types_model# create_types_modelcxx . . $ injeqt/internal/injector_core#create_types_model(5250c6e58d6688a7).() const;
178
179 /**
180 * @brief Return type that implements @p interface_type.
181 * @throw unknown_type if @p interface_type does not have corresponding implementation
182 */
183 typecxx . . $ injeqt/v1/type# implementation_forcxx . . $ injeqt/internal/injector_core#implementation_for(6d6526d4c0efaff3).(const typecxx . . $ injeqt/v1/type# &interface_type) const;
184
185 /**
186 * @brief Instantiate class of interface type @p interface_type and makes it available for use.
187 * @param interface_type type of interface of object to create
188 * @throw instantiation_failed if instantiation of one of required types failed
189 * @throw unknown_type if @p interface_type does not have corresponding implementation
190 *
191 * Instantiate class of interface type @p interface_type with all of its dependencies, then resolves them and
192 * calls INJEQT_INIT slots.
193 */
194 void instantiate_interfacecxx . . $ injeqt/internal/injector_core#instantiate_interface(ebaf86d70144d0a8).(const typecxx . . $ injeqt/v1/type# &interface_type);
195
196 /**
197 * @brief Instantiate class of type @p implementation_type and makes it available for use.
198 * @param interface_type type of interface of object to create
199 * @throw instantiation_failed if instantiation of one of required types failed
200 *
201 * Instantiate class of exact type @p implementation_type with all of its dependencies, then resolves them and
202 * calls INJEQT_INIT slots.
203 */
204 void instantiate_implementationcxx . . $ injeqt/internal/injector_core#instantiate_implementation(ebaf86d70144d0a8).(const typecxx . . $ injeqt/v1/type# &implementation_type);
205
206 /**
207 * @brief Return all dependencies for @p implementation_type.
208 */
209 dependenciescxx . . $ injeqt/internal/dependencies# implementation_type_dependenciescxx . . $ injeqt/internal/injector_core#implementation_type_dependencies(7e15a51b9934704e).(const typecxx . . $ injeqt/v1/type# &implementation_type) const;
210
211 /**
212 * @brief Instantiate classes of interface types from @p interface_types and makes them available for use.
213 * @param interface_types types of interfaces of objects to create
214 * @throw instantiation_failed if instantiation of one of required types failed
215 * @pre No class from @p interface_types contains dependency that is not already resolved or not in @p interface_types
216 *
217 * Instantiate all classes of interface type @p interface_types without looking for dependencies.
218 */
219 void instantiate_allcxx . . $ injeqt/internal/injector_core#instantiate_all(3e77e1d0e0e5e776).(const typescxx . . $ injeqt/internal/types# &interface_types);
220
221 /**
222 * @brief Instantiate classes that are required before instantiating any of @p types_to_instantiate.
223 * @param types_to_instantiate types that are checked for list of required types
224 * @throw instantiation_failed if instantiation of one of required types failed
225 *
226 * Instantiate all classes that are returned from @see provider::required_type() methods of any provider
227 * for these types.
228 */
229 void instantiate_required_types_forcxx . . $ injeqt/internal/injector_core#instantiate_required_types_for(3e77e1d0e0e5e776).(const typescxx . . $ injeqt/internal/types# &types_to_instantiate);
230
231 /**
232 * @brief Return list of providers required to instantiate types from @p for_types.
233 */
234 template<typename T>
235 stdcxx . . $ std/::vectorcxx . . $ std/vector#<providercxx . . $ injeqt/internal/provider# *> providers_forcxx . . $ injeqt/internal/injector_core#providers_for(61414923878c0a89).(const T &for_types) const
236 {
237 auto result = stdcxx . . $ std/vector#vector(b1ea180184b47f03).::vectorcxx . . $ std/vector#<providercxx . . $ injeqt/internal/provider# *>{};
238 result.reservecxx . . $ std/vector#reserve(2c44cd906ae4e829).(for_types.size());
239 for (auto &&for_type : for_types)
240 {
241 auto provider_it = _available_providerscxx . . $ injeqt/internal/injector_core#_available_providers..getcxx . . $ injeqt/internal/sorted_unique_vector#get(f1b725e3c61eba).(for_type);
242 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(provider_it != end(_available_providers));
243
244 result.push_backcxx . . $ std/vector#push_back(fe7a8a4d1eddb70).(provider_it->get());
245 }
246
247 return resultcxx . . $ std/vector#vector(b1ea180184b47f03).;
248 }
249
250 /**
251 * @brief Filter list of types from @p to_filter to include only non already instantiated types.
252 */
253 stdcxx . . $ std/::vectorcxx . . $ std/vector#<typecxx . . $ injeqt/v1/type#> non_instantiatedcxx . . $ injeqt/internal/injector_core#non_instantiated(d141fac86e8abaf0).(const typescxx . . $ injeqt/internal/types# &to_filter) const;
254
255 /**
256 * @brief Instantiate types with @p providers.
257 */
258 stdcxx . . $ std/::vectorcxx . . $ std/vector#<provided_objectcxx . . $ injeqt/internal/provided_object#> provide_objectscxx . . $ injeqt/internal/injector_core#provide_objects(393f2e4f0f16d2ec).(const stdcxx . . $ std/::vectorcxx . . $ std/vector#<providercxx . . $ injeqt/internal/provider# *> &providers);
259
260 /**
261 * @brief Return objects that needs resolving from @p provided_objects.
262 * @see provider::require_resolving()
263 */
264 stdcxx . . $ std/::vectorcxx . . $ std/vector#<implementationcxx . . $ injeqt/internal/implementation#> objects_to_resolvecxx . . $ injeqt/internal/injector_core#objects_to_resolve(77b7dc284b45368d).(const stdcxx . . $ std/::vectorcxx . . $ std/vector#<provided_objectcxx . . $ injeqt/internal/provided_object#> &provided_objects) const;
265
266 /**
267 * @brief Extract implementations from vector of @p provided_object.
268 */
269 stdcxx . . $ std/::vectorcxx . . $ std/vector#<implementationcxx . . $ injeqt/internal/implementation#> extract_implementationscxx . . $ injeqt/internal/injector_core#extract_implementations(77b7dc284b45368d).(const stdcxx . . $ std/::vectorcxx . . $ std/vector#<provided_objectcxx . . $ injeqt/internal/provided_object#> &provided_objects) const;
270
271 /**
272 * @brief Return objects to store in list of instantiated objects.
273 *
274 * Each implementation object is returned with set of @see implementation instances that contains all unique inferfaces
275 * for that object, so it is later avaialble under all types it implements.
276 */
277 stdcxx . . $ std/::vectorcxx . . $ std/vector#<implementationcxx . . $ injeqt/internal/implementation#> objects_to_storecxx . . $ injeqt/internal/injector_core#objects_to_store(3f4d54e2b105ac4d).(const stdcxx . . $ std/::vectorcxx . . $ std/vector#<implementationcxx . . $ injeqt/internal/implementation#> &objects) const;
278
279 /**
280 * @brief Resolve all @p objects dependencies, call all INJEQT_INIT slots and add types to list of resolved objects.
281 *
282 * This method assumes that all object dependencies are already instantiated.
283 */
284 void resolve_objectscxx . . $ injeqt/internal/injector_core#resolve_objects(f00003a32284dffb).(const stdcxx . . $ std/::vectorcxx . . $ std/vector#<implementationcxx . . $ injeqt/internal/implementation#> &objects);
285
286 /**
287 * @brief Resolve all @p object dependencies.
288 *
289 * This method assumes that all object dependencies are already instantiated.
290 */
291 void resolve_objectcxx . . $ injeqt/internal/injector_core#resolve_object(2f2bf958926a1747).(const implementationcxx . . $ injeqt/internal/implementation# &object) const;
292
293 /**
294 * @brief Resolve all @p object dependencies with @p object_dependencies.
295 */
296 void resolve_objectcxx . . $ injeqt/internal/injector_core#resolve_object(5db74c9facaddd12).(const dependenciescxx . . $ injeqt/internal/dependencies# &object_dependencies, const implementationcxx . . $ injeqt/internal/implementation# &object) const;
297
298 /**
299 * @brief Call all INJEQT_INIT methods on given object in proper order.
300 */
301 void call_init_methodscxx . . $ injeqt/internal/injector_core#call_init_methods(5facab8496d0a3e3).(QObjectcxx . . $ QObject# *object) const;
302
303 /**
304 * @brief Call all INJEQT_DONE methods on given object in proper order.
305 */
306 void call_done_methodscxx . . $ injeqt/internal/injector_core#call_done_methods(5facab8496d0a3e3).(QObjectcxx . . $ QObject# *object) const;
307
308 };
309
310 }}