Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
usServiceReferenceBase.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 "usServiceReferenceBase.h"
23 #include "usServiceReferenceBasePrivate.h"
24 #include "usServiceRegistrationBasePrivate.h"
25 
26 #include "usModule.h"
27 #include "usModulePrivate.h"
28 
29 #include <cassert>
30 
31 US_BEGIN_NAMESPACE
32 
33 ServiceReferenceBase::ServiceReferenceBase()
34  : d(new ServiceReferenceBasePrivate(0))
35 {
36 
37 }
38 
39 ServiceReferenceBase::ServiceReferenceBase(const ServiceReferenceBase& ref)
40  : d(ref.d)
41 {
42  d->ref.Ref();
43 }
44 
45 ServiceReferenceBase::ServiceReferenceBase(ServiceRegistrationBasePrivate* reg)
46  : d(new ServiceReferenceBasePrivate(reg))
47 {
48 }
49 
50 void ServiceReferenceBase::SetInterfaceId(const std::string& interfaceId)
51 {
52  if (d->ref > 1)
53  {
54  // detach
55  d->ref.Deref();
56  d = new ServiceReferenceBasePrivate(d->registration);
57  }
58  d->interfaceId = interfaceId;
59 }
60 
61 ServiceReferenceBase::operator bool_type() const
62 {
63  return GetModule() != 0 ? &ServiceReferenceBase::d : NULL;
64 }
65 
67 {
68  if (null == 0)
69  {
70  if (!d->ref.Deref())
71  delete d;
72  d = new ServiceReferenceBasePrivate(0);
73  }
74  return *this;
75 }
76 
78 {
79  if (!d->ref.Deref())
80  delete d;
81 }
82 
83 Any ServiceReferenceBase::GetProperty(const std::string& key) const
84 {
85  MutexLock lock(d->registration->propsLock);
86 
87  return d->registration->properties.Value(key);
88 }
89 
90 void ServiceReferenceBase::GetPropertyKeys(std::vector<std::string>& keys) const
91 {
92  MutexLock lock(d->registration->propsLock);
93 
94  const std::vector<std::string>& ks = d->registration->properties.Keys();
95  keys.assign(ks.begin(), ks.end());
96 }
97 
99 {
100  if (d->registration == 0 || d->registration->module == 0)
101  {
102  return 0;
103  }
104 
105  return d->registration->module->q;
106 }
107 
108 void ServiceReferenceBase::GetUsingModules(std::vector<Module*>& modules) const
109 {
110  MutexLock lock(d->registration->propsLock);
111 
112  ServiceRegistrationBasePrivate::ModuleToRefsMap::const_iterator end = d->registration->dependents.end();
113  for (ServiceRegistrationBasePrivate::ModuleToRefsMap::const_iterator iter = d->registration->dependents.begin();
114  iter != end; ++iter)
115  {
116  modules.push_back(iter->first);
117  }
118 }
119 
121 {
122  if (!(*this))
123  {
124  return true;
125  }
126 
127  if (!reference)
128  {
129  return false;
130  }
131 
133  const Any anyR2 = reference.GetProperty(ServiceConstants::SERVICE_RANKING());
134  assert(anyR1.Empty() || anyR1.Type() == typeid(int));
135  assert(anyR2.Empty() || anyR2.Type() == typeid(int));
136  const int r1 = anyR1.Empty() ? 0 : *any_cast<int>(&anyR1);
137  const int r2 = anyR2.Empty() ? 0 : *any_cast<int>(&anyR2);
138 
139  if (r1 != r2)
140  {
141  // use ranking if ranking differs
142  return r1 < r2;
143  }
144  else
145  {
146  const Any anyId1 = GetProperty(ServiceConstants::SERVICE_ID());
147  const Any anyId2 = reference.GetProperty(ServiceConstants::SERVICE_ID());
148  assert(anyId1.Type() == typeid(long int));
149  assert(anyId2.Type() == typeid(long int));
150  const long int id1 = *any_cast<long int>(&anyId1);
151  const long int id2 = *any_cast<long int>(&anyId2);
152 
153  // otherwise compare using IDs,
154  // is less than if it has a higher ID.
155  return id2 < id1;
156  }
157 }
158 
160 {
161  return d->registration == reference.d->registration;
162 }
163 
165 {
166  ServiceReferenceBasePrivate* curr_d = d;
167  d = reference.d;
168  d->ref.Ref();
169 
170  if (!curr_d->ref.Deref())
171  delete curr_d;
172 
173  return *this;
174 }
175 
176 bool ServiceReferenceBase::IsConvertibleTo(const std::string& interfaceId) const
177 {
178  return d->IsConvertibleTo(interfaceId);
179 }
180 
182 {
183  return d->interfaceId;
184 }
185 
186 std::size_t ServiceReferenceBase::Hash() const
187 {
188  using namespace US_HASH_FUNCTION_NAMESPACE;
189  return US_HASH_FUNCTION(ServiceRegistrationBasePrivate*, this->d->registration);
190 }
191 
192 US_END_NAMESPACE
193 
194 US_USE_NAMESPACE
195 
196 std::ostream& operator<<(std::ostream& os, const ServiceReferenceBase& serviceRef)
197 {
198  if (serviceRef)
199  {
200  assert(serviceRef.GetModule() != NULL);
201 
202  os << "Reference for service object registered from "
203  << serviceRef.GetModule()->GetName() << " " << serviceRef.GetModule()->GetVersion()
204  << " (";
205  std::vector<std::string> keys;
206  serviceRef.GetPropertyKeys(keys);
207  size_t keySize = keys.size();
208  for(size_t i = 0; i < keySize; ++i)
209  {
210  os << keys[i] << "=" << serviceRef.GetProperty(keys[i]).ToString();
211  if (i < keySize-1) os << ",";
212  }
213  os << ")";
214  }
215  else
216  {
217  os << "Invalid service reference";
218  }
219 
220  return os;
221 }
const std::type_info & Type() const
Definition: usAny.h:278
US_Core_EXPORT const std::string & SERVICE_RANKING()
bool operator==(const ServiceReferenceBase &reference) const
ValueType * any_cast(Any *operand)
Definition: usAny.h:377
void GetUsingModules(std::vector< Module * > &modules) const
bool operator<(const ServiceReferenceBase &reference) const
Definition: usAny.h:163
US_Core_EXPORT std::ostream & operator<<(std::ostream &os, ModuleEvent::Type eventType)
Any GetProperty(const std::string &key) const
std::string GetInterfaceId() const
ServiceReferenceBase & operator=(int null)
void GetPropertyKeys(std::vector< std::string > &keys) const
bool IsConvertibleTo(const std::string &interfaceid) const
US_Core_EXPORT const std::string & SERVICE_ID()
US_MSVC_POP_WARNING US_HASH_FUNCTION_NAMESPACE_BEGIN return US_HASH_FUNCTION(us::ServiceRegistrationBasePrivate *, arg.d)
bool Empty() const
Definition: usAny.h:246