[5/6] Make TUI window handle a unique_ptr

Message ID 20191002231202.10940-6-tom@tromey.com
State New, archived
Headers

Commit Message

Tom Tromey Oct. 2, 2019, 11:12 p.m. UTC
  This changes tui_gen_win_info::handle to be a specialization of
unique_ptr.  This is perhaps mildly uglier in some spots, due to the
proliferation of "get"; but on the other hand it cleans up some manual
management and it allows for the removal of tui_delete_win.

gdb/ChangeLog
2019-10-02  Tom Tromey  <tom@tromey.com>

	* tui/tui-wingeneral.h (tui_delete_win): Don't declare.
	* tui/tui-stack.c (tui_locator_window::rerender): Update.
	* tui/tui-command.c (tui_cmd_window::resize)
	(tui_refresh_cmd_win): Update.
	* tui/tui-win.c (tui_resize_all, tui_set_focus_command): Update.
	* tui/tui.c (tui_rl_other_window, tui_enable): Update.
	* tui/tui-data.c (~tui_gen_win_info): Remove.
	* tui/tui-layout.c (tui_gen_win_info::resize): Update.
	* tui/tui-io.c (update_cmdwin_start_line, tui_putc, tui_puts)
	(tui_redisplay_readline, tui_mld_flush)
	(tui_mld_erase_entire_line, tui_mld_getc, tui_getc): Update.
	* tui/tui-regs.c (tui_data_window::delete_data_content_windows)
	(tui_data_window::erase_data_content)
	(tui_data_item_window::rerender)
	(tui_data_item_window::refresh_window): Update.
	* tui/tui-wingeneral.c (tui_gen_win_info::refresh_window)
	(box_win, tui_gen_win_info::make_window)
	(tui_gen_win_info::make_visible): Update.
	(tui_delete_win): Remove.
	* tui/tui-winsource.c
	(tui_source_window_base::do_erase_source_content): Update.
	(tui_show_source_line, tui_source_window_base::update_tab_width)
	(tui_source_window_base::update_exec_info): Update.
	* tui/tui-data.h (struct curses_deleter): New.
	(struct tui_gen_win_info) <handle>: Now a unique_ptr.
	(struct tui_gen_win_info) <~tui_gen_win_info>: Define.
---
 gdb/ChangeLog            | 29 +++++++++++++++++++++++++++++
 gdb/tui/tui-command.c    |  8 ++++----
 gdb/tui/tui-data.c       |  5 -----
 gdb/tui/tui-data.h       | 15 +++++++++++++--
 gdb/tui/tui-io.c         | 23 ++++++++++-------------
 gdb/tui/tui-layout.c     |  9 ++++-----
 gdb/tui/tui-regs.c       | 29 +++++++++++++----------------
 gdb/tui/tui-stack.c      | 14 +++++++-------
 gdb/tui/tui-win.c        |  6 +++---
 gdb/tui/tui-wingeneral.c | 22 +++++-----------------
 gdb/tui/tui-wingeneral.h |  1 -
 gdb/tui/tui-winsource.c  | 22 +++++++++++-----------
 gdb/tui/tui.c            |  6 +++---
 13 files changed, 102 insertions(+), 87 deletions(-)
  

Patch

diff --git a/gdb/tui/tui-command.c b/gdb/tui/tui-command.c
index 62595808cd8..9a432971339 100644
--- a/gdb/tui/tui-command.c
+++ b/gdb/tui/tui-command.c
@@ -64,10 +64,10 @@  tui_cmd_window::resize (int height_, int width_, int origin_x, int origin_y)
 	 it.  However we can at least move it and keep the old size if
 	 wresize isn't available.  */
 #ifdef HAVE_WRESIZE
-      wresize (handle, height, width);
+      wresize (handle.get (), height, width);
 #endif
-      mvwin (handle, origin.y, origin.x);
-      wmove (handle, 0, 0);
+      mvwin (handle.get (), origin.y, origin.x);
+      wmove (handle.get (), 0, 0);
     }
 }
 
