[RFA,2/4] Change the as_*_interp functions to use dynamic_cast

Message ID 20180430051207.19979-3-tom@tromey.com
State New, archived
Headers

Commit Message

Tom Tromey April 30, 2018, 5:12 a.m. UTC
  This changes the various as_*_interp functions to be implemented using
dynamic_cast.  I believe this is a small improvement, because it is
more typesafe -- the C++ runtime does the type-checking for us.

ChangeLog
2018-04-29  Tom Tromey  <tom@tromey.com>

	* tui/tui-interp.c (as_tui_interp): Use dynamic_cast.
	* mi/mi-interp.c (as_mi_interp): Use dynamic_cast.
	* cli/cli-interp.c (as_cli_interp): Use dynamic_cast.
---
 gdb/ChangeLog        | 6 ++++++
 gdb/cli/cli-interp.c | 4 +---
 gdb/mi/mi-interp.c   | 4 +---
 gdb/tui/tui-interp.c | 4 +---
 4 files changed, 9 insertions(+), 9 deletions(-)
  

Comments

Pedro Alves May 25, 2018, 6:02 p.m. UTC | #1
On 04/30/2018 06:12 AM, Tom Tromey wrote:
> This changes the various as_*_interp functions to be implemented using
> dynamic_cast.  I believe this is a small improvement, because it is
> more typesafe -- the C++ runtime does the type-checking for us.

OK.

Thanks,
Pedro Alves
  

Patch

diff --git a/gdb/cli/cli-interp.c b/gdb/cli/cli-interp.c
index 0663301c19..e12d45b4b7 100644
--- a/gdb/cli/cli-interp.c
+++ b/gdb/cli/cli-interp.c
@@ -73,9 +73,7 @@  struct cli_suppress_notification cli_suppress_notification =
 static struct cli_interp *
 as_cli_interp (struct interp *interp)
 {
-  if (strcmp (interp_name (interp), INTERP_CONSOLE) == 0)
-    return (struct cli_interp *) interp;
-  return NULL;
+  return dynamic_cast<cli_interp *> (interp);
 }
 
 /* Longjmp-safe wrapper for "execute_command".  */
diff --git a/gdb/mi/mi-interp.c b/gdb/mi/mi-interp.c
index 8bfb1298a5..7536817f43 100644
--- a/gdb/mi/mi-interp.c
+++ b/gdb/mi/mi-interp.c
@@ -106,9 +106,7 @@  display_mi_prompt (struct mi_interp *mi)
 static struct mi_interp *
 as_mi_interp (struct interp *interp)
 {
-  if (interp_ui_out (interp)->is_mi_like_p ())
-    return (struct mi_interp *) interp;
-  return NULL;
+  return dynamic_cast<mi_interp *> (interp);
 }
 
 void
diff --git a/gdb/tui/tui-interp.c b/gdb/tui/tui-interp.c
index cf32c905c5..aa5e328ded 100644
--- a/gdb/tui/tui-interp.c
+++ b/gdb/tui/tui-interp.c
@@ -58,9 +58,7 @@  public:
 static tui_interp *
 as_tui_interp (struct interp *interp)
 {
-  if (strcmp (interp_name (interp), INTERP_TUI) == 0)
-    return (tui_interp *) interp;
-  return NULL;
+  return dynamic_cast<tui_interp *> (interp);
 }
 
 /* Cleanup the tui before exiting.  */