Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkFileWriterRegistry.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 "mitkFileWriterRegistry.h"
18 
19 // MITK
20 #include "mitkBaseData.h"
21 #include "mitkCoreServices.h"
22 #include "mitkIMimeTypeProvider.h"
23 
24 // Microservices
25 #include <usGetModuleContext.h>
26 #include <usLDAPProp.h>
27 #include <usModuleContext.h>
28 #include <usServiceProperties.h>
29 
31 {
32 }
33 
35 {
36  for (auto &elem : m_ServiceObjects)
37  {
38  elem.second.UngetService(elem.first);
39  }
40 }
41 
42 std::vector<mitk::FileWriterRegistry::WriterReference> mitk::FileWriterRegistry::GetReferences(
43  const mitk::BaseData *baseData, us::ModuleContext *context)
44 {
45  return GetReferences(baseData, std::string(), context);
46 }
47 
48 std::vector<mitk::FileWriterRegistry::WriterReference> mitk::FileWriterRegistry::GetReferences(
49  const mitk::BaseData *baseData, const std::string &mimeType, us::ModuleContext *context)
50 {
51  if (baseData == NULL)
52  {
53  mitkThrow() << "FileWriterRegistry::GetReferences was called with null basedata.";
54  std::vector<mitk::FileWriterRegistry::WriterReference> emptyResult;
55  return emptyResult;
56  }
57 
58  if (context == NULL)
59  context = us::GetModuleContext();
60 
61  std::vector<WriterReference> result;
62 
63  // loop over the class hierarchy of baseData and get all writers
64  // claiming to support the actual baseData class or any of its super classes
65  std::vector<std::string> classHierarchy = baseData->GetClassHierarchy();
66  for (std::vector<std::string>::const_iterator clIter = classHierarchy.begin(), clIterEnd = classHierarchy.end();
67  clIter != clIterEnd;
68  ++clIter)
69  {
70  std::string filter =
71  us::LDAPProp(us::ServiceConstants::OBJECTCLASS()) == us_service_interface_iid<IFileWriter>() &&
73  (mimeType.empty() ? us::LDAPPropExpr(std::string()) : us::LDAPProp(IFileWriter::PROP_MIMETYPE()) == mimeType);
74  std::vector<WriterReference> refs = context->GetServiceReferences<IFileWriter>(filter);
75  result.insert(result.end(), refs.begin(), refs.end());
76  }
77  return result;
78 }
79 
81  us::ModuleContext *context)
82 {
83  if (!ref)
84  return NULL;
85 
86  if (context == NULL)
87  context = us::GetModuleContext();
88 
89  us::ServiceObjects<mitk::IFileWriter> serviceObjects = context->GetServiceObjects(ref);
90  mitk::IFileWriter *writer = serviceObjects.GetService();
91  m_ServiceObjects.insert(std::make_pair(writer, serviceObjects));
92  return writer;
93 }
94 
95 std::vector<mitk::IFileWriter *> mitk::FileWriterRegistry::GetWriters(const mitk::BaseData *baseData,
96  const std::string &mimeType,
97  us::ModuleContext *context)
98 {
99  if (baseData == NULL)
100  {
101  mitkThrow() << "FileWriterRegistry::GetReferences was called with null basedata.";
102  std::vector<mitk::IFileWriter *> emptyResult;
103  return emptyResult;
104  }
105 
106  if (context == NULL)
107  context = us::GetModuleContext();
108 
109  std::vector<mitk::IFileWriter *> result;
110 
111  std::vector<us::ServiceReference<IFileWriter>> refs;
112  if (mimeType.empty())
113  {
114  refs = GetReferences(baseData, context);
115  }
116  else
117  {
118  refs = GetReferences(baseData, mimeType, context);
119  }
120  std::sort(refs.begin(), refs.end());
121 
122  result.reserve(refs.size());
123 
124  // Translate List of ServiceRefs to List of Pointers
125  for (std::vector<us::ServiceReference<IFileWriter>>::const_reverse_iterator iter = refs.rbegin(), end = refs.rend();
126  iter != end;
127  ++iter)
128  {
129  us::ServiceObjects<mitk::IFileWriter> serviceObjects = context->GetServiceObjects(*iter);
130  mitk::IFileWriter *writer = serviceObjects.GetService();
131  m_ServiceObjects.insert(std::make_pair(writer, serviceObjects));
132  result.push_back(writer);
133  }
134 
135  return result;
136 }
137 
139 {
140  std::map<mitk::IFileWriter *, us::ServiceObjects<mitk::IFileWriter>>::iterator writerIter =
141  m_ServiceObjects.find(writer);
142  if (writerIter != m_ServiceObjects.end())
143  {
144  writerIter->second.UngetService(writer);
145  m_ServiceObjects.erase(writerIter);
146  }
147 }
148 
149 void mitk::FileWriterRegistry::UngetWriters(const std::vector<mitk::IFileWriter *> &writers)
150 {
151  for (const auto &writer : writers)
152  {
153  this->UngetWriter(writer);
154  }
155 }
void UngetWriter(IFileWriter *writer)
S * GetService() const
Base of all data objects.
Definition: mitkBaseData.h:39
std::vector< IFileWriter * > GetWriters(const BaseData *baseData, const std::string &mimeType, us::ModuleContext *context=us::GetModuleContext())
static std::string PROP_MIMETYPE()
Service property name for the mime-type associated with this file writer.
Definition: mitkIFileIO.cpp:28
virtual std::vector< std::string > GetClassHierarchy() const
Definition: mitkBaseData.h:42
#define mitkThrow()
IFileWriter * GetWriter(const WriterReference &ref, us::ModuleContext *context=us::GetModuleContext())
void UngetWriters(const std::vector< IFileWriter * > &writers)
static std::vector< WriterReference > GetReferences(const BaseData *baseData, us::ModuleContext *context=us::GetModuleContext())
US_Core_EXPORT const std::string & OBJECTCLASS()
static std::string PROP_BASEDATA_TYPE()
Service property name for the supported mitk::BaseData sub-class.
The common interface of all MITK file writers.
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.