Fraxinus  18.10
An IGT application
cxServiceTrackerListener.h
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
12 #ifndef CXSERVICETRACKERLISTENER_H_
13 #define CXSERVICETRACKERLISTENER_H_
14 
15 #include <QSharedDataPointer>
16 #include <boost/function.hpp>
17 #include <boost/shared_ptr.hpp>
18 #undef REGISTERED //Needed on windows to avoid compiler error. Not sure why.
19 #include <ctkServiceEvent.h>
20 #include "ctkServiceTracker.h"
21 #include "ctkServiceTrackerCustomizer.h"
22 #include "ctkPluginContext.h"
23 #include <ctkPluginFramework.h>
25 
26 
27 namespace cx
28 {
52 template <class T >
54 {
55 
56 public:
57  ServiceTrackerListener(ctkPluginContext* context,
58  boost::function<void (T*)> serviceAdded,
59  boost::function<void (T*)> serviceModified,
60  boost::function<void (T*)> serviceRemoved)
61  {
62  boost::shared_ptr<ServiceTrackerCustomizer<T> > customizer(new ServiceTrackerCustomizer<T>);
63  mServiceTrackerCustomizer = customizer;
64  mServiceTrackerCustomizer->setServiceAddedCallback(serviceAdded);
65  mServiceTrackerCustomizer->setServiceModifiedCallback(serviceModified);
66  mServiceTrackerCustomizer->setServiceRemovedCallback(serviceRemoved);
67  mServiceTracker.reset(new ctkServiceTracker<T*>(context, mServiceTrackerCustomizer.get()));
68  }
69 
71  {
72 // mServiceTracker->close();//For some reason this causes a crash if a service (that uses another service with a ServiceTrackerListener) have been removed
73  //If close is needed: A possible workasround may be to clear all functions in mServiceTrackerCustomizer?
74 // mServiceTracker.reset();
75  }
76 
77  void open()
78  {
79  mServiceTracker->open();
80  }
81 
82  T* getServiceFromName(QString name)
83  {
84  QList<T*> services = mServiceTracker->getServices();
85  foreach(T* temp, services)
86  if(temp->getName() == name)
87  return temp;
88  return NULL;
89  }
90 
91  QList<T*> getServices()
92  {
93  return mServiceTracker->getServices();
94  }
95 
96 private:
97  boost::shared_ptr<ServiceTrackerCustomizer<T> > mServiceTrackerCustomizer;
98  boost::shared_ptr<ctkServiceTracker<T*> > mServiceTracker;
99 };
100 }//namespace cx
101 
102 #endif /* CXSERVICETRACKERLISTENER_H_ */
ServiceTrackerListener(ctkPluginContext *context, boost::function< void(T *)> serviceAdded, boost::function< void(T *)> serviceModified, boost::function< void(T *)> serviceRemoved)
Notify changes in a service lifecycle.
Helper class for listening to services being added, modified and removed.
Namespace for all CustusX production code.