solinject  1.0.0
C++17 Dependency Injection header-only library
ScopedServiceBuilders.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 
25 #include <map>
26 #include <vector>
27 #include <algorithm>
28 #include <memory>
29 #include <typeinfo>
30 #include <typeindex>
31 #include <iterator>
32 
33 #include "IService.hpp"
34 #include "IServiceTyped.hpp"
35 #include "ScopedServiceBuilder.hpp"
36 
37 namespace sol::di::impl
38 {
41  {
42  public:
47  template <class T>
49 
54  template <class T>
56 
59 
62 
65 
68 
74  template<class T>
76  {
78  std::type_index(typeid(T)),
80  );
81  }
82 
89  {
90  m_RegisteredServiceBuilders[type].push_back(serviceBuilder);
91  }
92 
98  {
99  RegisteredServicesMap result;
100 
101  for (const auto& pair : m_RegisteredServiceBuilders)
102  {
103  auto& serviceBuilders = pair.second;
104 
105  std::vector<DIServicePtr> builtServices;
106  builtServices.reserve(serviceBuilders.size());
107 
109  serviceBuilders.begin(),
110  serviceBuilders.end(),
111  std::back_inserter(builtServices),
112  [](auto& builder)
113  {
114  return builder->BuildDIService();
115  }
116  );
117 
118  result[pair.first] = builtServices;
119  }
120 
121  return result;
122  }
123 
124  private:
126  RegisteredServiceBuildersMap m_RegisteredServiceBuilders;
127  };
128 }
sol::di::impl::ScopedServiceBuilders::BuildDIServices
RegisteredServicesMap BuildDIServices() const
Builds DI services.
Definition: ScopedServiceBuilders.hpp:97
sol::di::impl::ScopedServiceBuilders::RegisterScopedService
void RegisterScopedService(std::type_index type, ScopedServiceBuilderPtr serviceBuilder)
Registers a scoped service.
Definition: ScopedServiceBuilders.hpp:88
std::shared_ptr
sol::di::impl::ScopedServiceBuilders::RegisterScopedService
void RegisterScopedService(Factory< T > factory)
Registers a scoped service.
Definition: ScopedServiceBuilders.hpp:75
sol::di::impl::ScopedServiceBuilders::ServicePtr
typename IServiceTyped< T >::ServicePtr ServicePtr
Pointer to an instance of a service.
Definition: ScopedServiceBuilders.hpp:55
std::vector::reserve
T reserve(T... args)
std::make_shared
T make_shared(T... args)
std::vector
ScopedServiceBuilder.hpp
std::back_inserter
T back_inserter(T... args)
sol::di::impl::ScopedServiceBuilders
Scoped DI service builders collection.
Definition: ScopedServiceBuilders.hpp:40
std::type_index
sol::di::impl::ScopedServiceBuilders::RegisteredServiceBuildersMap
std::map< std::type_index, std::vector< ScopedServiceBuilderPtr > > RegisteredServiceBuildersMap
Map of registered DI service builders.
Definition: ScopedServiceBuilders.hpp:67
IServiceTyped.hpp
std::map< std::type_index, std::vector< DIServicePtr > >
std::transform
T transform(T... args)
sol::di::impl::IServiceTyped::ServicePtr
typename std::shared_ptr< T > ServicePtr
Pointer to an instance of a service.
Definition: IServiceTyped.hpp:36
sol::di::impl::IServiceTyped::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
IService.hpp
sol::di::impl::ScopedServiceBuilder
Builder for scoped DI services.
Definition: ScopedServiceBuilder.hpp:33
sol::di::impl::ScopedServiceBuilders::Factory
typename IServiceTyped< T >::Factory Factory
Factory function that accepts a reference to a DI container and returns a pointer to an instance of a...
Definition: ScopedServiceBuilders.hpp:48