solinject  1.0.0
C++17 Dependency Injection header-only library
ServiceBase.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 "IServiceTyped.hpp"
27 
28 namespace sol::di::impl
29 {
34  template <class T>
35  class ServiceBase : public IServiceTyped<T>
36  {
37  public:
40 
42  using ServicePtr = typename Base::ServicePtr;
43 
45  using Factory = typename Base::Factory;
46 
48  using Container = typename Base::Container;
49 
50  virtual ~ServiceBase() = 0;
51 
58  virtual ServicePtr GetService(const Container& container)
59  {
60  solinject_assert(!m_IsLocked && "There are no circular dependencies");
61 
62  if (m_IsLocked)
63  {
64  m_IsLocked = false;
65  throw exc::CircularDependencyException(typeid(T));
66  }
67 
68  m_IsLocked = true;
69  auto service = this->GetServiceAsVoidPtr(container);
70  m_IsLocked = false;
71 
72  solinject_req_assert(service != nullptr);
73 
74  return std::static_pointer_cast<T>(service);
75  }
76 
77  protected:
86  bool m_IsLocked = false;
87  };
88 
89  template <class T>
91 }
CircularDependencyException.hpp
solinject_assert
#define solinject_assert(expression)
assert() macro, which is disabled in tests project.
Definition: Defines.hpp:34
sol::di::Container
Dependency Injection container.
Definition: Container.hpp:48
sol::di::impl::IServiceTyped
DI service interface.
Definition: IServiceTyped.hpp:32
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::IService::Container
sol::di::Container Container
DI container.
Definition: IService.hpp:35
sol::di::impl::ServiceBase
Base for the DI service classes.
Definition: ServiceBase.hpp:35
IServiceTyped.hpp
sol::di::impl::IServiceTyped< TServiceParents >::ServicePtr
typename std::shared_ptr< TServiceParents > ServicePtr
Pointer to an instance of a service.
Definition: IServiceTyped.hpp:36
sol::di::impl::ServiceBase::m_IsLocked
bool m_IsLocked
Field that indicates if the DI service is "locked".
Definition: ServiceBase.hpp:86
sol::di::impl::IService::GetServiceAsVoidPtr
virtual VoidPtr GetServiceAsVoidPtr(const Container &container)=0
Resolves a service.
sol::di::impl::ServiceBase::GetService
virtual ServicePtr GetService(const Container &container)
Resolves the service and checks for circular dependencies.
Definition: ServiceBase.hpp:58
sol::di::impl::IServiceTyped< TServiceParents >::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::exc::CircularDependencyException
Exception that is thrown when a circular dependency is detected.
Definition: CircularDependencyException.hpp:30
Defines.hpp