Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
ServiceFinder2.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* ServiceFinder2.h (C) 2000-2025 */
9/* */
10/* Class to find a given service. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_CORE_SERVICEFINDER2_H
13#define ARCANE_CORE_SERVICEFINDER2_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arcane/utils/String.h"
18#include "arcane/utils/Array.h"
19#include "arcane/utils/Collection.h"
20#include "arcane/utils/Enumerator.h"
21
22#include "arcane/core/IServiceInfo.h"
23#include "arcane/core/IFactoryService.h"
24#include "arcane/core/IApplication.h"
25#include "arcane/core/IServiceFactory.h"
26#include "arcane/core/IServiceMng.h"
27#include "arcane/core/ServiceBuildInfo.h"
28#include "arcane/core/ServiceInstance.h"
29
30#include <set>
31
32/*---------------------------------------------------------------------------*/
33/*---------------------------------------------------------------------------*/
34
35namespace Arcane::Internal
36{
37
38/*---------------------------------------------------------------------------*/
39/*---------------------------------------------------------------------------*/
40
41/*!
42 * \internal
43 * \brief Utility class to find one or more services
44 * implementing the \a InterfaceType interface.
45 */
46template <typename InterfaceType>
47class ServiceFinderBase2T
48{
49 public:
50
51 typedef IServiceFactory2T<InterfaceType> FactoryType;
52
53 public:
54
55 ServiceFinderBase2T(IApplication* app, const ServiceBuildInfoBase& sbi)
56 : m_application(app)
57 , m_service_build_info_base(sbi)
58 {
59 }
60
61 virtual ~ServiceFinderBase2T() {}
62
63 public:
64
65 /*!
66 * \brief Creates an instance of the service \a name.
67 *
68 * Returns null if no service with this name exists.
69 *
70 * \deprecated Use createReference() instead.
71 */
72 ARCCORE_DEPRECATED_2019("Use createReference() instead")
73 virtual InterfaceType* create(const String& name)
74 {
75 return _create(name, m_service_build_info_base);
76 }
77
78 /*!
79 * \brief Creates a reference to the service \a name.
80 *
81 * Returns a null reference if no service with this name exists.
82 */
84 {
85 return _createReference(name, m_service_build_info_base);
86 }
87
88 /*!
89 * \brief Creates an instance of the service \a name for the mesh \a mesh.
90 *
91 * This is only valid for subdomain services. For others,
92 * it has no effect.
93 * The caller must destroy these services.
94 * Returns null if no service with this name exists.
95 *
96 * \deprecated Use createReference() instead.
97 */
98 ARCCORE_DEPRECATED_2019("Use createReference() instead")
99 virtual InterfaceType* create(const String& name, IMesh* mesh)
100 {
101 ISubDomain* sd = m_service_build_info_base.subDomain();
102 if (!sd)
103 return {};
104 if (mesh)
105 return _create(name, ServiceBuildInfoBase(sd, mesh));
106 return _create(name, ServiceBuildInfoBase(sd));
107 }
108
109 /*!
110 * \brief Creates a reference to the service \a name for the mesh \a mesh.
111 *
112 * This is only valid for subdomain services. For others,
113 * it has no effect.
114 * The caller must destroy these services.
115 * Returns null if no service with this name exists.
116 */
117 virtual Ref<InterfaceType> createReference(const String& name, IMesh* mesh)
118 {
119 ISubDomain* sd = m_service_build_info_base.subDomain();
120 if (!sd)
121 return {};
122 if (mesh)
123 return _createReference(name, ServiceBuildInfoBase(sd, mesh));
124 return _createReference(name, ServiceBuildInfoBase(sd));
125 }
126
127 /*!
128 * \brief Singleton instance of the service having the \a InterfaceType interface.
129 *
130 * Returns null if no service is found
131 */
132 virtual InterfaceType* getSingleton()
133 {
134 IServiceMng* sm = m_service_build_info_base.serviceParent()->serviceMng();
135 SingletonServiceInstanceCollection singleton_services = sm->singletonServices();
136 for (typename SingletonServiceInstanceCollection::Enumerator i(singleton_services); ++i;) {
137 ISingletonServiceInstance* ssi = (*i).get();
138 if (ssi) {
139 for (typename ServiceInstanceCollection::Enumerator k(ssi->interfaceInstances()); ++k;) {
140 IServiceInstance* sub_isi = (*k).get();
141 auto m = dynamic_cast<IServiceInstanceT<InterfaceType>*>(sub_isi);
142 if (m)
143 return m->instance().get();
144 }
145 }
146 }
147 return nullptr;
148 }
149
150 /*!
151 * \brief Creates an instance of every service that implements \a InterfaceType.
152 *
153 * The caller must destroy these services via the call to 'operator delete'.
154 *
155 * \deprecated Use the overload taking an array of references instead.
156 */
157 ARCCORE_DEPRECATED_2019("Use createAll(Array<ServiceRef<InterfaceType>>&) instead")
158 virtual void createAll(Array<InterfaceType*>& instances)
159 {
160 _createAll(instances, m_service_build_info_base);
161 }
162
163 /*!
164 * \brief Creates an instance of every service that implements \a InterfaceType.
165 */
167 {
168 return _createAll(m_service_build_info_base);
169 }
170
171 public:
172
173 SharedArray<FactoryType*> factories()
174 {
175 SharedArray<FactoryType*> m_factories;
176 for (typename ServiceFactory2Collection::Enumerator j(this->m_application->serviceFactories2()); ++j;) {
177 IServiceFactory2* sf2 = *j;
179 //m_application->traceMng()->info() << " FOUND sf2=" << sf2 << " M=" << m;
180 if (m) {
181 m_factories.add(m);
182 }
183 }
184 return m_factories.constView();
185 }
186
187 void getServicesNames(Array<String>& names) const
188 {
189 for (typename ServiceFactory2Collection::Enumerator j(this->m_application->serviceFactories2()); ++j;) {
190 IServiceFactory2* sf2 = *j;
191 IServiceFactory2T<InterfaceType>* true_factory = dynamic_cast<IServiceFactory2T<InterfaceType>*>(sf2);
192 if (true_factory) {
193 IServiceInfo* si = sf2->serviceInfo();
194 names.add(si->localName());
195 }
196 }
197 }
198
199 protected:
200
201 InterfaceType* _create(const String& name, const ServiceBuildInfoBase& sbib)
202 {
203 return _createReference(name, sbib)._release();
204 }
205
206 Ref<InterfaceType> _createReference(const String& name, const ServiceBuildInfoBase& sbib)
207 {
208 for (typename ServiceFactory2Collection::Enumerator j(this->m_application->serviceFactories2()); ++j;) {
209 Internal::IServiceFactory2* sf2 = *j;
210 IServiceInfo* s = sf2->serviceInfo();
211 if (s->localName() != name)
212 continue;
213 IServiceFactory2T<InterfaceType>* m = dynamic_cast<IServiceFactory2T<InterfaceType>*>(sf2);
214 //m_application->traceMng()->info() << " FOUND sf2=" << sf2 << " M=" << m;
215 if (m) {
216 Ref<InterfaceType> tt = m->createServiceReference(sbib);
217 if (!tt.isNull())
218 return tt;
219 }
220 }
221 return {};
222 }
223
224 void _createAll(Array<InterfaceType*>& instances, const ServiceBuildInfoBase& sbib)
225 {
226 UniqueArray<Ref<InterfaceType>> ref_instances = _createAll(sbib);
227 for (auto& x : ref_instances)
228 instances.add(x._release());
229 }
230
231 UniqueArray<Ref<InterfaceType>> _createAll(const ServiceBuildInfoBase& sbib)
232 {
233 UniqueArray<Ref<InterfaceType>> instances;
234 for (typename ServiceFactory2Collection::Enumerator j(this->m_application->serviceFactories2()); ++j;) {
235 Internal::IServiceFactory2* sf2 = *j;
236 IServiceFactory2T<InterfaceType>* m = dynamic_cast<IServiceFactory2T<InterfaceType>*>(sf2);
237 if (m) {
238 Ref<InterfaceType> tt = m->createServiceReference(sbib);
239 if (tt.get()) {
240 instances.add(tt);
241 }
242 }
243 }
244 return instances;
245 }
246
247 protected:
248
249 IApplication* m_application;
250 ServiceBuildInfoBase m_service_build_info_base;
251};
252
253/*---------------------------------------------------------------------------*/
254/*---------------------------------------------------------------------------*/
255
256} // namespace Arcane::Internal
257
258namespace Arcane
259{
260
261/*---------------------------------------------------------------------------*/
262/*---------------------------------------------------------------------------*/
263
264/*!
265 * \brief Utility class to find one or more services
266 * implementing the \a InterfaceType interface.
267 * \deprecated This class should no longer be used directly.
268 * Use ServiceBuilder instead.
269 */
270template <typename InterfaceType, typename ParentType>
271class ServiceFinder2T
272: public Internal::ServiceFinderBase2T<InterfaceType>
273{
274 public:
275
276 ServiceFinder2T(IApplication* app, ParentType* parent)
278 {
279 }
280
281 ~ServiceFinder2T() {}
282
283 public:
284};
285
286/*---------------------------------------------------------------------------*/
287/*---------------------------------------------------------------------------*/
288
289} // End namespace Arcane
290
291/*---------------------------------------------------------------------------*/
292/*---------------------------------------------------------------------------*/
293
294#endif
Base class for 1D data vectors.
void add(ConstReferenceType val)
Adds element val to the end of the array.
ConstArrayView< T > constView() const
Constant view of this array.
Application interface.
virtual ServiceFactory2Collection serviceFactories2()=0
List of service factories.
Interface of a service instance.
Definition IService.h:69
Service manager interface.
Definition IServiceMng.h:32
virtual SingletonServiceInstanceCollection singletonServices() const =0
Returns the list of singleton services.
virtual ServiceInstanceCollection interfaceInstances()=0
List of instances of interfaces implemented by the singleton.
Interface of the subdomain manager.
Definition ISubDomain.h:75
virtual InterfaceType * create(const String &name)
Creates an instance of the service name.
virtual UniqueArray< Ref< InterfaceType > > createAll()
Creates an instance of every service that implements InterfaceType.
virtual void createAll(Array< InterfaceType * > &instances)
Creates an instance of every service that implements InterfaceType.
virtual InterfaceType * getSingleton()
Singleton instance of the service having the InterfaceType interface.
virtual Ref< InterfaceType > createReference(const String &name, IMesh *mesh)
Creates a reference to the service name for the mesh mesh.
virtual Ref< InterfaceType > createReference(const String &name)
Creates a reference to the service name.
InstanceType * get() const
Associated instance or nullptr if none.
Reference to an instance.
Information for creating a service.
1D vector of data with reference semantics.
1D data vector with value semantics (STL style).
Internal types of Arcane.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Collection< SingletonServiceInstanceRef > SingletonServiceInstanceCollection
Collection of singleton service instances.