# HG changeset patch # User Mike Becker # Date 1675879018 -3600 # Node ID dfd0403ff8b6865b61f014ad5ac433f0caa0f58e # Parent ec50abb285ad70b9742fc1754699a8ee0c934e98 add pointer swap utility diff -r ec50abb285ad -r dfd0403ff8b6 src/cx/utils.h --- a/src/cx/utils.h Thu Feb 02 20:25:34 2023 +0100 +++ b/src/cx/utils.h Wed Feb 08 18:56:58 2023 +0100 @@ -51,6 +51,15 @@ */ #define cx_for_n(varname, n) for (size_t varname = 0 ; (varname) < (n) ; (varname)++) +/** + * Convenience macro for swapping two pointers. + */ +#ifdef __cplusplus +#define cx_swap_ptr(left, right) do {auto cx_tmp_swap_var = left; left = right; right = cx_tmp_swap_var;} while(0) +#else +#define cx_swap_ptr(left, right) do {void *cx_tmp_swap_var = left; left = right; right = cx_tmp_swap_var;} while(0) +#endif + // cx_szmul() definition #if (__GNUC__ >= 5 || defined(__clang__)) && !defined(CX_NO_SZMUL_BUILTIN) diff -r ec50abb285ad -r dfd0403ff8b6 test/test_utils.cpp --- a/test/test_utils.cpp Thu Feb 02 20:25:34 2023 +0100 +++ b/test/test_utils.cpp Wed Feb 08 18:56:58 2023 +0100 @@ -39,6 +39,16 @@ } } +TEST(Utils, swap_ptr) { + int i = 5; + int j = 8; + int *ip = &i; + int *jp = &j; + cx_swap_ptr(ip, jp); + EXPECT_EQ(ip, &j); + EXPECT_EQ(jp, &i); +} + TEST(Utils, szmul) { size_t r; int e;