From 75e2e034a49f0b2d15d73f4f3b990407fc0da359 Mon Sep 17 00:00:00 2001 From: lixianjing Date: Thu, 24 Oct 2024 20:43:10 +0800 Subject: [PATCH] improve tk_swap --- docs/changes.md | 3 +++ src/tkc/types_def.h | 11 ++++++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/docs/changes.md b/docs/changes.md index 6139f16a6..93df090fa 100644 --- a/docs/changes.md +++ b/docs/changes.md @@ -1,5 +1,8 @@ # 最新动态 +2024/10/24 + * tk_swap防止临时变量重名(感谢兆坤提供补丁) + 2024/10/23 * 修复mingw编译下font_stb_get_vmetrics异常问题(感谢颖健提供补丁) * 增加tk_sha256_hash_from_str(感谢兆坤提供补丁) diff --git a/src/tkc/types_def.h b/src/tkc/types_def.h index ea0af7fa4..2bc8fe75d 100644 --- a/src/tkc/types_def.h +++ b/src/tkc/types_def.h @@ -407,11 +407,12 @@ typedef enum _ret_t { #define tk_roundi(a) (int32_t)(((a) >= 0) ? ((a) + 0.5f) : ((a)-0.5f)) #define tk_clamp(a, mn, mx) ((a) < (mn) ? (mn) : ((a) > (mx) ? (mx) : (a))) #define tk_clampi(a, mn, mx) (int32_t)((a) < (mn) ? (mn) : ((a) > (mx) ? (mx) : (a))) -#define tk_swap(a, b, type) \ - do { \ - type temp = a; \ - a = b; \ - b = temp; \ +#define tk_swap(a, b, type) \ + do { \ + ENSURE(!tk_str_eq("_tEMP__", #a) && !tk_str_eq("_tEMP__", #b)); \ + type _tEMP__ = a; \ + a = b; \ + b = _tEMP__; \ } while (0) #ifndef ARRAY_SIZE