[tbo] More flexible TboAction
- From: Daniel Garcia Moreno <danigm src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [tbo] More flexible TboAction
- Date: Fri, 3 Jun 2011 07:47:33 +0000 (UTC)
commit 0fc6ac6e65c1739a00a49c447d4a62aa15aa14a6
Author: danigm <danigm wadobo com>
Date: Fri Jun 3 09:45:49 2011 +0200
More flexible TboAction
src/tbo-undo.c | 11 ++++-------
src/tbo-undo.h | 8 ++++++--
src/undotest.c | 32 ++++++++++++++++++++++++++------
3 files changed, 36 insertions(+), 15 deletions(-)
---
diff --git a/src/tbo-undo.c b/src/tbo-undo.c
index d0fe4ba..ae5fad2 100644
--- a/src/tbo-undo.c
+++ b/src/tbo-undo.c
@@ -21,16 +21,13 @@
#include <stdlib.h>
#include "tbo-undo.h"
-TboAction *
-tbo_action_new (gpointer data,
+void
+tbo_action_set (TboAction *action,
gpointer action_do,
gpointer action_undo)
{
- TboAction *action = malloc (sizeof (TboAction));
- action->data = data;
action->action_do = action_do;
action->action_undo = action_undo;
- return action;
}
void
@@ -84,7 +81,7 @@ tbo_undo_stack_undo (TboUndoStack *stack)
// undo
TboAction *action = NULL;
action = (stack->list)->data;
- action->action_undo (action);
+ tbo_action_undo (action);
if (stack->list->next)
stack->list = (stack->list)->next;
@@ -109,7 +106,7 @@ tbo_undo_stack_redo (TboUndoStack *stack)
// redo
TboAction *action = NULL;
action = (stack->list)->data;
- action->action_do (action);
+ tbo_action_do (action);
}
void
diff --git a/src/tbo-undo.h b/src/tbo-undo.h
index c3f9332..9348e9a 100644
--- a/src/tbo-undo.h
+++ b/src/tbo-undo.h
@@ -23,16 +23,20 @@
#include <glib.h>
+#define tbo_action_new(action_type) (TboAction*) malloc (sizeof (action_type))
+#define tbo_action_do(action) ((TboAction*) action)->action_do ((TboAction*)action)
+#define tbo_action_undo(action) ((TboAction*) action)->action_undo ((TboAction*)action)
+
+
typedef struct _TboAction TboAction;
typedef struct _TboUndoStack TboUndoStack;
struct _TboAction {
- gpointer data;
void (*action_do) (TboAction *action);
void (*action_undo) (TboAction *action);
};
-TboAction * tbo_action_new (gpointer data, gpointer action_do, gpointer action_undo);
+void tbo_action_set (TboAction *action, gpointer action_do, gpointer action_undo);
void tbo_action_del (TboAction *action);
struct _TboUndoStack {
diff --git a/src/undotest.c b/src/undotest.c
index 8595c68..4e82a24 100644
--- a/src/undotest.c
+++ b/src/undotest.c
@@ -1,16 +1,36 @@
#include <stdio.h>
#include "tbo-undo.h"
+// Defining a custom TboAction with custom data
+typedef struct _TboActionString TboActionString;
+
+struct _TboActionString {
+ void (*action_do) (TboAction *action);
+ void (*action_undo) (TboAction *action);
+ char *data;
+};
+
void
-testdo (TboAction *action) {
+testdo (TboAction *act) {
+ TboActionString *action = (TboActionString*)act;
printf (" + doing %s\n", action->data);
}
void
-testundo (TboAction *action) {
+testundo (TboAction *act) {
+ TboActionString *action = (TboActionString*)act;
printf (" - UNdoing %s\n", action->data);
}
+TboAction *
+tbo_action_string_new (char *str) {
+ TboAction *act = tbo_action_new (TboActionString);
+ TboActionString *action = (TboActionString*)act;
+ action->data = str;
+ tbo_action_set (act, testdo, testundo);
+ return act;
+}
+
int
main (int argc, char **argv)
{
@@ -21,11 +41,11 @@ main (int argc, char **argv)
stack = tbo_undo_stack_new ();
- action = tbo_action_new ("Test action1", testdo, testundo);
+ action = tbo_action_string_new ("Test action1");
tbo_undo_stack_insert (stack, action);
- action = tbo_action_new ("Test action2", testdo, testundo);
+ action = tbo_action_string_new ("Test action2");
tbo_undo_stack_insert (stack, action);
- action = tbo_action_new ("Test action3", testdo, testundo);
+ action = tbo_action_string_new ("Test action3");
tbo_undo_stack_insert (stack, action);
tbo_undo_stack_undo (stack);
@@ -52,7 +72,7 @@ main (int argc, char **argv)
printf ("\nNow undo and redo\n");
tbo_undo_stack_undo (stack);
- action = tbo_action_new ("Test action4", testdo, testundo);
+ action = tbo_action_string_new ("Test action4");
tbo_undo_stack_insert (stack, action);
tbo_undo_stack_redo (stack);
tbo_undo_stack_undo (stack);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]