#include "system/nth_alloc.h"
#include "system/lt.h"
-#include "dynarray.h"
#include "system/stacktrace.h"
#include "undo_history.h"
-struct UndoHistory
-{
- Lt *lt;
- Dynarray *actions;
-};
+typedef struct {
+ RevertAction revert;
+} Action;
-UndoHistory *create_undo_history(void)
+void undo_history_push(UndoHistory *undo_history,
+ RevertAction revert,
+ void *context_data,
+ size_t context_data_size)
{
- Lt *lt = create_lt();
-
- UndoHistory *undo_history = PUSH_LT(
- lt,
- nth_calloc(1, sizeof(UndoHistory)),
- free);
- undo_history->lt = lt;
+ trace_assert(undo_history);
- undo_history->actions = PUSH_LT(
- lt,
- create_dynarray(sizeof(Action)),
- destroy_dynarray);
+ Action action = {
+ .revert = revert,
+ };
- return undo_history;
+ stack_push(&undo_history->actions, context_data, context_data_size);
+ stack_push(&undo_history->actions, &action, sizeof(action));
}
-void destroy_undo_history(UndoHistory *undo_history)
+void undo_history_pop(UndoHistory *undo_history)
{
trace_assert(undo_history);
- RETURN_LT0(undo_history->lt);
-}
-void undo_history_push(UndoHistory *undo_history, Action action)
-{
- trace_assert(undo_history);
- dynarray_push(undo_history->actions, &action);
-}
+ if (stack_empty(&undo_history->actions) > 0) {
+ Action action = *(Action *)stack_top_element(&undo_history->actions);
+ stack_pop(&undo_history->actions);
-void undo_history_pop(UndoHistory *undo_history)
-{
- trace_assert(undo_history);
+ size_t context_size = stack_top_size(&undo_history->actions);
+ void *context = stack_top_element(&undo_history->actions);
- if (dynarray_count(undo_history->actions) > 0) {
- Action action;
- dynarray_pop(undo_history->actions, &action);
- action.revert(action.layer, action.context);
+ action.revert(context, context_size);
+ stack_pop(&undo_history->actions);
}
}