Medical Imaging Interaction Toolkit  2026.06.00
Medical Imaging Interaction Toolkit
usServiceTracker.h
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 
14 #ifndef USSERVICETRACKER_H
15 #define USSERVICETRACKER_H
16 
17 #include <map>
18 
19 #include <usServiceReference.h>
21 #include <usLDAPFilter.h>
22 
23 namespace us {
24 
25 template<class S, class T> class TrackedService;
26 template<class S, class T> class ServiceTrackerPrivate;
27 class ModuleContext;
28 
50 template<class T, class TTT>
52 {
53  typedef T TrackedType;
54 
55  // Needed for S == void
57  {
58  throw std::runtime_error("A custom ServiceTrackerCustomizer instance is required for custom tracked objects.");
59  //return TTT::DefaultValue();
60  }
61 
62  // Needed for S != void
64  {
65  throw std::runtime_error("A custom ServiceTrackerCustomizer instance is required for custom tracked objects.");
66  //return TTT::DefaultValue();
67  }
68 };
69 
71 template<class S, class T>
72 struct TrackedTypeTraits;
74 
86 template<class S, class T>
87 struct TrackedTypeTraits<S,T*> : public TrackedTypeTraitsBase<T*,TrackedTypeTraits<S,T*> >
88 {
89  typedef T* TrackedType;
90 
91  static bool IsValid(const TrackedType& t)
92  {
93  return t != nullptr;
94  }
95 
97  {
98  return nullptr;
99  }
100 
101  static void Dispose(TrackedType& t)
102  {
103  t = nullptr;
104  }
105 };
106 
108 template<class S>
109 struct TrackedTypeTraits<S,S*>
110 {
111  typedef S* TrackedType;
112 
113  static bool IsValid(const TrackedType& t)
114  {
115  return t != nullptr;
116  }
117 
118  static TrackedType DefaultValue()
119  {
120  return nullptr;
121  }
122 
123  static void Dispose(TrackedType& t)
124  {
125  t = nullptr;
126  }
127 
128  static TrackedType ConvertToTrackedType(S* s)
129  {
130  return s;
131  }
132 };
134 
136 /*
137  * This specialization is "special" because the tracked type is not
138  * void* (as specified in the second template parameter) but InterfaceMap.
139  * This is in line with the ModuleContext::GetService(...) overloads to
140  * return either S* or InterfaceMap depending on the template parameter.
141  */
142 template<>
143 struct TrackedTypeTraits<void,void*>
144 {
145  typedef InterfaceMap TrackedType;
146 
147  static bool IsValid(const TrackedType& t)
148  {
149  return !t.empty();
150  }
151 
152  static TrackedType DefaultValue()
153  {
154  return TrackedType();
155  }
156 
157  static void Dispose(TrackedType& t)
158  {
159  t.clear();
160  }
161 
162  static TrackedType ConvertToTrackedType(const InterfaceMap& im)
163  {
164  return im;
165  }
166 };
168 
221 template<class S, class TTT = TrackedTypeTraits<S,S*> >
222 class ServiceTracker : protected ServiceTrackerCustomizer<S,typename TTT::TrackedType>
223 {
224 public:
225 
227  typedef S ServiceType;
229  typedef typename TTT::TrackedType T;
230 
233 
235  typedef std::map<ServiceReference<S>,T> TrackingMap;
236 
238  ~ServiceTracker() override;
239 
261  const ServiceReferenceType& reference,
262  ServiceTrackerCustomizer<S,T>* customizer = nullptr);
263 
282  ServiceTracker(ModuleContext* context, const std::string& clazz,
283  ServiceTrackerCustomizer<S,T>* customizer = 0);
284 
304  ServiceTracker(ModuleContext* context, const LDAPFilter& filter,
305  ServiceTrackerCustomizer<S,T>* customizer = nullptr);
306 
325 
338  virtual void Open();
339 
351  virtual void Close();
352 
370  virtual T WaitForService(unsigned long timeoutMillis = 0);
371 
378  virtual std::vector<ServiceReferenceType> GetServiceReferences() const;
379 
400 
411  virtual T GetService(const ServiceReferenceType& reference) const;
412 
426  virtual std::vector<T> GetServices() const;
427 
439  virtual T GetService() const;
440 
451  virtual void Remove(const ServiceReferenceType& reference);
452 
459  virtual int Size() const;
460 
480  virtual int GetTrackingCount() const;
481 
494  virtual void GetTracked(TrackingMap& tracked) const;
495 
502  virtual bool IsEmpty() const;
503 
504 protected:
505 
532  T AddingService(const ServiceReferenceType& reference) override;
533 
549  void ModifiedService(const ServiceReferenceType& reference, T service) override;
550 
572  void RemovedService(const ServiceReferenceType& reference, T service) override;
573 
574 private:
575 
580 
581  friend class TrackedService<S,TTT>;
582  friend class ServiceTrackerPrivate<S,TTT>;
583 
584  _ServiceTrackerPrivate* const d;
585 };
586 
587 }
588 
589 #include <usServiceTracker.tpp>
590 
591 #endif // USSERVICETRACKER_H
An RFC 1960-based Filter.
Definition: usLDAPFilter.h:45
virtual void GetTracked(TrackingMap &tracked) const
Return a sorted map of the ServiceReferences and service objects for all services being tracked by th...
~ServiceTracker() override
Destructor.
virtual void Close()
Close this ServiceTracker.
virtual T GetService(const ServiceReferenceType &reference) const
Returns the service object for the specified ServiceReference if the specified referenced service is ...
ServiceTracker(ModuleContext *context, const ServiceReferenceType &reference, ServiceTrackerCustomizer< S, T > *customizer=nullptr)
Create a ServiceTracker on the specified ServiceReference.
virtual int Size() const
Return the number of services being tracked by this ServiceTracker.
virtual T GetService() const
Returns a service object for one of the services being tracked by this ServiceTracker.
virtual void Remove(const ServiceReferenceType &reference)
Remove a service from this ServiceTracker.
TTT::TrackedType T
The type of the tracked object.
ServiceTracker(ModuleContext *context, ServiceTrackerCustomizer< S, T > *customizer=nullptr)
Create a ServiceTracker on the class template argument S.
virtual ServiceReferenceType GetServiceReference() const
Returns a ServiceReference for one of the services being tracked by this ServiceTracker.
ServiceReference< S > ServiceReferenceType
The service reference type.
T AddingService(const ServiceReferenceType &reference) override
Default implementation of the ServiceTrackerCustomizer::AddingService method.
ServiceTracker(ModuleContext *context, const LDAPFilter &filter, ServiceTrackerCustomizer< S, T > *customizer=nullptr)
Create a ServiceTracker on the specified LDAPFilter object.
void RemovedService(const ServiceReferenceType &reference, T service) override
Default implementation of the ServiceTrackerCustomizer::RemovedService method.
virtual bool IsEmpty() const
Return if this ServiceTracker is empty.
virtual int GetTrackingCount() const
Returns the tracking count for this ServiceTracker.
S ServiceType
The type of the service being tracked.
std::map< ServiceReference< S >, T > TrackingMap
A map type from ServiceReference to tracked objects.
ServiceTracker(ModuleContext *context, const std::string &clazz, ServiceTrackerCustomizer< S, T > *customizer=0)
Create a ServiceTracker on the specified class name.
virtual std::vector< ServiceReferenceType > GetServiceReferences() const
Return a list of ServiceReferences for all services being tracked by this ServiceTracker.
virtual void Open()
Open this ServiceTracker and begin tracking services.
void ModifiedService(const ServiceReferenceType &reference, T service) override
Default implementation of the ServiceTrackerCustomizer::ModifiedService method.
virtual T WaitForService(unsigned long timeoutMillis=0)
Wait for at least one service to be tracked by this ServiceTracker. This method will also return when...
virtual std::vector< T > GetServices() const
Return a list of service objects for all services being tracked by this ServiceTracker.
std::map< std::string, void * > InterfaceMap
A map containing interfaces ids and their corresponding service object pointers.
static TrackedType ConvertToTrackedType(void *)
static TrackedType ConvertToTrackedType(const InterfaceMap &)
static TrackedType DefaultValue()
static bool IsValid(const TrackedType &t)
static void Dispose(TrackedType &t)