diff options
author | Campbell Barton <ideasman42@gmail.com> | 2020-08-19 06:36:57 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2020-08-19 06:43:51 +0300 |
commit | 762e4cf221afa6edf6155c302e3839012b169abe (patch) | |
tree | 20fceb0b929fd174357e73e28431826b651dac1a | |
parent | d5b5b228e4d07ce8c7d32c40c87bebe6745663e2 (diff) |
BLI_listbase: add utility macro for looping over lists with an index
Add to the 2.90 branch to avoid problems if fixes from master use it.
-rw-r--r-- | .clang-format | 1 | ||||
-rw-r--r-- | source/blender/blenlib/BLI_listbase.h | 8 |
2 files changed, 9 insertions, 0 deletions
diff --git a/.clang-format b/.clang-format index cb5cdab496f..77b90dc68f9 100644 --- a/.clang-format +++ b/.clang-format @@ -236,6 +236,7 @@ ForEachMacros: - LISTBASE_CIRCULAR_FORWARD_BEGIN - LISTBASE_FOREACH - LISTBASE_FOREACH_BACKWARD + - LISTBASE_FOREACH_INDEX - LISTBASE_FOREACH_MUTABLE - LISTBASE_FOREACH_BACKWARD_MUTABLE - MAN_ITER_AXES_BEGIN diff --git a/source/blender/blenlib/BLI_listbase.h b/source/blender/blenlib/BLI_listbase.h index fa7cf7a1847..932b63e43f7 100644 --- a/source/blender/blenlib/BLI_listbase.h +++ b/source/blender/blenlib/BLI_listbase.h @@ -186,6 +186,14 @@ struct LinkData *BLI_genericNodeN(void *data); ((var != NULL) ? ((void)(var##_iter_prev = (type)(((Link *)(var))->prev)), 1) : 0); \ var = var##_iter_prev) +/** + * A version of #LISTBASE_FOREACH that takes an index variable (declared outside this macro). + * This avoids possible accidents where using `continue` could miss incrementing the counter. + */ +#define LISTBASE_FOREACH_INDEX(type, var, list, index_var) \ + for (type var = (((void)(index_var = 0)), (type)((list)->first)); var != NULL; \ + var = (type)(((Link *)(var))->next), index_var++) + #ifdef __cplusplus } #endif |