Cleanup: Simplify bContextStore API with C++ concepts

Remove the need for `CTX_store_copy` and `CTX_store_free` by using
C++ copy constructors, unique pointers, and `std::optional`. A few types
are made non-trivial to support this.
This commit is contained in:
Hans Goudey
2023-08-31 11:32:46 -04:00
parent 1763a46cbf
commit feb14a4d38
6 changed files with 23 additions and 48 deletions

View File

@@ -159,8 +159,6 @@ void CTX_store_set(bContext *C, bContextStore *store);
const PointerRNA *CTX_store_ptr_lookup(const bContextStore *store,
blender::StringRefNull name,
const StructRNA *type = nullptr);
bContextStore *CTX_store_copy(const bContextStore *store);
void CTX_store_free(bContextStore *store);
#endif

View File

@@ -191,16 +191,6 @@ const PointerRNA *CTX_store_ptr_lookup(const bContextStore *store,
return nullptr;
}
bContextStore *CTX_store_copy(const bContextStore *store)
{
return MEM_new<bContextStore>(__func__, *store);
}
void CTX_store_free(bContextStore *store)
{
MEM_delete(store);
}
/* is python initialized? */
bool CTX_py_init_get(bContext *C)

View File

@@ -503,7 +503,7 @@ struct uiAfterFunc {
uiBlockInteraction_CallbackData custom_interaction_callbacks;
uiBlockInteraction_Handle *custom_interaction_handle;
bContextStore *context;
std::optional<bContextStore> context;
char undostr[BKE_UNDO_STR_MAX];
char drawstr[UI_MAX_DRAW_STR];
@@ -789,7 +789,7 @@ static void ui_handle_afterfunc_add_operator_ex(wmOperatorType *ot,
}
if (context_but && context_but->context) {
after->context = CTX_store_copy(context_but->context);
after->context = *context_but->context;
}
if (context_but) {
@@ -905,7 +905,7 @@ static void ui_apply_but_func(bContext *C, uiBut *but)
}
if (but->context) {
after->context = CTX_store_copy(but->context);
after->context = *but->context;
}
ui_but_drawstr_without_sep_char(but, after->drawstr, sizeof(after->drawstr));
@@ -1020,7 +1020,7 @@ static void ui_apply_but_funcs_after(bContext *C)
MEM_delete(afterf);
if (after.context) {
CTX_store_set(C, after.context);
CTX_store_set(C, &after.context.value());
}
if (after.popup_op) {
@@ -1053,7 +1053,6 @@ static void ui_apply_but_funcs_after(bContext *C)
if (after.context) {
CTX_store_set(C, nullptr);
CTX_store_free(after.context);
}
if (after.func) {

View File

@@ -104,6 +104,7 @@ struct wmEvent;
struct wmOperator;
struct wmWindowManager;
#include <memory>
#include <string>
#include "BLI_compiler_attrs.h"
@@ -1169,7 +1170,7 @@ struct wmDragActiveDropState {
* If `active_dropbox` is set, additional context provided by the active (i.e. hovered) button.
* Activated before context sensitive operations (polling, drawing, dropping).
*/
bContextStore *ui_context;
std::unique_ptr<bContextStore> ui_context;
/**
* Text to show when a dropbox poll succeeds (so the dropbox itself is available) but the

View File

@@ -169,7 +169,7 @@ static void wm_dropbox_invoke(bContext *C, wmDrag *drag)
}
LISTBASE_FOREACH (wmDropBox *, drop, &dm->dropboxes) {
if (drag->drop_state.ui_context) {
CTX_store_set(C, drag->drop_state.ui_context);
CTX_store_set(C, drag->drop_state.ui_context.get());
}
if (drop->on_drag_start) {
@@ -183,7 +183,7 @@ static void wm_dropbox_invoke(bContext *C, wmDrag *drag)
wmDrag *WM_drag_data_create(
bContext *C, int icon, eWM_DragDataType type, void *poin, double value, uint flags)
{
wmDrag *drag = MEM_cnew<wmDrag>(__func__);
wmDrag *drag = MEM_new<wmDrag>(__func__);
/* Keep track of future multi-touch drag too, add a mouse-pointer id or so. */
/* if multiple drags are added, they're drawn as list */
@@ -264,7 +264,7 @@ void wm_drags_exit(wmWindowManager *wm, wmWindow *win)
}
}
static bContextStore *wm_drop_ui_context_create(const bContext *C)
static std::unique_ptr<bContextStore> wm_drop_ui_context_create(const bContext *C)
{
uiBut *active_but = UI_region_active_but_get(CTX_wm_region(C));
if (!active_but) {
@@ -276,16 +276,7 @@ static bContextStore *wm_drop_ui_context_create(const bContext *C)
return nullptr;
}
return CTX_store_copy(but_context);
}
static void wm_drop_ui_context_free(bContextStore **context_store)
{
if (!*context_store) {
return;
}
CTX_store_free(*context_store);
*context_store = nullptr;
return std::make_unique<bContextStore>(*but_context);
}
void WM_event_drag_image(wmDrag *drag, const ImBuf *imb, float scale)
@@ -327,7 +318,7 @@ void WM_drag_free(wmDrag *drag)
if (drag->flags & WM_DRAG_FREE_DATA) {
WM_drag_data_free(drag->type, drag->poin);
}
wm_drop_ui_context_free(&drag->drop_state.ui_context);
drag->drop_state.ui_context.reset();
if (drag->drop_state.free_disabled_info) {
MEM_SAFE_FREE(drag->drop_state.disabled_info);
}
@@ -338,7 +329,7 @@ void WM_drag_free(wmDrag *drag)
}
BLI_freelinkN(&drag->asset_items, asset_item);
}
MEM_freeN(drag);
MEM_delete(drag);
}
void WM_drag_free_list(ListBase *lb)
@@ -378,7 +369,7 @@ static wmDropBox *dropbox_active(bContext *C,
if (handler->dropboxes) {
LISTBASE_FOREACH (wmDropBox *, drop, handler->dropboxes) {
if (drag->drop_state.ui_context) {
CTX_store_set(C, drag->drop_state.ui_context);
CTX_store_set(C, drag->drop_state.ui_context.get());
}
if (!drop->poll(C, drag, event)) {
@@ -450,7 +441,7 @@ static void wm_drop_update_active(bContext *C, wmDrag *drag, const wmEvent *even
}
/* Update UI context, before polling so polls can query this context. */
wm_drop_ui_context_free(&drag->drop_state.ui_context);
drag->drop_state.ui_context.reset();
drag->drop_state.ui_context = wm_drop_ui_context_create(C);
wmDropBox *drop_prev = drag->drop_state.active_dropbox;
@@ -469,7 +460,7 @@ static void wm_drop_update_active(bContext *C, wmDrag *drag, const wmEvent *even
}
if (!drag->drop_state.active_dropbox) {
wm_drop_ui_context_free(&drag->drop_state.ui_context);
drag->drop_state.ui_context.reset();
}
}
@@ -478,7 +469,7 @@ void wm_drop_prepare(bContext *C, wmDrag *drag, wmDropBox *drop)
const wmOperatorCallContext opcontext = wm_drop_operator_context_get(drop);
if (drag->drop_state.ui_context) {
CTX_store_set(C, drag->drop_state.ui_context);
CTX_store_set(C, drag->drop_state.ui_context.get());
}
/* Optionally copy drag information to operator properties. Don't call it if the
@@ -1039,7 +1030,7 @@ void wm_drags_draw(bContext *C, wmWindow *win)
if (drag->drop_state.active_dropbox) {
CTX_wm_area_set(C, drag->drop_state.area_from);
CTX_wm_region_set(C, drag->drop_state.region_from);
CTX_store_set(C, drag->drop_state.ui_context);
CTX_store_set(C, drag->drop_state.ui_context.get());
if (region && drag->drop_state.active_dropbox->draw_in_view) {
wmViewport(&region->winrct);

View File

@@ -1862,7 +1862,7 @@ int WM_operator_call_py(bContext *C,
struct uiOperatorWaitForInput {
ScrArea *area;
wmOperatorCallParams optype_params;
bContextStore *context;
std::optional<bContextStore> context;
};
static void ui_handler_wait_for_input_remove(bContext *C, void *userdata)
@@ -1874,9 +1874,6 @@ static void ui_handler_wait_for_input_remove(bContext *C, void *userdata)
}
MEM_freeN(opwait->optype_params.opptr);
}
if (opwait->context) {
CTX_store_free(opwait->context);
}
if (opwait->area != nullptr) {
ED_area_status_text(opwait->area, nullptr);
@@ -1885,7 +1882,7 @@ static void ui_handler_wait_for_input_remove(bContext *C, void *userdata)
ED_workspace_status_text(C, nullptr);
}
MEM_freeN(opwait);
MEM_delete(opwait);
}
static int ui_handler_wait_for_input(bContext *C, const wmEvent *event, void *userdata)
@@ -1929,7 +1926,7 @@ static int ui_handler_wait_for_input(bContext *C, const wmEvent *event, void *us
WM_cursor_modal_restore(win);
if (state == EXECUTE) {
CTX_store_set(C, opwait->context);
CTX_store_set(C, opwait->context ? &opwait->context.value() : nullptr);
WM_operator_name_call_ptr(C,
opwait->optype_params.optype,
opwait->optype_params.opcontext,
@@ -1995,7 +1992,7 @@ void WM_operator_name_call_ptr_with_depends_on_cursor(bContext *C,
WM_cursor_modal_set(win, ot->cursor_pending);
uiOperatorWaitForInput *opwait = MEM_cnew<uiOperatorWaitForInput>(__func__);
uiOperatorWaitForInput *opwait = MEM_new<uiOperatorWaitForInput>(__func__);
opwait->optype_params.optype = ot;
opwait->optype_params.opcontext = opcontext;
opwait->optype_params.opptr = properties;
@@ -2011,9 +2008,8 @@ void WM_operator_name_call_ptr_with_depends_on_cursor(bContext *C,
}
}
bContextStore *store = CTX_store_get(C);
if (store) {
opwait->context = CTX_store_copy(store);
if (bContextStore *store = CTX_store_get(C)) {
opwait->context = *store;
}
WM_event_add_ui_handler(C,