@@ -56,12 +56,12 @@ struct SVPolyLineBuffer {
5656static std::map<int , ScrollView *> svmap;
5757static std::mutex *svmap_mu;
5858// A map of all semaphores waiting for a specific event on a specific window.
59- static std::map<std::pair<ScrollView *, SVEventType>, std::pair<SVSemaphore *, SVEvent *>>
60- waiting_for_events;
59+ static std::map<std::pair<ScrollView *, SVEventType>,
60+ std::pair<SVSemaphore *, std::unique_ptr<SVEvent>>> waiting_for_events;
6161static std::mutex *waiting_for_events_mu;
6262
63- SVEvent * SVEvent::copy () const {
64- auto * any = new SVEvent;
63+ std::unique_ptr< SVEvent> SVEvent::copy () const {
64+ auto any = std::unique_ptr<SVEvent>( new SVEvent) ;
6565 any->command_id = command_id;
6666 any->counter = counter;
6767 any->parameter = new char [strlen (parameter) + 1 ];
@@ -158,13 +158,13 @@ void ScrollView::MessageReceiver() {
158158 SVET_ANY);
159159 waiting_for_events_mu->lock ();
160160 if (waiting_for_events.count (awaiting_list) > 0 ) {
161- waiting_for_events[awaiting_list].second = cur. get ( );
161+ waiting_for_events[awaiting_list].second = std::move (cur );
162162 waiting_for_events[awaiting_list].first ->Signal ();
163163 } else if (waiting_for_events.count (awaiting_list_any) > 0 ) {
164- waiting_for_events[awaiting_list_any].second = cur. get ( );
164+ waiting_for_events[awaiting_list_any].second = std::move (cur );
165165 waiting_for_events[awaiting_list_any].first ->Signal ();
166166 } else if (waiting_for_events.count (awaiting_list_any_window) > 0 ) {
167- waiting_for_events[awaiting_list_any_window].second = cur. get ( );
167+ waiting_for_events[awaiting_list_any_window].second = std::move (cur );
168168 waiting_for_events[awaiting_list_any_window].first ->Signal ();
169169 }
170170 waiting_for_events_mu->unlock ();
@@ -319,38 +319,32 @@ void ScrollView::Initialize(const char *name, int x_pos, int y_pos, int x_size,
319319
320320// / Sits and waits for events on this window.
321321void ScrollView::StartEventHandler () {
322- SVEvent *new_event;
323-
324322 for (;;) {
325323 stream_->Flush ();
326324 semaphore_->Wait ();
327- new_event = nullptr ;
328325 int serial = -1 ;
329326 int k = -1 ;
330327 mutex_.lock ();
331328 // Check every table entry if it is valid and not already processed.
332329
333330 for (int i = 0 ; i < SVET_COUNT; i++) {
334331 if (event_table_[i] != nullptr && (serial < 0 || event_table_[i]->counter < serial)) {
335- new_event = event_table_[i];
336332 serial = event_table_[i]->counter ;
337333 k = i;
338334 }
339335 }
340336 // If we didn't find anything we had an old alarm and just sleep again.
341- if (new_event != nullptr ) {
342- event_table_[k] = nullptr ;
337+ if (k != - 1 ) {
338+ auto new_event = std::move ( event_table_[k]) ;
343339 mutex_.unlock ();
344340 if (event_handler_ != nullptr ) {
345- event_handler_->Notify (new_event);
341+ event_handler_->Notify (new_event. get () );
346342 }
347343 if (new_event->type == SVET_DESTROY) {
348344 // Signal the destructor that it is safe to terminate.
349345 event_handler_ended_ = true ;
350- delete new_event; // Delete the pointer after it has been processed.
351346 return ;
352347 }
353- delete new_event; // Delete the pointer after it has been processed.
354348 } else {
355349 mutex_.unlock ();
356350 }
@@ -367,8 +361,7 @@ ScrollView::~ScrollView() {
367361 // So the event handling thread can quit.
368362 SendMsg (" destroy()" );
369363
370- SVEvent *sve = AwaitEvent (SVET_DESTROY);
371- delete sve;
364+ AwaitEvent (SVET_DESTROY);
372365 svmap_mu->lock ();
373366 svmap[window_id_] = nullptr ;
374367 svmap_mu->unlock ();
@@ -383,9 +376,6 @@ ScrollView::~ScrollView() {
383376 }
384377 delete semaphore_;
385378 delete points_;
386- for (auto &i : event_table_) {
387- delete i;
388- }
389379#endif // !GRAPHICS_DISABLED
390380}
391381
@@ -425,36 +415,33 @@ void ScrollView::Signal() {
425415
426416void ScrollView::SetEvent (const SVEvent *svevent) {
427417 // Copy event
428- SVEvent * any = svevent->copy ();
429- SVEvent * specific = svevent->copy ();
418+ auto any = svevent->copy ();
419+ auto specific = svevent->copy ();
430420 any->counter = specific->counter + 1 ;
431421
432422 // Place both events into the queue.
433423 std::lock_guard<std::mutex> guard (mutex_);
434- // Delete the old objects..
435- delete event_table_[specific->type ];
436- delete event_table_[SVET_ANY];
437- // ...and put the new ones in the table.
438- event_table_[specific->type ] = specific;
439- event_table_[SVET_ANY] = any;
424+
425+ event_table_[specific->type ] = std::move (specific);
426+ event_table_[SVET_ANY] = std::move (any);
440427}
441428
442429// / Block until an event of the given type is received.
443430// / Note: The calling function is responsible for deleting the returned
444431// / SVEvent afterwards!
445- SVEvent * ScrollView::AwaitEvent (SVEventType type) {
432+ std::unique_ptr< SVEvent> ScrollView::AwaitEvent (SVEventType type) {
446433 // Initialize the waiting semaphore.
447434 auto *sem = new SVSemaphore ();
448435 std::pair<ScrollView *, SVEventType> ea (this , type);
449436 waiting_for_events_mu->lock ();
450- waiting_for_events[ea] = std::pair<SVSemaphore *, SVEvent *>( sem, (SVEvent *) nullptr ) ;
437+ waiting_for_events[ea] = { sem, nullptr } ;
451438 waiting_for_events_mu->unlock ();
452439 // Wait on it, but first flush.
453440 stream_->Flush ();
454441 sem->Wait ();
455442 // Process the event we got woken up for (its in waiting_for_events pair).
456443 waiting_for_events_mu->lock ();
457- SVEvent * ret = waiting_for_events[ea].second ;
444+ auto ret = std::move ( waiting_for_events[ea].second ) ;
458445 waiting_for_events.erase (ea);
459446 delete sem;
460447 waiting_for_events_mu->unlock ();
@@ -734,23 +721,19 @@ void ScrollView::Brush(Color color) {
734721// Shows a modal Input Dialog which can return any kind of String
735722char *ScrollView::ShowInputDialog (const char *msg) {
736723 SendMsg (" showInputDialog(\" %s\" )" , msg);
737- SVEvent *ev;
738724 // wait till an input event (all others are thrown away)
739- ev = AwaitEvent (SVET_INPUT);
725+ auto ev = AwaitEvent (SVET_INPUT);
740726 char *p = new char [strlen (ev->parameter ) + 1 ];
741727 strcpy (p, ev->parameter );
742- delete ev;
743728 return p;
744729}
745730
746731// Shows a modal Yes/No Dialog which will return 'y' or 'n'
747732int ScrollView::ShowYesNoDialog (const char *msg) {
748733 SendMsg (" showYesNoDialog(\" %s\" )" , msg);
749- SVEvent *ev;
750734 // Wait till an input event (all others are thrown away)
751- ev = AwaitEvent (SVET_INPUT);
735+ auto ev = AwaitEvent (SVET_INPUT);
752736 int a = ev->parameter [0 ];
753- delete ev;
754737 return a;
755738}
756739
0 commit comments