diff options
author | Julian Eisel <julian@blender.org> | 2020-09-21 16:03:02 +0300 |
---|---|---|
committer | Julian Eisel <julian@blender.org> | 2020-09-21 16:03:02 +0300 |
commit | a34e7c3e5d844fd2b9e628534e93892467a7897e (patch) | |
tree | 789aa93890ff92da33beb7da75975ee58f344aee | |
parent | 7883ccd29fe1dc37dff6c46ee0e9ba053444466d (diff) |
Cleanup (UI): Early-exit rather than having a big-ish conditional body
It's generally considered a better codestyle to check conditions early
and exit early when they are not met, over having most logic of a
function within a big `if`-block. Otherwise people have to go over the
entire block to see if there's possibly an `else` somewhere, or any
followup logic.
-rw-r--r-- | source/blender/editors/interface/interface.c | 69 |
1 files changed, 36 insertions, 33 deletions
diff --git a/source/blender/editors/interface/interface.c b/source/blender/editors/interface/interface.c index 8b149e2f97b..f0d19c38537 100644 --- a/source/blender/editors/interface/interface.c +++ b/source/blender/editors/interface/interface.c @@ -3972,41 +3972,44 @@ static uiBut *ui_but_alloc(const eButType type) */ uiBut *ui_but_change_type(uiBut *but, eButType new_type) { - if (but->type != new_type) { - size_t alloc_size; - const char *alloc_str; - uiBut *insert_after_but = but->prev; - bool new_has_custom_type, old_has_custom_type; - - /* Remove old button address */ - BLI_remlink(&but->block->buttons, but); - - ui_but_alloc_info(but->type, NULL, NULL, &old_has_custom_type); - ui_but_alloc_info(new_type, &alloc_size, &alloc_str, &new_has_custom_type); - - if (new_has_custom_type || old_has_custom_type) { - const void *old_but_ptr = but; - /* Button may have pointer to a member within itself, this will have to be updated. */ - const bool has_str_ptr_to_self = but->str == but->strdata; - const bool has_poin_ptr_to_self = but->poin == (char *)but; - - but = MEM_recallocN_id(but, alloc_size, alloc_str); - but->type = new_type; - if (has_str_ptr_to_self) { - but->str = but->strdata; - } - if (has_poin_ptr_to_self) { - but->poin = (char *)but; - } + if (but->type == new_type) { + /* Nothing to do. */ + return but; + } - BLI_insertlinkafter(&but->block->buttons, insert_after_but, but); + size_t alloc_size; + const char *alloc_str; + uiBut *insert_after_but = but->prev; + bool new_has_custom_type, old_has_custom_type; - if (but->layout) { - const bool found_layout = ui_layout_replace_but_ptr(but->layout, old_but_ptr, but); - BLI_assert(found_layout); - UNUSED_VARS_NDEBUG(found_layout); - ui_button_group_replace_but_ptr(but->layout, old_but_ptr, but); - } + /* Remove old button address */ + BLI_remlink(&but->block->buttons, but); + + ui_but_alloc_info(but->type, NULL, NULL, &old_has_custom_type); + ui_but_alloc_info(new_type, &alloc_size, &alloc_str, &new_has_custom_type); + + if (new_has_custom_type || old_has_custom_type) { + const void *old_but_ptr = but; + /* Button may have pointer to a member within itself, this will have to be updated. */ + const bool has_str_ptr_to_self = but->str == but->strdata; + const bool has_poin_ptr_to_self = but->poin == (char *)but; + + but = MEM_recallocN_id(but, alloc_size, alloc_str); + but->type = new_type; + if (has_str_ptr_to_self) { + but->str = but->strdata; + } + if (has_poin_ptr_to_self) { + but->poin = (char *)but; + } + + BLI_insertlinkafter(&but->block->buttons, insert_after_but, but); + + if (but->layout) { + const bool found_layout = ui_layout_replace_but_ptr(but->layout, old_but_ptr, but); + BLI_assert(found_layout); + UNUSED_VARS_NDEBUG(found_layout); + ui_button_group_replace_but_ptr(but->layout, old_but_ptr, but); } } |