Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
usServiceTracker.h
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 
23 #ifndef USSERVICETRACKER_H
24 #define USSERVICETRACKER_H
25 
26 #include <map>
27 
28 #include "usServiceReference.h"
30 #include "usLDAPFilter.h"
31 
32 US_BEGIN_NAMESPACE
33 
34 template<class S, class T> class TrackedService;
35 template<class S, class T> class ServiceTrackerPrivate;
36 class ModuleContext;
37 
59 template<class T, class TTT>
61 {
62  typedef T TrackedType;
63 
64  // Needed for S == void
65  static TrackedType ConvertToTrackedType(const InterfaceMap&)
66  {
67  throw std::runtime_error("A custom ServiceTrackerCustomizer instance is required for custom tracked objects.");
68  //return TTT::DefaultValue();
69  }
70 
71  // Needed for S != void
72  static TrackedType ConvertToTrackedType(void*)
73  {
74  throw std::runtime_error("A custom ServiceTrackerCustomizer instance is required for custom tracked objects.");
75  //return TTT::DefaultValue();
76  }
77 };
78 
80 template<class S, class T>
81 struct TrackedTypeTraits;
83 
95 template<class S, class T>
96 struct TrackedTypeTraits<S,T*> : public TrackedTypeTraitsBase<T*,TrackedTypeTraits<S,T*> >
97 {
98  typedef T* TrackedType;
99 
100  static bool IsValid(const TrackedType& t)
101  {
102  return t != NULL;
103  }
104 
105  static TrackedType DefaultValue()
106  {
107  return NULL;
108  }
109 
110  static void Dispose(TrackedType& t)
111  {
112  t = 0;
113  }
114 };
115 
117 template<class S>
118 struct TrackedTypeTraits<S,S*>
119 {
120  typedef S* TrackedType;
121 
122  static bool IsValid(const TrackedType& t)
123  {
124  return t != NULL;
125  }
126 
127  static TrackedType DefaultValue()
128  {
129  return NULL;
130  }
131 
132  static void Dispose(TrackedType& t)
133  {
134  t = 0;
135  }
136 
137  static TrackedType ConvertToTrackedType(S* s)
138  {
139  return s;
140  }
141 };
143 
145 /*
146  * This specialization is "special" because the tracked type is not
147  * void* (as specified in the second template parameter) but InterfaceMap.
148  * This is in line with the ModuleContext::GetService(...) overloads to
149  * return either S* or InterfaceMap dependening on the template parameter.
150  */
151 template<>
152 struct TrackedTypeTraits<void,void*>
153 {
154  typedef InterfaceMap TrackedType;
155 
156  static bool IsValid(const TrackedType& t)
157  {
158  return !t.empty();
159  }
160 
161  static TrackedType DefaultValue()
162  {
163  return TrackedType();
164  }
165 
166  static void Dispose(TrackedType& t)
167  {
168  t.clear();
169  }
170 
171  static TrackedType ConvertToTrackedType(const InterfaceMap& im)
172  {
173  return im;
174  }
175 };
177 
230 template<class S, class TTT = TrackedTypeTraits<S,S*> >
231 class ServiceTracker : protected ServiceTrackerCustomizer<S,typename TTT::TrackedType>
232 {
233 public:
234 
236  typedef S ServiceType;
238  typedef typename TTT::TrackedType T;
239 
241 
242  typedef std::map<ServiceReference<S>,T> TrackingMap;
243 
244  ~ServiceTracker();
245 
266  ServiceTracker(ModuleContext* context,
267  const ServiceReferenceType& reference,
268  ServiceTrackerCustomizer<S,T>* customizer = 0);
269 
288  ServiceTracker(ModuleContext* context, const std::string& clazz,
289  ServiceTrackerCustomizer<S,T>* customizer = 0);
290 
310  ServiceTracker(ModuleContext* context, const LDAPFilter& filter,
311  ServiceTrackerCustomizer<S,T>* customizer = 0);
312 
331 
344  virtual void Open();
345 
357  virtual void Close();
358 
376  virtual T WaitForService(unsigned long timeoutMillis = 0);
377 
384  virtual std::vector<ServiceReferenceType> GetServiceReferences() const;
385 
405  virtual ServiceReferenceType GetServiceReference() const;
406 
417  virtual T GetService(const ServiceReferenceType& reference) const;
418 
432  virtual std::vector<T> GetServices() const;
433 
445  virtual T GetService() const;
446 
457  virtual void Remove(const ServiceReferenceType& reference);
458 
465  virtual int Size() const;
466 
486  virtual int GetTrackingCount() const;
487 
500  virtual void GetTracked(TrackingMap& tracked) const;
501 
508  virtual bool IsEmpty() const;
509 
510 protected:
511 
538  T AddingService(const ServiceReferenceType& reference);
539 
555  void ModifiedService(const ServiceReferenceType& reference, T service);
556 
578  void RemovedService(const ServiceReferenceType& reference, T service);
579 
580 private:
581 
586 
587  friend class TrackedService<S,TTT>;
588  friend class ServiceTrackerPrivate<S,TTT>;
589 
590  _ServiceTrackerPrivate* const d;
591 };
592 
593 US_END_NAMESPACE
594 
595 #include "usServiceTracker.tpp"
596 
597 #endif // USSERVICETRACKER_H
ServiceReference< S > ServiceReferenceType
std::map< ServiceReference< S >, T > TrackingMap
S ServiceType
The type of the service being tracked.
static bool IsValid(const TrackedType &t)
std::map< std::string, void * > InterfaceMap
static TrackedType DefaultValue()
static TrackedType ConvertToTrackedType(void *)
TTT::TrackedType T
The type of the tracked object.
static TrackedType ConvertToTrackedType(const InterfaceMap &)
static void Dispose(TrackedType &t)