From e44bb0ecbc36adb14a447a27dd0178a2d0bad04a Mon Sep 17 00:00:00 2001 From: Max Prokhorov Date: Tue, 12 May 2020 23:03:21 +0300 Subject: [PATCH] ws: directly iterate over internal callbacks array (#2248) tnx to @RDobrinov https://gist.github.com/RDobrinov/9e739fd77990d0a77fc1b867176bf48c --- code/espurna/ws_internal.h | 45 +++++++++----------------------------- 1 file changed, 10 insertions(+), 35 deletions(-) diff --git a/code/espurna/ws_internal.h b/code/espurna/ws_internal.h index c8242237..c6e6bfaa 100644 --- a/code/espurna/ws_internal.h +++ b/code/espurna/ws_internal.h @@ -31,32 +31,6 @@ struct ws_ticket_t { // WS callbacks // ----------------------------------------------------------------------------- -struct ws_counter_t { - - ws_counter_t() : current(0), start(0), stop(0) {} - - ws_counter_t(uint32_t start, uint32_t stop) : - current(start), start(start), stop(stop) {} - - void reset() { - current = start; - } - - void next() { - if (current < stop) { - ++current; - } - } - - bool done() { - return (current >= stop); - } - - uint32_t current; - uint32_t start; - uint32_t stop; -}; - struct ws_data_t { enum mode_t { @@ -69,7 +43,7 @@ struct ws_data_t { client_id(0), mode(ALL), callbacks(*storage.get()), - counter(0, 1) + current(callbacks.begin()) {} ws_data_t(uint32_t client_id, const ws_on_send_callback_f& cb) : @@ -77,7 +51,7 @@ struct ws_data_t { client_id(client_id), mode(ALL), callbacks(*storage.get()), - counter(0, 1) + current(callbacks.begin()) {} ws_data_t(const uint32_t client_id, ws_on_send_callback_list_t&& callbacks, mode_t mode = SEQUENCE) : @@ -85,30 +59,31 @@ struct ws_data_t { client_id(client_id), mode(mode), callbacks(*storage.get()), - counter(0, (storage.get())->size()) + current(callbacks.begin()) {} ws_data_t(const uint32_t client_id, const ws_on_send_callback_list_t& callbacks, mode_t mode = SEQUENCE) : client_id(client_id), mode(mode), callbacks(callbacks), - counter(0, callbacks.size()) + current(callbacks.begin()) {} bool done() { - return counter.done(); + return current == callbacks.end(); } void sendAll(JsonObject& root) { - while (!counter.done()) counter.next(); + current = callbacks.end(); for (auto& callback : callbacks) { callback(root); } } void sendCurrent(JsonObject& root) { - callbacks[counter.current](root); - counter.next(); + if (current == callbacks.end()) return; + (*current)(root); + ++current; } void send(JsonObject& root) { @@ -123,7 +98,7 @@ struct ws_data_t { const uint32_t client_id; const mode_t mode; const ws_on_send_callback_list_t& callbacks; - ws_counter_t counter; + ws_on_send_callback_list_t::const_iterator current; }; // -----------------------------------------------------------------------------