Medical Imaging Interaction Toolkit  2016.11.0
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,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #include "mitkCoreServices.h"
18 
19 #include <mitkIMimeTypeProvider.h>
20 #include <mitkIPropertyAliases.h>
23 #include <mitkIPropertyFilters.h>
25 #include <mitkIShaderRepository.h>
26 
27 #include <usGetModuleContext.h>
28 #include <usModuleContext.h>
29 #include <usServiceReference.h>
30 #include <usServiceTracker.h>
31 
32 #include <itkMutexLockHolder.h>
33 #include <itkSimpleFastMutexLock.h>
34 
35 namespace mitk
36 {
37  itk::SimpleFastMutexLock &s_ContextToServicesMapMutex()
38  {
39  static itk::SimpleFastMutexLock mutex;
40  return mutex;
41  }
42 
43  std::map<us::ModuleContext *, std::map<void *, us::ServiceReferenceU>> &s_ContextToServicesMap()
44  {
45  static std::map<us::ModuleContext *, std::map<void *, us::ServiceReferenceU>> serviceMap;
46  return serviceMap;
47  }
48 
49  template <class S>
50  static S *GetCoreService(us::ModuleContext *context)
51  {
52  if (context == NULL)
53  context = us::GetModuleContext();
54 
55  S *coreService = NULL;
56  us::ServiceReference<S> serviceRef = context->GetServiceReference<S>();
57  if (serviceRef)
58  {
59  coreService = context->GetService(serviceRef);
60  }
61 
62  assert(coreService && "Asserting non-NULL MITK core service");
63  {
65  s_ContextToServicesMap()[context].insert(std::make_pair(coreService, serviceRef));
66  }
67 
68  return coreService;
69  }
70 
72  {
74  tracker.Open();
75  return tracker.GetService();
76  }
77 
79  {
80  return GetCoreService<IPropertyAliases>(context);
81  }
82 
84  {
85  return GetCoreService<IPropertyDescriptions>(context);
86  }
87 
89  {
90  return GetCoreService<IPropertyExtensions>(context);
91  }
92 
94  {
95  return GetCoreService<IPropertyFilters>(context);
96  }
97 
99  {
100  return GetCoreService<IPropertyPersistence>(context);
101  }
102 
104  {
105  return GetCoreService<IMimeTypeProvider>(context);
106  }
107 
108  bool CoreServices::Unget(us::ModuleContext *context, const std::string & /*interfaceId*/, void *service)
109  {
110  bool success = false;
111 
113  std::map<us::ModuleContext *, std::map<void *, us::ServiceReferenceU>>::iterator iter =
114  s_ContextToServicesMap().find(context);
115  if (iter != s_ContextToServicesMap().end())
116  {
117  std::map<void *, us::ServiceReferenceU>::iterator iter2 = iter->second.find(service);
118  if (iter2 != iter->second.end())
119  {
120  us::ServiceReferenceU serviceRef = iter2->second;
121  if (serviceRef)
122  {
123  success = context->UngetService(serviceRef);
124  if (success)
125  {
126  iter->second.erase(iter2);
127  }
128  }
129  }
130  }
131 
132  return success;
133  }
134 }
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.
static IShaderRepository * GetShaderRepository()
Get an IShaderRepository instance.
DataCollection - Class to facilitate loading/accessing structured data.
Interface of property extensions service.
virtual T GetService(const ServiceReferenceType &reference) const
static IPropertyDescriptions * GetPropertyDescriptions(us::ModuleContext *context=us::GetModuleContext())
Get an IPropertyDescriptions instance.
Interface of property descriptions service.
Management class for vtkShader XML descriptions.
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)
virtual void Open()
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.