From 6c4e0278c884292b5b9b011e6c15874db93083c7 Mon Sep 17 00:00:00 2001 From: Hideaki Tai Date: Wed, 30 Sep 2020 18:45:41 +0900 Subject: [PATCH] change callback map to std/arx::map --- Artnet.h | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/Artnet.h b/Artnet.h index 61327ef..13b9caa 100644 --- a/Artnet.h +++ b/Artnet.h @@ -136,16 +136,17 @@ namespace arx using CallbackAllType = std::function; using CallbackType = std::function; - struct Map { uint32_t universe; CallbackType func; }; #if ARX_HAVE_LIBSTDCPLUSPLUS >= 201103L // Have libstdc++11 template using Array = std::array; - using CallbackMap = std::vector; + using CallbackMap = std::map; + using namespace std; #else template using Array = arx::vector; - using CallbackMap = arx::vector; + using CallbackMap = arx::map; + using namespace arx; #endif template @@ -264,7 +265,7 @@ namespace arx IPAddress remote_ip; uint16_t remote_port; CallbackMap callbacks; - CallbackAllType callback_all {nullptr}; + CallbackAllType callback_all; S* stream; public: @@ -288,7 +289,7 @@ namespace arx remote_port = (uint16_t)stream->S::remotePort(); if (callback_all) callback_all(universe15bit(), data(), size); for (auto& c : callbacks) - if (universe15bit() == c.universe) c.func(data(), size); + if (universe15bit() == c.first) c.second(data(), size); return true; } } @@ -359,7 +360,7 @@ namespace arx inline void subscribe(const uint32_t universe, const CallbackType& func) { - callbacks.push_back(Map{universe, func}); + callbacks.insert(make_pair(universe, func)); } inline void subscribe(const CallbackAllType& func) { @@ -373,15 +374,9 @@ namespace arx inline void unsubscribe(const uint32_t universe) { - size_t idx = callbacks.size(); - for (size_t i = 0; i < callbacks.size(); ++i) - if (callbacks[i].universe == universe) { - idx = i; - break; - } - - if (idx != callbacks.size()) - callbacks.erase(callbacks.begin() + idx); + auto it = callbacks.find(universe); + if (it != callbacks.end()) + callbacks.erase(it); } inline void unsubscribe() {