35 template <
class Service>
70 template <
class Service>
74 std::is_abstract<Service>::value,
75 "Only abstract classes are injectable");
77 std::has_virtual_destructor<Service>::value,
78 "An injectable service must declare a virtual destructor");
92 assert(_injector.acquire &&
"Missing dependency injection");
93 _instance = _injector.acquire();
94 assert(_instance &&
"An injector retrieved a null provider");
103 if (_injector.release)
104 _injector.release(_instance);
144 (_injector.acquire ==
nullptr) &&
145 (_injector.release ==
nullptr) &&
146 "Dependency already injected");
147 assert(injector.acquire &&
"Invalid injector");
148 _injector = injector;
158 template <
class Provider,
typename... _Args>
162 std::is_base_of<Service, Provider>::value,
163 "Provider does not implement Service");
165 (_injector.acquire ==
nullptr) &&
166 (_injector.release ==
nullptr) &&
167 "Dependency already injected");
168 _injector.release =
nullptr;
169 _injector.acquire = [... args = std::forward<_Args>(__args)]() -> Service *
171 static Provider p(args...);
183 template <
class Provider,
typename... _Args>
187 std::is_base_of<Service, Provider>::value,
188 "Provider does not implement Service");
190 (_injector.acquire ==
nullptr) &&
191 (_injector.release ==
nullptr) &&
192 "Dependency already injected");
193 _injector.release =
nullptr;
194 _injector.acquire = [... args = std::forward<_Args>(__args)]() -> Service *
196 static thread_local Provider p(args...);
208 template <
class Provider,
typename... _Args>
212 std::is_base_of<Service, Provider>::value,
213 "Provider does not implement Service");
215 (_injector.acquire ==
nullptr) &&
216 (_injector.release ==
nullptr) &&
217 "Dependency already injected");
219 [... args = std::forward<_Args>(__args)]() -> Service *
221 return new Provider(args...);
223 _injector.release = [](Service *provider) ->
void
238 _injector.acquire =
nullptr;
239 _injector.release =
nullptr;
257 template <
class Service>
273 template <
class Service,
class Provider,
typename... _Args>
277 std::forward<_Args>(args)...);
290 template <
class Service,
class Provider,
typename... _Args>
294 std::forward<_Args>(args)...);
305 template <
class Service,
class Provider,
typename... _Args>
309 std::forward<_Args>(args)...);
317 template <
class Service>
321 std::is_abstract<Service>::value,
322 "Only abstract classes are injectable");
324 std::has_virtual_destructor<Service>::value,
325 "An injectable service must declare a virtual destructor");
332 using iterator = std::vector<service_type>::iterator;
339 std::vector<service_type>::const_reverse_iterator;
347 assert(!_injectors.empty() &&
"No dependency injections");
348 for (
auto injector : _injectors)
350 assert(injector.acquire &&
"Missing dependency injection");
352 assert(
instance &&
"An injector retrieved a null provider");
363 for (std::size_t i = 0; i < _injectors.size(); i++)
364 if (_injectors[i].release)
365 _injectors[i].release(_instances.at(i));
378 constexpr std::size_t
size() const noexcept
380 return _instances.size();
391 return _instances[index];
402 return _instances[index];
413 return _instances.at(index);
424 return _instances.at(index);
455 return _instances.crbegin();
467 return _instances.crend();
477 assert(injector.acquire &&
"Invalid injector");
478 _injectors.push_back(injector);
488 template <
class Provider,
typename... _Args>
492 std::is_base_of<Service, Provider>::value,
493 "Provider does not implement Service");
496 [... args = std::forward<_Args>(__args)]() -> Service *
498 static Provider p(args...);
511 template <
class Provider,
typename... _Args>
515 std::is_base_of<Service, Provider>::value,
516 "Provider does not implement Service");
519 [... args = std::forward<_Args>(__args)]() -> Service *
521 static thread_local Provider p(args...);
534 template <
class Provider,
typename... _Args>
538 std::is_base_of<Service, Provider>::value,
539 "Provider does not implement Service");
542 [... args = std::forward<_Args>(__args)]() -> Service *
544 static thread_local Provider p(args...);
547 .release = [](Service *provider) ->
void
568 std::vector<service_type> _instances;
569 inline static std::vector<Injector<Service>> _injectors;
580 template <
class Service>
596 template <
class Service,
class Provider,
typename... _Args>
600 std::forward<_Args>(args)...);
613 template <
class Service,
class Provider,
typename... _Args>
617 std::forward<_Args>(args)...);
630 template <
class Service,
class Provider,
typename... _Args>
634 std::forward<_Args>(args)...);
Dependency injection pattern.
Definition: dip.hpp:29
void inject_transient(_Args &&...args)
Inject a transient instance to a Service.
Definition: dip.hpp:274
void inject_singleton(_Args &&...args)
Inject a singleton instance to a Service.
Definition: dip.hpp:291
void add_transient(_Args &&...args)
Inject a transient instance to a Service.
Definition: dip.hpp:597
void add_singleton(_Args &&...args)
Inject a singleton instance to a Service.
Definition: dip.hpp:614
void inject_thread_singleton(_Args &&...args)
Inject a per-thread singleton instance to a Service.
Definition: dip.hpp:306
void inject(const Injector< Service > &injector)
Inject a service provider using a custom injector.
Definition: dip.hpp:258
void add_thread_singleton(_Args &&...args)
Inject a per-thread singleton instance to a Service.
Definition: dip.hpp:631
void add(const Injector< Service > &injector)
Inject a service provider using a custom injector.
Definition: dip.hpp:581
Custom injector.
Definition: dip.hpp:37
AcquireFunction acquire
Custom function to retrieve instances.
Definition: dip.hpp:55
std::function< Service *()> AcquireFunction
Type of a function able to retrieve instances.
Definition: dip.hpp:42
std::function< void(Service *)> ReleaseFunction
Type of a function able to remove unneeded instances.
Definition: dip.hpp:48
ReleaseFunction release
Custom function to remove uneeded instances.
Definition: dip.hpp:62
Set of injected instances of a service.
Definition: dip.hpp:319
reverse_iterator rbegin()
returns a reverse iterator to the beginning
Definition: dip.hpp:447
const_service_type operator[](std::size_t index) const
Get a service provider instance in the set.
Definition: dip.hpp:400
const Service * const_service_type
Const type of the instances of the service provider.
Definition: dip.hpp:330
service_type operator[](std::size_t index)
Get a service provider instance in the set.
Definition: dip.hpp:389
service_type at(std::size_t index)
Get a service provider instance in the set.
Definition: dip.hpp:411
iterator end()
returns an iterator to the end
Definition: dip.hpp:438
const_iterator begin() const
returns an iterator to the beginning
Definition: dip.hpp:432
const_reverse_iterator rend() const
returns a reverse iterator to the end
Definition: dip.hpp:462
const_service_type at(std::size_t index) const
Get a service provider instance in the set.
Definition: dip.hpp:422
std::vector< service_type >::const_reverse_iterator const_reverse_iterator
Const reverse iterator.
Definition: dip.hpp:339
std::vector< service_type >::const_iterator const_iterator
Const forward iterator.
Definition: dip.hpp:334
std::vector< service_type >::iterator iterator
Forward iterator.
Definition: dip.hpp:332
instance_set()
Retrieve a set of instances providing the service.
Definition: dip.hpp:345
const_iterator cend() const noexcept
returns an iterator to the end
Definition: dip.hpp:444
static void add_transient(_Args &&...__args)
Inject a service provider with transient life cycle.
Definition: dip.hpp:535
const_iterator end() const
returns an iterator to the end
Definition: dip.hpp:441
const_iterator cbegin() const noexcept
returns an iterator to the end
Definition: dip.hpp:435
Service * service_type
Type of the instances of the service provider.
Definition: dip.hpp:328
const_reverse_iterator crend() const noexcept
returns a reverse iterator to the end
Definition: dip.hpp:465
const_reverse_iterator rbegin() const
returns a reverse iterator to the beginning
Definition: dip.hpp:450
static void add(const Injector< Service > &injector) noexcept
Inject a service provider using a custom injector.
Definition: dip.hpp:475
static void clear_injections() noexcept
Clear all the injected dependencies for testing purposes.
Definition: dip.hpp:562
reverse_iterator rend()
returns a reverse iterator to the end
Definition: dip.hpp:459
constexpr std::size_t size() const noexcept
Get the count of instances injected into the service.
Definition: dip.hpp:378
static void add_thread_singleton(_Args &&...__args)
Inject a service provider with per-thread singleton life cycle.
Definition: dip.hpp:512
const_reverse_iterator crbegin() const noexcept
returns a reverse iterator to the beginning
Definition: dip.hpp:453
std::vector< service_type >::reverse_iterator reverse_iterator
Reverse iterator.
Definition: dip.hpp:336
static void add_singleton(_Args &&...__args)
Inject a service provider with singleton life cycle.
Definition: dip.hpp:489
iterator begin()
returns an iterator to the beginning
Definition: dip.hpp:429
~instance_set() noexcept
Remove the set of instances providing the service.
Definition: dip.hpp:361
Injected instance of a service.
Definition: dip.hpp:72
static void clear_injection() noexcept
Clear the injected dependency for testing purposes.
Definition: dip.hpp:236
~instance() noexcept
Remove the instance providing the service.
Definition: dip.hpp:101
service_type operator->() const noexcept
Access the instance providing the service.
Definition: dip.hpp:115
Service & operator*() const noexcept
Get the instance providing the service.
Definition: dip.hpp:126
Service * service_type
Type of the injected instances.
Definition: dip.hpp:81
static void inject_transient(_Args &&...__args)
Inject a service provider with transient life cycle.
Definition: dip.hpp:209
const Service * const_service_type
Const type of the injected instances.
Definition: dip.hpp:84
static void inject_singleton(_Args &&...__args)
Inject a service provider with singleton life cycle.
Definition: dip.hpp:159
static void inject(const Injector< Service > &injector) noexcept
Inject a service provider using a custom injector.
Definition: dip.hpp:141
instance()
Retrieve an instance providing the service.
Definition: dip.hpp:90
static void inject_thread_singleton(_Args &&...__args)
Inject a service provider with per-thread singleton life cycle.
Definition: dip.hpp:184