solinject  1.0.0
C++17 Dependency Injection header-only library
SharedService.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>
34  class SharedService :
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 
57 
59  using VoidPtr = typename IService::VoidPtr;
60 
65  SharedService(Factory factory) : m_Factory(factory), m_ServicePtr()
66  {
67  }
68 
69  protected:
70  virtual VoidPtr GetServiceAsVoidPtr(const Container& container) override
71  {
72  ServicePtr instancePtr = m_ServicePtr.lock();
73 
74  if (instancePtr != nullptr)
75  {
76  return std::static_pointer_cast<void>(instancePtr);
77  }
78  else
79  {
80  ServicePtr newInstancePtr = m_Factory(container);
81 
82  solinject_req_assert(newInstancePtr != nullptr && "Factory should never return nullptr");
83 
84  m_ServicePtr = ServiceWeakPtr(newInstancePtr);
85  return std::static_pointer_cast<void>(newInstancePtr);
86  }
87  }
88 
89  private:
91  ServiceWeakPtr m_ServicePtr;
92 
94  Factory m_Factory;
95  }; // class SharedService
96 } // sol::di::impl
std::weak_ptr::lock
T lock(T... args)
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::SharedService::ServicePtr
typename Base::ServicePtr ServicePtr
Pointer to an instance of a service.
Definition: SharedService.hpp:50
sol::di::impl::SharedService
Shared DI service.
Definition: SharedService.hpp:34
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::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::SharedService::ServiceWeakPtr
std::weak_ptr< TService > ServiceWeakPtr
std::weak_ptr to a service instance
Definition: SharedService.hpp:56
sol::di::impl::IService::VoidPtr
std::shared_ptr< void > VoidPtr
Pointer to void.
Definition: IService.hpp:38
ServiceBase.hpp
std::weak_ptr< TService >
sol::di::impl::SharedService::GetServiceAsVoidPtr
virtual VoidPtr GetServiceAsVoidPtr(const Container &container) override
Resolves a service.
Definition: SharedService.hpp:70
sol::di::impl::SharedService::SharedService
SharedService(Factory factory)
Constructor.
Definition: SharedService.hpp:65
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
sol::di::impl::SharedService::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: SharedService.hpp:53
Defines.hpp
std::is_base_of