write_context: allow mutating the ostream used
authorMatthias Maennich <maennich@google.com>
Tue, 21 May 2019 04:39:18 +0000 (05:39 +0100)
committerDodji Seketeli <dodji@redhat.com>
Wed, 22 May 2019 12:33:45 +0000 (14:33 +0200)
Allowing the mutation of the ostream, allows heavy reuse of the
write_context as this is the distinction in most places where
write_context is used.

Hence, fixup various users of write_context and use common objects where
applicable.

* include/abg-writer.h (set_ostream): Declare new function.
* src/abg-writer.cc (write_context::m_ostream): Make this data
member be a pointer rather than a reference.
(write_context::{write_context, get_ostream): Adjust.  member.
(write_context::set_ostream): Define new member function.
(set_ostream): Define new free-form function.
* tools/abidw.cc (load_corpus_and_write_abixml)
(load_kernel_corpus_group_and_write_abixml): Use the feature of
mutating the ostream and reuse the write_context in most cases.

Signed-off-by: Matthias Maennich <maennich@google.com>
include/abg-writer.h
src/abg-writer.cc
tools/abidw.cc

index 45303e4..dddb554 100644 (file)
@@ -53,6 +53,9 @@ set_show_locs(write_context& ctxt, bool flag);
 void
 set_annotate(write_context& ctxt, bool flag);
 
+void
+set_ostream(write_context& ctxt, ostream& os);
+
 bool
 write_translation_unit(write_context&         ctxt,
                       const translation_unit& tu,
index 8280e23..d67e64f 100644 (file)
@@ -162,7 +162,7 @@ class write_context
   const environment*                   m_env;
   id_manager                           m_id_manager;
   config                               m_config;
-  ostream&                             m_ostream;
+  ostream*                             m_ostream;
   bool                                 m_annotate;
   bool                                 m_show_locs;
   mutable type_ptr_map                 m_type_id_map;
@@ -191,7 +191,7 @@ public:
   write_context(const environment* env, ostream& os)
     : m_env(env),
       m_id_manager(env),
-      m_ostream(os),
+      m_ostream(&os),
       m_annotate(false),
       m_show_locs(true)
   {}
@@ -207,9 +207,19 @@ public:
   get_config() const
   {return m_config;}
 
+  /// Getter for the current ostream
+  ///
+  /// @return a reference to the current ostream
   ostream&
   get_ostream()
-  {return m_ostream;}
+  {return *m_ostream;}
+
+  /// Setter for the current ostream
+  ///
+  /// @param os the new ostream
+  void
+  set_ostream(ostream& os)
+  {m_ostream = &os;}
 
   /// Getter of the annotation option.
   ///
@@ -1742,6 +1752,17 @@ void
 set_annotate(write_context& ctxt, bool flag)
 {ctxt.set_annotate(flag);}
 
+/// Set the new ostream.
+///
+/// The ostream refers to the object, writers should stream new output to.
+///
+/// @param ctxt the context to set this to.
+///
+/// @param os the new ostream
+void
+set_ostream(write_context& ctxt, ostream& os)
+{ctxt.set_ostream(os);}
+
 /// Serialize a translation unit to an output stream.
 ///
 /// @param ctxt the context of the serialization.  It contains e.g,
index d470886..2ff6cf3 100644 (file)
@@ -442,15 +442,18 @@ load_corpus_and_write_abixml(char* argv[],
     }
   else
     {
+      const write_context_sptr& write_ctxt
+         = create_write_context(corp->get_environment(), cout);
+      set_annotate(*write_ctxt, opts.annotate);
+      set_show_locs(*write_ctxt, opts.show_locs);
+
       if (opts.abidiff)
        {
          // Save the abi in abixml format in a temporary file, read
          // it back, and compare the ABI of what we've read back
          // against the ABI of the input ELF file.
          temp_file_sptr tmp_file = temp_file::create();
-         const write_context_sptr& write_ctxt = create_write_context(
-             corp->get_environment(), tmp_file->get_stream());
-         set_annotate(*write_ctxt, opts.annotate);
+         set_ostream(*write_ctxt, tmp_file->get_stream());
          write_corpus(*write_ctxt, corp, 0);
          tmp_file->get_stream().flush();
          corpus_sptr corp2 =
@@ -494,20 +497,13 @@ load_corpus_and_write_abixml(char* argv[],
                << opts.out_file_path << "'\n";
              return 1;
            }
-         const write_context_sptr& write_ctxt
-             = create_write_context(corp->get_environment(), of);
-         set_show_locs(*write_ctxt, opts.show_locs);
-         set_annotate(*write_ctxt, opts.annotate);
+         set_ostream(*write_ctxt, of);
          write_corpus(*write_ctxt, corp, 0);
          of.close();
          return 0;
        }
       else
        {
-         write_context_sptr write_ctxt
-             = create_write_context(corp->get_environment(), cout);
-         set_show_locs(*write_ctxt, opts.show_locs);
-         set_annotate(*write_ctxt, opts.annotate);
          exit_code = !write_corpus(*write_ctxt, corp, 0);
        }
     }
@@ -560,6 +556,10 @@ load_kernel_corpus_group_and_write_abixml(char* argv[],
 
   if (!opts.noout)
     {
+      const xml_writer::write_context_sptr& ctxt
+         = xml_writer::create_write_context(group->get_environment(), cout);
+      set_annotate(*ctxt, opts.annotate);
+
       if (!opts.out_file_path.empty())
        {
          ofstream of(opts.out_file_path.c_str(), std::ios_base::trunc);
@@ -570,16 +570,11 @@ load_kernel_corpus_group_and_write_abixml(char* argv[],
                << opts.out_file_path << "'\n";
              return 1;
            }
-         const write_context_sptr& ctxt
-             = create_write_context(group->get_environment(), of);
-         set_annotate(*ctxt, opts.annotate);
+         set_ostream(*ctxt, of);
          exit_code = !write_corpus_group(*ctxt, group, 0);
        }
       else
        {
-         const write_context_sptr& ctxt
-             = create_write_context(group->get_environment(), cout);
-         set_annotate(*ctxt, opts.annotate);
          exit_code = !write_corpus_group(*ctxt, group, 0);
        }
     }