From 6c07e829335060e434804fb7d7353cd64aac70a2 Mon Sep 17 00:00:00 2001 From: Matthias Maennich Date: Tue, 21 May 2019 05:39:18 +0100 Subject: [PATCH] write_context: allow mutating the ostream used 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 --- include/abg-writer.h | 3 +++ src/abg-writer.cc | 27 ++++++++++++++++++++++++--- tools/abidw.cc | 29 ++++++++++++----------------- 3 files changed, 39 insertions(+), 20 deletions(-) diff --git a/include/abg-writer.h b/include/abg-writer.h index 45303e4..dddb554 100644 --- a/include/abg-writer.h +++ b/include/abg-writer.h @@ -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, diff --git a/src/abg-writer.cc b/src/abg-writer.cc index 8280e23..d67e64f 100644 --- a/src/abg-writer.cc +++ b/src/abg-writer.cc @@ -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, diff --git a/tools/abidw.cc b/tools/abidw.cc index d470886..2ff6cf3 100644 --- a/tools/abidw.cc +++ b/tools/abidw.cc @@ -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); } } -- 2.7.4