Files
test/source/blender/editors/interface/interface_undo.cc
Bastien Montagne dd168a35c5 Refactor: Replace MEM_cnew with a type-aware template version of MEM_callocN.
The general idea is to keep the 'old', C-style MEM_callocN signature, and slowly
replace most of its usages with the new, C++-style type-safer template version.

* `MEM_cnew<T>` allocation version is renamed to `MEM_callocN<T>`.
* `MEM_cnew_array<T>` allocation version is renamed to `MEM_calloc_arrayN<T>`.
* `MEM_cnew<T>` duplicate version is renamed to `MEM_dupallocN<T>`.

Similar templates type-safe version of `MEM_mallocN` will be added soon
as well.

Following discussions in !134452.

NOTE: For now static type checking in `MEM_callocN` and related are slightly
different for Windows MSVC. This compiler seems to consider structs using the
`DNA_DEFINE_CXX_METHODS` macro as non-trivial (likely because their default
copy constructors are deleted). So using checks on trivially
constructible/destructible instead on this compiler/system.

Pull Request: https://projects.blender.org/blender/blender/pulls/134771
2025-03-05 16:35:09 +01:00

115 lines
2.8 KiB
C++

/* SPDX-FileCopyrightText: 2020 Blender Authors
*
* SPDX-License-Identifier: GPL-2.0-or-later */
/** \file
* \ingroup edinterface
*
* Undo stack to use for UI widgets that manage their own editing state.
*/
#include <cstring>
#include "BLI_listbase.h"
#include "DNA_listBase.h"
#include "MEM_guardedalloc.h"
#include "interface_intern.hh"
/* -------------------------------------------------------------------- */
/** \name Text Field Undo Stack
* \{ */
struct uiUndoStack_Text_State {
uiUndoStack_Text_State *next, *prev;
int cursor_index;
char text[0];
};
struct uiUndoStack_Text {
ListBase states;
uiUndoStack_Text_State *current;
};
static const char *ui_textedit_undo_impl(uiUndoStack_Text *stack, int *r_cursor_index)
{
/* Don't undo if no data has been pushed yet. */
if (stack->current == nullptr) {
return nullptr;
}
/* Travel backwards in the stack and copy information to the caller. */
if (stack->current->prev != nullptr) {
stack->current = stack->current->prev;
*r_cursor_index = stack->current->cursor_index;
return stack->current->text;
}
return nullptr;
}
static const char *ui_textedit_redo_impl(uiUndoStack_Text *stack, int *r_cursor_index)
{
/* Don't redo if no data has been pushed yet. */
if (stack->current == nullptr) {
return nullptr;
}
/* Only redo if new data has not been entered since the last undo. */
if (stack->current->next) {
stack->current = stack->current->next;
*r_cursor_index = stack->current->cursor_index;
return stack->current->text;
}
return nullptr;
}
const char *ui_textedit_undo(uiUndoStack_Text *stack, int direction, int *r_cursor_index)
{
BLI_assert(ELEM(direction, -1, 1));
if (direction < 0) {
return ui_textedit_undo_impl(stack, r_cursor_index);
}
return ui_textedit_redo_impl(stack, r_cursor_index);
}
void ui_textedit_undo_push(uiUndoStack_Text *stack, const char *text, int cursor_index)
{
/* Clear all redo actions from the current state. */
if (stack->current != nullptr) {
while (stack->current->next) {
uiUndoStack_Text_State *state = stack->current->next;
BLI_remlink(&stack->states, state);
MEM_freeN(state);
}
}
/* Create the new state. */
const int text_size = strlen(text) + 1;
stack->current = static_cast<uiUndoStack_Text_State *>(
MEM_mallocN(sizeof(uiUndoStack_Text_State) + text_size, __func__));
stack->current->cursor_index = cursor_index;
memcpy(stack->current->text, text, text_size);
BLI_addtail(&stack->states, stack->current);
}
uiUndoStack_Text *ui_textedit_undo_stack_create()
{
uiUndoStack_Text *stack = MEM_callocN<uiUndoStack_Text>(__func__);
stack->current = nullptr;
BLI_listbase_clear(&stack->states);
return stack;
}
void ui_textedit_undo_stack_destroy(uiUndoStack_Text *stack)
{
BLI_freelistN(&stack->states);
MEM_freeN(stack);
}
/** \} */