@@ -76,7 +76,7 @@  tui_cmd_window::resize (int height_, int width_, int origin_x, int origin_y)
 void
 tui_refresh_cmd_win (void)
 {
-  WINDOW *w = TUI_CMD_WIN->handle;
+  WINDOW *w = TUI_CMD_WIN->handle.get ();
 
   wrefresh (w);
 
diff --git a/gdb/tui/tui-data.c b/gdb/tui/tui-data.c
index f1f3947e028..522bb9acebd 100644
--- a/gdb/tui/tui-data.c
+++ b/gdb/tui/tui-data.c
@@ -205,11 +205,6 @@  tui_win_info::tui_win_info (enum tui_win_type type)
 {
 }
 
-tui_gen_win_info::~tui_gen_win_info ()
-{
-  tui_delete_win (handle);
-}
-
 void
 tui_win_info::rerender ()
 {
diff --git a/gdb/tui/tui-data.h b/gdb/tui/tui-data.h
index 6d4fb78334f..4710c760211 100644
--- a/gdb/tui/tui-data.h
+++ b/gdb/tui/tui-data.h
@@ -36,6 +36,15 @@  struct tui_point
   int x, y;
 };
 
+/* A deleter that calls delwin.  */
+struct curses_deleter
+{
+  void operator() (WINDOW *win) const
+  {
+    delwin (win);
+  }
+};
+
 /* Generic window information.  */
 struct tui_gen_win_info
 {
@@ -57,7 +66,9 @@  protected:
 public:
   tui_gen_win_info (tui_gen_win_info &&) = default;
 
-  virtual ~tui_gen_win_info ();
+  virtual ~tui_gen_win_info ()
+  {
+  }
 
   /* Call to refresh this window.  */
   virtual void refresh_window ();
@@ -83,7 +94,7 @@  public:
   }
 
   /* Window handle.  */
-  WINDOW *handle = nullptr;
+  std::unique_ptr<WINDOW, curses_deleter> handle;
   /* Type of window.  */
   enum tui_win_type type;
   /* Window width.  */
diff --git a/gdb/tui/tui-io.c b/gdb/tui/tui-io.c
index ee581a2ff66..6bb495beaa9 100644
--- a/gdb/tui/tui-io.c
+++ b/gdb/tui/tui-io.c
@@ -179,8 +179,7 @@  do_tui_putc (WINDOW *w, char c)
 static void
 update_cmdwin_start_line ()
 {
-  TUI_CMD_WIN->start_line
-    = getcury (TUI_CMD_WIN->handle);
+  TUI_CMD_WIN->start_line = getcury (TUI_CMD_WIN->handle.get ());
 }
 
 /* Print a character in the curses command window.  The output is
@@ -190,9 +189,7 @@  update_cmdwin_start_line ()
 static void
 tui_putc (char c)
 {
-  WINDOW *w = TUI_CMD_WIN->handle;
-
-  do_tui_putc (w, c);
+  do_tui_putc (TUI_CMD_WIN->handle.get (), c);
   update_cmdwin_start_line ();
 }
 
@@ -495,7 +492,7 @@  tui_puts_internal (WINDOW *w, const char *string, int *height)
 	    }
 	}
     }
-  if (TUI_CMD_WIN != nullptr && w == TUI_CMD_WIN->handle)
+  if (TUI_CMD_WIN != nullptr && w == TUI_CMD_WIN->handle.get ())
     update_cmdwin_start_line ();
   if (saw_nl)
     wrefresh (w);
@@ -509,7 +506,7 @@  void
 tui_puts (const char *string, WINDOW *w)
 {
   if (w == nullptr)
-    w = TUI_CMD_WIN->handle;
+    w = TUI_CMD_WIN->handle.get ();
   tui_puts_internal (w, string, nullptr);
 }
 
@@ -545,13 +542,13 @@  tui_redisplay_readline (void)
   
   c_pos = -1;
   c_line = -1;
-  w = TUI_CMD_WIN->handle;
+  w = TUI_CMD_WIN->handle.get ();
   start_line = TUI_CMD_WIN->start_line;
   wmove (w, start_line, 0);
   prev_col = 0;
   height = 1;
   if (prompt != nullptr)
-    tui_puts_internal (TUI_CMD_WIN->handle, prompt, &height);
+    tui_puts_internal (w, prompt, &height);
 
   prev_col = getcurx (w);
   for (in = 0; in <= rl_end; in++)
@@ -670,7 +667,7 @@  tui_mld_puts (const struct match_list_displayer *displayer, const char *s)
 static void
 tui_mld_flush (const struct match_list_displayer *displayer)
 {
-  wrefresh (TUI_CMD_WIN->handle);
+  wrefresh (TUI_CMD_WIN->handle.get ());
 }
 
 /* TUI version of displayer.erase_entire_line.  */
@@ -678,7 +675,7 @@  tui_mld_flush (const struct match_list_displayer *displayer)
 static void
 tui_mld_erase_entire_line (const struct match_list_displayer *displayer)
 {
-  WINDOW *w = TUI_CMD_WIN->handle;
+  WINDOW *w = TUI_CMD_WIN->handle.get ();
   int cur_y = getcury (w);
 
   wmove (w, cur_y, 0);
@@ -716,7 +713,7 @@  gdb_wgetch (WINDOW *win)
 static int
 tui_mld_getc (FILE *fp)
 {
-  WINDOW *w = TUI_CMD_WIN->handle;
+  WINDOW *w = TUI_CMD_WIN->handle.get ();
   int c = gdb_wgetch (w);
 
   return c;
@@ -970,7 +967,7 @@  tui_getc (FILE *fp)
   int ch;
   WINDOW *w;
 
-  w = TUI_CMD_WIN->handle;
+  w = TUI_CMD_WIN->handle.get ();
 
 #ifdef TUI_USE_PIPE_FOR_READLINE
   /* Flush readline output.  */
diff --git a/gdb/tui/tui-layout.c b/gdb/tui/tui-layout.c
index ccc750e1db4..3a510f436d2 100644
--- a/gdb/tui/tui-layout.c
+++ b/gdb/tui/tui-layout.c
@@ -511,12 +511,11 @@  tui_gen_win_info::resize (int height_, int width_,
   if (handle != nullptr)
     {
 #ifdef HAVE_WRESIZE
-      wresize (handle, height, width);
-      mvwin (handle, origin.y, origin.x);
-      wmove (handle, 0, 0);
+      wresize (handle.get (), height, width);
+      mvwin (handle.get (), origin.y, origin.x);
+      wmove (handle.get (), 0, 0);
 #else
-      tui_delete_win (handle);
-      handle = NULL;
+      handle.reset (nullptr);
 #endif
     }
 
diff --git a/gdb/tui/tui-regs.c b/gdb/tui/tui-regs.c
index c4769cbcb43..474b62e2047 100644
--- a/gdb/tui/tui-regs.c
+++ b/gdb/tui/tui-regs.c
@@ -361,17 +361,14 @@  void
 tui_data_window::delete_data_content_windows ()
 {
   for (auto &&win : m_regs_content)
-    {
-      tui_delete_win (win.handle);
-      win.handle = NULL;
-    }
+    win.handle.reset (nullptr);
 }
 
 
 void
 tui_data_window::erase_data_content (const char *prompt)
 {
-  werase (handle);
+  werase (handle.get ());
   check_and_display_highlight_if_needed ();
   if (prompt != NULL)
     {
@@ -382,9 +379,9 @@  tui_data_window::erase_data_content (const char *prompt)
 	x_pos = 1;
       else
 	x_pos = half_width - strlen (prompt);
-      mvwaddstr (handle, (height / 2), x_pos, (char *) prompt);
+      mvwaddstr (handle.get (), (height / 2), x_pos, (char *) prompt);
     }
-  wrefresh (handle);
+  wrefresh (handle.get ());
 }
 
 /* See tui-regs.h.  */
@@ -470,21 +467,21 @@  tui_data_item_window::rerender ()
 {
   int i;
 
-  scrollok (handle, FALSE);
+  scrollok (handle.get (), FALSE);
   if (highlight)
     /* We ignore the return value, casting it to void in order to avoid
        a compiler warning.  The warning itself was introduced by a patch
        to ncurses 5.7 dated 2009-08-29, changing this macro to expand
        to code that causes the compiler to generate an unused-value
        warning.  */
-    (void) wstandout (handle);
+    (void) wstandout (handle.get ());
       
-  wmove (handle, 0, 0);
+  wmove (handle.get (), 0, 0);
   for (i = 1; i < width; i++)
-    waddch (handle, ' ');
-  wmove (handle, 0, 0);
+    waddch (handle.get (), ' ');
+  wmove (handle.get (), 0, 0);
   if (content)
-    waddstr (handle, content.get ());
+    waddstr (handle.get (), content.get ());
 
   if (highlight)
     /* We ignore the return value, casting it to void in order to avoid
@@ -492,7 +489,7 @@  tui_data_item_window::rerender ()
        to ncurses 5.7 dated 2009-08-29, changing this macro to expand
        to code that causes the compiler to generate an unused-value
        warning.  */
-    (void) wstandend (handle);
+    (void) wstandend (handle.get ());
   refresh_window ();
 }
 
@@ -504,8 +501,8 @@  tui_data_item_window::refresh_window ()
       /* This seems to be needed because the data items are nested
 	 windows, which according to the ncurses man pages aren't well
 	 supported.  */
-      touchwin (handle);
-      wrefresh (handle);
+      touchwin (handle.get ());
+      wrefresh (handle.get ());
     }
 }
 
diff --git a/gdb/tui/tui-stack.c b/gdb/tui/tui-stack.c
index d66e3589e42..078f81992ab 100644
--- a/gdb/tui/tui-stack.c
+++ b/gdb/tui/tui-stack.c
@@ -229,19 +229,19 @@  tui_locator_window::rerender ()
   if (handle != NULL)
     {
       std::string string = make_status_line ();
-      scrollok (handle, FALSE);
-      wmove (handle, 0, 0);
+      scrollok (handle.get (), FALSE);
+      wmove (handle.get (), 0, 0);
       /* We ignore the return value from wstandout and wstandend, casting
 	 them to void in order to avoid a compiler warning.  The warning
 	 itself was introduced by a patch to ncurses 5.7 dated 2009-08-29,
 	 changing these macro to expand to code that causes the compiler
 	 to generate an unused-value warning.  */
-      (void) wstandout (handle);
-      waddstr (handle, string.c_str ());
-      wclrtoeol (handle);
-      (void) wstandend (handle);
+      (void) wstandout (handle.get ());
+      waddstr (handle.get (), string.c_str ());
+      wclrtoeol (handle.get ());
+      (void) wstandend (handle.get ());
       refresh_window ();
-      wmove (handle, 0, 0);
+      wmove (handle.get (), 0, 0);
     }
 }
 
diff --git a/gdb/tui/tui-win.c b/gdb/tui/tui-win.c
index 37e22c550f9..41c61f12b21 100644
--- a/gdb/tui/tui-win.c
+++ b/gdb/tui/tui-win.c
@@ -529,7 +529,7 @@  tui_resize_all (void)
 #endif      
       /* Turn keypad off while we resize.  */
       if (win_with_focus != TUI_CMD_WIN)
-	keypad (TUI_CMD_WIN->handle, FALSE);
+	keypad (TUI_CMD_WIN->handle.get (), FALSE);
       tui_update_gdb_sizes ();
       tui_set_term_height_to (screenheight);
       tui_set_term_width_to (screenwidth);
@@ -639,7 +639,7 @@  tui_resize_all (void)
       /* Turn keypad back on, unless focus is in the command
 	 window.  */
       if (win_with_focus != TUI_CMD_WIN)
-	keypad (TUI_CMD_WIN->handle, TRUE);
+	keypad (TUI_CMD_WIN->handle.get (), TRUE);
     }
 }
 
