]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Turn wrapped_file into a template
authorTom Tromey <tom@tromey.com>
Tue, 24 Jun 2025 20:20:12 +0000 (14:20 -0600)
committerTom Tromey <tom@tromey.com>
Mon, 9 Feb 2026 15:09:08 +0000 (08:09 -0700)
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 <aburgess@redhat.com>
gdb/pager.h
gdb/tui/tui-io.c
gdb/ui-file.h
gdb/ui.c
gdb/utils.c

index 5d64ffcf16cf882e75d3c2b18a5f6f2c6b2fe719..697134be80512250100c93b40bfec4b502ae3bed 100644 (file)
 
 /* A ui_file that implements output paging and unfiltered output.  */
 
-class pager_file : public wrapped_file
+class pager_file : public wrapped_file<ui_file_up>
 {
 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;
index bf212da92d0b2052fc4a8cadaf5d9ded734b3ad1..7abe4fa3e81632103e1f51a06ef065d180692ded 100644 (file)
@@ -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<tui_file> (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);
index 22badec4abec8353724ed38c72cea7324a7648ef..231c32deafc4843efa5fb42ec658d6eedc14ad52 100644 (file)
@@ -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<typename T>
 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;
+  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<ui_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<ui_file *>
 {
 public:
 
index 6d29026117e9bad1a74fc6a45e8bcd92b2287414..647e63d1bdef951f2d854455ce0ab047a382ccd6 100644 (file)
--- 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<stdio_file> (outstream))),
     m_gdb_stdin (new stdio_file (instream)),
     m_gdb_stderr (new stderr_file (errstream)),
     m_gdb_stdlog (new timestamped_file (m_gdb_stderr)),
index a121a6ea57631941bed75cb3e9811fafb3856f20..80b08f4e211ab55a835edcbaa42bd37ccceafa44 100644 (file)
@@ -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