diff options
author | David Faure <faure@kde.org> | 2012-07-27 14:17:38 +0200 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-01-14 23:44:15 +0100 |
commit | 327b2ba3b77e7a738ccfbe84c6ca5e9525010630 (patch) | |
tree | f89c8299d94d4f407ddb91bc4dfff3464680a85e /src/corelib/io/qdebug.cpp | |
parent | ad265c55beb596e28d6b28c16700795fa8555ee6 (diff) |
Add class QDebugStateSaver for writing QDebug operators correctly
Had to move QTextStreamPrivate to a private header, to be able to use
its new internal Params struct from qdebug.cpp
Change-Id: If28e25f27bbd04b1825a5eb3e2ef83ecad72e7b2
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/io/qdebug.cpp')
-rw-r--r-- | src/corelib/io/qdebug.cpp | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/src/corelib/io/qdebug.cpp b/src/corelib/io/qdebug.cpp index 656f26b0fe..164e22dab2 100644 --- a/src/corelib/io/qdebug.cpp +++ b/src/corelib/io/qdebug.cpp @@ -47,6 +47,9 @@ #endif #include "qdebug.h" +#include <private/qtextstream_p.h> + +QT_BEGIN_NAMESPACE // This file is needed to force compilation of QDebug into the kernel library. @@ -170,6 +173,8 @@ between writes. \since 5.0 + + \sa QDebugStateSaver */ /*! @@ -179,6 +184,8 @@ automatic insertion of spaces is disabled. \since 5.0 + + \sa QDebugStateSaver */ /*! @@ -321,3 +328,69 @@ \fn QDebug &QDebug::operator<<(QTextStreamManipulator m) \internal */ + +/*! + \class QDebugStateSaver + + \brief Convenience class for custom QDebug operators + + Saves the settings used by QDebug, and restores them upon destruction. + + The automatic insertion of spaces between writes is one of the settings + that QDebugStateSaver stores for the duration of the current block. + + The settings of the internal QTextStream are also saved and restored, + so that using << hex in a QDebug operator doesn't affect other QDebug + operators. + + \since 5.1 +*/ + +class QDebugStateSaverPrivate +{ +public: + QDebugStateSaverPrivate(QDebug &dbg) + : m_dbg(dbg), + m_spaces(dbg.autoInsertSpaces()), + m_streamParams(dbg.stream->ts.d_ptr->params) + { + } + void restoreState() + { + m_dbg.setAutoInsertSpaces(m_spaces); + m_dbg.stream->ts.d_ptr->params = m_streamParams; + } + + QDebug &m_dbg; + + // QDebug state + const bool m_spaces; + + // QTextStream state + const QTextStreamPrivate::Params m_streamParams; +}; + + +/*! + Creates a QDebugStateSaver instance, which saves the settings + currently used by \a dbg. + + \sa QDebug::setAutoInsertSpaces(), QDebug::autoInsertSpaces() +*/ +QDebugStateSaver::QDebugStateSaver(QDebug &dbg) + : d(new QDebugStateSaverPrivate(dbg)) +{ +} + +/*! + Destroyes a QDebugStateSaver instance, which restores the settings + used by \a dbg when the QDebugStateSaver instance was created. + + \sa QDebug::setAutoInsertSpaces(), QDebug::autoInsertSpaces() +*/ +QDebugStateSaver::~QDebugStateSaver() +{ + d->restoreState(); +} + +QT_END_NAMESPACE |