12#ifndef ARCANE_UTILS_INTERNAL_DEPENDENCYINJECTION_H
13#define ARCANE_UTILS_INTERNAL_DEPENDENCYINJECTION_H
21#include "arcane/utils/Ref.h"
22#include "arcane/utils/ExternalRef.h"
23#include "arcane/utils/GenericRegisterer.h"
28#include "arcane/utils/NotImplementedException.h"
43namespace Arcane::DependencyInjection
48namespace Arcane::DependencyInjection::impl
52class IInstanceFactory;
53template <
typename InterfaceType>
54class IConcreteFactory;
55class ConcreteFactoryTypeInfo;
63namespace Arcane::DependencyInjection
73 virtual bool hasName(
const String& str)
const = 0;
74 virtual bool hasTypeInfo(
const std::type_info&
tinfo)
const = 0;
95 const char* name()
const {
return m_name; }
110namespace Arcane::DependencyInjection::impl
127 template <
typename Type>
static TypeInfo create()
131 const TraceInfo& traceInfo()
const {
return m_trace_info; }
132 const std::type_info& stdTypeInfo()
const {
return m_type_info; }
137 const std::type_info& m_type_info;
154 : m_interface_info(a)
156 , m_constructor_info(
c)
161 template <
typename InterfaceType,
typename ConcreteType,
typename ConstructorType>
165 TypeInfo::create<ConcreteType>(),
166 TypeInfo::create<ConstructorType>());
168 const TypeInfo& interfaceTypeInfo()
const {
return m_interface_info; }
169 const TypeInfo& concreteTypeInfo()
const {
return m_concrete_info; }
170 const TypeInfo& constructorTypeInfo()
const {
return m_constructor_info; }
185template <
typename InterfaceType>
202template <
typename InterfaceType>
220 bool hasName(
const String& str)
const override {
return m_name == str; }
235template <
typename Type>
241 virtual Type instance()
const = 0;
250template <
typename Type>
267 Type instance()
const override {
return m_instance; }
268 bool hasName(
const String& str)
const override {
return m_name == str; }
317 void reset() { m_instance.reset(); }
343 virtual Int32 nbConstructorArg()
const = 0;
369 bool hasName(
const String& str)
const;
374 std::shared_ptr<FactoryInfoImpl> m_p;
395template <
typename InterfaceType>
406 : m_factory_info_impl(
si)
413 delete m_sub_factory;
418 return _create(_createReference(
injector), name);
428 return m_factory_info_impl;
433 return m_sub_factory->concreteFactoryInfo();
436 Int32 nbConstructorArg()
const override
438 return m_sub_factory->nbConstructorArg();
450 return m_sub_factory->createReference(
injector);
456 return InjectedInstanceRef::createRef(x);
472 virtual Int32 nbConstructorArg()
const = 0;
482template <
typename InterfaceType>
503 static BaseClass::Info m_global_registerer_info;
509 return m_global_registerer_info;
527 FactoryCreateFunc infoCreatorWithPropertyFunction() {
return m_factory_create_func; }
530 const char*
name() {
return m_name; }
536 FactoryCreateFunc m_factory_create_func =
nullptr;
537 const char* m_name =
nullptr;
548namespace Arcane::DependencyInjection
561 template <
class Type>
572 return i._getValue<
Type>(name);
577 template <
class Po
interType>
592 return i._getRef<PointerType>(name);
662 template <
typename Type>
void
668 template <
typename Type>
Type
671 return InjectorHelper<Type>::get(*
this, name);
693 auto*
t =
dynamic_cast<FactoryType*
>(v);
713 return dynamic_cast<FactoryType*
>(v) !=
nullptr;
718 String printFactories()
const;
720 void fillWithGlobalFactories();
732 IInstanceVisitorFunctor*
lambda);
733 Integer _nbValue()
const;
744 void _iterateFactories(
const String&
factory_name, IFactoryVisitorFunctor* functor)
const;
745 Integer _nbFactory()
const;
758 InstanceVisitorFunctor
ff(f);
761 return t->instance();
766 template <
typename Type>
Type
767 _getValue(
const String& instance_name)
769 using InjectedType = impl::IInjectedValueInstance<Type>;
770 InjectedType* t =
nullptr;
771 auto f = [&](IInjectedInstance* v) ->
bool {
772 t =
dynamic_cast<InjectedType*
>(v);
775 InstanceVisitorFunctor ff(f);
776 _iterateInstances(
typeid(
Type), instance_name, &ff);
778 return t->instance();
779 _doError(A_FUNCINFO,
"Can not find value for type");
781 [[noreturn]]
void _printValidImplementationAndThrow(
const TraceInfo& ti,
782 const String& implementation_name,
783 FactoryFilterFunc filter_func);
784 [[noreturn]]
void _doError(
const TraceInfo& ti,
const String& message);
792namespace Arcane::DependencyInjection::impl
811template <
typename... Args>
817 using ArgsType = std::tuple<Args...>;
822 template <std::
size_t I>
823 static auto _get(
Injector& i) -> std::tuple_element_t<I, ArgsType>
833 constexpr int tuple_size = std::tuple_size<ArgsType>();
834 static_assert(
tuple_size < 3,
"Too many arguments for createTuple (max=2)");
845 _doError1(
"Too many arguments for createTuple n={0} max=2",
tuple_size);
856template <
typename InterfaceType,
typename ConcreteType,
typename ConstructorType>
860 using Args =
typename ConstructorType::ArgsType;
872 return ConcreteFactoryTypeInfo::create<InterfaceType, ConcreteType, ConstructorType>();
874 Int32 nbConstructorArg()
const override
876 return std::tuple_size<Args>();
911 template <
typename ConcreteType,
typename ConstructorType>
void
927 if constexpr (
sizeof...(OtherInterfaces) > 0)
940template <
typename ConcreteType,
typename InterfaceList>
960 template <
typename ConstructorType>
void
984#define ARCANE_DI_CONSTRUCTOR(...) \
985 ::Arcane::DependencyInjection::impl::ConstructorRegisterer<__VA_ARGS__>()
987#define ARCANE_DI_EMPTY_CONSTRUCTOR(...) \
988 ::Arcane::DependencyInjection::impl::ConstructorRegisterer<>()
992#define ARCANE_DI_INTERFACES(...) \
993 ::Arcane::DependencyInjection::impl::InterfaceListRegisterer<__VA_ARGS__>
995#define ARCANE_DI_REGISTER_PROVIDER(t_class, t_provider_property, t_interfaces, ...) \
998 Arcane::DependencyInjection::impl::FactoryInfo \
999 ARCANE_JOIN_WITH_LINE(arcaneCreateDependencyInjectionProviderInfo##t_class)(const Arcane::DependencyInjection::ProviderProperty& property) \
1001 auto si = Arcane::DependencyInjection::impl::FactoryInfo::create(property, __FILE__, __LINE__); \
1002 Arcane::DependencyInjection::impl::InjectionRegisterer<t_class, t_interfaces> injection_registerer; \
1003 injection_registerer.registerProviderInfo(si, __VA_ARGS__); \
1007 Arcane::DependencyInjection::impl::GlobalRegisterer ARCANE_EXPORT ARCANE_JOIN_WITH_LINE(globalServiceRegisterer##aclass)(&ARCANE_JOIN_WITH_LINE(arcaneCreateDependencyInjectionProviderInfo##t_class), t_provider_property)
#define ARCANE_THROW(exception_class,...)
Macro pour envoyer une exception avec formattage.
#define ARCCORE_DECLARE_REFERENCE_COUNTED_INCLASS_METHODS()
Macro pour déclarer les méthodes virtuelles gérant les compteurs de référence.
#define ARCCORE_DECLARE_REFERENCE_COUNTED_CLASS(class_name)
Macro pour déclarer qu'une classe utilise un compteur de référence.
#define ARCCORE_DEFINE_REFERENCE_COUNTED_INCLASS_METHODS()
Macro pour définir les méthodes gérant les compteurs de référence.
Interface d'un fonctor pour appliqué à chaque fabrique.
Interface d'un fonctor pour appliqué à chaque fabrique.
Ref< InterfaceType > createInstance(const String &implementation_name, bool allow_null=false)
Créé une instance implémentant une interface.
Informations sur les types d'une fabrique.
Fabrique pour le type ConcreteType pour l'interface InterfaceType via le constructeur ConstructorType...
ConcreteType * _create(const Args &&tuple_args)
Ref< InterfaceType > createReference(Injector &injector) override
Créé une instance du service .
Classe permettant d'enregistrer un constructeur pour créer un objet via un Injector....
Informations pour une fabrique.
const char * name()
Nom du service.
Interface d'un fonctor de création d'une instance de service correspondant à l'interface InterfaceTyp...
virtual Ref< InterfaceType > createReference(Injector &)=0
Créé une instance du service .
Interface typée gérant l'instance d'un service.
Interface typée gérant une instance.
Fabrique pour une instance encapsulée par une référence (i.e Ref<T>).
Référence sur une instance injectée.
Interface typée gérant l'instance d'un service.
Interface typée gérant l'instance d'un service.
Classe permettant d'enregistrer des constructeurs pour créer un type ConcreteType implémentant les in...
void registerProviderInfo(FactoryInfo &si, const Constructors &... args)
Enregistre dans si les fabriques correspondentes aux constructeurs Constructors.
void _create(FactoryInfo &si, const C1 &c1, const C2 &c2, const OtherConstructors &... args)
Surcharge pour 2 constructeurs ou plus.
void _create(FactoryInfo &si, const ConstructorType &)
Surcharge pour 1 constructeur.
Classe permettant d'enregistrer la liste des interfaces d'un service. Interfaces contient la liste de...
void registerFactory(FactoryInfo &si)
Enregistre une fabrique.
Classe template pour gérer une liste globale permettant d'enregistrer des fabriques.
Lecteur des fichiers de maillage via la bibliothèque LIMA.
Gestion des références sur un objet externe.
Exception lorsqu'une fonction n'est pas implémentée.
Référence à une instance.
Implémentation thread-safe d'un compteur de référence.
Chaîne de caractères unicode.