Skip to content

Commit

Permalink
viewer: Use std::unique_ptr in event_table_ data structure
Browse files Browse the repository at this point in the history
  • Loading branch information
p12tic committed Jul 18, 2022
1 parent 9a74c4c commit 0107687
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 24 deletions.
32 changes: 10 additions & 22 deletions src/viewer/scrollview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ static std::map<std::pair<ScrollView *, SVEventType>,
std::pair<SVSemaphore *, std::unique_ptr<SVEvent>>> waiting_for_events;
static std::mutex *waiting_for_events_mu;

SVEvent *SVEvent::copy() const {
auto *any = new SVEvent;
std::unique_ptr<SVEvent> SVEvent::copy() const {
auto any = std::unique_ptr<SVEvent>(new SVEvent);
any->command_id = command_id;
any->counter = counter;
any->parameter = new char[strlen(parameter) + 1];
Expand Down Expand Up @@ -319,38 +319,32 @@ 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();
// Check every table entry if it is valid and not already processed.

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();
}
Expand Down Expand Up @@ -382,9 +376,6 @@ ScrollView::~ScrollView() {
}
delete semaphore_;
delete points_;
for (auto &i : event_table_) {
delete i;
}
#endif // !GRAPHICS_DISABLED
}

Expand Down Expand Up @@ -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<std::mutex> 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.
Expand Down
4 changes: 2 additions & 2 deletions src/viewer/scrollview.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ struct SVEvent {
~SVEvent() {
delete[] parameter;
}
SVEvent *copy() const;
std::unique_ptr<SVEvent> 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.
Expand Down Expand Up @@ -414,7 +414,7 @@ class TESS_API ScrollView {
static SVNetwork *stream_;

// Table of all the currently queued events.
SVEvent *event_table_[SVET_COUNT];
std::unique_ptr<SVEvent> event_table_[SVET_COUNT];

// Mutex to access the event_table_ in a synchronized fashion.
std::mutex mutex_;
Expand Down

0 comments on commit 0107687

Please sign in to comment.