22 #include "usServiceHooks_p.h"
25 #include "usCoreModuleContext_p.h"
29 #include "usServiceReferenceBasePrivate.h"
33 ServiceHooks::ServiceHooks(CoreModuleContext* coreCtx)
35 , listenerHookTracker(NULL)
40 ServiceHooks::~ServiceHooks()
45 ServiceHooks::TrackedType ServiceHooks::AddingService(
const ServiceReferenceType& reference)
50 lh->Added(coreCtx->listeners.GetListenerInfoCollection());
52 catch (
const std::exception& e)
60 <<
": unknown exception type";
65 void ServiceHooks::ModifiedService(
const ServiceReferenceType& , TrackedType )
70 void ServiceHooks::RemovedService(
const ServiceReferenceType& reference, TrackedType )
75 void ServiceHooks::Open()
77 US_UNUSED(Lock(
this));
79 listenerHookTracker =
new ServiceTracker<ServiceListenerHook>(
GetModuleContext(),
this);
80 listenerHookTracker->Open();
85 void ServiceHooks::Close()
87 US_UNUSED(Lock(
this));
88 if (listenerHookTracker)
90 listenerHookTracker->Close();
91 delete listenerHookTracker;
92 listenerHookTracker = NULL;
98 bool ServiceHooks::IsOpen()
const
100 US_UNUSED(Lock(
this));
104 void ServiceHooks::FilterServiceReferences(
ModuleContext* mc,
const std::string& service,
105 const std::string& filter, std::vector<ServiceReferenceBase>& refs)
107 std::vector<ServiceRegistrationBase> srl;
108 coreCtx->services.Get_unlocked(us_service_interface_iid<ServiceFindHook>(), srl);
111 ShrinkableVector<ServiceReferenceBase> filtered(refs);
113 std::sort(srl.begin(), srl.end());
114 for (std::vector<ServiceRegistrationBase>::reverse_iterator fhrIter = srl.rbegin(), fhrEnd = srl.rend();
115 fhrIter != fhrEnd; ++fhrIter)
117 ServiceReference<ServiceFindHook> sr = fhrIter->GetReference();
118 ServiceFindHook*
const fh =
reinterpret_cast<ServiceFindHook*
>(sr.d->GetService(
GetModuleContext()->GetModule()));
123 fh->Find(mc, service, filter, filtered);
125 catch (
const std::exception& e)
133 <<
": unknown exception type";
140 void ServiceHooks::FilterServiceEventReceivers(
const ServiceEvent& evt,
141 ServiceListeners::ServiceListenerEntries& receivers)
143 std::vector<ServiceRegistrationBase> eventListenerHooks;
144 coreCtx->services.Get_unlocked(us_service_interface_iid<ServiceEventListenerHook>(), eventListenerHooks);
145 if (!eventListenerHooks.empty())
147 std::sort(eventListenerHooks.begin(), eventListenerHooks.end());
148 std::map<ModuleContext*, std::vector<ServiceListenerHook::ListenerInfo> > listeners;
149 for (ServiceListeners::ServiceListenerEntries::iterator sleIter = receivers.begin(),
150 sleEnd = receivers.end(); sleIter != sleEnd; ++sleIter)
152 listeners[sleIter->GetModuleContext()].push_back(*sleIter);
155 std::map<ModuleContext*, ShrinkableVector<ServiceListenerHook::ListenerInfo> > shrinkableListeners;
156 for (
std::map<
ModuleContext*, std::vector<ServiceListenerHook::ListenerInfo> >::iterator iter = listeners.begin(),
157 iterEnd = listeners.end(); iter != iterEnd; ++iter)
159 shrinkableListeners.insert(std::make_pair(iter->first, ShrinkableVector<ServiceListenerHook::ListenerInfo>(iter->second)));
162 ShrinkableMap<ModuleContext*, ShrinkableVector<ServiceListenerHook::ListenerInfo> > filtered(shrinkableListeners);
164 for(std::vector<ServiceRegistrationBase>::reverse_iterator sriIter = eventListenerHooks.rbegin(),
165 sriEnd = eventListenerHooks.rend(); sriIter != sriEnd; ++sriIter)
167 ServiceReference<ServiceEventListenerHook> sr = sriIter->GetReference();
173 elh->
Event(evt, filtered);
175 catch(
const std::exception& e)
183 <<
": unknown exception type";
188 for(
std::map<
ModuleContext*, std::vector<ServiceListenerHook::ListenerInfo> >::iterator iter = listeners.begin(),
189 iterEnd = listeners.end(); iter != iterEnd; ++iter)
191 receivers.insert(iter->second.begin(), iter->second.end());
196 void ServiceHooks::HandleServiceListenerReg(
const ServiceListenerEntry& sle)
198 if(!IsOpen() || listenerHookTracker->Size() == 0)
203 std::vector<ServiceReference<ServiceListenerHook> > srl
204 = listenerHookTracker->GetServiceReferences();
208 std::sort(srl.begin(), srl.end());
210 std::vector<ServiceListenerHook::ListenerInfo> set;
212 for (std::vector<ServiceReference<ServiceListenerHook> >::reverse_iterator srIter = srl.rbegin(),
213 srEnd = srl.rend(); srIter != srEnd; ++srIter)
215 ServiceListenerHook* lh = listenerHookTracker->GetService(*srIter);
220 catch (
const std::exception& e)
228 <<
": unknown exception";
234 void ServiceHooks::HandleServiceListenerUnreg(
const ServiceListenerEntry& sle)
238 std::vector<ServiceListenerEntry> set;
240 HandleServiceListenerUnreg(set);
244 void ServiceHooks::HandleServiceListenerUnreg(
const std::vector<ServiceListenerEntry>& set)
246 if(!IsOpen() || listenerHookTracker->Size() == 0)
251 std::vector<ServiceReference<ServiceListenerHook> > srl
252 = listenerHookTracker->GetServiceReferences();
256 std::vector<ServiceListenerHook::ListenerInfo> lis;
257 for (std::vector<ServiceListenerEntry>::const_iterator sleIter = set.begin(),
258 sleEnd = set.end(); sleIter != sleEnd; ++sleIter)
260 lis.push_back(*sleIter);
263 std::sort(srl.begin(), srl.end());
264 for (std::vector<ServiceReference<ServiceListenerHook> >::reverse_iterator srIter = srl.rbegin(),
265 srEnd = srl.rend(); srIter != srEnd; ++srIter)
267 ServiceListenerHook*
const lh = listenerHookTracker->GetService(*srIter);
272 catch (
const std::exception& e)
280 <<
": unknown exception type";
bool UngetService(const ServiceReferenceBase &reference)
void * GetService(const ServiceReferenceBase &reference)
virtual void Event(const ServiceEvent &event, ShrinkableMapType &listeners)=0
MITKMATCHPOINTREGISTRATION_EXPORT ResultImageType::Pointer map(const InputImageType *input, const RegistrationType *registration, bool throwOnOutOfInputAreaError=false, const double &paddingValue=0, const ResultImageGeometryType *resultGeometry=NULL, bool throwOnMappingError=true, const double &errorValue=0, mitk::ImageMappingInterpolator::Type interpolatorType=mitk::ImageMappingInterpolator::Linear)
US_Core_EXPORT const std::string & SERVICE_ID()
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.