[amarok] src/core/support: Add another debug helper: DEBUG_ASSERT(cond, stmt)

Kevin Funk krf at electrostorm.net
Sun Jun 5 21:05:56 CEST 2011


Git commit 8737ace03a1311d4de1ce8deac18b57071bb9b97 by Kevin Funk.
Committed on 05/06/2011 at 21:00.
Pushed by kfunk into branch 'master'.

Add another debug helper: DEBUG_ASSERT(cond, stmt)

Please refer to the doxygen documentation to see how to use it. It's
basically a copy of QTC_ASSERT from Qt Creator.

It's very useful for applying (what I call) "soft" assertions, to
replace code like this in one line:
 * bool someMethod() {
 *   if (!pointer)
 *     qWarning() << "Warning pointer is null, aborting";
 *     return false;
 *   (...)
 *   return someBoolean;

If code is triggered, a warning is printing which tells you where the
assertion happened. Additionally, stmt is evaluated so you can actually
leave the function safely.

Sending a copy to amarok-devel so people know that this exists and they
can use it.

CCMAIL: amarok-devel at kde.org

M  +1    -1    src/core/support/Debug.cpp     
M  +36   -1    src/core/support/Debug.h     

http://commits.kde.org/amarok/8737ace03a1311d4de1ce8deac18b57071bb9b97

diff --git a/src/core/support/Debug.cpp b/src/core/support/Debug.cpp
index 309c6bb..b1fce88 100644
--- a/src/core/support/Debug.cpp
+++ b/src/core/support/Debug.cpp
@@ -1,7 +1,7 @@
 /*
     Copyright (c) 2003-2005 Max Howell <max.howell at methylblue.com>
     Copyright (c) 2007-2009 Mark Kretschmann <kretschmann at kde.org>
-    Copyright (c) 2010 Kevin Funk <krf at electrostorm.net>
+    Copyright (c) 2010-2011 Kevin Funk <krf at electrostorm.net>
 
     This library is free software; you can redistribute it and/or
     modify it under the terms of the GNU Lesser General Public
diff --git a/src/core/support/Debug.h b/src/core/support/Debug.h
index dfbff6a..e7e1e73 100644
--- a/src/core/support/Debug.h
+++ b/src/core/support/Debug.h
@@ -1,7 +1,7 @@
 /*
     Copyright (c) 2003-2005 Max Howell <max.howell at methylblue.com>
     Copyright (c) 2007-2009 Mark Kretschmann <kretschmann at kde.org>
-    Copyright (c) 2010 Kevin Funk <krf at electrostorm.net>
+    Copyright (c) 2010-2011 Kevin Funk <krf at electrostorm.net>
 
     This library is free software; you can redistribute it and/or
     modify it under the terms of the GNU Lesser General Public
@@ -30,6 +30,41 @@
 
 #include <QMutex>
 
+// BEGIN: DEBUG_ASSERT
+/**
+ * Debug helper to write "soft" assertions with escape statements more easily
+ * If the assertions fails, a
+ *
+ * \author Kevin Funk
+ * \sa http://qt.gitorious.org/qt-creator/qt-creator/blobs/master/src/libs/utils/qtcassert.h
+ *
+ * (pseudo code)
+ * \code
+ * bool someMethod() {
+ *   if (!pointer)
+ *     qWarning() << "Warning pointer is null, aborting";
+ *     return false;
+ *   (...)
+ *   return someBoolean;
+ * }
+ * \endcode
+ *
+ * (replace by)
+ * \code
+ * bool someMethod() {
+ *   DEBUG_ASSERT(pointer, return false)
+ *   (...)
+ *   return someBoolean;
+ * }
+ * \endcode
+ */
+#define DEBUG_ASSERT(cond, action) \
+    if(cond){}else{debug()<<"ASSERTION " #cond " FAILED AT " __FILE__ ":" DEBUG_ASSERT_STRINGIFY(__LINE__);action;}
+
+#define DEBUG_ASSERT_STRINGIFY_INTERNAL(x) #x
+#define DEBUG_ASSERT_STRINGIFY(x) DEBUG_ASSERT_STRINGIFY_INTERNAL(x)
+// END__: DEBUG_ASSERT
+
 #if QT_VERSION >= 0x040700
 # include <QElapsedTimer>
 #else



More information about the Amarok-devel mailing list