public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [PATCH 17/24] Change how TUI windows are instantiated
Date: Sat, 04 Jan 2020 18:54:00 -0000	[thread overview]
Message-ID: <20200104183410.17114-18-tom@tromey.com> (raw)
In-Reply-To: <20200104183410.17114-1-tom@tromey.com>

This adds a new global that maps from window names to window
constructor functions, and then changes tui_get_window_by_name and
validate_window_name to use it.  This is another step toward
user-defined window types.

2020-01-04  Tom Tromey  <tom@tromey.com>

	* tui/tui-layout.c (make_standard_window, get_locator_window): New
	functions.
	(known_window_types): New global.
	(tui_get_window_by_name): Reimplement.
	(initialize_known_windows): New function.
	(validate_window_name): Rewrite.
	(_initialize_tui_layout): Call initialize_known_windows.

Change-Id: I9037aac550299b9d945899220a30c2d3af9dd0de
---
 gdb/ChangeLog        | 10 +++++
 gdb/tui/tui-layout.c | 99 ++++++++++++++++++++++++++++++--------------
 2 files changed, 77 insertions(+), 32 deletions(-)

diff --git a/gdb/tui/tui-layout.c b/gdb/tui/tui-layout.c
index 898d2f0b8d2..797acc6e8b7 100644
--- a/gdb/tui/tui-layout.c
+++ b/gdb/tui/tui-layout.c
@@ -29,6 +29,7 @@
 #include "cli/cli-decode.h"
 #include "cli/cli-utils.h"
 #include <ctype.h>
+#include <unordered_map>
 #include <unordered_set>
 
 #include "tui/tui.h"
@@ -322,40 +323,74 @@ tui_gen_win_info::resize (int height_, int width_,
 
 \f
 
+/* Helper function to create one of the built-in (non-locator)
+   windows.  */
+
+template<enum tui_win_type V, class T>
+static tui_gen_win_info *
+make_standard_window (const char *)
+{
+  if (tui_win_list[V] == nullptr)
+    tui_win_list[V] = new T ();
+  return tui_win_list[V];
+}
+
+/* Helper function to wrap tui_locator_win_info_ptr for
+   tui_get_window_by_name.  */
+
+static tui_gen_win_info *
+get_locator_window (const char *)
+{
+  return tui_locator_win_info_ptr ();
+}
+
+/* A map holding all the known window types, keyed by name.  Note that
+   this is heap-allocated and "leaked" at gdb exit.  This avoids
+   ordering issues with destroying elements in the map at shutdown.
+   In particular, destroying this map can occur after Python has been
+   shut down, causing crashes if any window destruction requires
+   running Python code.  */
+
+static std::unordered_map<std::string, window_factory> *known_window_types;
+
 /* Helper function that returns a TUI window, given its name.  */
 
 static tui_gen_win_info *
 tui_get_window_by_name (const std::string &name)
 {
-  if (name == "src")
-    {
-      if (tui_win_list[SRC_WIN] == nullptr)
-	tui_win_list[SRC_WIN] = new tui_source_window ();
-      return tui_win_list[SRC_WIN];
-    }
-  else if (name == "cmd")
-    {
-      if (tui_win_list[CMD_WIN] == nullptr)
-	tui_win_list[CMD_WIN] = new tui_cmd_window ();
-      return tui_win_list[CMD_WIN];
-    }
-  else if (name == "regs")
-    {
-      if (tui_win_list[DATA_WIN] == nullptr)
-	tui_win_list[DATA_WIN] = new tui_data_window ();
-      return tui_win_list[DATA_WIN];
-    }
-  else if (name == "asm")
-    {
-      if (tui_win_list[DISASSEM_WIN] == nullptr)
-	tui_win_list[DISASSEM_WIN] = new tui_disasm_window ();
-      return tui_win_list[DISASSEM_WIN];
-    }
-  else
-    {
-      gdb_assert (name == "locator");
-      return tui_locator_win_info_ptr ();
-    }
+  for (tui_win_info *window : saved_tui_windows)
+    if (name == window->name ())
+      return window;
+
+  auto iter = known_window_types->find (name);
+  if (iter == known_window_types->end ())
+    error (_("Unknown window type \"%s\""), name.c_str ());
+
+  tui_gen_win_info *result = iter->second (name.c_str ());
+  if (result == nullptr)
+    error (_("Could not create window \"%s\""), name.c_str ());
+  return result;
+}
+
+/* Initialize the known window types.  */
+
+static void
+initialize_known_windows ()
+{
+  known_window_types = new std::unordered_map<std::string, window_factory>;
+
+  known_window_types->emplace ("src",
+			       make_standard_window<SRC_WIN,
+						    tui_source_window>);
+  known_window_types->emplace ("cmd",
+			       make_standard_window<CMD_WIN, tui_cmd_window>);
+  known_window_types->emplace ("regs",
+			       make_standard_window<DATA_WIN,
+						    tui_data_window>);
+  known_window_types->emplace ("asm",
+			       make_standard_window<DISASSEM_WIN,
+						    tui_disasm_window>);
+  known_window_types->emplace ("locator", get_locator_window);
 }
 
 /* See tui-layout.h.  */
@@ -886,9 +921,8 @@ initialize_layouts ()
 static bool
 validate_window_name (const std::string &name)
 {
-  return (name == "src" || name == "cmd"
-	  || name == "regs" || name == "asm"
-	  || name == "locator");
+  auto iter = known_window_types->find (name);
+  return iter != known_window_types->end ();
 }
 
 /* Implementation of the "tui new-layout" command.  */
@@ -1022,4 +1056,5 @@ to be allocated to the window."),
 	   tui_get_cmd_list ());
 
   initialize_layouts ();
