From 5d10a2041eb843fd321ce1d850cf3e0df7648bc7 Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Mon, 24 Jan 2022 20:00:46 -0500 Subject: gdb: add string_file::release method A common pattern for string_file is to want to move out the internal string buffer, because it is the result of the computation that we want to return. It is the reason why string_file::string returns a non-const reference, as explained in the comment. I think it would make sense to have a dedicated method for that instead and make string_file::string return a const reference. This allows removing the explicit std::move in the typical case. Note that compile_program::compute was missing a move, meaning that the resulting string was copied. With the new version, it's not possible to forget to move. Change-Id: Ieaefa35b73daa7930b2f3a26988b6e3b4121bb79 --- gdb/ui-file.h | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) (limited to 'gdb/ui-file.h') diff --git a/gdb/ui-file.h b/gdb/ui-file.h index c097abf..7c7b00d 100644 --- a/gdb/ui-file.h +++ b/gdb/ui-file.h @@ -160,17 +160,19 @@ public: /* string_file-specific public API. */ /* Accesses the std::string containing the entire output collected - so far. + so far. */ + const std::string &string () { return m_string; } - Returns a non-const reference so that it's easy to move the - string contents out of the string_file. E.g.: + /* Return an std::string containing the entire output collected so far. - string_file buf; - buf.printf (....); - buf.printf (....); - return std::move (buf.string ()); - */ - std::string &string () { return m_string; } + The internal buffer is cleared, such that it's ready to build a new + string. */ + std::string release () + { + std::string ret = std::move (m_string); + m_string.clear (); + return ret; + } /* Provide a few convenience methods with the same API as the underlying std::string. */ -- cgit v1.1