22 #include "usModuleHooks_p.h"
26 #include "usCoreModuleContext_p.h"
29 #include "usServiceReferenceBasePrivate.h"
33 ModuleHooks::ModuleHooks(CoreModuleContext* ctx)
38 Module* ModuleHooks::FilterModule(
const ModuleContext* mc, Module* module)
const
45 std::vector<ServiceRegistrationBase> srl;
46 coreCtx->services.Get(us_service_interface_iid<ModuleFindHook>(), srl);
53 std::vector<Module*> ml;
55 this->FilterModules(mc, ml);
56 return ml.empty() ? NULL : module;
60 void ModuleHooks::FilterModules(
const ModuleContext* mc, std::vector<Module*>& modules)
const
62 std::vector<ServiceRegistrationBase> srl;
63 coreCtx->services.Get(us_service_interface_iid<ModuleFindHook>(), srl);
64 ShrinkableVector<Module*> filtered(modules);
66 std::sort(srl.begin(), srl.end());
67 for (std::vector<ServiceRegistrationBase>::reverse_iterator srBaseIter = srl.rbegin(), srBaseEnd = srl.rend();
68 srBaseIter != srBaseEnd; ++srBaseIter)
70 ServiceReference<ModuleFindHook> sr = srBaseIter->GetReference();
71 ModuleFindHook*
const fh =
reinterpret_cast<ModuleFindHook*
>(sr.d->GetService(
GetModuleContext()->GetModule()));
76 fh->Find(mc, filtered);
78 catch (
const std::exception& e)
86 <<
": unknown exception type";
92 void ModuleHooks::FilterModuleEventReceivers(
const ModuleEvent& evt,
93 ServiceListeners::ModuleListenerMap& moduleListeners)
95 std::vector<ServiceRegistrationBase> eventHooks;
96 coreCtx->services.Get(us_service_interface_iid<ModuleEventHook>(), eventHooks);
99 MutexLock lock(coreCtx->listeners.moduleListenerMapMutex);
100 moduleListeners = coreCtx->listeners.moduleListenerMap;
103 if(!eventHooks.empty())
105 std::vector<ModuleContext*> moduleContexts;
106 for (ServiceListeners::ModuleListenerMap::iterator le = moduleListeners.begin(),
107 leEnd = moduleListeners.end(); le != leEnd; ++le)
109 moduleContexts.push_back(le->first);
111 std::sort(moduleContexts.begin(), moduleContexts.end());
112 moduleContexts.erase(std::unique(moduleContexts.begin(), moduleContexts.end()), moduleContexts.end());
114 const std::size_t unfilteredSize = moduleContexts.size();
115 ShrinkableVector<ModuleContext*> filtered(moduleContexts);
117 std::sort(eventHooks.begin(), eventHooks.end());
118 for (std::vector<ServiceRegistrationBase>::reverse_iterator iter = eventHooks.rbegin(),
119 iterEnd = eventHooks.rend(); iter != iterEnd; ++iter)
121 ServiceReference<ModuleEventHook> sr;
124 sr = iter->GetReference();
126 catch (
const std::logic_error& e)
128 US_WARN <<
"Failed to get event hook service reference: " << e.what();
132 ModuleEventHook* eh =
reinterpret_cast<ModuleEventHook*
>(sr.d->GetService(
GetModuleContext()->GetModule()));
137 eh->Event(evt, filtered);
139 catch (
const std::exception& e)
147 <<
": unknown exception type";
152 if (unfilteredSize != moduleContexts.size())
154 for (ServiceListeners::ModuleListenerMap::iterator le = moduleListeners.begin();
155 le != moduleListeners.end();)
157 if(std::find(moduleContexts.begin(), moduleContexts.end(), le->first) == moduleContexts.end())
159 moduleListeners.erase(le++);
US_Core_EXPORT const std::string & SERVICE_ID()
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.