17 #ifndef COM_BORA_SOFTWARE__BALAU_APPLICATION__BINDING_BUILDER 18 #define COM_BORA_SOFTWARE__BALAU_APPLICATION__BINDING_BUILDER 20 #include <Balau/Application/Impl/Binding.hpp> 21 #include <Balau/Application/Impl/InjectorLogger.hpp> 27 template <
typename ValueT>
class ValuePropertyBindingBuilderFactory;
28 template <
typename ValueT>
class UniquePropertyBindingBuilderFactory;
35 template <
typename BaseT>
43 template <
typename BaseT,
typename DeleterT>
45 private:
using UniquePtrProviderFunction = std::function<std::unique_ptr<BaseT> ()>;
53 warnInvalidConstQualifier(Impl::BindingMetaType::Value);
54 using U =
typename std::remove_const<BaseT>::type;
55 using T = Impl::BindingKeyType<Impl::BindingMetaType::Value, U>;
57 supplier = std::unique_ptr<BindingSupplier>(
new InstantiatingValueBindingSupplier());
66 public:
template <
typename ValueT = BaseT>
void toValue(ValueT prototype) {
67 warnInvalidConstQualifier(Impl::BindingMetaType::Value);
68 using U =
typename std::remove_const<BaseT>::type;
69 using T = Impl::BindingKeyType<Impl::BindingMetaType::Value, U>;
71 supplier = std::unique_ptr<BindingSupplier>(
new PrototypeBindingSupplier(prototype));
79 public:
template <
typename Prov
iderFunctionT>
void toValueProvider(ProviderFunctionT provider) {
80 warnInvalidConstQualifier(Impl::BindingMetaType::Value);
81 using U =
typename std::remove_const<BaseT>::type;
82 using T = Impl::BindingKeyType<Impl::BindingMetaType::Value, U>;
84 supplier = std::unique_ptr<BindingSupplier>(
new ProvidingFunctionValueBindingSupplier<ProviderFunctionT>(provider));
93 warnInvalidConstQualifier(Impl::BindingMetaType::Value);
94 using U =
typename std::remove_const<BaseT>::type;
95 using T = Impl::BindingKeyType<Impl::BindingMetaType::Value, U>;
97 supplier = std::unique_ptr<BindingSupplier>(
new ProvidingClassValueBindingSupplier<ProviderT>());
107 public:
template <
typename Prov
iderT>
void toValueProvider(std::shared_ptr<ProviderT> provider) {
108 warnInvalidConstQualifier(Impl::BindingMetaType::Value);
109 using U =
typename std::remove_const<BaseT>::type;
110 using T = Impl::BindingKeyType<Impl::BindingMetaType::Value, U>;
112 supplier = std::unique_ptr<BindingSupplier>(
new ProvidingClassInstanceValueBindingSupplier<ProviderT>(std::move(provider)));
120 public:
template <
typename DerivedT>
void toUnique() {
121 warnInvalidConstQualifier(Impl::BindingMetaType::Unique);
122 using U =
typename std::remove_const<BaseT>::type;
123 using T = Impl::BindingKeyType<Impl::BindingMetaType::Unique, U, DeleterT>;
125 supplier = std::unique_ptr<BindingSupplier>(
new InstantiatingUniquePtrBindingSupplier<DerivedT>());
134 warnInvalidConstQualifier(Impl::BindingMetaType::Unique);
135 using U =
typename std::remove_const<BaseT>::type;
136 using T = Impl::BindingKeyType<Impl::BindingMetaType::Unique, U, DeleterT>;
138 supplier = std::unique_ptr<BindingSupplier>(
new ProvidingFunctionUniquePtrBindingSupplier(provider));
147 warnInvalidConstQualifier(Impl::BindingMetaType::Unique);
148 using U =
typename std::remove_const<BaseT>::type;
149 using T = Impl::BindingKeyType<Impl::BindingMetaType::Unique, U, DeleterT>;
151 supplier = std::unique_ptr<BindingSupplier>(
new ProvidingClassUniquePtrBindingSupplier<ProviderT>());
161 public:
template <
typename Prov
iderT>
void toUniqueProvider(std::shared_ptr<ProviderT> provider) {
162 warnInvalidConstQualifier(Impl::BindingMetaType::Unique);
163 using U =
typename std::remove_const<BaseT>::type;
164 using T = Impl::BindingKeyType<Impl::BindingMetaType::Unique, U, DeleterT>;
166 supplier = std::unique_ptr<BindingSupplier>(
new ProvidingClassInstanceUniquePtrBindingSupplier<ProviderT>(std::move(provider)));
175 setKeyType<Impl::BindingKeyType<Impl::BindingMetaType::Reference, BaseT>>();
176 supplier = std::unique_ptr<BindingSupplier>(
new ReferenceBindingSupplier(reference));
185 setKeyType<Impl::BindingKeyType<Impl::BindingMetaType::Shared, BaseT>>();
186 supplier = std::unique_ptr<BindingSupplier>(
new ThreadLocalSingletonBindingSupplier<DerivedT>());
194 public:
template <
typename DerivedT,
typename SharedDeleterT>
void toThreadLocal() {
195 setKeyType<Impl::BindingKeyType<Impl::BindingMetaType::Shared, BaseT>>();
196 supplier = std::unique_ptr<BindingSupplier>(
new ThreadLocalSingletonBindingSupplier<DerivedT, SharedDeleterT>());
205 setKeyType<Impl::BindingKeyType<Impl::BindingMetaType::Shared, BaseT>>();
206 supplier = std::unique_ptr<BindingSupplier>(
new ThreadLocalSingletonBindingSupplier<BaseT>());
215 setKeyType<Impl::BindingKeyType<Impl::BindingMetaType::Shared, BaseT>>();
216 supplier = std::unique_ptr<BindingSupplier>(
new LazySingletonBindingSupplier<DerivedT>());
224 public:
template <
typename DerivedT,
typename SharedDeleterT>
void toSingleton() {
225 setKeyType<Impl::BindingKeyType<Impl::BindingMetaType::Shared, BaseT>>();
226 supplier = std::unique_ptr<BindingSupplier>(
new LazySingletonBindingSupplier<DerivedT, SharedDeleterT>());
235 setKeyType<Impl::BindingKeyType<Impl::BindingMetaType::Shared, BaseT>>();
236 supplier = std::unique_ptr<BindingSupplier>(
new LazySingletonBindingSupplier<BaseT>());
248 setKeyType<Impl::BindingKeyType<Impl::BindingMetaType::Shared, BaseT>>();
249 supplier = std::unique_ptr<BindingSupplier>(
new ProvidedSingletonBindingSupplier(instance));
262 setKeyType<Impl::BindingKeyType<Impl::BindingMetaType::Shared, BaseT>>();
263 toSingleton(std::shared_ptr<BaseT>(instance));
275 public:
template <
typename SharedDeleterT>
void toSingleton(BaseT * instance) {
276 setKeyType<Impl::BindingKeyType<Impl::BindingMetaType::Shared, BaseT>>();
277 toSingleton(std::shared_ptr<BaseT>(instance, SharedDeleterT()));
286 setKeyType<Impl::BindingKeyType<Impl::BindingMetaType::Shared, BaseT>>();
287 supplier = std::unique_ptr<BindingSupplier>(
new ProvidingClassSingletonBindingSupplier<ProviderT>());
298 setKeyType<Impl::BindingKeyType<Impl::BindingMetaType::Shared, BaseT>>();
299 supplier = std::unique_ptr<BindingSupplier>(
new ProvidingClassInstanceSingletonBindingSupplier<ProviderT>(std::move(provider)));
308 setKeyType<Impl::BindingKeyType<Impl::BindingMetaType::Shared, BaseT>>();
309 supplier = std::unique_ptr<BindingSupplier>(
new ProvidingFunctionSingletonBindingSupplier(provider));
318 setKeyType<Impl::BindingKeyType<Impl::BindingMetaType::Shared, BaseT>>();
319 supplier = std::unique_ptr<BindingSupplier>(
new EagerSingletonBindingSupplier<DerivedT>());
328 setKeyType<Impl::BindingKeyType<Impl::BindingMetaType::Shared, BaseT>>();
329 supplier = std::unique_ptr<BindingSupplier>(
new EagerSingletonBindingSupplier<DerivedT, SharedDeleterT>());
338 setKeyType<Impl::BindingKeyType<Impl::BindingMetaType::Shared, BaseT>>();
339 supplier = std::unique_ptr<BindingSupplier>(
new EagerSingletonBindingSupplier<BaseT>());
348 private:
class BindingSupplier {
349 public:
virtual ~BindingSupplier() =
default;
351 public:
virtual std::unique_ptr<Impl::AbstractBinding> build(Impl::BindingKey && key)
const = 0;
354 private:
class InstantiatingValueBindingSupplier :
public BindingSupplier {
355 public: std::unique_ptr<Impl::AbstractBinding> build(Impl::BindingKey && key)
const override {
356 return std::unique_ptr<Impl::AbstractBinding>(
new Impl::InstantiatingValueBinding<BaseT>(std::move(key)));
360 private:
class PrototypeBindingSupplier :
public BindingSupplier {
361 private:
const BaseT prototype;
363 public:
explicit PrototypeBindingSupplier(
const BaseT & prototype_)
364 : prototype(prototype_) {}
366 public: std::unique_ptr<Impl::AbstractBinding> build(Impl::BindingKey && key)
const override {
367 return std::unique_ptr<Impl::AbstractBinding>(
new Impl::PrototypeBinding<BaseT>(std::move(key), prototype));
371 private:
template <
typename Prov
iderFunctionT>
372 class ProvidingFunctionValueBindingSupplier :
public BindingSupplier {
373 private:
const ProviderFunctionT provide;
375 public:
explicit ProvidingFunctionValueBindingSupplier(ProviderFunctionT provide_)
376 : provide(provide_) {}
378 public: std::unique_ptr<Impl::AbstractBinding> build(Impl::BindingKey && key)
const override {
379 return std::unique_ptr<Impl::AbstractBinding>(
380 new Impl::ProvidingFunctionValueBinding<BaseT, ProviderFunctionT>(std::move(key), provide)
385 private:
template <
typename Prov
iderT>
class ProvidingClassValueBindingSupplier :
public BindingSupplier {
386 public: std::unique_ptr<Impl::AbstractBinding> build(Impl::BindingKey && key)
const override {
387 return std::unique_ptr<Impl::AbstractBinding>(
388 new Impl::ProvidingClassValueBinding<BaseT, ProviderT>(std::move(key))
393 private:
template <
typename Prov
iderT>
class ProvidingClassInstanceValueBindingSupplier :
public BindingSupplier {
394 public:
explicit ProvidingClassInstanceValueBindingSupplier(std::shared_ptr<ProviderT> && provider_) noexcept
395 : provider(std::move(provider_)) {}
397 public: std::unique_ptr<Impl::AbstractBinding> build(Impl::BindingKey && key)
const override {
398 return std::unique_ptr<Impl::AbstractBinding>(
399 new Impl::ProvidingClassInstanceValueBinding<BaseT, ProviderT>(std::move(key), provider)
403 std::shared_ptr<ProviderT> provider;
406 private:
class ReferenceBindingSupplier :
public BindingSupplier {
407 private: BaseT & reference;
409 public:
explicit ReferenceBindingSupplier(BaseT & reference_)
410 : reference(reference_) {}
412 public: std::unique_ptr<Impl::AbstractBinding> build(Impl::BindingKey && key)
const override {
413 return std::unique_ptr<Impl::AbstractBinding>(
414 new Impl::StandardReferenceBinding<BaseT>(std::move(key), reference)
419 private:
template <
typename DerivedT>
class InstantiatingUniquePtrBindingSupplier :
public BindingSupplier {
420 public: std::unique_ptr<Impl::AbstractBinding> build(Impl::BindingKey && key)
const override {
421 return std::unique_ptr<Impl::AbstractBinding>(
422 new Impl::InstantiatingUniquePtrBinding<BaseT, DerivedT, DeleterT>(std::move(key))
427 private:
class ProvidingFunctionUniquePtrBindingSupplier :
public BindingSupplier {
428 private:
const UniquePtrProviderFunction provide;
430 public:
explicit ProvidingFunctionUniquePtrBindingSupplier(UniquePtrProviderFunction provide_)
431 : provide(provide_) {}
433 public: std::unique_ptr<Impl::AbstractBinding> build(Impl::BindingKey && key)
const override {
434 return std::unique_ptr<Impl::AbstractBinding>(
435 new Impl::ProvidingFunctionUniquePtrBinding<BaseT, DeleterT>(std::move(key), provide)
440 private:
template <
typename Prov
iderT>
class ProvidingClassUniquePtrBindingSupplier :
public BindingSupplier {
441 public: std::unique_ptr<Impl::AbstractBinding> build(Impl::BindingKey && key)
const override {
442 return std::unique_ptr<Impl::AbstractBinding>(
443 new Impl::ProvidingClassUniquePtrBinding<BaseT, ProviderT, DeleterT>(std::move(key))
448 private:
template <
typename Prov
iderT>
class ProvidingClassInstanceUniquePtrBindingSupplier :
public BindingSupplier {
449 public:
explicit ProvidingClassInstanceUniquePtrBindingSupplier(std::shared_ptr<ProviderT> && provider_) noexcept
450 : provider(std::move(provider_)) {}
452 public: std::unique_ptr<Impl::AbstractBinding> build(Impl::BindingKey && key)
const override {
453 return std::unique_ptr<Impl::AbstractBinding>(
454 new Impl::ProvidingClassInstanceUniquePtrBinding<BaseT, ProviderT, DeleterT>(std::move(key), provider)
458 private: std::shared_ptr<ProviderT> provider;
461 private:
template <
typename DerivedT,
typename ThreadLocalDeleterT = std::default_delete<BaseT>>
class ThreadLocalSingletonBindingSupplier :
public BindingSupplier {
462 public: std::unique_ptr<Impl::AbstractBinding> build(Impl::BindingKey && key)
const override {
463 return std::unique_ptr<Impl::AbstractBinding>(
464 new Impl::ThreadLocalSingletonBinding<BaseT, DerivedT, ThreadLocalDeleterT>(std::move(key))
469 private:
class ProvidedSingletonBindingSupplier :
public BindingSupplier {
470 private:
const std::shared_ptr<BaseT> instance;
472 public:
explicit ProvidedSingletonBindingSupplier(std::shared_ptr<BaseT> instance_)
473 : instance(std::move(instance_)) {}
475 public: std::unique_ptr<Impl::AbstractBinding> build(Impl::BindingKey && key)
const override {
476 return std::unique_ptr<Impl::AbstractBinding>(
477 new Impl::ProvidedSingletonBinding<BaseT>(std::move(key), instance)
482 private:
template <
typename Prov
iderT>
class ProvidingClassSingletonBindingSupplier :
public BindingSupplier {
483 public: std::unique_ptr<Impl::AbstractBinding> build(Impl::BindingKey && key)
const override {
484 return std::unique_ptr<Impl::AbstractBinding>(
485 new Impl::ProvidingClassSingletonBinding<BaseT, ProviderT>(std::move(key))
490 private:
template <
typename Prov
iderT>
class ProvidingClassInstanceSingletonBindingSupplier :
public BindingSupplier {
491 public:
explicit ProvidingClassInstanceSingletonBindingSupplier(std::shared_ptr<ProviderT> && provider_) noexcept
492 : provider(std::move(provider_)) {}
494 public: std::unique_ptr<Impl::AbstractBinding> build(Impl::BindingKey && key)
const override {
495 return std::unique_ptr<Impl::AbstractBinding>(
496 new Impl::ProvidingClassInstanceSingletonBinding<BaseT, ProviderT>(std::move(key), provider)
500 private: std::shared_ptr<ProviderT> provider;
503 private:
class ProvidingFunctionSingletonBindingSupplier :
public BindingSupplier {
504 public:
explicit ProvidingFunctionSingletonBindingSupplier(
const std::function<std::shared_ptr<BaseT> ()> & provider_) noexcept
505 : provider(provider_) {}
507 public: std::unique_ptr<Impl::AbstractBinding> build(Impl::BindingKey && key)
const override {
508 return std::unique_ptr<Impl::AbstractBinding>(
509 new Impl::ProvidingFunctionSingletonBinding<BaseT>(std::move(key), provider)
513 private:
const std::function<std::shared_ptr<BaseT> ()> provider;
516 private:
template <
typename DerivedT,
typename SharedDeleterT = std::default_delete<BaseT>>
class LazySingletonBindingSupplier :
public BindingSupplier {
517 public: std::unique_ptr<Impl::AbstractBinding> build(Impl::BindingKey && key)
const override {
518 return std::unique_ptr<Impl::AbstractBinding>(
519 new Impl::LazySingletonBinding<BaseT, DerivedT, SharedDeleterT>(std::move(key))
524 private:
template <
typename DerivedT,
typename SharedDeleterT = std::default_delete<BaseT>>
class EagerSingletonBindingSupplier :
public BindingSupplier {
525 public: std::unique_ptr<Impl::AbstractBinding> build(Impl::BindingKey && key)
const override {
526 return std::unique_ptr<Impl::AbstractBinding>(
527 new Impl::EagerSingletonBinding<BaseT, DerivedT, SharedDeleterT>(std::move(key))
532 private:
void warnInvalidConstQualifier(Impl::BindingMetaType metaType) {
533 if (!std::is_same<BaseT,
typename std::remove_const<BaseT>::type>::value) {
534 Impl::InjectorLogger::log().warn(
535 "{} binding key {} has been defined with a const type. This " 536 "qualifier will be removed from the key because {} types cannot " 537 "be const qualified. This will not affect binding semantics." 547 template <
typename ValueT>
friend class Impl::ValuePropertyBindingBuilderFactory;
548 template <
typename ValueT>
friend class Impl::UniquePropertyBindingBuilderFactory;
551 : Impl::BindingBuilderBase(std::move(name)) {}
553 private: std::unique_ptr<Impl::AbstractBinding> build()
override {
554 return supplier->build(std::move(key));
557 private: std::unique_ptr<BindingSupplier> supplier;
562 #endif // COM_BORA_SOFTWARE__BALAU_APPLICATION__BINDING_BUILDER void toEagerSingleton()
Bind an interface to a concrete singleton type.
Definition: BindingBuilder.hpp:317
Application configurations specify application injector bindings.
Definition: ApplicationConfiguration.hpp:34
void toValue(ValueT prototype)
Bind a concrete type to a prototype value.
Definition: BindingBuilder.hpp:66
void toThreadLocal()
Bind an interface to a concrete type with thread-local singleton semantics.
Definition: BindingBuilder.hpp:184
void toUniqueProvider()
Bind an interface to a unique pointer provider class.
Definition: BindingBuilder.hpp:146
An injector binding candidate created via the injector configuration.
Definition: BindingBuilder.hpp:44
void toUniqueProvider(std::shared_ptr< ProviderT > provider)
Bind an interface to a unique pointer provider class instance.
Definition: BindingBuilder.hpp:161
void toSingleton(BaseT *instance)
Bind an interface to a specific instance, via a pointer container type syntax.
Definition: BindingBuilder.hpp:261
void toSingleton(std::shared_ptr< BaseT > instance)
Bind an interface to a specific instance.
Definition: BindingBuilder.hpp:247
The root Balau namespace.
Definition: ApplicationConfiguration.hpp:23
void toEagerSingleton()
Bind a concrete singleton type.
Definition: BindingBuilder.hpp:337
void toSingleton()
Bind an interface to a concrete singleton type, using the supplied deleter type.
Definition: BindingBuilder.hpp:224
Environment configurations specify typed and untyped environment injector bindings.
Definition: EnvironmentConfiguration.hpp:97
void toReference(BaseT &reference)
Bind a reference type to a reference value.
Definition: BindingBuilder.hpp:174
void toUniqueProvider(UniquePtrProviderFunction provider)
Bind an interface to a unique pointer provider function.
Definition: BindingBuilder.hpp:133
void toEagerSingleton()
Bind an interface to a concrete singleton type, using the supplied deleter type.
Definition: BindingBuilder.hpp:327
void toSingletonProvider()
Bind an interface to a singleton provider class.
Definition: BindingBuilder.hpp:285
void toSingleton()
Bind an interface to a concrete singleton type.
Definition: BindingBuilder.hpp:214
void toValueProvider()
Bind a concrete type to a value provider class.
Definition: BindingBuilder.hpp:92
void toValueProvider(ProviderFunctionT provider)
Bind a concrete type to a value provider function.
Definition: BindingBuilder.hpp:79
void toSingleton()
Bind a concrete singleton type.
Definition: BindingBuilder.hpp:234
void toSingleton(BaseT *instance)
Bind an interface to a specific instance, via a pointer container type syntax and using the supplied ...
Definition: BindingBuilder.hpp:275
void toValueProvider(std::shared_ptr< ProviderT > provider)
Bind a concrete type to a value provider class instance.
Definition: BindingBuilder.hpp:107
void toUnique()
Bind an interface to an implementing class.
Definition: BindingBuilder.hpp:120
void toSingletonProvider(const std::function< std::shared_ptr< BaseT >()> &provider)
Bind an interface to a singleton provider function.
Definition: BindingBuilder.hpp:307
void toValue()
Bind a concrete type.
Definition: BindingBuilder.hpp:52
void toThreadLocal()
Bind a concrete type with thread-local singleton semantics.
Definition: BindingBuilder.hpp:204
std::function< std::unique_ptr< BaseT >(const std::unique_ptr< const BaseT > &)> UniquePropertyCloner
The clone function type for unique pointer prototypes.
Definition: BindingBuilder.hpp:36
void toSingletonProvider(std::shared_ptr< ProviderT > provider)
Bind an interface to a singleton provider class instance.
Definition: BindingBuilder.hpp:297
void toThreadLocal()
Bind an interface to a concrete type with thread-local singleton semantics, using the supplied delete...
Definition: BindingBuilder.hpp:194