Debian Codegraph @

src/Base/ServiceProvider.h

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /****************************************************************************
3 * *
4 * Copyright (c) 2024 Kacper Donat <[email protected]> *
5 * *
6 * This file is part of FreeCAD. *
7 * *
8 * FreeCAD is free software: you can redistribute it and/or modify it *
9 * under the terms of the GNU Lesser General Public License as *
10 * published by the Free Software Foundation, either version 2.1 of the *
11 * License, or (at your option) any later version. *
12 * *
13 * FreeCAD is distributed in the hope that it will be useful, but *
14 * WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16 * Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public *
19 * License along with FreeCAD. If not, see *
20 * <https://www.gnu.org/licenses/>. *
21 * *
22 ***************************************************************************/
23
24 #ifndef APP_SERVICE_PROVIDER_H
25 #define APP_SERVICE_PROVIDER_H
26
27 #include <FCGlobal.h>
28
29 #include <algorithm>
30 #include <deque>
31 #include <map>
32 #include <string>
33 #include <any>
34 #include <list>
35
36 namespace Base
37 {
38
39 /**
40 * Class that implements basic service container that can be used to obtain different implementation
41 * of various services.
42 *
43 * Primary use of such container is to provide ability to define global services that can be
44 * implemented within non-core modules. This for example allows to use code that is available only
45 * in Part module from Base with the only requirement being that Part implements specific interface
46 * and registers the service within service provider.
47 *
48 * For ease of use global service provider instance is provided with convenience functions:
49 * - Base::provideService
50 * - Base::provideServiceImplementations
51 * - Base::registerServiceImplementation
52 *
53 * As the example, we can define service that provides placement of sub objects in App:
54 * @code
55 * class SubObjectPlacementProvider
56 * {
57 * public:
58 * virtual Base::Placement calculate(SubObjectT object, Base::Placement basePlacement) const =
59 * 0;
60 * };
61 * @endcode
62 *
63 * App does not know how to implement this service, but it can be implemented within Part module:
64 * @code
65 * class AttacherSubObjectPlacement final: public App::SubObjectPlacementProvider { ... }
66 *
67 * // later in module initialization method
68 *
69 * Base::registerServiceImplementation<App::SubObjectPlacementProvider>(new
70 * AttacherSubObjectPlacement);
71 * @endcode
72 *
73 * This service can then be obtained inside other modules, without them being aware of the
74 * implementation - only the interface:
75 *
76 * @code
77 * auto subObjectPlacementProvider = Base::provideService<App::SubObjectPlacementProvider>();
78 * @endcode
79 *
80 * This function can (and should) be used as default for constructor injection of services.
81 */
82 class BaseExport ServiceProvider
83 {
84 struct ServiceDescriptor
85 {
86 std::string name;
87 std::any instance;
88
89 template<typename T>
90 T* get() const
91 {
92 return std::any_cast<T*>(instance);
93 }
94 };
95
96 public:
97 ServiceProvider() = default;
98
99 /**
100 * Returns most recent implementation of service specified as T param.
101 *
102 * @tparam T Service interface
103 */
104 template<typename T>
105 T* provide() const
106 {
107 if (auto it = _implementations.find(typeid(T).name()); it != _implementations.end()) {
108 auto descriptors = it->second;
109
110 if (descriptors.empty()) {
111 return nullptr;
112 }
113
114 return descriptors.front().get<T>();
115 }
116
117 return nullptr;
118 }
119
120 /**
121 * Returns all implementations of service specified as T param.
122 *
123 * @tparam T Service interface
124 */
125 template<typename T>
126 std::list<T*> all() const
127 {
128 if (auto it = _implementations.find(typeid(T).name()); it != _implementations.end()) {
129 auto source = it->second;
130
131 std::list<T*> result(source.size());
132
133 std::transform(
134 source.begin(),
135 source.end(),
136 result.begin(),
137 [](const ServiceDescriptor& descriptor) { return descriptor.get<T>(); }
138 );
139
140 return result;
141 }
142
143 return {};
144 }
145
146 /**
147 * Adds new implementation of service T.
148 *
149 * @tparam T Service interface
150 */
151 template<typename T>
152 void registerImplementation(T* contract)
153 {
154 ServiceDescriptor descriptor {typeid(T).name(), contract};
155
156 _implementations[typeid(T).name()].push_front(descriptor);
157 }
158
159 private:
160 std::map<std::string, std::deque<ServiceDescriptor>> _implementations;
161 };
162
163 BaseExport extern ServiceProvider globalServiceProvider;
164
165 /**
166 * Obtains primary implementation of requested service from the global service provider.
167 *
168 * @tparam T Service kind to obtain.
169 * @return Primary implementation of the service or nullptr if there is no implementation available.
170 */
171 template<typename T>
172 T* provideService()
173 {
174 return globalServiceProvider.provide<T>();
175 }
176
177 /**
178 * Obtains all available implementations of requested service in the global service provider.
179 *
180 * @tparam T Service kind to obtain.
181 * @return List of available service implementation.
182 */
183 template<typename T>
184 std::list<T*> provideServiceImplementations()
185 {
186 return globalServiceProvider.all<T>();
187 }
188
189 /**
190 * Registers implementation of service in the global service provider.
191 *
192 * @tparam T Service kind to obtain.
193 * @return List of available service implementation.
194 */
195 template<typename T>
196 void registerServiceImplementation(T* implementation)
197 {
198 globalServiceProvider.registerImplementation<T>(implementation);
199 }
200
201 } // namespace Base
202
203
204 #endif // APP_SERVICE_PROVIDER_H