From 8613d04c030cb880162144d0afbccf8fa92b84a8 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Wed, 23 Jan 2013 18:29:28 +0000 Subject: [PATCH] core: implement a cool ASSERT() macro. --- src/Makefile.am | 12 +++--- src/{sys => base}/log.cpp | 0 src/core.h | 5 +-- src/lol/base/assert.h | 78 +++++++++++++++++++++++++++++++++++++ src/lol/{sys => base}/log.h | 6 +-- src/lol/debug.h | 36 ----------------- src/lolcore.vcxproj | 6 +-- src/lolcore.vcxproj.filters | 6 +-- 8 files changed, 95 insertions(+), 54 deletions(-) rename src/{sys => base}/log.cpp (100%) create mode 100644 src/lol/base/assert.h rename src/lol/{sys => base}/log.h (92%) delete mode 100644 src/lol/debug.h diff --git a/src/Makefile.am b/src/Makefile.am index 9e05cd35..5a21c005 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -15,13 +15,13 @@ liblol_a_SOURCES = \ worldentity.cpp worldentity.h gradient.cpp gradient.h gradient.lolfx \ platform.cpp platform.h sprite.cpp sprite.h camera.cpp camera.h \ \ - lol/base/types.h lol/base/array.h lol/base/string.h lol/base/hash.h \ - lol/base/map.h \ + lol/base/log.h lol/base/array.h lol/base/types.h lol/base/array.h \ + lol/base/string.h lol/base/hash.h lol/base/map.h \ lol/math/vector.h lol/math/half.h lol/math/real.h lol/math/remez.h \ lol/math/math.h lol/math/geometry.h \ - lol/sys/init.h lol/sys/log.h lol/sys/thread.h lol/sys/timer.h \ + lol/sys/init.h lol/sys/thread.h lol/sys/timer.h \ lol/image/color.h \ - lol/unit.h lol/debug.h \ + lol/unit.h \ \ generated/location.hh generated/position.hh generated/stack.hh \ \ @@ -42,7 +42,7 @@ liblol_a_SOURCES = \ $(d3d9_sources) \ $(android_sources) \ \ - base/hash.cpp base/string.cpp \ + base/hash.cpp base/log.cpp base/string.cpp \ \ math/vector.cpp math/real.cpp math/half.cpp math/trig.cpp \ math/geometry.cpp \ @@ -69,7 +69,7 @@ liblol_a_SOURCES = \ \ mesh/mesh.cpp mesh/mesh.h \ \ - sys/init.cpp sys/log.cpp sys/timer.cpp \ + sys/init.cpp sys/timer.cpp \ sys/threadbase.h \ \ image/image.cpp image/image.h image/image-private.h \ diff --git a/src/sys/log.cpp b/src/base/log.cpp similarity index 100% rename from src/sys/log.cpp rename to src/base/log.cpp diff --git a/src/core.h b/src/core.h index 59ad46ca..c56fd78a 100644 --- a/src/core.h +++ b/src/core.h @@ -74,9 +74,9 @@ static inline int isnan(float f) #endif // Base types -#include - #include +#include +#include #include #include #include @@ -89,7 +89,6 @@ static inline int isnan(float f) #include #include -#include #include #include diff --git a/src/lol/base/assert.h b/src/lol/base/assert.h new file mode 100644 index 00000000..8725cc60 --- /dev/null +++ b/src/lol/base/assert.h @@ -0,0 +1,78 @@ +// +// Lol Engine +// +// Copyright: (c) 2010-2013 Sam Hocevar +// This program is free software; you can redistribute it and/or +// modify it under the terms of the Do What The Fuck You Want To +// Public License, Version 2, as published by Sam Hocevar. See +// http://www.wtfpl.net/ for more details. +// + +#if !defined __LOL_BASE_ASSERT_H__ +#define __LOL_BASE_ASSERT_H__ + +#include + +namespace lol +{ + +static inline void Abort() +{ +#if defined __CELLOS_LV2__ + *(uint32_t *)NULL = 0xdead; +#else + std::abort(); +#endif +} + +#define LOL_CALL(macro, args) macro args +#define LOL_EVAL(a) a +#define LOL_1ST(a, ...) a + +#define LOL_NUM2(a01, a02, a03, a04, a05, a06, a07, a08, a09, a10, \ + a11, a12, a13, a14, a15, a16, a17, a18, a19, a20, \ + a21, a22, a23, a24, a25, a26, a27, a28, a29, a30, \ + a31, a32, a33, a34, a35, a36, a37, a38, a39, a40, \ + a41, a42, a43, a44, a45, a46, a47, a48, a49, a50, \ + a51, a52, a53, a54, a55, a56, a57, a58, a59, a60, \ + a61, a62, a63, ...) a63 +#define LOL_NUM(...) \ + LOL_EVAL(LOL_NUM2(__VA_ARGS__, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, \ + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, \ + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, \ + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, \ + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, \ + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, \ + 2, 1, TOO_MANY_ARGUMENTS)) + +/* Three levels of dispatch are needed because of Visual Studio's bizarre + * handling of __VA_ARGS__ inside macro calls */ +#define LOL_CAT3(a, b) a##b +#define LOL_CAT2(a, b) LOL_CAT3(a,b) +#define LOL_CAT(a, b) LOL_CAT2(a,b) + +#define LOL_ERROR_1(t) \ + Log::Error("assertion failure: " #t "\n") + +#define LOL_ERROR_2(t, s) \ + Log::Error("assertion failure: %s\n", s) + +#define LOL_ERROR_3(t, s, ...) \ + Log::Error("assertion failure: " s "\n", __VA_ARGS__) + +#if FINAL_RELEASE +# define ASSERT(...) (void)sizeof(LOL_CALL(LOL_1ST, (__VA_ARGS__))) +#else +# define ASSERT(...) \ + if (!(LOL_CALL(LOL_1ST, (__VA_ARGS__)))) \ + { \ + LOL_CALL(LOL_CAT(LOL_ERROR_, LOL_CALL(LOL_NUM, (__VA_ARGS__))), \ + (__VA_ARGS__)); \ + Abort(); \ + } +#endif + +} /* namespace lol */ + +#endif // __LOL_BASE_ASSERT_H__ + diff --git a/src/lol/sys/log.h b/src/lol/base/log.h similarity index 92% rename from src/lol/sys/log.h rename to src/lol/base/log.h index cebf89db..c9f3db6d 100644 --- a/src/lol/sys/log.h +++ b/src/lol/base/log.h @@ -14,8 +14,8 @@ // The central logging system. // -#if !defined __LOL_SYS_LOG_H__ -#define __LOL_SYS_LOG_H__ +#if !defined __LOL_BASE_LOG_H__ +#define __LOL_BASE_LOG_H__ #include #include @@ -51,5 +51,5 @@ private: } /* namespace lol */ -#endif // __LOL_SYS_LOG_H__ +#endif // __LOL_BASE_LOG_H__ diff --git a/src/lol/debug.h b/src/lol/debug.h deleted file mode 100644 index 7eeea719..00000000 --- a/src/lol/debug.h +++ /dev/null @@ -1,36 +0,0 @@ -// -// Lol Engine -// -// Copyright: (c) 2010-2013 Sam Hocevar -// This program is free software; you can redistribute it and/or -// modify it under the terms of the Do What The Fuck You Want To -// Public License, Version 2, as published by Sam Hocevar. See -// http://www.wtfpl.net/ for more details. -// - -// -// Debug utilities -// --------------- -// - -#if !defined __LOL_DEBUG_H__ -#define __LOL_DEBUG_H__ - -#include - -namespace lol -{ - -static inline void Abort() -{ -#if defined __CELLOS_LV2__ - *(uint32_t *)NULL = 0; -#else - std::abort(); -#endif -} - -} /* namespace lol */ - -#endif // __LOL_DEBUG_H__ - diff --git a/src/lolcore.vcxproj b/src/lolcore.vcxproj index 20cef4cc..09411af1 100644 --- a/src/lolcore.vcxproj +++ b/src/lolcore.vcxproj @@ -235,6 +235,7 @@ + @@ -293,7 +294,6 @@ - @@ -586,11 +586,12 @@ + + - @@ -599,7 +600,6 @@ - diff --git a/src/lolcore.vcxproj.filters b/src/lolcore.vcxproj.filters index f094d8e2..60028b51 100644 --- a/src/lolcore.vcxproj.filters +++ b/src/lolcore.vcxproj.filters @@ -731,9 +731,6 @@ gpu - - lol - input @@ -1613,6 +1610,9 @@ lol\base + + lol\base + lol\base