@@ -791,7 +791,7 @@  tui_set_focus_command (const char *arg, int from_tty)
 	error (_("Window \"%s\" is not visible"), arg);
 
       tui_set_win_focus_to (win_info);
-      keypad (TUI_CMD_WIN->handle, (win_info != TUI_CMD_WIN));
+      keypad (TUI_CMD_WIN->handle.get (), win_info != TUI_CMD_WIN);
       printf_filtered (_("Focus set to %s window.\n"),
 		       tui_win_with_focus ()->name ());
     }
diff --git a/gdb/tui/tui-wingeneral.c b/gdb/tui/tui-wingeneral.c
index 713059d6632..b6dd3f9b26a 100644
--- a/gdb/tui/tui-wingeneral.c
+++ b/gdb/tui/tui-wingeneral.c
@@ -34,18 +34,9 @@  void
 tui_gen_win_info::refresh_window ()
 {
   if (handle != NULL)
-    wrefresh (handle);
+    wrefresh (handle.get ());
 }
 
-/* Function to delete the curses window, checking for NULL.  */
-void
-tui_delete_win (WINDOW *window)
-{
-  if (window != NULL)
-    delwin (window);
-}
-
-
 /* Draw a border arround the window.  */
 static void
 box_win (struct tui_win_info *win_info, 
@@ -54,7 +45,7 @@  box_win (struct tui_win_info *win_info,
   WINDOW *win;
   int attrs;
 
-  win = win_info->handle;
+  win = win_info->handle.get ();
   if (highlight_flag)
     attrs = tui_active_border_attrs;
   else
@@ -132,9 +123,9 @@  tui_win_info::check_and_display_highlight_if_needed ()
 void
 tui_gen_win_info::make_window ()
 {
-  handle = newwin (height, width, origin.y, origin.x);
+  handle.reset (newwin (height, width, origin.y, origin.x));
   if (handle != NULL)
-    scrollok (handle, TRUE);
+    scrollok (handle.get (), TRUE);
 }
 
 void
@@ -157,10 +148,7 @@  tui_gen_win_info::make_visible (bool visible)
   if (visible)
     make_window ();
   else
-    {
-      tui_delete_win (handle);
-      handle = NULL;
-    }
+    handle.reset (nullptr);
 }
 
 /* See tui-wingeneral.h.  */
diff --git a/gdb/tui/tui-wingeneral.h b/gdb/tui/tui-wingeneral.h
index 1831d7b7cb6..9995250ce9a 100644
--- a/gdb/tui/tui-wingeneral.h
+++ b/gdb/tui/tui-wingeneral.h
@@ -32,6 +32,5 @@  extern void tui_make_all_invisible (void);
 extern void tui_unhighlight_win (struct tui_win_info *);
 extern void tui_highlight_win (struct tui_win_info *);
 extern void tui_refresh_all ();
-extern void tui_delete_win (WINDOW *window);
 
 #endif /* TUI_TUI_WINGENERAL_H */
diff --git a/gdb/tui/tui-winsource.c b/gdb/tui/tui-winsource.c
index 683856de817..f1c9f958a99 100644
--- a/gdb/tui/tui-winsource.c
+++ b/gdb/tui/tui-winsource.c
@@ -193,14 +193,14 @@  tui_source_window_base::do_erase_source_content (const char *str)
   content.clear ();
   if (handle != NULL)
     {
-      werase (handle);
+      werase (handle.get ());
       check_and_display_highlight_if_needed ();
 
       if (strlen (str) >= half_width)
 	x_pos = 1;
       else
 	x_pos = half_width - strlen (str);
-      mvwaddstr (handle,
+      mvwaddstr (handle.get (),
 		 (height / 2),
 		 x_pos,
 		 (char *) str);
@@ -219,19 +219,19 @@  tui_show_source_line (struct tui_source_window_base *win_info, int lineno)
 
   line = &win_info->content[lineno - 1];
   if (line->is_exec_point)
-    tui_set_reverse_mode (win_info->handle, true);
+    tui_set_reverse_mode (win_info->handle.get (), true);
 
-  wmove (win_info->handle, lineno, TUI_EXECINFO_SIZE);
-  tui_puts (line->line.get (), win_info->handle);
+  wmove (win_info->handle.get (), lineno, TUI_EXECINFO_SIZE);
+  tui_puts (line->line.get (), win_info->handle.get ());
   if (line->is_exec_point)
-    tui_set_reverse_mode (win_info->handle, false);
+    tui_set_reverse_mode (win_info->handle.get (), false);
 
   /* Clear to end of line but stop before the border.  */
-  x = getcurx (win_info->handle);
+  x = getcurx (win_info->handle.get ());
   while (x + 1 < win_info->width)
     {
-      waddch (win_info->handle, ' ');
-      x = getcurx (win_info->handle);
+      waddch (win_info->handle.get (), ' ');
+      x = getcurx (win_info->handle.get ());
     }
 }
 
@@ -261,7 +261,7 @@  tui_source_window_base::tui_source_window_base (enum tui_win_type type)
 void
 tui_source_window_base::update_tab_width ()
 {
-  werase (handle);
+  werase (handle.get ());
   rerender ();
 }
 
@@ -479,7 +479,7 @@  tui_source_window_base::update_exec_info ()
       if (src_element->is_exec_point)
 	element[TUI_EXEC_POS] = '>';
 
-      mvwaddstr (handle, i + 1, 1, element);
+      mvwaddstr (handle.get (), i + 1, 1, element);
     }
   refresh_window ();
 }
diff --git a/gdb/tui/tui.c b/gdb/tui/tui.c
index 30bf5488794..e765d58f97d 100644
--- a/gdb/tui/tui.c
+++ b/gdb/tui/tui.c
@@ -237,7 +237,7 @@  tui_rl_other_window (int count, int key)
   if (win_info)
     {
       tui_set_win_focus_to (win_info);
-      keypad (TUI_CMD_WIN->handle, (win_info != TUI_CMD_WIN));
+      keypad (TUI_CMD_WIN->handle.get (), win_info != TUI_CMD_WIN);
     }
   return 0;
 }
@@ -478,8 +478,8 @@  tui_enable (void)
       tui_show_frame_info (0);
       tui_set_layout (SRC_COMMAND);
       tui_set_win_focus_to (TUI_SRC_WIN);
-      keypad (TUI_CMD_WIN->handle, TRUE);
-      wrefresh (TUI_CMD_WIN->handle);
+      keypad (TUI_CMD_WIN->handle.get (), TRUE);
+      wrefresh (TUI_CMD_WIN->handle.get ());
       tui_finish_init = 0;
     }
   else