Medical Imaging Interaction Toolkit  2024.06.99-60d9b802
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 (DKFZ)
6  All rights reserved.
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  https://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 
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
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
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 != nullptr;
103  }
104 
106  {
107  return nullptr;
108  }
109 
110  static void Dispose(TrackedType& t)
111  {
112  t = nullptr;
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 != nullptr;
125  }
126 
127  static TrackedType DefaultValue()
128  {
129  return nullptr;
130  }
131 
132  static void Dispose(TrackedType& t)
133  {
134  t = nullptr;
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 depending 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() override;
245 
266  ServiceTracker(ModuleContext* context,
267  const ServiceReferenceType& reference,
268  ServiceTrackerCustomizer<S,T>* customizer = nullptr);
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 = nullptr);
312 
330  ServiceTracker(ModuleContext* context, ServiceTrackerCustomizer<S,T>* customizer = nullptr);
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) override;
539 
555  void ModifiedService(const ServiceReferenceType& reference, T service) override;
556 
578  void RemovedService(const ServiceReferenceType& reference, T service) override;
579 
580 private:
581 
586 
587  friend class TrackedService<S,TTT>;
588  friend class ServiceTrackerPrivate<S,TTT>;
589 
590  _ServiceTrackerPrivate* const d;
591 };
592 
594 
595 #include "usServiceTracker.tpp"
596 
597 #endif // USSERVICETRACKER_H
ModuleContext
Definition: usModuleContext.h:91
us::ServiceTracker
Definition: usServiceTracker.h:231
us::TrackedTypeTraits< S, T * >::IsValid
static bool IsValid(const TrackedType &t)
Definition: usServiceTracker.h:100
usLDAPFilter.h
us::TrackedTypeTraits< S, T * >::DefaultValue
static TrackedType DefaultValue()
Definition: usServiceTracker.h:105
us::TrackedTypeTraitsBase::ConvertToTrackedType
static TrackedType ConvertToTrackedType(const InterfaceMap &)
Definition: usServiceTracker.h:65
us::ServiceReference
Definition: usModule.h:40
us::ServiceTracker::T
TTT::TrackedType T
The type of the tracked object.
Definition: usServiceTracker.h:238
us::TrackedTypeTraitsBase::TrackedType
T TrackedType
Definition: usServiceTracker.h:62
us::TrackedService
Definition: usServiceTracker.h:34
usServiceReference.h
US_BEGIN_NAMESPACE
#define US_BEGIN_NAMESPACE
Definition: usGlobalConfig.h:76
us::ServiceTrackerPrivate
Definition: usServiceTracker.h:35
us::ServiceTracker::ServiceReferenceType
ServiceReference< S > ServiceReferenceType
Definition: usServiceTracker.h:240
us::ServiceTracker::TrackingMap
std::map< ServiceReference< S >, T > TrackingMap
Definition: usServiceTracker.h:242
usServiceTrackerCustomizer.h
US_END_NAMESPACE
#define US_END_NAMESPACE
Definition: usGlobalConfig.h:77
us::ServiceTracker::ServiceType
S ServiceType
The type of the service being tracked.
Definition: usServiceTracker.h:236
us::TrackedTypeTraits< S, T * >::Dispose
static void Dispose(TrackedType &t)
Definition: usServiceTracker.h:110
us::TrackedTypeTraitsBase::ConvertToTrackedType
static TrackedType ConvertToTrackedType(void *)
Definition: usServiceTracker.h:72
us::InterfaceMap
std::map< std::string, void * > InterfaceMap
Definition: usServiceInterface.h:138
us::TrackedTypeTraitsBase
Definition: usServiceTracker.h:60
us::ServiceTrackerCustomizer
Definition: usServiceTrackerCustomizer.h:63
us::LDAPFilter
Definition: usLDAPFilter.h:54
us::TrackedTypeTraits< S, T * >::TrackedType
T * TrackedType
Definition: usServiceTracker.h:98