Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
usServiceObjects.cpp
Go to the documentation of this file.
1 /*=============================================================================
2 
3  Library: CppMicroServices
4 
5  Copyright (c) German Cancer Research Center,
6  Division of Medical and Biological Informatics
7 
8  Licensed under the Apache License, Version 2.0 (the "License");
9  you may not use this file except in compliance with the License.
10  You may obtain a copy of the License at
11 
12  http://www.apache.org/licenses/LICENSE-2.0
13 
14  Unless required by applicable law or agreed to in writing, software
15  distributed under the License is distributed on an "AS IS" BASIS,
16  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  See the License for the specific language governing permissions and
18  limitations under the License.
19 
20 =============================================================================*/
21 
22 #include "usServiceObjects.h"
23 
24 #include "usServiceReferenceBasePrivate.h"
25 
26 #include <set>
27 
28 US_BEGIN_NAMESPACE
29 
30 class ServiceObjectsBasePrivate
31 {
32 public:
33 
34  AtomicInt ref;
35 
36  ModuleContext* m_context;
37  ServiceReferenceBase m_reference;
38 
39  // This is used by all ServiceObjects<S> instances with S != void
40  std::map<void*, InterfaceMap> m_serviceInstances;
41  // This is used by ServiceObjects<void>
42  std::set<InterfaceMap> m_serviceInterfaceMaps;
43 
44  ServiceObjectsBasePrivate(ModuleContext* context, const ServiceReferenceBase& reference)
45  : m_context(context)
46  , m_reference(reference)
47  {}
48 
49  InterfaceMap GetServiceInterfaceMap()
50  {
51  InterfaceMap result;
52 
53  bool isPrototypeScope = m_reference.GetProperty(ServiceConstants::SERVICE_SCOPE()).ToString() ==
55 
56  if (isPrototypeScope)
57  {
58  result = m_reference.d->GetPrototypeService(m_context->GetModule());
59  }
60  else
61  {
62  result = m_reference.d->GetServiceInterfaceMap(m_context->GetModule());
63  }
64 
65  return result;
66  }
67 };
68 
69 ServiceObjectsBase::ServiceObjectsBase(ModuleContext* context, const ServiceReferenceBase& reference)
70  : d(new ServiceObjectsBasePrivate(context, reference))
71 {
72  if (!reference)
73  {
74  delete d;
75  throw std::invalid_argument("The service reference is invalid");
76  }
77  d->ref.Ref();
78 }
79 
81 {
82  if (!d->m_reference)
83  {
84  return NULL;
85  }
86 
87  InterfaceMap im = d->GetServiceInterfaceMap();
88  void* result = im.find(d->m_reference.GetInterfaceId())->second;
89 
90  if (result)
91  {
92  d->m_serviceInstances.insert(std::make_pair(result, im));
93  }
94  return result;
95 }
96 
98 {
99  InterfaceMap result;
100  if (!d->m_reference)
101  {
102  return result;
103  }
104 
105  result = d->GetServiceInterfaceMap();
106 
107  if (!result.empty())
108  {
109  d->m_serviceInterfaceMaps.insert(result);
110  }
111  return result;
112 }
113 
115 {
116  if (service == NULL)
117  {
118  return;
119  }
120 
121  std::map<void*,InterfaceMap>::iterator serviceIter = d->m_serviceInstances.find(service);
122  if (serviceIter == d->m_serviceInstances.end())
123  {
124  throw std::invalid_argument("The provided service has not been retrieved via this ServiceObjects instance");
125  }
126 
127  if (!d->m_reference.d->UngetPrototypeService(d->m_context->GetModule(), serviceIter->second))
128  {
129  US_WARN << "Ungetting service unsuccessful";
130  }
131  else
132  {
133  d->m_serviceInstances.erase(serviceIter);
134  }
135 }
136 
138 {
139  if (interfaceMap.empty())
140  {
141  return;
142  }
143 
144  std::set<InterfaceMap>::iterator serviceIter = d->m_serviceInterfaceMaps.find(interfaceMap);
145  if (serviceIter == d->m_serviceInterfaceMaps.end())
146  {
147  throw std::invalid_argument("The provided service has not been retrieved via this ServiceObjects instance");
148  }
149 
150  if (!d->m_reference.d->UngetPrototypeService(d->m_context->GetModule(), interfaceMap))
151  {
152  US_WARN << "Ungetting service unsuccessful";
153  }
154  else
155  {
156  d->m_serviceInterfaceMaps.erase(serviceIter);
157  }
158 }
159 
161 {
162  return d->m_reference;
163 }
164 
166  : d(other.d)
167 {
168  d->ref.Ref();
169 }
170 
172 {
173  if (!d->ref.Deref())
174  {
175  delete d;
176  }
177 }
178 
180 {
181  ServiceObjectsBasePrivate* curr_d = d;
182  d = other.d;
183  d->ref.Ref();
184 
185  if (!curr_d->ref.Deref())
186  delete curr_d;
187 
188  return *this;
189 }
190 
192 {
194 }
195 
197 {
198  this->ServiceObjectsBase::UngetService(service);
199 }
200 
202 {
203  return this->ServiceObjectsBase::GetReference();
204 }
205 
207  : ServiceObjectsBase(context, reference)
208 {}
209 
210 US_END_NAMESPACE
S * GetService() const
void UngetService(void *service)
US_Core_EXPORT const std::string & SCOPE_PROTOTYPE()
std::map< std::string, void * > InterfaceMap
void UngetService(S *service)
US_Core_EXPORT const std::string & SERVICE_SCOPE()
ServiceObjectsBase & operator=(const ServiceObjectsBase &other)
ServiceReferenceBase GetReference() const
void * GetService() const
Module * GetModule() const
ServiceReference< S > GetServiceReference() const
InterfaceMap GetServiceInterfaceMap() const
ServiceObjectsBase(ModuleContext *context, const ServiceReferenceBase &reference)