Sauce-0.10.1
A C++ Dependency Injection Framework
Public Member Functions | List of all members
sauce::internal::Bindings< ImplicitBindings > Class Template Reference

A container for bindings. More...

#include <sauce/internal/bindings.h>

Public Member Functions

void put (OpaqueBindingPtr binding)
 Insert the given binding. More...
 
template<typename Dependency >
sauce::shared_ptr< ResolvedBinding< Dependency > > getProvidingBinding (std::string const name) const
 
template<typename Dependency >
std::vector< sauce::shared_ptr< ResolvedBinding< Dependency > > > getModifierBindings (std::string const name) const
 
template<typename Dependency >
void validateAcyclic (bool validateProviding, InjectorPtr injector, TypeIds &ids, std::string const name) const
 
template<typename Dependency >
void get (typename Key< Dependency >::Ptr &injected, InjectorPtr injector, std::string const name) const
 Inject the named Dependency. More...
 
template<typename Scope >
void eagerlyInject (InjectorPtr injector) const
 

Detailed Description

template<typename ImplicitBindings>
class sauce::internal::Bindings< ImplicitBindings >

A container for bindings.

Each Modules objects creates a Bindings, and passes a const copy to each Injector it creates. The set of bindings an Injector uses is therefore read-only.

When providing instances (eagerly and not) the injector is passed in and the dependency is returned directly, instead of giving the binding to the injector (the binding details stay hidden.)

The template parameter is a strategy type that attempts to located unknown bindings dynamically (implicitly.) Due to the threat of circular dependencies (in Sauce's type space), Bindings itself can't be aware of concrete Binding implementations. The only time it needs to know about them is when resolving implicit bindings. So, this functionality is hidden behind the templated strategy.

Constructor & Destructor Documentation

◆ Bindings()

template<typename ImplicitBindings >
sauce::internal::Bindings< ImplicitBindings >::Bindings ( )
inline
62 :
63 providingBindingMap(),
64 modifyingBindingMap(),
65 scopeMap() {}

Member Function Documentation

◆ eagerlyInject()

template<typename ImplicitBindings >
template<typename Scope >
void sauce::internal::Bindings< ImplicitBindings >::eagerlyInject ( InjectorPtr  injector) const
inline
157 {
158 TypeId scopeKey = typeIdOf<Scope>();
159 ScopeMap::const_iterator i = scopeMap.lower_bound(scopeKey);
160 ScopeMap::const_iterator end = scopeMap.upper_bound(scopeKey);
161
162 for (; i != end; ++i) {
163 OpaqueBindingPtr const & binding = i->second;
164 binding->eagerlyInject(binding, injector);
165 }
166 }

◆ get()

template<typename ImplicitBindings >
template<typename Dependency >
void sauce::internal::Bindings< ImplicitBindings >::get ( typename Key< Dependency >::Ptr &  injected,
InjectorPtr  injector,
std::string const  name 
) const
inline

Inject the named Dependency.

If no binding is found, the implicit bindings are checked.

138 {
139 typedef sauce::shared_ptr<ResolvedBinding<Dependency> > BindingPtr;
140 BindingPtr binding;
141
142 if (injected.get() == NULL) {
143 binding = getProvidingBinding<Dependency>(name);
144 binding->get(injected, binding, injector);
145 }
146
147 typedef std::vector<BindingPtr> BindingPtrs;
148 BindingPtrs bindings = getModifierBindings<Dependency>(name);
149
150 for (typename BindingPtrs::iterator i = bindings.begin(); i != bindings.end(); ++i) {
151 binding = *i;
152 binding->get(injected, binding, injector);
153 }
154 }

◆ getModifierBindings()

template<typename ImplicitBindings >
template<typename Dependency >
std::vector< sauce::shared_ptr< ResolvedBinding< Dependency > > > sauce::internal::Bindings< ImplicitBindings >::getModifierBindings ( std::string const  name) const
inline
96 {
97 ImplicitBindings implicitBindings;
98 std::vector<sauce::shared_ptr<ResolvedBinding<Dependency> > > bindings =
99 implicitBindings.template getModifyings<Dependency>(*this, name);
100
101 NamedTypeId bindingKey = namedTypeIdOf<Dependency>(name);
102 ModifyingBindingMap::const_iterator i = modifyingBindingMap.lower_bound(bindingKey);
103 ModifyingBindingMap::const_iterator end = modifyingBindingMap.upper_bound(bindingKey);
104
105 for (; i != end; ++i) {
106 OpaqueBindingPtr const & binding = i->second;
107 bindings.push_back(resolve<Dependency>(binding));
108 }
109
110 return bindings;
111 }

◆ getProvidingBinding()

template<typename ImplicitBindings >
template<typename Dependency >
sauce::shared_ptr< ResolvedBinding< Dependency > > sauce::internal::Bindings< ImplicitBindings >::getProvidingBinding ( std::string const  name) const
inline
81 {
82 sauce::shared_ptr<ResolvedBinding<Dependency> > binding;
83
84 ProvidingBindingMap::const_iterator i = providingBindingMap.find(namedTypeIdOf<Dependency>(name));
85 if (i == providingBindingMap.end()) {
86 ImplicitBindings implicitBindings;
87 binding = implicitBindings.template getProviding<Dependency>(*this, name);
88 } else {
89 binding = resolve<Dependency>(i->second);
90 }
91
92 return binding;
93 }

◆ put()

template<typename ImplicitBindings >
void sauce::internal::Bindings< ImplicitBindings >::put ( OpaqueBindingPtr  binding)
inline

Insert the given binding.

70 {
71 if (binding->isModifier()) {
72 modifyingBindingMap.insert(std::make_pair(binding->getKey(), binding));
73 } else {
74 providingBindingMap.insert(std::make_pair(binding->getKey(), binding));
75 TypeId scopeKey = binding->getScopeKey();
76 scopeMap.insert(std::make_pair(scopeKey, binding));
77 }
78 }

◆ validateAcyclic()

template<typename ImplicitBindings >
template<typename Dependency >
void sauce::internal::Bindings< ImplicitBindings >::validateAcyclic ( bool  validateProviding,
InjectorPtr  injector,
TypeIds &  ids,
std::string const  name 
) const
inline
114 {
115 typedef sauce::shared_ptr<ResolvedBinding<Dependency> > BindingPtr;
116 BindingPtr binding;
117
118 if (validateProviding) {
119 binding = getProvidingBinding<Dependency>(name);
120 binding->validateAcyclic(injector, ids);
121 }
122
123 typedef std::vector<BindingPtr> BindingPtrs;
124 BindingPtrs bindings = getModifierBindings<Dependency>(name);
125
126 for (typename BindingPtrs::iterator i = bindings.begin(); i != bindings.end(); ++i) {
127 binding = *i;
128 binding->validateAcyclic(injector, ids);
129 }
130 }

The documentation for this class was generated from the following file: