From b50c475cd251cbb730ac4af26cb99bc71e529147 Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Tue, 13 Dec 2022 18:12:52 +0100 Subject: [PATCH] Cleanup: move listbase.c to c++ --- intern/ghost/test/CMakeLists.txt | 2 +- source/blender/blenlib/CMakeLists.txt | 2 +- .../intern/{listbase.c => listbase.cc} | 246 +++++++++--------- source/blender/makesdna/intern/CMakeLists.txt | 2 +- 4 files changed, 127 insertions(+), 125 deletions(-) rename source/blender/blenlib/intern/{listbase.c => listbase.cc} (72%) diff --git a/intern/ghost/test/CMakeLists.txt b/intern/ghost/test/CMakeLists.txt index 3f74f97f115..11dd6f75ef9 100644 --- a/intern/ghost/test/CMakeLists.txt +++ b/intern/ghost/test/CMakeLists.txt @@ -167,7 +167,7 @@ add_library(bli_lib "../../../source/blender/blenlib/intern/rct.c" "../../../source/blender/blenlib/intern/string.c" "../../../source/blender/blenlib/intern/string_utf8.c" - "../../../source/blender/blenlib/intern/listbase.c" + "../../../source/blender/blenlib/intern/listbase.cc" "../../../source/blender/blenlib/intern/math_color.c" "../../../source/blender/blenlib/intern/math_geom.c" "../../../source/blender/blenlib/intern/math_matrix.c" diff --git a/source/blender/blenlib/CMakeLists.txt b/source/blender/blenlib/CMakeLists.txt index 1229344cb05..17218a2daed 100644 --- a/source/blender/blenlib/CMakeLists.txt +++ b/source/blender/blenlib/CMakeLists.txt @@ -89,7 +89,7 @@ set(SRC intern/lasso_2d.c intern/lazy_threading.cc intern/length_parameterize.cc - intern/listbase.c + intern/listbase.cc intern/math_base.c intern/math_base_inline.c intern/math_base_safe_inline.c diff --git a/source/blender/blenlib/intern/listbase.c b/source/blender/blenlib/intern/listbase.cc similarity index 72% rename from source/blender/blenlib/intern/listbase.c rename to source/blender/blenlib/intern/listbase.cc index 3932e5eb051..ee0c6fcd5c3 100644 --- a/source/blender/blenlib/intern/listbase.c +++ b/source/blender/blenlib/intern/listbase.cc @@ -22,56 +22,56 @@ void BLI_movelisttolist(ListBase *dst, ListBase *src) { - if (src->first == NULL) { + if (src->first == nullptr) { return; } - if (dst->first == NULL) { + if (dst->first == nullptr) { dst->first = src->first; dst->last = src->last; } else { - ((Link *)dst->last)->next = src->first; - ((Link *)src->first)->prev = dst->last; + ((Link *)dst->last)->next = static_cast(src->first); + ((Link *)src->first)->prev = static_cast(dst->last); dst->last = src->last; } - src->first = src->last = NULL; + src->first = src->last = nullptr; } void BLI_movelisttolist_reverse(ListBase *dst, ListBase *src) { - if (src->first == NULL) { + if (src->first == nullptr) { return; } - if (dst->first == NULL) { + if (dst->first == nullptr) { dst->first = src->first; dst->last = src->last; } else { - ((Link *)src->last)->next = dst->first; - ((Link *)dst->first)->prev = src->last; + ((Link *)src->last)->next = static_cast(dst->first); + ((Link *)dst->first)->prev = static_cast(src->last); dst->first = src->first; } - src->first = src->last = NULL; + src->first = src->last = nullptr; } void BLI_addhead(ListBase *listbase, void *vlink) { - Link *link = vlink; + Link *link = static_cast(vlink); - if (link == NULL) { + if (link == nullptr) { return; } - link->next = listbase->first; - link->prev = NULL; + link->next = static_cast(listbase->first); + link->prev = nullptr; if (listbase->first) { ((Link *)listbase->first)->prev = link; } - if (listbase->last == NULL) { + if (listbase->last == nullptr) { listbase->last = link; } listbase->first = link; @@ -79,19 +79,19 @@ void BLI_addhead(ListBase *listbase, void *vlink) void BLI_addtail(ListBase *listbase, void *vlink) { - Link *link = vlink; + Link *link = static_cast(vlink); - if (link == NULL) { + if (link == nullptr) { return; } - link->next = NULL; - link->prev = listbase->last; + link->next = nullptr; + link->prev = static_cast(listbase->last); if (listbase->last) { ((Link *)listbase->last)->next = link; } - if (listbase->first == NULL) { + if (listbase->first == nullptr) { listbase->first = link; } listbase->last = link; @@ -99,9 +99,9 @@ void BLI_addtail(ListBase *listbase, void *vlink) void BLI_remlink(ListBase *listbase, void *vlink) { - Link *link = vlink; + Link *link = static_cast(vlink); - if (link == NULL) { + if (link == nullptr) { return; } @@ -132,8 +132,8 @@ bool BLI_remlink_safe(ListBase *listbase, void *vlink) void BLI_listbase_swaplinks(ListBase *listbase, void *vlinka, void *vlinkb) { - Link *linka = vlinka; - Link *linkb = vlinkb; + Link *linka = static_cast(vlinka); + Link *linkb = static_cast(vlinkb); if (!linka || !linkb) { return; @@ -185,15 +185,15 @@ void BLI_listbase_swaplinks(ListBase *listbase, void *vlinka, void *vlinkb) void BLI_listbases_swaplinks(ListBase *listbasea, ListBase *listbaseb, void *vlinka, void *vlinkb) { - Link *linka = vlinka; - Link *linkb = vlinkb; - Link linkc = {NULL}; + Link *linka = static_cast(vlinka); + Link *linkb = static_cast(vlinkb); + Link linkc = {nullptr}; if (!linka || !linkb) { return; } - /* The reference to `linkc` assigns NULL, not a dangling pointer so it can be ignored. */ + /* The reference to `linkc` assigns nullptr, not a dangling pointer so it can be ignored. */ #if defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__) >= 1201 /* gcc12.1+ only */ # pragma GCC diagnostic push # pragma GCC diagnostic ignored "-Wdangling-pointer" @@ -221,7 +221,7 @@ void BLI_listbases_swaplinks(ListBase *listbasea, ListBase *listbaseb, void *vli void *BLI_pophead(ListBase *listbase) { Link *link; - if ((link = listbase->first)) { + if ((link = static_cast(listbase->first))) { BLI_remlink(listbase, link); } return link; @@ -230,7 +230,7 @@ void *BLI_pophead(ListBase *listbase) void *BLI_poptail(ListBase *listbase) { Link *link; - if ((link = listbase->last)) { + if ((link = static_cast(listbase->last))) { BLI_remlink(listbase, link); } return link; @@ -238,9 +238,9 @@ void *BLI_poptail(ListBase *listbase) void BLI_freelinkN(ListBase *listbase, void *vlink) { - Link *link = vlink; + Link *link = static_cast(vlink); - if (link == NULL) { + if (link == nullptr) { return; } @@ -253,7 +253,7 @@ void BLI_freelinkN(ListBase *listbase, void *vlink) */ static void listbase_double_from_single(Link *iter, ListBase *listbase) { - Link *prev = NULL; + Link *prev = nullptr; listbase->first = iter; do { iter->prev = prev; @@ -281,7 +281,7 @@ static void listbase_double_from_single(Link *iter, ListBase *listbase) void BLI_listbase_sort(ListBase *listbase, int (*cmp)(const void *, const void *)) { if (listbase->first != listbase->last) { - Link *head = listbase->first; + Link *head = static_cast(listbase->first); head = listbase_sort_fn(head, cmp); listbase_double_from_single(head, listbase); } @@ -292,7 +292,7 @@ void BLI_listbase_sort_r(ListBase *listbase, void *thunk) { if (listbase->first != listbase->last) { - Link *head = listbase->first; + Link *head = static_cast(listbase->first); head = listbase_sort_fn_r(head, cmp, thunk); listbase_double_from_single(head, listbase); } @@ -300,25 +300,25 @@ void BLI_listbase_sort_r(ListBase *listbase, void BLI_insertlinkafter(ListBase *listbase, void *vprevlink, void *vnewlink) { - Link *prevlink = vprevlink; - Link *newlink = vnewlink; + Link *prevlink = static_cast(vprevlink); + Link *newlink = static_cast(vnewlink); /* newlink before nextlink */ - if (newlink == NULL) { + if (newlink == nullptr) { return; } /* empty list */ - if (listbase->first == NULL) { + if (listbase->first == nullptr) { listbase->first = newlink; listbase->last = newlink; return; } /* insert at head of list */ - if (prevlink == NULL) { - newlink->prev = NULL; - newlink->next = listbase->first; + if (prevlink == nullptr) { + newlink->prev = nullptr; + newlink->next = static_cast(listbase->first); newlink->next->prev = newlink; listbase->first = newlink; return; @@ -339,25 +339,25 @@ void BLI_insertlinkafter(ListBase *listbase, void *vprevlink, void *vnewlink) void BLI_insertlinkbefore(ListBase *listbase, void *vnextlink, void *vnewlink) { - Link *nextlink = vnextlink; - Link *newlink = vnewlink; + Link *nextlink = static_cast(vnextlink); + Link *newlink = static_cast(vnewlink); /* newlink before nextlink */ - if (newlink == NULL) { + if (newlink == nullptr) { return; } /* empty list */ - if (listbase->first == NULL) { + if (listbase->first == nullptr) { listbase->first = newlink; listbase->last = newlink; return; } /* insert at end of list */ - if (nextlink == NULL) { - newlink->prev = listbase->last; - newlink->next = NULL; + if (nextlink == nullptr) { + newlink->prev = static_cast(listbase->last); + newlink->next = nullptr; ((Link *)listbase->last)->next = newlink; listbase->last = newlink; return; @@ -378,14 +378,14 @@ void BLI_insertlinkbefore(ListBase *listbase, void *vnextlink, void *vnewlink) void BLI_insertlinkreplace(ListBase *listbase, void *vreplacelink, void *vnewlink) { - Link *l_old = vreplacelink; - Link *l_new = vnewlink; + Link *l_old = static_cast(vreplacelink); + Link *l_new = static_cast(vnewlink); /* update adjacent links */ - if (l_old->next != NULL) { + if (l_old->next != nullptr) { l_old->next->prev = l_new; } - if (l_old->prev != NULL) { + if (l_old->prev != nullptr) { l_old->prev->next = l_new; } @@ -404,7 +404,7 @@ void BLI_insertlinkreplace(ListBase *listbase, void *vreplacelink, void *vnewlin bool BLI_listbase_link_move(ListBase *listbase, void *vlink, int step) { - Link *link = vlink; + Link *link = static_cast(vlink); Link *hook = link; const bool is_up = step < 0; @@ -453,7 +453,7 @@ void BLI_freelist(ListBase *listbase) { Link *link, *next; - link = listbase->first; + link = static_cast(listbase->first); while (link) { next = link->next; free(link); @@ -467,7 +467,7 @@ void BLI_freelistN(ListBase *listbase) { Link *link, *next; - link = listbase->first; + link = static_cast(listbase->first); while (link) { next = link->next; MEM_freeN(link); @@ -482,7 +482,8 @@ int BLI_listbase_count_at_most(const ListBase *listbase, const int count_max) Link *link; int count = 0; - for (link = listbase->first; link && count != count_max; link = link->next) { + for (link = static_cast(listbase->first); link && count != count_max; + link = link->next) { count++; } @@ -494,7 +495,7 @@ int BLI_listbase_count(const ListBase *listbase) Link *link; int count = 0; - for (link = listbase->first; link; link = link->next) { + for (link = static_cast(listbase->first); link; link = link->next) { count++; } @@ -503,11 +504,11 @@ int BLI_listbase_count(const ListBase *listbase) void *BLI_findlink(const ListBase *listbase, int number) { - Link *link = NULL; + Link *link = nullptr; if (number >= 0) { - link = listbase->first; - while (link != NULL && number != 0) { + link = static_cast(listbase->first); + while (link != nullptr && number != 0) { number--; link = link->next; } @@ -518,11 +519,11 @@ void *BLI_findlink(const ListBase *listbase, int number) void *BLI_rfindlink(const ListBase *listbase, int number) { - Link *link = NULL; + Link *link = nullptr; if (number >= 0) { - link = listbase->last; - while (link != NULL && number != 0) { + link = static_cast(listbase->last); + while (link != nullptr && number != 0) { number--; link = link->prev; } @@ -533,11 +534,11 @@ void *BLI_rfindlink(const ListBase *listbase, int number) void *BLI_findlinkfrom(Link *start, int number) { - Link *link = NULL; + Link *link = nullptr; if (number >= 0) { link = start; - while (link != NULL && number != 0) { + while (link != nullptr && number != 0) { number--; link = link->next; } @@ -548,14 +549,14 @@ void *BLI_findlinkfrom(Link *start, int number) int BLI_findindex(const ListBase *listbase, const void *vlink) { - Link *link = NULL; + Link *link = nullptr; int number = 0; - if (vlink == NULL) { + if (vlink == nullptr) { return -1; } - link = listbase->first; + link = static_cast(listbase->first); while (link) { if (link == vlink) { return number; @@ -570,14 +571,14 @@ int BLI_findindex(const ListBase *listbase, const void *vlink) void *BLI_findstring(const ListBase *listbase, const char *id, const int offset) { - Link *link = NULL; + Link *link = nullptr; const char *id_iter; - if (id == NULL) { - return NULL; + if (id == nullptr) { + return nullptr; } - for (link = listbase->first; link; link = link->next) { + for (link = static_cast(listbase->first); link; link = link->next) { id_iter = ((const char *)link) + offset; if (id[0] == id_iter[0] && STREQ(id, id_iter)) { @@ -585,16 +586,16 @@ void *BLI_findstring(const ListBase *listbase, const char *id, const int offset) } } - return NULL; + return nullptr; } void *BLI_rfindstring(const ListBase *listbase, const char *id, const int offset) { /* Same as #BLI_findstring but find reverse. */ - Link *link = NULL; + Link *link = nullptr; const char *id_iter; - for (link = listbase->last; link; link = link->prev) { + for (link = static_cast(listbase->last); link; link = link->prev) { id_iter = ((const char *)link) + offset; if (id[0] == id_iter[0] && STREQ(id, id_iter)) { @@ -602,15 +603,15 @@ void *BLI_rfindstring(const ListBase *listbase, const char *id, const int offset } } - return NULL; + return nullptr; } void *BLI_findstring_ptr(const ListBase *listbase, const char *id, const int offset) { - Link *link = NULL; + Link *link = nullptr; const char *id_iter; - for (link = listbase->first; link; link = link->next) { + for (link = static_cast(listbase->first); link; link = link->next) { /* exact copy of BLI_findstring(), except for this line */ id_iter = *((const char **)(((const char *)link) + offset)); @@ -619,16 +620,16 @@ void *BLI_findstring_ptr(const ListBase *listbase, const char *id, const int off } } - return NULL; + return nullptr; } void *BLI_rfindstring_ptr(const ListBase *listbase, const char *id, const int offset) { /* Same as #BLI_findstring_ptr but find reverse. */ - Link *link = NULL; + Link *link = nullptr; const char *id_iter; - for (link = listbase->last; link; link = link->prev) { + for (link = static_cast(listbase->last); link; link = link->prev) { /* exact copy of BLI_rfindstring(), except for this line */ id_iter = *((const char **)(((const char *)link) + offset)); @@ -637,15 +638,15 @@ void *BLI_rfindstring_ptr(const ListBase *listbase, const char *id, const int of } } - return NULL; + return nullptr; } void *BLI_findptr(const ListBase *listbase, const void *ptr, const int offset) { - Link *link = NULL; + Link *link = nullptr; const void *ptr_iter; - for (link = listbase->first; link; link = link->next) { + for (link = static_cast(listbase->first); link; link = link->next) { /* exact copy of BLI_findstring(), except for this line */ ptr_iter = *((const void **)(((const char *)link) + offset)); @@ -654,16 +655,16 @@ void *BLI_findptr(const ListBase *listbase, const void *ptr, const int offset) } } - return NULL; + return nullptr; } void *BLI_rfindptr(const ListBase *listbase, const void *ptr, const int offset) { /* Same as #BLI_findptr but find reverse. */ - Link *link = NULL; + Link *link = nullptr; const void *ptr_iter; - for (link = listbase->last; link; link = link->prev) { + for (link = static_cast(listbase->last); link; link = link->prev) { /* exact copy of BLI_rfindstring(), except for this line */ ptr_iter = *((const void **)(((const char *)link) + offset)); @@ -672,7 +673,7 @@ void *BLI_rfindptr(const ListBase *listbase, const void *ptr, const int offset) } } - return NULL; + return nullptr; } void *BLI_listbase_bytes_find(const ListBase *listbase, @@ -680,10 +681,10 @@ void *BLI_listbase_bytes_find(const ListBase *listbase, const size_t bytes_size, const int offset) { - Link *link = NULL; + Link *link = nullptr; const void *ptr_iter; - for (link = listbase->first; link; link = link->next) { + for (link = static_cast(listbase->first); link; link = link->next) { ptr_iter = (const void *)(((const char *)link) + offset); if (memcmp(bytes, ptr_iter, bytes_size) == 0) { @@ -691,7 +692,7 @@ void *BLI_listbase_bytes_find(const ListBase *listbase, } } - return NULL; + return nullptr; } void *BLI_listbase_bytes_rfind(const ListBase *listbase, const void *bytes, @@ -700,10 +701,10 @@ void *BLI_listbase_bytes_rfind(const ListBase *listbase, { /* Same as #BLI_listbase_bytes_find but find reverse. */ - Link *link = NULL; + Link *link = nullptr; const void *ptr_iter; - for (link = listbase->last; link; link = link->prev) { + for (link = static_cast(listbase->last); link; link = link->prev) { ptr_iter = (const void *)(((const char *)link) + offset); if (memcmp(bytes, ptr_iter, bytes_size) == 0) { @@ -711,7 +712,7 @@ void *BLI_listbase_bytes_rfind(const ListBase *listbase, } } - return NULL; + return nullptr; } void *BLI_listbase_string_or_index_find(const ListBase *listbase, @@ -719,12 +720,13 @@ void *BLI_listbase_string_or_index_find(const ListBase *listbase, const size_t string_offset, const int index) { - Link *link = NULL; - Link *link_at_index = NULL; + Link *link = nullptr; + Link *link_at_index = nullptr; int index_iter; - for (link = listbase->first, index_iter = 0; link; link = link->next, index_iter++) { - if (string != NULL && string[0] != '\0') { + for (link = static_cast(listbase->first), index_iter = 0; link; + link = link->next, index_iter++) { + if (string != nullptr && string[0] != '\0') { const char *string_iter = ((const char *)link) + string_offset; if (string[0] == string_iter[0] && STREQ(string, string_iter)) { @@ -740,11 +742,11 @@ void *BLI_listbase_string_or_index_find(const ListBase *listbase, int BLI_findstringindex(const ListBase *listbase, const char *id, const int offset) { - Link *link = NULL; + Link *link = nullptr; const char *id_iter; int i = 0; - link = listbase->first; + link = static_cast(listbase->first); while (link) { id_iter = ((const char *)link) + offset; @@ -761,17 +763,17 @@ int BLI_findstringindex(const ListBase *listbase, const char *id, const int offs ListBase BLI_listbase_from_link(Link *some_link) { ListBase list = {some_link, some_link}; - if (some_link == NULL) { + if (some_link == nullptr) { return list; } /* Find the first element. */ - while (((Link *)list.first)->prev != NULL) { + while (((Link *)list.first)->prev != nullptr) { list.first = ((Link *)list.first)->prev; } /* Find the last element. */ - while (((Link *)list.last)->next != NULL) { + while (((Link *)list.last)->next != nullptr) { list.last = ((Link *)list.last)->next; } @@ -783,11 +785,11 @@ void BLI_duplicatelist(ListBase *dst, const ListBase *src) struct Link *dst_link, *src_link; /* in this order, to ensure it works if dst == src */ - src_link = src->first; - dst->first = dst->last = NULL; + src_link = static_cast(src->first); + dst->first = dst->last = nullptr; while (src_link) { - dst_link = MEM_dupallocN(src_link); + dst_link = static_cast(MEM_dupallocN(src_link)); BLI_addtail(dst, dst_link); src_link = src_link->next; @@ -796,9 +798,9 @@ void BLI_duplicatelist(ListBase *dst, const ListBase *src) void BLI_listbase_reverse(ListBase *lb) { - struct Link *curr = lb->first; - struct Link *prev = NULL; - struct Link *next = NULL; + struct Link *curr = static_cast(lb->first); + struct Link *prev = nullptr; + struct Link *next = nullptr; while (curr) { next = curr->next; curr->next = prev; @@ -808,7 +810,7 @@ void BLI_listbase_reverse(ListBase *lb) } /* swap first/last */ - curr = lb->first; + curr = static_cast(lb->first); lb->first = lb->last; lb->last = curr; } @@ -816,39 +818,39 @@ void BLI_listbase_reverse(ListBase *lb) void BLI_listbase_rotate_first(ListBase *lb, void *vlink) { /* make circular */ - ((Link *)lb->first)->prev = lb->last; - ((Link *)lb->last)->next = lb->first; + ((Link *)lb->first)->prev = static_cast(lb->last); + ((Link *)lb->last)->next = static_cast(lb->first); lb->first = vlink; lb->last = ((Link *)vlink)->prev; - ((Link *)lb->first)->prev = NULL; - ((Link *)lb->last)->next = NULL; + ((Link *)lb->first)->prev = nullptr; + ((Link *)lb->last)->next = nullptr; } void BLI_listbase_rotate_last(ListBase *lb, void *vlink) { /* make circular */ - ((Link *)lb->first)->prev = lb->last; - ((Link *)lb->last)->next = lb->first; + ((Link *)lb->first)->prev = static_cast(lb->last); + ((Link *)lb->last)->next = static_cast(lb->first); lb->first = ((Link *)vlink)->next; lb->last = vlink; - ((Link *)lb->first)->prev = NULL; - ((Link *)lb->last)->next = NULL; + ((Link *)lb->first)->prev = nullptr; + ((Link *)lb->last)->next = nullptr; } LinkData *BLI_genericNodeN(void *data) { LinkData *ld; - if (data == NULL) { - return NULL; + if (data == nullptr) { + return nullptr; } /* create new link, and make it hold the given data */ - ld = MEM_callocN(sizeof(LinkData), __func__); + ld = MEM_cnew(__func__); ld->data = data; return ld; diff --git a/source/blender/makesdna/intern/CMakeLists.txt b/source/blender/makesdna/intern/CMakeLists.txt index 50fd51e88a1..2bf83d7abf0 100644 --- a/source/blender/makesdna/intern/CMakeLists.txt +++ b/source/blender/makesdna/intern/CMakeLists.txt @@ -135,7 +135,7 @@ set(SRC ../../blenlib/intern/BLI_mempool.c ../../blenlib/intern/endian_switch.c ../../blenlib/intern/hash_mm2a.c - ../../blenlib/intern/listbase.c + ../../blenlib/intern/listbase.cc ${SRC_DNA_DEFAULTS_INC} )