}
static
-void background_undo_color(void *layer, Context context)
+void background_undo_color(void *layer, void *context, size_t context_size)
{
trace_assert(layer);
- BackgroundLayer *background_layer = layer;
+ trace_assert(context);
+ trace_assert(sizeof(Color) == context_size);
- trace_assert(sizeof(Color) < CONTEXT_SIZE);
- Color *color = (Color *)context.data;
+ BackgroundLayer *background_layer = layer;
+ Color *color = context;
background_layer->color_picker = create_color_picker_from_rgba(*color);
}
}
static
-void player_layer_undo(void *layer, Context context)
+void player_layer_undo(void *layer, void *context, size_t context_size)
{
trace_assert(layer);
- PlayerLayer *player_layer = layer;
+ trace_assert(context);
+ trace_assert(sizeof(UndoContext) == context_size);
- UndoContext *undo_context = (UndoContext *)context.data;
+ PlayerLayer *player_layer = layer;
+ UndoContext *undo_context = context;
player_layer->position = undo_context->position;
player_layer->color_picker = create_color_picker_from_rgba(undo_context->color);
}
static
-void point_layer_undo(void *layer, Context context)
+void point_layer_undo(void *layer, void *context, size_t context_size)
{
trace_assert(layer);
- PointLayer *point_layer = layer;
+ trace_assert(context);
+ trace_assert(sizeof(UndoContext) == context_size);
- UndoContext *undo_context = (UndoContext *)context.data;
+ PointLayer *point_layer = layer;
+ UndoContext *undo_context = context;
switch (undo_context->type) {
case UNDO_ADD: {
}
static
-void rect_layer_undo(void *layer, Context context)
+void rect_layer_undo(void *layer, void *context, size_t context_size)
{
trace_assert(layer);
- RectLayer *rect_layer = layer;
+ trace_assert(context);
+ trace_assert(sizeof(UndoContext) == context_size);
- trace_assert(sizeof(UndoContext) < CONTEXT_SIZE);
- UndoContext *undo_context = (UndoContext *)context.data;
+ RectLayer *rect_layer = layer;
+ UndoContext *undo_context = context;
switch (undo_context->type) {
case UNDO_ADD: {
#include "undo_history.h"
#include "stack.h"
-struct UndoHistory
-{
+struct UndoHistory {
Lt *lt;
Stack actions;
};
-
-
typedef struct {
- void *layer;
- Context context;
RevertAction revert;
+ void *layer;
} Action;
-static inline
-Action create_action(void *layer, RevertAction revert,
- void *context_data,
- size_t context_data_size)
-{
- trace_assert(layer);
- trace_assert(revert);
- trace_assert(context_data_size < CONTEXT_SIZE);
-
- Action action = {
- .layer = layer,
- .revert = revert
- };
-
- if (context_data) {
- memcpy(action.context.data, context_data, context_data_size);
- }
-
- return action;
-}
-
UndoHistory *create_undo_history(void)
{
Lt *lt = create_lt();
{
trace_assert(undo_history);
- Action action = create_action(
- layer,
- revert,
- context_data,
- context_data_size);
+ Action action = {
+ .revert = revert,
+ .layer = layer
+ };
- stack_push(
- &undo_history->actions,
- &action,
- sizeof(action));
+ stack_push(&undo_history->actions, context_data, context_data_size);
+ stack_push(&undo_history->actions, &action, sizeof(action));
}
void undo_history_pop(UndoHistory *undo_history)
trace_assert(undo_history);
if (stack_empty(&undo_history->actions) > 0) {
- Action *action = stack_top_element(&undo_history->actions);
- action->revert(action->layer, action->context);
+ Action action = *(Action *)stack_top_element(&undo_history->actions);
+ stack_pop(&undo_history->actions);
+
+ size_t context_size = stack_top_size(&undo_history->actions);
+ void *context = stack_top_element(&undo_history->actions);
+
+ action.revert(action.layer, context, context_size);
stack_pop(&undo_history->actions);
}
}
#include "layer.h"
-#define CONTEXT_SIZE 256
-
-#define ASSERT_CONTEXT_SIZE(context) \
- trace_assert(sizeof(context) <= CONTEXT_SIZE)
-
typedef enum {
UNDO_ADD,
UNDO_DELETE,
UNDO_UPDATE
} UndoType;
-typedef struct {
- char data[CONTEXT_SIZE];
-} Context;
-
-typedef void (*RevertAction)(void *layer, Context context);
+typedef void (*RevertAction)(void *layer, void *context, size_t context_size);
typedef struct UndoHistory UndoHistory;