From d8a316514c03d85b771a9dce4a8a51b875d955b3 Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Tue, 4 Dec 2012 17:29:01 +0100 Subject: autoconf preparation: move everything into src/ subdir --- src/undo.h | 134 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 src/undo.h (limited to 'src/undo.h') diff --git a/src/undo.h b/src/undo.h new file mode 100644 index 0000000..43692e9 --- /dev/null +++ b/src/undo.h @@ -0,0 +1,134 @@ +#ifndef __UNDO_H +#define __UNDO_H + +#include + +#include +#include + +#include + +#ifdef DEBUG +#include "parser.h" +#endif + +class UndoToken { +public: + SLIST_ENTRY(UndoToken) tokens; + + gint pos; + + virtual ~UndoToken() {} + + virtual void run() = 0; +}; + +class UndoTokenMessage : public UndoToken { + unsigned int iMessage; + uptr_t wParam; + sptr_t lParam; + +public: + UndoTokenMessage(unsigned int _iMessage, + uptr_t _wParam = 0, sptr_t _lParam = 0) + : UndoToken(), iMessage(_iMessage), + wParam(_wParam), lParam(_lParam) {} + + void run(void); +}; + +template +class UndoTokenVariable : public UndoToken { + Type *ptr; + Type value; + +public: + UndoTokenVariable(Type &variable, Type _value) + : UndoToken(), ptr(&variable), value(_value) {} + + void + run(void) + { +#ifdef DEBUG + if ((State **)ptr == &States::current) + g_printf("undo state -> %p\n", (void *)value); +#endif + *ptr = value; + } +}; + +class UndoTokenString : public UndoToken { + gchar **ptr; + gchar *str; + +public: + UndoTokenString(gchar *&variable, gchar *_str) + : UndoToken(), ptr(&variable) + { + str = _str ? g_strdup(_str) : NULL; + } + + ~UndoTokenString() + { + g_free(str); + } + + void + run(void) + { + g_free(*ptr); + *ptr = str; + str = NULL; + } +}; + +extern class UndoStack { + SLIST_HEAD(Head, UndoToken) head; + +public: + bool enabled; + + UndoStack(bool _enabled = false) : enabled(_enabled) + { + SLIST_INIT(&head); + } + ~UndoStack(); + + void push(UndoToken *token); + + void push_msg(unsigned int iMessage, + uptr_t wParam = 0, sptr_t lParam = 0); + + template + inline Type & + push_var(Type &variable, Type value) + { + push(new UndoTokenVariable(variable, value)); + return variable; + } + + template + inline Type & + push_var(Type &variable) + { + return push_var(variable, variable); + } + + inline gchar *& + push_str(gchar *&variable, gchar *str) + { + push(new UndoTokenString(variable, str)); + return variable; + } + inline gchar *& + push_str(gchar *&variable) + { + return push_str(variable, variable); + } + + void pop(gint pos); + + void clear(void); +} undo; + +#endif \ No newline at end of file -- cgit v1.2.3