From: Tom Tromey Date: Tue, 24 Jun 2025 20:20:12 +0000 (-0600) Subject: Turn wrapped_file into a template X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0e186376c474c2c3b8e78c799d51681326284877;p=thirdparty%2Fbinutils-gdb.git Turn wrapped_file into a template This turns wrapped_file into a template, specialized by the type of pointer it holds. This makes it cleaner to have it own the stream it points to. Approved-By: Andrew Burgess --- diff --git a/gdb/pager.h b/gdb/pager.h index 5d64ffcf16c..697134be805 100644 --- a/gdb/pager.h +++ b/gdb/pager.h @@ -23,21 +23,16 @@ /* A ui_file that implements output paging and unfiltered output. */ -class pager_file : public wrapped_file +class pager_file : public wrapped_file { public: /* Create a new pager_file. The new object takes ownership of STREAM. */ - explicit pager_file (ui_file *stream) - : wrapped_file (stream) + explicit pager_file (ui_file_up stream) + : wrapped_file (std::move (stream)) { } - ~pager_file () - { - delete m_stream; - } - DISABLE_COPY_AND_ASSIGN (pager_file); void write (const char *buf, long length_buf) override; diff --git a/gdb/tui/tui-io.c b/gdb/tui/tui-io.c index bf212da92d0..7abe4fa3e81 100644 --- a/gdb/tui/tui-io.c +++ b/gdb/tui/tui-io.c @@ -933,7 +933,7 @@ tui_initialize_io (void) #endif /* Create tui output streams. */ - tui_stdout = new pager_file (new tui_file (stdout, true)); + tui_stdout = new pager_file (std::make_unique (stdout, true)); tui_stderr = new tui_file (stderr, false); tui_stdlog = new timestamped_file (tui_stderr); tui_out = new cli_ui_out (tui_stdout, 0); diff --git a/gdb/ui-file.h b/gdb/ui-file.h index 22badec4abe..231c32deafc 100644 --- a/gdb/ui-file.h +++ b/gdb/ui-file.h @@ -416,8 +416,11 @@ protected: void do_write (const char *buf, long len) override; }; -/* A base class for ui_file types that wrap another ui_file. */ +/* A base class for ui_file types that wrap another ui_file. The + precise underlying ui_file type is a template parameter, so that + either owning or non-owning wrappers can be made. */ +template class wrapped_file : public ui_file { public: @@ -451,22 +454,19 @@ public: protected: - /* Note that this class does not assume ownership of the stream. - However, a subclass may choose to, by adding a 'delete' to its - destructor. */ - explicit wrapped_file (ui_file *stream) - : m_stream (stream) + explicit wrapped_file (T stream) + : m_stream (std::move (stream)) { } /* The underlying stream. */ - ui_file *m_stream; + T m_stream; }; /* A ui_file that optionally puts a timestamp at the start of each line of output. */ -class timestamped_file : public wrapped_file +class timestamped_file : public wrapped_file { public: explicit timestamped_file (ui_file *stream) @@ -490,7 +490,7 @@ private: Note that this only really handles ASCII output correctly. */ -class tab_expansion_file : public wrapped_file +class tab_expansion_file : public wrapped_file { public: diff --git a/gdb/ui.c b/gdb/ui.c index 6d29026117e..647e63d1bde 100644 --- a/gdb/ui.c +++ b/gdb/ui.c @@ -48,7 +48,7 @@ ui::ui (FILE *instream_, FILE *outstream_, FILE *errstream_) errstream (errstream_), input_fd (fileno (instream)), m_input_interactive_p (ISATTY (instream)), - m_gdb_stdout (new pager_file (new stdio_file (outstream))), + m_gdb_stdout (new pager_file (std::make_unique (outstream))), m_gdb_stdin (new stdio_file (instream)), m_gdb_stderr (new stderr_file (errstream)), m_gdb_stdlog (new timestamped_file (m_gdb_stderr)), diff --git a/gdb/utils.c b/gdb/utils.c index a121a6ea576..80b08f4e211 100644 --- a/gdb/utils.c +++ b/gdb/utils.c @@ -1838,7 +1838,7 @@ static void test_pager () { string_file *strfile = new string_file (); - pager_file pager (strfile); + pager_file pager { ui_file_up (strfile) }; /* Make sure the pager is disabled. */ scoped_restore save_enabled