+  initialize_known_windows ();
 }
-- 
2.17.2

  parent reply	other threads:[~2020-01-04 18:54 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-04 18:34 [PATCH 00/24] Horizontal TUI layout + windows in Python Tom Tromey
2020-01-04 18:34 ` [PATCH 24/24] Fix cast in TUI_DISASM_WIN Tom Tromey
2020-01-04 18:34 ` [PATCH 23/24] Add "usage" text to all TUI command help Tom Tromey
2020-01-04 18:34 ` [PATCH 02/24] Simplify tui_add_win_to_layout Tom Tromey
2020-01-04 18:34 ` [PATCH 21/24] Make some tui_source_window_base members "protected" Tom Tromey
2020-01-04 18:34 ` [PATCH 15/24] Remove tui_delete_invisible_windows and tui_make_all_invisible Tom Tromey
2020-01-04 18:34 ` [PATCH 04/24] Simplify TUI C-x 2 binding Tom Tromey
2020-01-04 18:34 ` [PATCH 05/24] Reimplement TUI "C-x 1" binding Tom Tromey
2020-01-04 18:34 ` [PATCH 19/24] Remove the TUI annotation hack Tom Tromey
2020-01-04 18:34 ` [PATCH 08/24] Add the "tui new-layout" command Tom Tromey
2020-01-04 18:44   ` Eli Zaretskii
2020-01-04 18:34 ` [PATCH 20/24] Allow TUI windows in Python Tom Tromey
2020-01-04 18:57   ` Eli Zaretskii
2020-02-22 19:57     ` Tom Tromey
2020-02-22 20:18       ` Eli Zaretskii
2020-03-10 22:23   ` Simon Marchi
2020-03-11  0:23     ` Tom Tromey
2020-03-11  4:47       ` Simon Marchi
2020-03-11  5:07         ` Simon Marchi
2020-03-11 18:05         ` Tom Tromey
2020-01-04 18:34 ` [PATCH 14/24] Handle ambiguity in tui_partial_win_by_name Tom Tromey
2020-01-04 18:34 ` [PATCH 16/24] TUI windows do not need to store their type Tom Tromey
2020-01-04 18:34 ` [PATCH 13/24] Reimplement tui_next_win and tui_prev_win Tom Tromey
2020-01-04 18:34 ` [PATCH 11/24] Add horizontal splitting to TUI layout Tom Tromey
2020-01-04 18:47   ` Eli Zaretskii
2020-01-04 18:34 ` [PATCH 12/24] Change TUI window iteration Tom Tromey
2020-01-04 18:34 ` [PATCH 01/24] Use TUI_DISASM_WIN instead of tui_win_list array Tom Tromey
2020-01-04 18:34 ` [PATCH 07/24] Remove hard-coded TUI layouts Tom Tromey
2020-01-04 18:34 ` [PATCH 22/24] Use error_no_arg in TUI Tom Tromey
2020-01-04 18:34 ` [PATCH 03/24] Fix latent display bug in tui_data_window Tom Tromey
2020-01-04 18:34 ` [PATCH 10/24] Change return type of tui_layout_base::adjust_size Tom Tromey
2020-01-04 18:34 ` [PATCH 09/24] Allow TUI sub-layouts in "new-layout" command Tom Tromey
2020-01-04 18:34 ` [PATCH 06/24] Reimplement "tui reg" command Tom Tromey
2020-01-04 18:34 ` [PATCH 18/24] Remove tui_set_win_focus_to Tom Tromey
2020-01-04 18:54 ` Tom Tromey [this message]
2020-02-22 20:22 ` [PATCH 00/24] Horizontal TUI layout + windows in Python Tom Tromey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200104183410.17114-18-tom@tromey.com \
    --to=tom@tromey.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).