Medical Imaging Interaction Toolkit  2026.06.00
Medical Imaging Interaction Toolkit
usServiceInterface.h
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 
14 #ifndef USSERVICEINTERFACE_H
15 #define USSERVICEINTERFACE_H
16 
17 #include <usGlobalConfig.h>
18 #include <usServiceException.h>
19 
20 #include <map>
21 #include <string>
22 #include <typeinfo>
23 
24 namespace us {
25 
33 std::string GetDemangledName(const std::type_info& typeInfo);
34 }
35 
49 template<class T> std::string us_service_interface_iid()
50 {
51  return us::GetDemangledName(typeid(T));
52 }
53 
55 template<> inline std::string us_service_interface_iid<void>() { return std::string(); }
57 
58 
103 #define US_DECLARE_SERVICE_INTERFACE(_service_interface_type, _service_interface_id) \
104  template<> inline std::string us_service_interface_iid<_service_interface_type>() \
105  { return _service_interface_id; } \
106 
107 
108 namespace us {
109 
110 class ServiceFactory;
111 
118 template<class Interface>
119 struct InterfaceType {};
120 
139 typedef std::map<std::string, void*> InterfaceMap;
140 
141 
142 template<class I>
144 {
145  if (us_service_interface_iid<I>().empty())
146  {
147  throw ServiceException(std::string("The interface class ") + typeid(I).name() +
148  " uses an invalid id in its US_DECLARE_SERVICE_INTERFACE macro call.");
149  }
150  im.insert(std::make_pair(std::string(us_service_interface_iid<I>()),
151  static_cast<void*>(static_cast<I*>(i))));
152  return true;
153 }
154 
155 template<>
157 {
158  return false;
159 }
160 
161 
179 template<class I1, class I2 = void, class I3 = void>
181 {
186 
193  template<class Impl>
194  MakeInterfaceMap(Impl* impl)
195  : m_factory(nullptr)
196  , m_interface1(static_cast<I1*>(impl))
197  , m_interface2(static_cast<I2*>(impl))
198  , m_interface3(static_cast<I3*>(impl))
199  {}
200 
207  : m_factory(factory)
208  , m_interface1(nullptr)
209  , m_interface2(nullptr)
210  , m_interface3(nullptr)
211  {
212  if (factory == nullptr)
213  {
214  throw ServiceException("The service factory argument must not be nullptr.");
215  }
216  }
217 
219  operator InterfaceMap ()
220  {
221  InterfaceMap sim;
225 
226  if (m_factory)
227  {
228  sim.insert(std::make_pair(std::string("org.cppmicroservices.factory"),
229  static_cast<void*>(m_factory)));
230  }
231 
232  return sim;
233  }
234 };
235 
237 template<class I1, class I2>
238 struct MakeInterfaceMap<I1,I2,void>
239 {
240  ServiceFactory* m_factory;
241  I1* m_interface1;
242  I2* m_interface2;
243 
244  template<class Impl>
245  MakeInterfaceMap(Impl* impl)
246  : m_factory(nullptr)
247  , m_interface1(static_cast<I1*>(impl))
248  , m_interface2(static_cast<I2*>(impl))
249  {}
250 
251  MakeInterfaceMap(ServiceFactory* factory)
252  : m_factory(factory)
253  , m_interface1(nullptr)
254  , m_interface2(nullptr)
255  {
256  if (factory == nullptr)
257  {
258  throw ServiceException("The service factory argument must not be nullptr.");
259  }
260  }
261 
262  operator InterfaceMap ()
263  {
264  InterfaceMap sim;
267 
268  if (m_factory)
269  {
270  sim.insert(std::make_pair(std::string("org.cppmicroservices.factory"),
271  static_cast<void*>(m_factory)));
272  }
273 
274  return sim;
275  }
276 };
277 
278 template<class I1>
279 struct MakeInterfaceMap<I1,void,void>
280 {
281  ServiceFactory* m_factory;
282  I1* m_interface1;
283 
284  template<class Impl>
285  MakeInterfaceMap(Impl* impl)
286  : m_factory(nullptr)
287  , m_interface1(static_cast<I1*>(impl))
288  {}
289 
290  MakeInterfaceMap(ServiceFactory* factory)
291  : m_factory(factory)
292  , m_interface1(nullptr)
293  {
294  if (factory == nullptr)
295  {
296  throw ServiceException("The service factory argument must not be nullptr.");
297  }
298  }
299 
300  operator InterfaceMap ()
301  {
302  InterfaceMap sim;
304 
305  if (m_factory)
306  {
307  sim.insert(std::make_pair(std::string("org.cppmicroservices.factory"),
308  static_cast<void*>(m_factory)));
309  }
310 
311  return sim;
312  }
313 };
314 
315 template<>
316 struct MakeInterfaceMap<void,void,void>;
318 
331 template<class I1>
333 {
334  InterfaceMap::const_iterator iter = map.find(us_service_interface_iid<I1>());
335  if (iter != map.end())
336  {
337  return reinterpret_cast<I1*>(iter->second);
338  }
339  return nullptr;
340 }
341 
342 }
343 
344 
345 #endif // USSERVICEINTERFACE_H
I1 * ExtractInterface(const InterfaceMap &map)
Extract a service interface pointer from a given InterfaceMap instance.
std::map< std::string, void * > InterfaceMap
A map containing interfaces ids and their corresponding service object pointers.
std::string GetDemangledName(const std::type_info &typeInfo)
Returns the demangled name for the given type_info.
std::string us_service_interface_iid()
Returns a unique id for a given type. By default, the demangled name of T is returned.
bool InsertInterfaceType< void >(InterfaceMap &, void *)
bool InsertInterfaceType(InterfaceMap &im, I *i)
A helper type used in several methods to get proper method overload resolutions.
Helper class for constructing InterfaceMap instances based on service implementations or service fact...
ServiceFactory * m_factory
MakeInterfaceMap(ServiceFactory *factory)
Constructor taking a service factory.
MakeInterfaceMap(Impl *impl)
Constructor taking a service implementation pointer.