Browse Source

ws: directly iterate over internal callbacks array (#2248)

tnx to @RDobrinov
https://gist.github.com/RDobrinov/9e739fd77990d0a77fc1b867176bf48c
mcspr-patch-1
Max Prokhorov 4 years ago
committed by GitHub
parent
commit
e44bb0ecbc
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 10 additions and 35 deletions
  1. +10
    -35
      code/espurna/ws_internal.h

+ 10
- 35
code/espurna/ws_internal.h View File

@ -31,32 +31,6 @@ struct ws_ticket_t {
// WS callbacks // 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 { struct ws_data_t {
enum mode_t { enum mode_t {
@ -69,7 +43,7 @@ struct ws_data_t {
client_id(0), client_id(0),
mode(ALL), mode(ALL),
callbacks(*storage.get()), callbacks(*storage.get()),
counter(0, 1)
current(callbacks.begin())
{} {}
ws_data_t(uint32_t client_id, const ws_on_send_callback_f& cb) : 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), client_id(client_id),
mode(ALL), mode(ALL),
callbacks(*storage.get()), 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) : 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), client_id(client_id),
mode(mode), mode(mode),
callbacks(*storage.get()), 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) : ws_data_t(const uint32_t client_id, const ws_on_send_callback_list_t& callbacks, mode_t mode = SEQUENCE) :
client_id(client_id), client_id(client_id),
mode(mode), mode(mode),
callbacks(callbacks), callbacks(callbacks),
counter(0, callbacks.size())
current(callbacks.begin())
{} {}
bool done() { bool done() {
return counter.done();
return current == callbacks.end();
} }
void sendAll(JsonObject& root) { void sendAll(JsonObject& root) {
while (!counter.done()) counter.next();
current = callbacks.end();
for (auto& callback : callbacks) { for (auto& callback : callbacks) {
callback(root); callback(root);
} }
} }
void sendCurrent(JsonObject& root) { void sendCurrent(JsonObject& root) {
callbacks[counter.current](root);
counter.next();
if (current == callbacks.end()) return;
(*current)(root);
++current;
} }
void send(JsonObject& root) { void send(JsonObject& root) {
@ -123,7 +98,7 @@ struct ws_data_t {
const uint32_t client_id; const uint32_t client_id;
const mode_t mode; const mode_t mode;
const ws_on_send_callback_list_t& callbacks; const ws_on_send_callback_list_t& callbacks;
ws_counter_t counter;
ws_on_send_callback_list_t::const_iterator current;
}; };
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------


Loading…
Cancel
Save