Refactor wxLog and wxMessageOutput classes to avoid duplication

Add wxMessageOutputWithConv mix-in class to avoid duplicating the same
code in wxLogStream and wxMessageOutputStderr.

Also derive wxLogStderr from wxMessageOutputStderr to reuse its code
without having to create a temporary object of this type (which will be
more expensive now that doing it involves creating a heap-allocated
conversion object copy).
This commit is contained in:
Vadim Zeitlin
2017-10-21 22:06:08 +02:00
parent 6b73bd9136
commit a7dddd9f3b
4 changed files with 72 additions and 74 deletions

View File

@@ -58,25 +58,51 @@ private:
};
// ----------------------------------------------------------------------------
// implementation which sends output to stderr or specified file
// helper mix-in for output targets that can use difference encodings
// ----------------------------------------------------------------------------
class WXDLLIMPEXP_BASE wxMessageOutputStderr : public wxMessageOutput
class WXDLLIMPEXP_BASE wxMessageOutputWithConv
{
public:
wxMessageOutputStderr(FILE *fp = stderr,
const wxMBConv &conv = wxConvWhateverWorks);
virtual ~wxMessageOutputStderr();
virtual void Output(const wxString& str) wxOVERRIDE;
protected:
explicit wxMessageOutputWithConv(const wxMBConv& conv)
: m_conv(conv.Clone())
{
}
~wxMessageOutputWithConv()
{
delete m_conv;
}
// return the string with "\n" appended if it doesn't already terminate
// with it (in which case it's returned unchanged)
wxString AppendLineFeedIfNeeded(const wxString& str);
// Prepare the given string for output by appending a new line to it, if
// necessary, and converting it to a narrow string using our conversion
// object.
wxCharBuffer PrepareForOutput(const wxString& str);
const wxMBConv* const m_conv;
};
// ----------------------------------------------------------------------------
// implementation which sends output to stderr or specified file
// ----------------------------------------------------------------------------
class WXDLLIMPEXP_BASE wxMessageOutputStderr : public wxMessageOutput,
protected wxMessageOutputWithConv
{
public:
wxMessageOutputStderr(FILE *fp = stderr,
const wxMBConv &conv = wxConvWhateverWorks);
virtual void Output(const wxString& str) wxOVERRIDE;
protected:
FILE *m_fp;
const wxMBConv *m_conv;
wxDECLARE_NO_COPY_CLASS(wxMessageOutputStderr);
};
// ----------------------------------------------------------------------------