I want to define a new macro ASSERT_RETURN(expr, retval, ...) that
asserts in debug builds, and returns with the given return value in
non-debug builds. This to make it easier to do the right thing when
coding, which is to first check an entry condition with an assert, and
then check it again to return on failure. It also has the added
benefit that it will only evaluate the expression once, unlike
ASSERT(expr); if (!expr) ... code, since even in non-debug builds we
evaluate the expression to output logging in case of error.

An example: ASSERT_RETURN(false, 1, "testing");

This macro will test the expression 'false' and if it fails (always)
it will assert, if asserts are enabled, and if not (or if continue
from gdb is used) return 1.

Since we have had quite a few rounds on the exact definition of the
ASSERT macro, please have a close look at this patch.

  - Per
Index: lib/framework/debug.h
===================================================================
--- lib/framework/debug.h	(revision 6540)
+++ lib/framework/debug.h	(working copy)
@@ -54,6 +54,22 @@
 /** Whether asserts are currently enabled. */
 extern bool assertEnabled;
 
+/* Internal assert helper macro */
+#define ASSERT_INTERNAL(expr, expr_string, location_description, function, ...) \
+( \
+	( \
+		(expr) ? /* if (expr) */ \
+			(void)0 \
+		: /* else */\
+		( \
+			(void)_debug(LOG_ERROR, function, __VA_ARGS__), \
+			(void)_debug(LOG_ERROR, function, "Assert in Warzone: %s (%s), last script event: '%s'", \
+		                                  location_description, expr_string, last_called_script_event) \
+		) \
+	), \
+	( assertEnabled ? assert(expr) : (void)0 )\
+)
+
 /**
  * ASSERT helper macro to allow some debug functions to use an alternate
  * calling location.
@@ -70,19 +86,7 @@
  *         so unless you have a good reason to, don't depend on it.
  */
 #define ASSERT_HELPER(expr, location_description, function, ...) \
-( \
-	( \
-		(expr) ? /* if (expr) */ \
-			(void)0 \
-		: /* else */\
-		( \
-			(void)_debug(LOG_ERROR, function, __VA_ARGS__), \
-			(void)_debug(LOG_ERROR, function, "Assert in Warzone: %s (%s), last script event: '%s'", \
-		                                  location_description, (#expr), last_called_script_event) \
-		) \
-	), \
-	assertEnabled ? assert(expr) : (void)0 \
-)
+	ASSERT_INTERNAL(expr, #expr, location_description, function, __VA_ARGS__)
 
 /**
  *
@@ -94,7 +98,14 @@
 #define ASSERT(expr, ...) \
 	ASSERT_HELPER(expr, AT_MACRO, __FUNCTION__, __VA_ARGS__)
 
+/**
+ *
+ * Assert that returns given return value on failure in non-debug builds.
+ */
+#define ASSERT_RETURN(expr, retval, ...) \
+	do { bool _wzeval = (expr); ASSERT_INTERNAL(_wzeval, #expr, AT_MACRO, __FUNCTION__, __VA_ARGS__); if (!_wzeval) return retval; } while (0)
 
+
 /**
  * Compile time assert
  * Not to be used in global context!
_______________________________________________
Warzone-dev mailing list
Warzone-dev@gna.org
https://mail.gna.org/listinfo/warzone-dev

Reply via email to