14#include "arcane/utils/internal/DependencyInjection.h"
16#include "arcane/utils/UniqueArray.h"
17#include "arcane/utils/ExternalRef.h"
18#include "arcane/utils/FatalErrorException.h"
23namespace Arcane::DependencyInjection
38 : m_instance(instance)
44 IInjectedInstance* m_instance =
nullptr;
52 for (Integer i = 0, n = m_instance_list.size(); i < n; ++i)
53 delete m_instance_list[i].m_instance;
54 m_instance_list.clear();
59 void addInstance(IInjectedInstance* instance)
61 Int32 index = m_instance_list.size();
62 m_instance_list.add(InstanceInfo{ instance, index });
64 IInjectedInstance* instance(Int32 index)
const {
return m_instance_list[index].m_instance; }
65 Int32 nbInstance()
const {
return m_instance_list.size(); }
69 UniqueArray<InstanceInfo> m_instance_list;
73 UniqueArray<Ref<impl::IInstanceFactory>> m_factories;
98_add(IInjectedInstance* instance)
100 m_p->addInstance(instance);
109 return m_p->nbInstance();
115IInjectedInstance* Injector::
116_value(Integer i)
const
118 return m_p->instance(i);
127 return m_p->m_factories.size();
133impl::IInstanceFactory* Injector::
134_factory(Integer i)
const
136 return m_p->m_factories[i].get();
147namespace Arcane::DependencyInjection::impl
153void ConstructorRegistererBase::
154_doError1(
const String& message,
int nb_value)
165 Impl(
const ProviderProperty& property)
166 : m_property(property)
167 , m_name(property.name())
172 const ProviderProperty m_property;
181FactoryInfo(
const ProviderProperty& property)
182: m_p{ new
Impl(property) }
199addFactory(Ref<IInstanceFactory> f)
201 m_p->m_factories.
add(f);
208hasName(
const String& str)
const
210 return str == m_p->m_name;
217fillWithImplementationNames(Array<String>& names)
const
219 names.add(m_p->m_name);
229GlobalRegisterer(FactoryCreateFunc func,
const ProviderProperty& property) noexcept
230: m_factory_create_func(func)
231, m_factory_property(property)
238GenericRegisterer<GlobalRegisterer>::Info GlobalRegisterer::m_global_registerer_info;
245namespace Arcane::DependencyInjection
252fillWithGlobalFactories()
254 impl::GlobalRegisterer* g = impl::GlobalRegisterer::firstRegisterer();
257 auto func = g->infoCreatorWithPropertyFunction();
258 impl::FactoryInfo* fi =
nullptr;
260 fi = (*func)(g->property());
262 m_p->m_factories.addRange(fi->m_p->m_factories);
264 g = g->nextRegisterer();
267 ARCANE_FATAL(
"Infinite loop in DependencyInjection global factories");
275printFactories()
const
277 std::ostringstream ostr;
279 auto f = [&](impl::IInstanceFactory* v) ->
bool {
280 const impl::ConcreteFactoryTypeInfo& cfi = v->concreteFactoryInfo();
281 ostr <<
"I=" << index <<
" " <<
typeid(v).name()
282 <<
"\n interface=" << cfi.interfaceTypeInfo().traceInfo()
283 <<
"\n concrete=" << cfi.concreteTypeInfo().traceInfo()
284 <<
"\n constructor=" << cfi.constructorTypeInfo().traceInfo()
289 FactoryVisitorFunctor ff(f);
290 _iterateFactories(String(), &ff);
291 String s = ostr.str();
299_iterateFactories(
const String& factory_name, IFactoryVisitorFunctor* functor)
const
306 bool has_no_name = factory_name.empty();
308 Integer nb_instance = _nbValue();
309 for (Integer i = 0; i < n; ++i) {
310 impl::IInstanceFactory* f = _factory(i);
311 Int32 nb_constructor_arg = f->nbConstructorArg();
312 if (nb_constructor_arg >= 0 && nb_constructor_arg != nb_instance)
314 if (has_no_name || f->factoryInfo()->hasName(factory_name)) {
315 if (functor->execute(f))
326_iterateInstances(
const std::type_info& t_info,
const String& instance_name,
327 IInstanceVisitorFunctor* lambda)
329 bool has_no_name = instance_name.empty();
331 for (Integer i = 0; i < n; ++i) {
332 IInjectedInstance* ii = _value(i);
333 if (!ii->hasTypeInfo(t_info))
335 if (has_no_name || ii->hasName(instance_name)) {
336 if (lambda->execute(ii))
346_doError(
const TraceInfo& ti,
const String& message)
355_printValidImplementationAndThrow(
const TraceInfo& ti,
356 const String& implementation_name,
357 FactoryFilterFunc filter_func)
362 UniqueArray<String> valid_names;
363 for (Int32 i = 0, n = _nbFactory(); i < n; ++i) {
364 impl::IInstanceFactory* f = _factory(i);
365 if (filter_func(f)) {
366 f->factoryInfo()->fillWithImplementationNames(valid_names);
369 String message = String::format(
"No implementation named '{0}' found", implementation_name);
373 if (valid_names.size() == 0)
374 message2 =
" and no implementation is available.";
375 else if (valid_names.size() == 1)
376 message2 = String::format(
". Valid value is: '{0}'.", valid_names[0]);
378 message2 = String::format(
". Valid values are: '{0}'.", String::join(
", ", valid_names));
379 _doError(ti, message + message2);
#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.