Created
November 21, 2014 17:13
-
-
Save ykst/63ec7981431e42ba1489 to your computer and use it in GitHub Desktop.
いつものASSERTマクロ ref: http://qiita.com/ykst/items/f368e6ec8d0160b19ba8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef unlikely | |
# ifdef __builtin_expect | |
# define unlikely(x) __builtin_expect((x), 0) | |
# else | |
# define unlikely(x) (x) | |
# endif | |
#endif | |
#define ERROR(fmt, ...) do { \ | |
fprintf(stderr, "\x1b[1;31mERROR:%s:%s:%d: " fmt "\x1b[0m", __BASE_FILE__, __FUNCTION__, __LINE__, ##__VA_ARGS__); \ | |
} while (0) | |
#define ASSERT(b,action) ({ int __b = (int)(b); if(unlikely(!(__b))){ ERROR("failed (%s)\n", #b); action; } __b;}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ASSERT(<条件式>, <失敗時アクション>) | |
ASSERT(maybe_failed() == SUCCESS, goto error); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ERROR :main.c:main:17: failed (maybe_failed() == SUCCESS) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ASSERT(maybe_failed() == SUCCESS, { do_something(); goto error; }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment