14#include "arcane/utils/DependencyInjection.h"
16#include "arcane/utils/UniqueArray.h"
17#include "arcane/utils/ExternalRef.h"
18#include "arcane/utils/FatalErrorException.h"
23namespace Arcane::DependencyInjection
36 : m_instance(instance)
41 IInjectedInstance* m_instance =
nullptr;
48 for (Integer i = 0, n = m_instance_list.size(); i < n; ++i)
49 delete m_instance_list[i].m_instance;
50 m_instance_list.clear();
54 void addInstance(IInjectedInstance* instance)
56 Int32 index = m_instance_list.size();
57 m_instance_list.add(InstanceInfo{ instance, index });
59 IInjectedInstance* instance(Int32 index)
const {
return m_instance_list[index].m_instance; }
60 Int32 nbInstance()
const {
return m_instance_list.size(); }
63 UniqueArray<InstanceInfo> m_instance_list;
66 UniqueArray<Ref<impl::IInstanceFactory>> m_factories;
91_add(IInjectedInstance* instance)
93 m_p->addInstance(instance);
102 return m_p->nbInstance();
108IInjectedInstance* Injector::
109_value(Integer i)
const
111 return m_p->instance(i);
120 return m_p->m_factories.size();
126impl::IInstanceFactory* Injector::
127_factory(Integer i)
const
129 return m_p->m_factories[i].get();
140namespace Arcane::DependencyInjection::impl
146void ConstructorRegistererBase::
147_doError1(
const String& message,
int nb_value)
158 Impl(
const ProviderProperty& property)
159 : m_property(property)
160 , m_name(property.name())
165 const ProviderProperty m_property;
174FactoryInfo(
const ProviderProperty& property)
175: m_p{ new
Impl(property) }
192addFactory(Ref<IInstanceFactory> f)
194 m_p->m_factories.
add(f);
201hasName(
const String& str)
const
203 return str == m_p->m_name;
213GlobalRegisterer(FactoryCreateFunc func,
const ProviderProperty& property) noexcept
214: m_factory_create_func(func)
215, m_factory_property(property)
222GenericRegisterer<GlobalRegisterer>::Info GlobalRegisterer::m_global_registerer_info;
229namespace Arcane::DependencyInjection
236fillWithGlobalFactories()
238 impl::GlobalRegisterer* g = impl::GlobalRegisterer::firstRegisterer();
241 auto func = g->infoCreatorWithPropertyFunction();
242 impl::FactoryInfo* fi =
nullptr;
244 fi = (*func)(g->property());
246 m_p->m_factories.addRange(fi->m_p->m_factories);
248 g = g->nextRegisterer();
251 ARCANE_FATAL(
"Infinite loop in DependencyInjection global factories");
259printFactories()
const
261 std::ostringstream ostr;
263 auto f = [&](impl::IInstanceFactory* v) ->
bool {
264 const impl::ConcreteFactoryTypeInfo& cfi = v->concreteFactoryInfo();
265 ostr <<
"I=" << index <<
" " <<
typeid(v).name()
266 <<
"\n interface=" << cfi.interfaceTypeInfo().traceInfo()
267 <<
"\n concrete=" << cfi.concreteTypeInfo().traceInfo()
268 <<
"\n constructor=" << cfi.constructorTypeInfo().traceInfo()
273 FactoryVisitorFunctor ff(f);
274 _iterateFactories(String(), &ff);
275 String s = ostr.str();
283_iterateFactories(
const String& factory_name, IFactoryVisitorFunctor* functor)
const
290 bool has_no_name = factory_name.empty();
292 Integer nb_instance = _nbValue();
293 for (Integer i = 0; i < n; ++i) {
294 impl::IInstanceFactory* f = _factory(i);
295 Int32 nb_constructor_arg = f->nbConstructorArg();
296 if (nb_constructor_arg >= 0 && nb_constructor_arg != nb_instance)
298 if (has_no_name || f->factoryInfo()->hasName(factory_name)) {
299 if (functor->execute(f))
310_iterateInstances(
const std::type_info& t_info,
const String& instance_name,
311 IInstanceVisitorFunctor* lambda)
313 bool has_no_name = instance_name.empty();
315 for (Integer i = 0; i < n; ++i) {
316 IInjectedInstance* ii = _value(i);
317 if (!ii->hasTypeInfo(t_info))
319 if (has_no_name || ii->hasName(instance_name)) {
320 if (lambda->execute(ii))
330_doError(
const TraceInfo& ti,
const String& message)
#define ARCANE_FATAL(...)
Macro envoyant une exception FatalErrorException.
#define ARCCORE_DEFINE_REFERENCE_COUNTED_CLASS(class_name)
Macro pour définir les méthodes et types une classe qui utilise un compteur de référence.
void add(ConstReferenceType val)
Ajoute l'élément val à la fin du tableau.
Chaîne de caractères unicode.
Vecteur 1D de données avec sémantique par valeur (style STL).
Espace de nom de Arccore.
Int32 Integer
Type représentant un entier.