summaryrefslogtreecommitdiff
path: root/src/common/nullpo.h
diff options
context:
space:
mode:
authorHaru <haru@dotalux.com>2013-12-14 15:14:23 +0100
committerHaru <haru@dotalux.com>2013-12-17 01:02:33 +0100
commita23d072a66d2569ba13921522be3c82ae9aad576 (patch)
tree7e71523316ba55f2b08477331ecab561e53700d0 /src/common/nullpo.h
parent853c5a3141d1f431af95aed55d991334a2b995f6 (diff)
downloadhercules-a23d072a66d2569ba13921522be3c82ae9aad576.tar.gz
hercules-a23d072a66d2569ba13921522be3c82ae9aad576.tar.bz2
hercules-a23d072a66d2569ba13921522be3c82ae9aad576.tar.xz
hercules-a23d072a66d2569ba13921522be3c82ae9aad576.zip
Added support for non-aborting assertions
- Added Assert_ret, Assert_retv, Assert_retb, Assert_retr, working similarly to the corresponding nullpo_ functions. - Moved Assert-related macros to nullpo.h, since they share some functions. Signed-off-by: Haru <haru@dotalux.com>
Diffstat (limited to 'src/common/nullpo.h')
-rw-r--r--src/common/nullpo.h75
1 files changed, 58 insertions, 17 deletions
diff --git a/src/common/nullpo.h b/src/common/nullpo.h
index 5400acaa1..ae39b1151 100644
--- a/src/common/nullpo.h
+++ b/src/common/nullpo.h
@@ -1,19 +1,37 @@
// Copyright (c) Athena Dev Teams - Licensed under GNU GPL
// For more information, see LICENCE in the main folder
-#ifndef _NULLPO_H_
-#define _NULLPO_H_
-
+#ifndef COMMON_NULLPO_H
+#define COMMON_NULLPO_H
#include "../common/cbasetypes.h"
-#define NLP_MARK __FILE__, __LINE__, __func__
-
// enabled by default on debug builds
#if defined(DEBUG) && !defined(NULLPO_CHECK)
#define NULLPO_CHECK
#endif
+// Skip assert checks on release builds
+#if !defined(RELEASE) && !defined(ASSERT_CHECK)
+#define ASSERT_CHECK
+#endif
+
+/** Assert */
+
+#if defined(ASSERT_CHECK)
+// extern "C" {
+#include <assert.h>
+// }
+#if !defined(DEFCPP) && defined(WIN32) && !defined(MINGW)
+#include <crtdbg.h>
+#endif // !DEFCPP && WIN && !MINGW
+#define Assert(EX) assert(EX)
+#define Assert_chk(EX) ( (EX) ? false : (assert_report(__FILE__, __LINE__, __func__, #EX, "failed assertion"), true) )
+#else // ! ASSERT_CHECK
+#define Assert(EX) (EX)
+#define Assert_chk(EX) ((EX), false)
+#endif // ASSERT_CHECK
+
#if defined(NULLPO_CHECK)
/**
* Reports NULL pointer information if the passed pointer is NULL
@@ -21,7 +39,7 @@
* @param t pointer to check
* @return true if the passed pointer is NULL, false otherwise
*/
-#define nullpo_chk(t) ( (t) != NULL ? false : (nullpo_info(NLP_MARK, #t), true) )
+#define nullpo_chk(t) ( (t) != NULL ? false : (assert_report(__FILE__, __LINE__, __func__, #t, "nullpo info"), true) )
#else // ! NULLPO_CHECK
#define nullpo_chk(t) ((t), false)
#endif // NULLPO_CHECK
@@ -46,6 +64,14 @@
do { if (nullpo_chk(t)) return(0); } while(0)
/**
+ * Returns 0 if the given assertion fails.
+ *
+ * @param t statement to check
+ */
+#define Assert_ret(t) \
+ do { if (Assert_chk(t)) return(0); } while(0)
+
+/**
* Returns void if a NULL pointer is found.
*
* @param t pointer to check
@@ -54,6 +80,14 @@
do { if (nullpo_chk(t)) return; } while(0)
/**
+ * Returns void if the given assertion fails.
+ *
+ * @param t statement to check
+ */
+#define Assert_retv(t) \
+ do { if (Assert_chk(t)) return; } while(0)
+
+/**
* Returns the given value if a NULL pointer is found.
*
* @param ret value to return
@@ -63,7 +97,16 @@
do { if (nullpo_chk(t)) return(ret); } while(0)
/**
- * Breaks fromt he current loop/switch if a NULL pointer is found.
+ * Returns the given value if the given assertion fails.
+ *
+ * @param ret value to return
+ * @param t statement to check
+ */
+#define Assert_retr(ret, t) \
+ do { if (Assert_chk(t)) return(ret); } while(0)
+
+/**
+ * Breaks from the current loop/switch if a NULL pointer is found.
*
* @param t pointer to check
*/
@@ -71,16 +114,14 @@
if (nullpo_chk(t)) break; else (void)0
/**
- * Reports NULL pointer information
- *
- * @param file Source file where the error was detected
- * @param line Line
- * @param func Function
- * @param targetname Name of the checked symbol
+ * Breaks from the current loop/switch if the given assertion fails.
*
- * It is possible to use the NLP_MARK macro that expands to:
- * __FILE__, __LINE__, __func__
+ * @param t statement to check
*/
-void nullpo_info(const char *file, int line, const char *func, const char *targetname);
+#define Assert_retb(t) \
+ if (Assert_chk(t)) break; else (void)0
+
+
+void assert_report(const char *file, int line, const char *func, const char *targetname, const char *title);
-#endif /* _NULLPO_H_ */
+#endif /* COMMON_NULLPO_H */