From f7099c57d1bd2cbed983596abaee85c7dc9194c3 Mon Sep 17 00:00:00 2001 From: Torbjörn Andersson Date: Fri, 31 May 2013 19:30:36 +0200 Subject: NEVERHOOD: Made multi-line function-line macros safer Wrapped the code in the SetUpdateHandler(), SetMessageHandler(), SetSpriteUpdate(), SetFilterX(), SetFilterY() and NextState() macros in "do { ... } while (0)". Otherwise you may fool yourself because in "if (condition) macro();" the "if" will only cover the first statement of the macro. CID 1022340, 1022341, 1022342, 1022343 --- engines/neverhood/entity.h | 16 ++++++++++------ engines/neverhood/sprite.h | 27 +++++++++++++++++++++++---- 2 files changed, 33 insertions(+), 10 deletions(-) (limited to 'engines') diff --git a/engines/neverhood/entity.h b/engines/neverhood/entity.h index fb8941ae43..cba1bb9a7f 100644 --- a/engines/neverhood/entity.h +++ b/engines/neverhood/entity.h @@ -60,14 +60,18 @@ protected: // TODO: Disable heavy debug stuff in release mode #define SetUpdateHandler(handler) \ - _updateHandlerCb = static_cast (handler); \ - debug(5, "SetUpdateHandler(" #handler ")"); \ - _updateHandlerCbName = #handler + do { \ + _updateHandlerCb = static_cast (handler); \ + debug(5, "SetUpdateHandler(" #handler ")"); \ + _updateHandlerCbName = #handler; \ + } while (0) #define SetMessageHandler(handler) \ - _messageHandlerCb = static_cast (handler); \ - debug(5, "SetMessageHandler(" #handler ")"); \ - _messageHandlerCbName = #handler + do { \ + _messageHandlerCb = static_cast (handler); \ + debug(5, "SetMessageHandler(" #handler ")"); \ + _messageHandlerCbName = #handler; \ + } while (0) const uint kMaxSoundResources = 16; diff --git a/engines/neverhood/sprite.h b/engines/neverhood/sprite.h index 80da1768bd..1d17bf0e70 100644 --- a/engines/neverhood/sprite.h +++ b/engines/neverhood/sprite.h @@ -30,9 +30,24 @@ namespace Neverhood { -#define SetSpriteUpdate(callback) _spriteUpdateCb = static_cast (callback); debug(2, "SetSpriteUpdate(" #callback ")"); _spriteUpdateCbName = #callback -#define SetFilterX(callback) _filterXCb = static_cast (callback); debug(2, "SetFilterX(" #callback ")") -#define SetFilterY(callback) _filterYCb = static_cast (callback); debug(2, "SetFilterY(" #callback ")") +#define SetSpriteUpdate(callback) \ + do { \ + _spriteUpdateCb = static_cast (callback); \ + debug(2, "SetSpriteUpdate(" #callback ")"); \ + _spriteUpdateCbName = #callback; \ + } while (0) + +#define SetFilterX(callback) \ + do { \ + _filterXCb = static_cast (callback); \ + debug(2, "SetFilterX(" #callback ")"); \ + } while (0) + +#define SetFilterY(callback) \ + do { \ + _filterYCb = static_cast (callback); \ + debug(2, "SetFilterY(" #callback ")"); \ + } while (0) const int16 kDefPosition = -32768; @@ -113,7 +128,11 @@ protected: #define AnimationCallback(callback) static_cast (callback) #define GotoState(callback) gotoState(static_cast (callback)) -#define NextState(callback) _nextStateCb = static_cast (callback); debug(2, "NextState(" #callback ")"); _nextStateCbName = #callback +#define NextState(callback) \ + do { \ + _nextStateCb = static_cast (callback); \ + debug(2, "NextState(" #callback ")"); _nextStateCbName = #callback; \ + } while (0) #define FinalizeState(callback) setFinalizeState(static_cast (callback)); const int STICK_LAST_FRAME = -2; -- cgit v1.2.3