change callback map to std/arx::map

master
Hideaki Tai 4 years ago
parent 37a6d10949
commit 6c4e0278c8

@ -136,16 +136,17 @@ namespace arx
using CallbackAllType = std::function<void(uint32_t universe, uint8_t* data, uint16_t size)>; using CallbackAllType = std::function<void(uint32_t universe, uint8_t* data, uint16_t size)>;
using CallbackType = std::function<void(uint8_t* data, uint16_t size)>; using CallbackType = std::function<void(uint8_t* data, uint16_t size)>;
struct Map { uint32_t universe; CallbackType func; };
#if ARX_HAVE_LIBSTDCPLUSPLUS >= 201103L // Have libstdc++11 #if ARX_HAVE_LIBSTDCPLUSPLUS >= 201103L // Have libstdc++11
template <uint16_t SIZE> template <uint16_t SIZE>
using Array = std::array<uint8_t, SIZE>; using Array = std::array<uint8_t, SIZE>;
using CallbackMap = std::vector<Map>; using CallbackMap = std::map<uint32_t, CallbackType>;
using namespace std;
#else #else
template <uint16_t SIZE> template <uint16_t SIZE>
using Array = arx::vector<uint8_t, SIZE>; using Array = arx::vector<uint8_t, SIZE>;
using CallbackMap = arx::vector<Map, 8>; using CallbackMap = arx::map<uint32_t, CallbackType, 8>;
using namespace arx;
#endif #endif
template <typename S> template <typename S>
@ -264,7 +265,7 @@ namespace arx
IPAddress remote_ip; IPAddress remote_ip;
uint16_t remote_port; uint16_t remote_port;
CallbackMap callbacks; CallbackMap callbacks;
CallbackAllType callback_all {nullptr}; CallbackAllType callback_all;
S* stream; S* stream;
public: public:
@ -288,7 +289,7 @@ namespace arx
remote_port = (uint16_t)stream->S::remotePort(); remote_port = (uint16_t)stream->S::remotePort();
if (callback_all) callback_all(universe15bit(), data(), size); if (callback_all) callback_all(universe15bit(), data(), size);
for (auto& c : callbacks) for (auto& c : callbacks)
if (universe15bit() == c.universe) c.func(data(), size); if (universe15bit() == c.first) c.second(data(), size);
return true; return true;
} }
} }
@ -359,7 +360,7 @@ namespace arx
inline void subscribe(const uint32_t universe, const CallbackType& func) 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) inline void subscribe(const CallbackAllType& func)
{ {
@ -373,15 +374,9 @@ namespace arx
inline void unsubscribe(const uint32_t universe) inline void unsubscribe(const uint32_t universe)
{ {
size_t idx = callbacks.size(); auto it = callbacks.find(universe);
for (size_t i = 0; i < callbacks.size(); ++i) if (it != callbacks.end())
if (callbacks[i].universe == universe) { callbacks.erase(it);
idx = i;
break;
}
if (idx != callbacks.size())
callbacks.erase(callbacks.begin() + idx);
} }
inline void unsubscribe() inline void unsubscribe()
{ {

Loading…
Cancel
Save