From 5ba9da18286b36328ac11d9e3f4f302e8b0d72fd Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Wed, 18 Apr 2018 21:43:58 +1000 Subject: [PATCH] debug: check sanity at end of scope for CHECK_SANITY --- debug.hpp | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/debug.hpp b/debug.hpp index c69babf1..57f72fd7 100644 --- a/debug.hpp +++ b/debug.hpp @@ -96,14 +96,9 @@ /////////////////////////////////////////////////////////////////////////////// -#define CHECK_SANITY(A) do { \ - DEBUG_ONLY ( \ - const auto &__a = (A); \ - if (!::util::debug::is_valid (__a)) { \ - _CHECK_PANIC("failed sanity test for %s, %!\n", #A, __a); \ - } \ - ); \ -} while (0) +#define CHECK_SANITY(A) \ +::util::debug::scoped_sanity PASTE(__scoped_sanity_checker,__LINE__) (A); \ +(void)PASTE(__scoped_sanity_checker,__LINE__); /////////////////////////////////////////////////////////////////////////////// @@ -615,7 +610,26 @@ namespace util::debug { (void)t; CHECK (is_valid (t)); } -} + + + template + class scoped_sanity { + public: + scoped_sanity (ValueT &_value): + m_value (_value) + { + sanity (m_value); + } + + ~scoped_sanity () + { + sanity (m_value); + } + + private: + const ValueT& m_value; + }; +}; ///////////////////////////////////////////////////////////////////////////////