diff options
author | Max Kellermann <max@duempel.org> | 2014-01-13 22:05:45 +0100 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2014-01-13 22:24:02 +0100 |
commit | ca43e634b5adfebdcb9c3c8f61cebf38d42827e0 (patch) | |
tree | fab541d75386417187052650dcee16eb5c48f3b4 | |
parent | 85324f80fe1d72a066cc49847815830080eb1bc3 (diff) | |
download | mpd-ca43e634b5adfebdcb9c3c8f61cebf38d42827e0.tar.gz mpd-ca43e634b5adfebdcb9c3c8f61cebf38d42827e0.tar.xz mpd-ca43e634b5adfebdcb9c3c8f61cebf38d42827e0.zip |
db/upnp: use std::function for the libupnp callback
Replaces the bloated std::map.
-rw-r--r-- | src/db/upnp/Discovery.cxx | 10 | ||||
-rw-r--r-- | src/db/upnp/upnpplib.cxx | 16 | ||||
-rw-r--r-- | src/db/upnp/upnpplib.hxx | 21 |
3 files changed, 16 insertions, 31 deletions
diff --git a/src/db/upnp/Discovery.cxx b/src/db/upnp/Discovery.cxx index a94f29d8c..8a2b7046d 100644 --- a/src/db/upnp/Discovery.cxx +++ b/src/db/upnp/Discovery.cxx @@ -158,7 +158,7 @@ discoExplorer(void *) // mutex just for clarifying the message printing, the workqueue is // mt-safe of course. static int -cluCallBack(Upnp_EventType et, void *evp, void *) +cluCallBack(Upnp_EventType et, void *evp) { static Mutex cblock; const ScopeLock protect(cblock); @@ -231,11 +231,9 @@ UPnPDeviceDirectory::UPnPDeviceDirectory() if (lib == nullptr) return; - lib->registerHandler(UPNP_DISCOVERY_SEARCH_RESULT, cluCallBack, this); - lib->registerHandler(UPNP_DISCOVERY_ADVERTISEMENT_ALIVE, - cluCallBack, this); - lib->registerHandler(UPNP_DISCOVERY_ADVERTISEMENT_BYEBYE, - cluCallBack, this); + lib->SetHandler([](Upnp_EventType type, void *event){ + cluCallBack(type, event); + }); search(); } diff --git a/src/db/upnp/upnpplib.cxx b/src/db/upnp/upnpplib.cxx index d7c65ccf4..508315b26 100644 --- a/src/db/upnp/upnpplib.cxx +++ b/src/db/upnp/upnpplib.cxx @@ -65,15 +65,6 @@ LibUPnP::LibUPnP() ixmlRelaxParser(1); } -void -LibUPnP::registerHandler(Upnp_EventType et, Upnp_FunPtr handler, void *cookie) -{ - if (handler == nullptr) - m_handlers.erase(et); - else - m_handlers.emplace(et, Handler(handler, cookie)); -} - int LibUPnP::o_callback(Upnp_EventType et, void* evp, void* cookie) { @@ -83,10 +74,9 @@ LibUPnP::o_callback(Upnp_EventType et, void* evp, void* cookie) ulib = theLib; } - auto it = ulib->m_handlers.find(et); - if (it != ulib->m_handlers.end()) { - (it->second.handler)(et, evp, it->second.cookie); - } + if (ulib->handler) + ulib->handler(et, evp); + return UPNP_E_SUCCESS; } diff --git a/src/db/upnp/upnpplib.hxx b/src/db/upnp/upnpplib.hxx index b6ce80212..c1443624c 100644 --- a/src/db/upnp/upnpplib.hxx +++ b/src/db/upnp/upnpplib.hxx @@ -22,24 +22,18 @@ #include "util/Error.hxx" -#include <map> - #include <upnp/upnp.h> +#include <functional> + /** Our link to libupnp. Initialize and keep the handle around */ class LibUPnP { - // A Handler object records the data from registerHandler. - class Handler { - public: - Handler(Upnp_FunPtr h, void *c) - : handler(h), cookie(c) {} - Upnp_FunPtr handler; - void *cookie; - }; + typedef std::function<void(Upnp_EventType type, void *event)> Handler; Error init_error; UpnpClient_Handle m_clh; - std::map<Upnp_EventType, Handler> m_handlers; + + Handler handler; LibUPnP(); @@ -65,7 +59,10 @@ public: return init_error; } - void registerHandler(Upnp_EventType et, Upnp_FunPtr handler, void *cookie); + template<typename T> + void SetHandler(T &&_handler) { + handler = std::forward<T>(_handler); + } UpnpClient_Handle getclh() { |