Commit 8a727493 authored by Dmitry Kazakov's avatar Dmitry Kazakov

Make a default KIS_ASSERT throw an exception

It is really improbable that falling through an assert will let the user
to continue his work. So just try to restart from the event loop.
parent e71b73b2
......@@ -21,6 +21,7 @@
#include <QString>
#include <QMessageBox>
#include <klocale.h>
#include <kis_assert_exception.h>
/**
* TODO: Add automatic saving of the documents
......@@ -35,7 +36,7 @@
* lead to an infinite loop.
*/
void kis_assert(const char *assertion, const char *file, int line)
void kis_assert_common(const char *assertion, const char *file, int line, bool throwException)
{
QString shortMessage =
QString("ASSERT (krita): \"%1\" in file %2, line %3")
......@@ -60,5 +61,32 @@ void kis_assert(const char *assertion, const char *file, int line)
if (button == QMessageBox::Abort) {
qFatal("%s", shortMessage.toLatin1().data());
} else if (throwException) {
throw KisAssertException(shortMessage.toLatin1().data());
}
}
void kis_assert_recoverable(const char *assertion, const char *file, int line)
{
kis_assert_common(assertion, file, line, false);
}
void kis_assert_exception(const char *assertion, const char *file, int line)
{
kis_assert_common(assertion, file, line, true);
}
void kis_assert_x_exception(const char *assertion,
const char *where,
const char *what,
const char *file, int line)
{
QString res =
QString("ASSERT failure in %1: \"%2\" (%3)")
.arg(where)
.arg(what)
.arg(assertion);
kis_assert_common(res.toLatin1().data(), file, line, true);
}
......@@ -22,10 +22,14 @@
#include <QtGlobal>
#include <krita_export.h>
KRITAIMAGE_EXPORT void kis_assert(const char *assertion, const char *file, int line);
KRITAIMAGE_EXPORT void kis_assert_exception(const char *assertion, const char *file, int line);
KRITAIMAGE_EXPORT void kis_assert_recoverable(const char *assertion, const char *file, int line);
KRITAIMAGE_EXPORT void kis_assert_x_exception(const char *assertion, const char *where, const char *what, 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(cond) ((!(cond)) ? kis_assert_exception(#cond,__FILE__,__LINE__) : qt_noop())
#define KIS_ASSERT_X(cond, where, what) ((!(cond)) ? kis_assert_x_exception(#cond,where, what,__FILE__,__LINE__) : qt_noop())
#define KIS_ASSERT_RECOVER(cond) if (!(cond) && (kis_assert_recoverable(#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); }
......
/*
* 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_EXCEPTION_H
#define __KIS_ASSERT_EXCEPTION_H
#include <QtCore>
#include <stdexcept>
class KisAssertException : public std::runtime_error, public QtConcurrent::Exception
{
public:
KisAssertException(const std::string& what_arg)
: std::runtime_error(what_arg)
{
}
Exception* clone() const { return new KisAssertException(*this); }
void raise() const { throw *this; }
};
#endif /* __KIS_ASSERT_EXCEPTION_H */
......@@ -171,6 +171,7 @@ QList<KisCanvasDecoration*> KisCanvasWidgetBase::decorations() const
void KisCanvasWidgetBase::setWrapAroundViewingMode(bool value)
{
Q_UNUSED(value);
qWarning() << "Wrap Around mode is not available for this type of canvas!";
}
......
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