Commit 6c9a6c79 authored by Dmitry Kazakov's avatar Dmitry Kazakov

Added a small KisAssert framework

Now if the error is not too much critical one can use KIS_ASSERT or
KIS_ASSERT_RECOVER macros. These macros check the condition and ask
the user what to do: try to continue or abort the program and see the
backtrace.

These macros can be use in the following way:

KIS_ASSERT(condition);

or

KIS_ASSERT_RECOVER(condition) {
    // the code that recovers your program after a falure
}

There are three shortcuts available:

KIS_ASSERT_RECOVER_BREAK(cond); --- calls 'break;'
KIS_ASSERT_RECOVER_RETURN(cond); --- returns from current function
KIS_ASSERT_RECOVER_RETURN_VALUE(cond, retval) --- returns from the function with some result

WARNING: These asserts are supposed to be present even in NDEBUG mode!
TODO: Implement automatic saving of the document. See the note
      in kis_assert.cpp
parent c8384daf
......@@ -60,6 +60,7 @@ endif(HAVE_VC)
set(kritaimage_LIB_SRCS
${libkritatile_SRCS}
kis_assert.cpp
kis_distance_information.cpp
kis_painter.cc
kis_progress_updater.cpp
......
/*
* Copyright (c) 2013 Dmitry Kazakov <dimula73@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "kis_assert.h"
#include <QString>
#include <QMessageBox>
#include <klocale.h>
/**
* TODO: Add automatic saving of the documents
*
* Requirements:
* 1) Should save all open KisDoc2 objects
* 2) Should *not* overwrite original document since the saving
* process may fail.
* 3) Should *not* overwrite any autosaved documents since the saving
* process may fail.
* 4) Double-fault tolerance! Assert during emergency saving should not
* lead to an infinite loop.
*/
void kis_assert(const char *assertion, const char *file, int line)
{
QString shortMessage =
QString("ASSERT (krita): \"%1\" in file %2, line %3")
.arg(assertion)
.arg(file)
.arg(line);
QString longMessage =
QString(
"Krita has encountered an internal error:\n\n"
"%1\n\n"
"Please report a bug to developers!\n\n"
"Press Ignore to try to continue.\n"
"Press Abort to see developers information (all unsaved data will be lost)")
.arg(shortMessage);
QMessageBox::StandardButton button =
QMessageBox::critical(0, i18n("Krita Internal Error"),
longMessage,
QMessageBox::Ignore | QMessageBox::Abort,
QMessageBox::Ignore);
if (button == QMessageBox::Abort) {
qFatal("%s", shortMessage.toLatin1().data());
}
}
/*
* Copyright (c) 2013 Dmitry Kazakov <dimula73@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef __KIS_ASSERT_H
#define __KIS_ASSERT_H
#include <QtGlobal>
#include <krita_export.h>
KRITAIMAGE_EXPORT void kis_assert(const char *assertion, const char *file, int line);
#define KIS_ASSERT(cond) ((!(cond)) ? kis_assert(#cond,__FILE__,__LINE__) : qt_noop())
#define KIS_ASSERT_RECOVER(cond) if (!(cond) && (kis_assert(#cond,__FILE__,__LINE__), true))
#define KIS_ASSERT_RECOVER_BREAK(cond) KIS_ASSERT_RECOVER(cond) { break; }
#define KIS_ASSERT_RECOVER_RETURN(cond) KIS_ASSERT_RECOVER(cond) { return; }
#define KIS_ASSERT_RECOVER_RETURN_VALUE(cond, val) KIS_ASSERT_RECOVER(cond) { return (val); }
#endif /* __KIS_ASSERT_H */
......@@ -25,6 +25,8 @@
#include <calligraversion.h>
#include <KoConfig.h>
#include "kis_assert.h"
#define KRITA_VERSION CALLIGRA_VERSION
......
......@@ -396,10 +396,8 @@ void KisOpenGLCanvas2::drawImage() const
KisTextureTile *tile =
d->openGLImageTextures->getTextureTileCR(effectiveCol, effectiveRow);
Q_ASSERT(tile);
if (!tile) {
continue;
}
KIS_ASSERT_RECOVER_BREAK(tile);
/*
* We create a float rect here to workaround Qt's
* "history reasons" in calculation of right()
......
......@@ -271,10 +271,9 @@ void KisOpenGLImageTextures::recalculateCache(KisUpdateInfoSP info)
tileInfo.convertTo(dstCS, m_renderingIntent, m_conversionFlags);
KisTextureTile *tile = getTextureTileCR(tileInfo.tileCol(), tileInfo.tileRow());
Q_ASSERT(tile);
if (tile) {
tile->update(tileInfo);
}
KIS_ASSERT_RECOVER_RETURN(tile);
tile->update(tileInfo);
tileInfo.destroy();
KIS_OPENGL_PRINT_ERROR();
......
......@@ -105,13 +105,9 @@ public:
inline KisTextureTile* getTextureTileCR(int col, int row) {
int tile = row * m_numCols + col;
Q_ASSERT(m_textureTiles.size() > tile);
if (m_textureTiles.size() > tile) {
return m_textureTiles[tile];
}
else {
return 0;
}
KIS_ASSERT_RECOVER_RETURN_VALUE(m_textureTiles.size() > tile, 0);
return m_textureTiles[tile];
}
inline KisTextureTile* getTextureTile(int x, int y) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment