solinject  1.0.0
C++17 Dependency Injection header-only library
TransientService.hpp
Go to the documentation of this file.
1 // SPDX-License-Identifier: LGPL-3.0-or-later
2 
3 /*
4  * solinject - C++ Dependency Injection header-only library
5  * Copyright (C) 2022 SemperSolus0x3d
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this program. If not, see <https://www.gnu.org/licenses/>.
19  */
20 
22 
23 #pragma once
24 #include <type_traits>
25 #include "solinject/Defines.hpp"
26 #include "ServiceBase.hpp"
27 
28 namespace sol::di::impl
29 {
34  template<class TService, class...TServiceParents>
36  public ServiceBase<TService>,
37  public ServiceBase<TServiceParents>...
38  {
39  static_assert(
40  std::conjunction_v<std::is_base_of<TServiceParents, TService>...>,
41  "The TServiceParents types must be derived from the TService type"
42  );
43  public:
46 
48  using Container = typename Base::Container;
49 
51  using ServicePtr = typename Base::ServicePtr;
52 
54  using Factory = typename Base::Factory;
55 
57  using VoidPtr = typename IService::VoidPtr;
58 
63  TransientService(Factory factory) : m_Factory(factory)
64  {
65  }
66 
67  protected:
68  virtual VoidPtr GetServiceAsVoidPtr(const Container& container) override
69  {
70  auto service = m_Factory(container);
71 
72  solinject_req_assert(service != nullptr && "Factory should never return nullptr");
73 
74  return std::static_pointer_cast<void>(service);
75  }
76 
77  private:
79  Factory m_Factory;
80  }; // class TransientService
81 } // sol::di::impl
std::shared_ptr
sol::di::Container
Dependency Injection container.
Definition: Container.hpp:48
sol::di::impl::ServiceBase< TService >::Container
typename Base::Container Container
DI container.
Definition: ServiceBase.hpp:48
sol::di::impl::TransientService::TransientService
TransientService(Factory factory)
Constructor.
Definition: TransientService.hpp:63
solinject_req_assert
#define solinject_req_assert(expression)
Required assert, which is disabled only when the assert() macro from assert.h is disabled.
Definition: Defines.hpp:41
sol::di::impl::TransientService::GetServiceAsVoidPtr
virtual VoidPtr GetServiceAsVoidPtr(const Container &container) override
Resolves a service.
Definition: TransientService.hpp:68
sol::di::impl::TransientService
Transient DI service.
Definition: TransientService.hpp:35
sol::di::impl::ServiceBase< TService >::ServicePtr
typename Base::ServicePtr ServicePtr
Pointer to an instance of a service.
Definition: ServiceBase.hpp:42
sol::di::impl::TransientService::Factory
typename Base::Factory Factory
Factory function that accepts a reference to a DI container and returns a pointer to an instance of a...
Definition: TransientService.hpp:54
sol::di::impl::ServiceBase
Base for the DI service classes.
Definition: ServiceBase.hpp:35
sol::di::impl::IService::VoidPtr
std::shared_ptr< void > VoidPtr
Pointer to void.
Definition: IService.hpp:38
ServiceBase.hpp
sol::di::impl::TransientService::ServicePtr
typename Base::ServicePtr ServicePtr
Pointer to an instance of a service.
Definition: TransientService.hpp:51
sol::di::impl::ServiceBase< TService >::Factory
typename Base::Factory Factory
Factory function that accepts a reference to a DI container and returns a pointer to an instance of a...
Definition: ServiceBase.hpp:45
Defines.hpp
std::is_base_of