From 17c62e3d58a6f8cc301be10060f772f10ab4466c Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Thu, 14 Feb 2013 02:57:54 +0100 Subject: undo tokens for changing an object pointer will care about the eventual deletion of the object --- src/undo.h | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'src') diff --git a/src/undo.h b/src/undo.h index 0fd9809..8d5d5d6 100644 --- a/src/undo.h +++ b/src/undo.h @@ -99,6 +99,31 @@ public: } }; +template +class UndoTokenObject : public UndoToken { + Type **ptr; + Type *obj; + +public: + UndoTokenObject(Type *&variable, Type *_obj) + : UndoToken(), ptr(&variable), obj(_obj) {} + + ~UndoTokenObject() + { + if (obj) + delete obj; + } + + void + run(void) + { + if (*ptr) + delete *ptr; + *ptr = obj; + obj = NULL; + } +}; + extern class UndoStack { SLIST_HEAD(Head, UndoToken) head; @@ -143,6 +168,21 @@ public: return push_str(variable, variable); } + template + inline Type *& + push_obj(Type *&variable, Type *obj) + { + push(new UndoTokenObject(variable, obj)); + return variable; + } + + template + inline Type *& + push_obj(Type *&variable) + { + return push_obj(variable, variable); + } + void pop(gint pos); void clear(void); -- cgit v1.2.3