Medical Imaging Interaction Toolkit  2018.4.99-1640525a
Medical Imaging Interaction Toolkit
mitkCoreServices.cpp
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 #include "mitkCoreServices.h"
14 
15 #include <mitkIMimeTypeProvider.h>
16 #include <mitkIPropertyAliases.h>
19 #include <mitkIPropertyFilters.h>
21 #include <mitkIPropertyRelations.h>
22 
23 #include <usGetModuleContext.h>
24 #include <usModuleContext.h>
25 #include <usServiceReference.h>
26 #include <usServiceTracker.h>
27 
28 #include <itkMutexLockHolder.h>
29 #include <itkSimpleFastMutexLock.h>
30 
31 namespace mitk
32 {
33  itk::SimpleFastMutexLock &s_ContextToServicesMapMutex()
34  {
35  static itk::SimpleFastMutexLock mutex;
36  return mutex;
37  }
38 
39  std::map<us::ModuleContext *, std::map<void *, us::ServiceReferenceU>> &s_ContextToServicesMap()
40  {
41  static std::map<us::ModuleContext *, std::map<void *, us::ServiceReferenceU>> serviceMap;
42  return serviceMap;
43  }
44 
45  template <class S>
46  static S *GetCoreService(us::ModuleContext *context)
47  {
48  if (context == nullptr)
49  context = us::GetModuleContext();
50 
51  S *coreService = nullptr;
52  us::ServiceReference<S> serviceRef = context->GetServiceReference<S>();
53  if (serviceRef)
54  {
55  coreService = context->GetService(serviceRef);
56  }
57 
58  assert(coreService && "Asserting non-nullptr MITK core service");
59  {
61  s_ContextToServicesMap()[context].insert(std::make_pair(coreService, serviceRef));
62  }
63 
64  return coreService;
65  }
66 
68  {
69  return GetCoreService<IPropertyAliases>(context);
70  }
71 
73  {
74  return GetCoreService<IPropertyDescriptions>(context);
75  }
76 
78  {
79  return GetCoreService<IPropertyExtensions>(context);
80  }
81 
83  {
84  return GetCoreService<IPropertyFilters>(context);
85  }
86 
88  {
89  return GetCoreService<IPropertyPersistence>(context);
90  }
91 
93  {
94  return GetCoreService<IPropertyRelations>(context);
95  }
96 
98  {
99  return GetCoreService<IMimeTypeProvider>(context);
100  }
101 
102  bool CoreServices::Unget(us::ModuleContext *context, const std::string & /*interfaceId*/, void *service)
103  {
104  bool success = false;
105 
107  auto iter =
108  s_ContextToServicesMap().find(context);
109  if (iter != s_ContextToServicesMap().end())
110  {
111  auto iter2 = iter->second.find(service);
112  if (iter2 != iter->second.end())
113  {
114  us::ServiceReferenceU serviceRef = iter2->second;
115  if (serviceRef)
116  {
117  success = context->UngetService(serviceRef);
118  if (success)
119  {
120  iter->second.erase(iter2);
121  }
122  }
123  }
124  }
125 
126  return success;
127  }
128 }
Interface of property relations service.
Interface of property aliases service.
static IMimeTypeProvider * GetMimeTypeProvider(us::ModuleContext *context=us::GetModuleContext())
Get an IMimeTypeProvider instance.
static IPropertyExtensions * GetPropertyExtensions(us::ModuleContext *context=us::GetModuleContext())
Get an IPropertyExtensions instance.
DataCollection - Class to facilitate loading/accessing structured data.
Interface of property extensions service.
static IPropertyDescriptions * GetPropertyDescriptions(us::ModuleContext *context=us::GetModuleContext())
Get an IPropertyDescriptions instance.
Interface of property descriptions service.
static IPropertyRelations * GetPropertyRelations(us::ModuleContext *context=us::GetModuleContext())
Get an IPropertyRelations instance.
static IPropertyPersistence * GetPropertyPersistence(us::ModuleContext *context=us::GetModuleContext())
Get an IPropertyPersistence instance.
The IMimeTypeProvider service interface allows to query all registered mime types.
std::map< us::ModuleContext *, std::map< void *, us::ServiceReferenceU > > & s_ContextToServicesMap()
static S * GetCoreService(us::ModuleContext *context)
Interface of property persistence service.
Interface of property filters service.
itk::SimpleFastMutexLock & s_ContextToServicesMapMutex()
static IPropertyFilters * GetPropertyFilters(us::ModuleContext *context=us::GetModuleContext())
Get an IPropertyFilters instance.
static IPropertyAliases * GetPropertyAliases(us::ModuleContext *context=us::GetModuleContext())
Get an IPropertyAliases instance.
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.
static bool Unget(S *service, us::ModuleContext *context=us::GetModuleContext())
Unget a previously acquired service instance.