From 0107687a9b85e288456ef3ed026e1c2eb86db029 Mon Sep 17 00:00:00 2001 From: Povilas Kanapickas Date: Mon, 18 Jul 2022 18:04:31 +0300 Subject: [PATCH] viewer: Use std::unique_ptr in event_table_ data structure --- src/viewer/scrollview.cpp | 32 ++++++++++---------------------- src/viewer/scrollview.h | 4 ++-- 2 files changed, 12 insertions(+), 24 deletions(-) diff --git a/src/viewer/scrollview.cpp b/src/viewer/scrollview.cpp index 087dabe8..f5336c58 100644 --- a/src/viewer/scrollview.cpp +++ b/src/viewer/scrollview.cpp @@ -60,8 +60,8 @@ static std::map, std::pair>> waiting_for_events; static std::mutex *waiting_for_events_mu; -SVEvent *SVEvent::copy() const { - auto *any = new SVEvent; +std::unique_ptr SVEvent::copy() const { + auto any = std::unique_ptr(new SVEvent); any->command_id = command_id; any->counter = counter; any->parameter = new char[strlen(parameter) + 1]; @@ -319,12 +319,9 @@ void ScrollView::Initialize(const char *name, int x_pos, int y_pos, int x_size, /// Sits and waits for events on this window. void ScrollView::StartEventHandler() { - SVEvent *new_event; - for (;;) { stream_->Flush(); semaphore_->Wait(); - new_event = nullptr; int serial = -1; int k = -1; mutex_.lock(); @@ -332,25 +329,22 @@ void ScrollView::StartEventHandler() { for (int i = 0; i < SVET_COUNT; i++) { if (event_table_[i] != nullptr && (serial < 0 || event_table_[i]->counter < serial)) { - new_event = event_table_[i]; serial = event_table_[i]->counter; k = i; } } // If we didn't find anything we had an old alarm and just sleep again. - if (new_event != nullptr) { - event_table_[k] = nullptr; + if (k != -1) { + auto new_event = std::move(event_table_[k]); mutex_.unlock(); if (event_handler_ != nullptr) { - event_handler_->Notify(new_event); + event_handler_->Notify(new_event.get()); } if (new_event->type == SVET_DESTROY) { // Signal the destructor that it is safe to terminate. event_handler_ended_ = true; - delete new_event; // Delete the pointer after it has been processed. return; } - delete new_event; // Delete the pointer after it has been processed. } else { mutex_.unlock(); } @@ -382,9 +376,6 @@ ScrollView::~ScrollView() { } delete semaphore_; delete points_; - for (auto &i : event_table_) { - delete i; - } #endif // !GRAPHICS_DISABLED } @@ -424,18 +415,15 @@ void ScrollView::Signal() { void ScrollView::SetEvent(const SVEvent *svevent) { // Copy event - SVEvent *any = svevent->copy(); - SVEvent *specific = svevent->copy(); + auto any = svevent->copy(); + auto specific = svevent->copy(); any->counter = specific->counter + 1; // Place both events into the queue. std::lock_guard guard(mutex_); - // Delete the old objects.. - delete event_table_[specific->type]; - delete event_table_[SVET_ANY]; - // ...and put the new ones in the table. - event_table_[specific->type] = specific; - event_table_[SVET_ANY] = any; + + event_table_[specific->type] = std::move(specific); + event_table_[SVET_ANY] = std::move(any); } /// Block until an event of the given type is received. diff --git a/src/viewer/scrollview.h b/src/viewer/scrollview.h index f583eba5..b9d06ef0 100644 --- a/src/viewer/scrollview.h +++ b/src/viewer/scrollview.h @@ -70,7 +70,7 @@ struct SVEvent { ~SVEvent() { delete[] parameter; } - SVEvent *copy() const; + std::unique_ptr copy() const; SVEventType type = SVET_DESTROY; // What kind of event. ScrollView *window = nullptr; // Window event relates to. char *parameter = nullptr; // Any string that might have been passed as argument. @@ -414,7 +414,7 @@ private: static SVNetwork *stream_; // Table of all the currently queued events. - SVEvent *event_table_[SVET_COUNT]; + std::unique_ptr event_table_[SVET_COUNT]; // Mutex to access the event_table_ in a synchronized fashion. std::mutex mutex_;