12#ifndef ARCANE_CORE_SERVICEFACTORY_H
13#define ARCANE_CORE_SERVICEFACTORY_H
17#include "arcane/utils/NotSupportedException.h"
18#include "arcane/utils/TraceInfo.h"
19#include "arcane/utils/ExternalRef.h"
21#include "arcane/core/IApplication.h"
22#include "arcane/core/ISession.h"
23#include "arcane/core/ISubDomain.h"
24#include "arcane/core/ServiceBuildInfo.h"
26#include "arcane/core/IServiceFactory.h"
27#include "arcane/core/ServiceRegisterer.h"
28#include "arcane/core/ServiceInfo.h"
29#include "arcane/core/IService.h"
31#include "arcane/core/ServiceInstance.h"
45class DotNetServiceInstance
60 Int32 v = std::atomic_fetch_add(&m_nb_ref, -1);
69 return m_service_info;
71 void setDotNetHandle(ExternalRef handle) { m_handle = handle; }
76 std::atomic<Int32> m_nb_ref = 0;
88template <
typename InterfaceType>
104 Int32 v = std::atomic_fetch_add(&m_nb_ref, -1);
117 return m_service_info;
122 std::atomic<Int32> m_nb_ref = 0;
123 Ref<InterfaceType> m_instance;
124 IServiceInfo* m_service_info;
134class ARCANE_CORE_EXPORT ServiceFactoryInfo
141 , m_is_autoload(
false)
142 , m_is_singleton(
false)
144 ~ServiceFactoryInfo()
override {}
153 virtual bool isModule()
const {
return false; }
154 virtual void initializeModuleFactory(
ISubDomain*) {}
155 virtual IModule* createModule(ISubDomain*, IMesh*) {
return nullptr; }
159 void setAutoload(
bool v) { m_is_autoload = v; }
160 void setSingleton(
bool v) { m_is_singleton = v; }
161 void initProperties(
int v)
163 if (v & SFP_Singleton)
165 if (v & SFP_Autoload)
168 void initProperties() {}
172 IServiceInfo* m_service_info;
185template <
typename InterfaceType>
208template <
typename InterfaceType>
209class ServiceFactory2TV2
216 , m_sub_factory(sub_factory)
221 ~ServiceFactory2TV2()
override
223 delete m_sub_factory;
228 return _create(this->createServiceReference(sbi));
235 return _createReference(sbi);
240 return m_service_info;
253 InterfaceType* it = m_sub_factory->createInstance(
ServiceBuildInfo(m_service_info, sbib));
262 ServiceInstanceRef _create(Ref<InterfaceType> it)
264 IServiceInstance* x = (!it) ?
nullptr : new ServiceInstanceT<InterfaceType>(it, m_service_info);
265 return ServiceInstanceRef::createRef(x);
276template <
typename ServiceType,
typename InterfaceType>
284 ServiceType* st =
new ServiceType(sbi);
312class ARCANE_CORE_EXPORT SingletonServiceFactoryBase
350template <
typename ServiceType,
typename... Interfaces>
351class SingletonServiceFactory
352:
public SingletonServiceFactoryBase
371 template <
typename InterfaceType>
void _create()
373 InterfaceType* x = m_service;
377 auto x_ref = Ref<InterfaceType>::_createNoDestroy(x);
380 auto instance_ref = ServiceInstanceRef::createRef(instance);
381 m_adder->addInstance(instance_ref);
384 template <
typename I1,
typename I2,
typename... OtherInterfaces>
389 _create<I2, OtherInterfaces...>();
394 void createInterfaceInstances()
401 ServiceType* m_service;
409 : SingletonServiceFactoryBase(si)
414 ServiceInstanceRef _createInstance(
const ServiceBuildInfoBase& sbib, IServiceInstanceAdder* instance_adder)
override
417 ServiceType* st =
new ServiceType(sbi);
421 ssf.createInterfaceInstances();
422 IServiceInstance* si =
new ServiceInstanceT<ServiceType>(st_ref,
serviceInfo());
423 return ServiceInstanceRef::createRef(si);
435template <
typename InterfaceType>
436class ServiceInterfaceRegisterer
440 typedef InterfaceType Interface;
442 explicit ServiceInterfaceRegisterer(
const char* name)
444 , m_namespace_name(
nullptr)
448 ServiceInterfaceRegisterer(
const char* namespace_name,
const char* name)
450 , m_namespace_name(namespace_name)
457 template <
typename ServiceType>
void
461 if (m_namespace_name)
471 const char* m_namespace_name;
481template <
typename ServiceType>
487 template <
typename InterfaceType>
static void
493 template <
typename I1,
typename I2,
typename... OtherInterfaces>
498 _create<I2, OtherInterfaces...>(si, i2, args...);
504 template <
typename... Interfaces>
static void
538#define ARCANE_SERVICE_INTERFACE(ainterface) \
539 Arcane::Internal::ServiceInterfaceRegisterer<ainterface>(#ainterface)
542#define ARCANE_SERVICE_INTERFACE_NS(ainterface_ns, ainterface) \
543 Arcane::Internal::ServiceInterfaceRegisterer<ainterface_ns ::ainterface>(#ainterface_ns, #ainterface)
578#define ARCANE_REGISTER_SERVICE(aclass, a_service_property, ...) \
581 Arcane::IServiceInfo* \
582 ARCANE_JOIN_WITH_LINE(arcaneCreateServiceInfo##aclass)(const Arcane::ServiceProperty& property) \
584 auto* si = Arcane::Internal::ServiceInfo::create(property, __FILE__, __LINE__); \
585 Arcane::Internal::ServiceAllInterfaceRegisterer<aclass>::registerToServiceInfo(si, __VA_ARGS__); \
589 Arcane::ServiceRegisterer ARCANE_EXPORT ARCANE_JOIN_WITH_LINE(globalServiceRegisterer##aclass)(&ARCANE_JOIN_WITH_LINE(arcaneCreateServiceInfo##aclass), a_service_property)
600#define ARCANE_REGISTER_AXL_SERVICE(aclass, a_service_properties) \
603 Arcane::IServiceInfo* \
604 ARCANE_JOIN_WITH_LINE(arcaneCreateServiceInfo##aclass)(const Arcane::ServiceProperty& properties) \
606 Arcane::ServiceInfo* si = Arcane::ServiceInfo::create(properties, __FILE__, __LINE__); \
607 aclass ::fillServiceInfo<aclass>(si); \
611 Arcane::ServiceRegisterer ARCANE_EXPORT ARCANE_JOIN_WITH_LINE(globalServiceRegisterer##aclass)(&ARCANE_JOIN_WITH_LINE(arcaneCreateServiceInfo##aclass), a_service_properties)
636#define ARCANE_REGISTER_APPLICATION_FACTORY(aclass, ainterface, aname) \
637 ARCANE_REGISTER_SERVICE(aclass, Arcane::ServiceProperty(#aname, Arcane::ST_Application), \
638 ARCANE_SERVICE_INTERFACE(ainterface))
651#define ARCANE_REGISTER_SUB_DOMAIN_FACTORY(aclass, ainterface, aname) \
652 ARCANE_REGISTER_SERVICE(aclass, Arcane::ServiceProperty(#aname, Arcane::ST_SubDomain), \
653 ARCANE_SERVICE_INTERFACE(ainterface))
666#define ARCANE_REGISTER_SUB_DOMAIN_FACTORY4(aclass, ainterface_ns, ainterface, aname) \
667 ARCANE_REGISTER_SERVICE(aclass, Arcane::ServiceProperty(#aname, Arcane::ST_SubDomain), \
668 ARCANE_SERVICE_INTERFACE_NS(ainterface_ns, ainterface))
681#define ARCANE_REGISTER_CASE_OPTIONS_NOAXL_FACTORY(aclass, ainterface, aname) \
682 ARCANE_REGISTER_SERVICE(aclass, Arcane::ServiceProperty(#aname, Arcane::ST_CaseOption), \
683 ARCANE_SERVICE_INTERFACE(ainterface))
696#define ARCANE_REGISTER_CASE_OPTIONS_NOAXL_FACTORY4(aclass, ainterface_ns, ainterface, aname) \
697 ARCANE_REGISTER_SERVICE(aclass, Arcane::ServiceProperty(#aname, Arcane::ST_CaseOption), \
698 ARCANE_SERVICE_INTERFACE_NS(ainterface_ns, ainterface))
This file contains the various types and classes for specifying service properties.
Information about a service factory.
Interface for service or module information.
virtual int usageType() const =0
Indicates where the service can be used.
Typed interface managing a service instance.
Interface of a service instance.
Interface of the subdomain manager.
ExternalRef _internalDotNetHandle() const override
void removeReference() override
Removes a reference.
void addReference() override
Adds a reference.
Management of references to an external object.
Factory for a service implementing the InterfaceType interface.
Interface for a factory function (functor) that creates a service instance corresponding to the Inter...
virtual Ref< InterfaceType > createReference(const ServiceBuildInfo &sbi)=0
Creates an instance of the service.
Class allowing the creation and registration of factories for a service.
static void _create(ServiceInfo *si, const I1 &i1, const I2 &i2, const OtherInterfaces &... args)
Overload for 2 or more interfaces.
static void registerToServiceInfo(ServiceInfo *si, const Interfaces &... args)
Registers the factories for the Interfaces interfaces in the service.
static void _create(ServiceInfo *si, const InterfaceType &i1)
Overload for 1 interface.
IServiceInfo * serviceInfo() const override
Returns the IServiceInfo associated with this factory.
ServiceInstanceRef createServiceInstance(const ServiceBuildInfoBase &sbi) override
Create a service instance from the info in sbi.
IServiceInfo * serviceInfo() const override
Information about the service that can be created by this factory.
bool isSingleton() const override
true if the service is a singleton service (a single instance)
bool isAutoload() const override
true if the service is a module and must be loaded automatically
Information about a service.
void addImplementedInterface(const String &name) override
Adds the name interface name to the interfaces implemented by this service.
void removeReference() override
Removes a reference.
void addReference() override
Adds a reference.
Factory for the ServiceType service for the InterfaceType interface.
Ref< InterfaceType > createReference(const ServiceBuildInfo &sbi) override
Creates an instance of the service.
void registerToServiceInfo(ServiceInfo *si) const
Registers in si a factory to create an instance of the service ServiceType.
Singleton service instances.
Ref< ISingletonServiceInstance > createSingletonServiceInstance(const ServiceBuildInfoBase &sbib) override
Creates a singleton service instance.
IServiceInfo * serviceInfo() const override
Returns the IServiceInfo associated with this factory.
void _create()
Overload for 2 or more interfaces.
void _create()
Overload for 1 interface.
Factory for the singleton service of type ServiceType implementing the Interfaces interfaces.
Reference to an instance.
Information for creating a service.
eServiceType creationType() const
Type of service that can be created by this instance.
Structure containing the information to create a service.
Reference to a service instance.
Unicode character string.
Internal types of Arcane.
auto makeRef(InstanceType *t) -> Ref< InstanceType >
Creates a reference on a pointer.
std::int32_t Int32
Signed integer type of 32 bits.