solinject  1.0.0
C++17 Dependency Injection header-only library
SingletonService.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 "solinject/Defines.hpp"
25 #include "ServiceBase.hpp"
26 
27 namespace sol::di::impl
28 {
33  template<class TService, class...TServiceParents>
35  public ServiceBase<TService>,
36  public ServiceBase<TServiceParents>...
37  {
38  static_assert(
39  std::conjunction_v<std::is_base_of<TServiceParents, TService>...>,
40  "The TServiceParents types must be derived from the TService type"
41  );
42  public:
45 
47  using Container = typename Base::Container;
48 
50  using ServicePtr = typename Base::ServicePtr;
51 
53  using Factory = typename Base::Factory;
54 
56  using VoidPtr = typename IService::VoidPtr;
57 
62  SingletonService(ServicePtr service) : m_ServicePtr(service)
63  {
64  }
65 
70  SingletonService(Factory factory) : m_Factory(factory), m_ServicePtr(nullptr)
71  {
72  }
73 
74  virtual ~SingletonService() {}
75 
76  protected:
78  virtual VoidPtr GetServiceAsVoidPtr(const Container& container) override
79  {
80  if (m_ServicePtr == nullptr)
81  {
82  m_ServicePtr = m_Factory(container);
83  solinject_req_assert(m_ServicePtr != nullptr && "Factory should never return nullptr");
84  }
85 
86  return std::static_pointer_cast<void>(m_ServicePtr);
87  }
88 
89  private:
91  ServicePtr m_ServicePtr;
92 
94  Factory m_Factory;
95  }; // class SingletonService
96 } // 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::IServiceTyped< TService >
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::SingletonService::ServicePtr
typename Base::ServicePtr ServicePtr
Pointer to an instance of a service.
Definition: SingletonService.hpp:50
sol::di::impl::SingletonService
Singleton DI service.
Definition: SingletonService.hpp:34
sol::di::impl::ServiceBase< TService >::ServicePtr
typename Base::ServicePtr ServicePtr
Pointer to an instance of a service.
Definition: ServiceBase.hpp:42
sol::di::impl::ServiceBase
Base for the DI service classes.
Definition: ServiceBase.hpp:35
sol::di::impl::SingletonService::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: SingletonService.hpp:53
sol::di::impl::SingletonService::SingletonService
SingletonService(ServicePtr service)
Constructor.
Definition: SingletonService.hpp:62
sol::di::impl::IService::VoidPtr
std::shared_ptr< void > VoidPtr
Pointer to void.
Definition: IService.hpp:38
ServiceBase.hpp
sol::di::impl::IServiceTyped< TService >::ServicePtr
typename std::shared_ptr< TService > ServicePtr
Pointer to an instance of a service.
Definition: IServiceTyped.hpp:36
sol::di::impl::IServiceTyped< TService >::Factory
typename std::function< ServicePtr(const Container &)> Factory
Factory function that accepts a reference to a DI container and returns a pointer to an instance of a...
Definition: IServiceTyped.hpp:42
sol::di::impl::SingletonService::GetServiceAsVoidPtr
virtual VoidPtr GetServiceAsVoidPtr(const Container &container) override
Resolves a service.
Definition: SingletonService.hpp:78
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
sol::di::impl::SingletonService::SingletonService
SingletonService(Factory factory)
Constructor.
Definition: SingletonService.hpp:70
std::is_base_of