Sauce-0.10.1 A C++ Dependency Injection Framework

- Main Page
- Classes
- Files

- File List

implicit_bindings.h
1 #ifndef SAUCE_INTERNAL_IMPLICIT_BINDINGS_H_
2 #define SAUCE_INTERNAL_IMPLICIT_BINDINGS_H_
3
4 #include <string>
5 #include <vector>
6
7 #include <sauce/exceptions.h>
8 #include <sauce/injector.h>
9 #include <sauce/memory.h>
10 #include <sauce/provider.h>
11 #include <sauce/scopes.h>
12 #include <sauce/internal/resolved_binding.h>
13 #include <sauce/internal/binding.h>
14 #include <sauce/internal/bindings.h>
15 #include <sauce/internal/key.h>
16
17 namespace sauce {
18 namespace internal {
19
20 class ImplicitBindings;
21
22 typedef Bindings<ImplicitBindings> Concrete;
23
24 template<typename ImplicitInjection_>
25 class ImplicitBindingTraits {
26 public:
27 typedef ImplicitInjection_ ImplicitInjection;
28 typedef typename ImplicitInjection::Dependency Dependency;
29 typedef typename ResolvedBinding<Dependency>::BindingPtr BindingPtr;
30 };
31
35 template<typename Dependency>
36 class ImplicitBinding {
37 public:
38
39 typedef sauce::shared_ptr<ResolvedBinding<Dependency> > BindingPtr;
40
44 static BindingPtr get(Concrete const &, std::string const name) {
45 throw UnboundExceptionFor<Dependency>(name);
46 }
47
48 };
49
53 class ImplicitBindings {
54 public:
55
59 template<typename Dependency>
60 sauce::shared_ptr<ResolvedBinding<Dependency> > getProviding(
61 Concrete const & bindings, std::string const name) const {
62 return ImplicitBinding<Dependency>::get(bindings, name);
63 }
64
68 template<typename Dependency>
69 std::vector<sauce::shared_ptr<ResolvedBinding<Dependency> > > getModifyings(
70 Concrete const &, std::string const) const {
71 return std::vector<sauce::shared_ptr<ResolvedBinding<Dependency> > >(); // TODO
72 }
73
74 };
75
79 template<>
80 class ImplicitBinding<Named<Injector, Unnamed> >: ImplicitBindingTraits<i::InjectorBinding> {
81 public:
82 static BindingPtr get(Concrete const &, std::string const name) {
83 if (name != unnamed()) {
84 throw UnboundExceptionFor<Named<Injector, Unnamed> >(name);
85 }
86 BindingPtr binding(new ImplicitInjection());
87 return binding;
88 }
89 };
90
94 template<typename ProvidedDependency, typename Name>
95 class ImplicitBinding<Named<Provider<ProvidedDependency>, Name> > {
96 public:
97 typedef ImplicitBindingTraits<i::ImplicitProviderBinding<ProvidedDependency, Name> > Traits;
98 typedef typename Traits::ImplicitInjection ImplicitInjection;
99 typedef typename Traits::Dependency Dependency;
100 typedef typename Traits::BindingPtr BindingPtr;
101
102 static BindingPtr get(Concrete const & bindings, std::string const name) {
103 typedef typename Key<ProvidedDependency>::Normalized Normalized;
104 typedef typename ResolvedBinding<Normalized>::BindingPtr ProvidedBindingPtr;
105
106 ProvidedBindingPtr providedBinding(bindings.getProvidingBinding<Normalized>(name));
107 BindingPtr binding(new ImplicitInjection(providedBinding));
108 return binding;
109 }
110 };
111
112 }
113
114 namespace i = ::sauce::internal;
115
116 }
117
118 #endif // SAUCE_INTERNAL_IMPLICIT_BINDINGS_H_
sauce::internal::ResolvedBinding
A binding for an acknowledged interface.
Definition: resolved_binding.h:20
sauce::Unnamed
The name of all statically unnamed dependencies.
Definition: named.h:17
sauce::internal
Definition: binder.h:317
sauce::internal::ImplicitBinding::get
static BindingPtr get(Concrete const &, std::string const name)
Attempt to supply an unknown providing Binding at injection time.
Definition: implicit_bindings.h:44
sauce::Named
Wrap dependency requests with Named to choose one of several (statically) named alternatives.
Definition: named.h:12
sauce::internal::ImplicitBindingTraits
Definition: implicit_bindings.h:25
sauce::internal::ImplicitBinding
Attempts to supply a Binding when the given Dependency is not found.
Definition: implicit_bindings.h:36
sauce
Definition: binder.h:21
sauce::internal::ImplicitBindings::getProviding
sauce::shared_ptr< ResolvedBinding< Dependency > > getProviding(Concrete const &bindings, std::string const name) const
Attempt to supply an unknown providing Binding at injection time.
Definition: implicit_bindings.h:60
sauce::internal::InjectorBinding
An injection that provides the injector itself.
Definition: injector_binding.h:16
sauce::internal::ImplicitBindings::getModifyings
std::vector< sauce::shared_ptr< ResolvedBinding< Dependency > > > getModifyings(Concrete const &, std::string const) const
Attempt to supply unknown modifying Bindings at injection time.
Definition: implicit_bindings.h:69
sauce::Injector
Definition: injector.h:27
sauce::Provider
An interface for including custom factories in an Injector.
Definition: provider.h:15
sauce::UnboundExceptionFor
Thrown when a binding cannot be found for the given interface.
Definition: exceptions.h:31
sauce::internal::ImplicitBindings
Attempts to supply a Binding when none is found for a dependency.
Definition: implicit_bindings.h:53