solinject  1.0.0
C++17 Dependency Injection header-only library
Macros.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 
46 #define FROM_DI(class_) (c.template GetRequiredService<class_>())
47 
71 #define FROM_DI_OPTIONAL(class_) (c.template GetService<class_>())
72 
92 #define FROM_DI_MULTIPLE(class_) (c.template GetServices<class_>())
93 
99 #define FACTORY(class_, ...) \
100  [](const sol::di::Container& c) \
101  { \
102  return std::make_shared<class_>(__VA_ARGS__); \
103  }
104 
118 #define RegisterSingletonService(container, class_, ...) \
119  (container).template RegisterSingletonService<class_>(FACTORY(class_, __VA_ARGS__))
120 
136 #define RegisterSingletonInterface(container, interface_, implementation, ...) \
137  (container).template RegisterSingletonService<interface_>(FACTORY(implementation, __VA_ARGS__))
138 
152 #define RegisterTransientService(container, class_, ...) \
153  (container).template RegisterTransientService<class_>(FACTORY(class_, __VA_ARGS__))
154 
170 #define RegisterTransientInterface(container, interface_, implementation, ...) \
171  (container).template RegisterTransientService<interface_>(FACTORY(implementation, __VA_ARGS__))
172 
189 #define RegisterSharedService(container, class_, ...) \
190  (container).template RegisterSharedService<class_>(FACTORY(class_, __VA_ARGS__))
191 
210 #define RegisterSharedInterface(container, interface_, implementation, ...) \
211  (container).template RegisterSharedService<interface_>(FACTORY(implementation, __VA_ARGS__))
212 
226 #define RegisterScopedService(container, class_, ...) \
227  (container).template RegisterScopedService<class_>(FACTORY(class_, __VA_ARGS__))
228 
244 #define RegisterScopedInterface(container, interface_, implementation, ...) \
245  (container).template RegisterScopedService<interface_>(FACTORY(implementation, __VA_ARGS__))