From 2b99d5f24d891b887338af863f78a84a7231a7c1 Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Wed, 7 Nov 2012 20:12:18 +0100 Subject: simplified/cleaned up undo operations on value stack * special undo tokens are private to the ValueStack class and automatically parameterized * undo_push() and undo_pop() methods hide the internals of pushing the undo tokens --- expressions.h | 79 +++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 44 insertions(+), 35 deletions(-) (limited to 'expressions.h') diff --git a/expressions.h b/expressions.h index 0f4c9eb..1f1637f 100644 --- a/expressions.h +++ b/expressions.h @@ -7,6 +7,40 @@ template class ValueStack { + class UndoTokenPush : public UndoToken { + ValueStack *stack; + + Type value; + int index; + + public: + UndoTokenPush(ValueStack *_stack, + Type _value, int _index = 1) + : stack(_stack), value(_value), index(_index) {} + + void + run(void) + { + stack->push(value, index); + } + }; + + class UndoTokenPop : public UndoToken { + ValueStack *stack; + + int index; + + public: + UndoTokenPop(ValueStack *_stack, int _index = 1) + : stack(_stack), index(_index) {} + + void + run(void) + { + stack->pop(index); + } + }; + int size; Type *stack; @@ -38,6 +72,11 @@ public: top++; return peek(index) = value; } + inline void + undo_push(Type value, int index = 1) + { + undo.push(new UndoTokenPush(this, value, index)); + } inline Type pop(int index = 1) @@ -50,6 +89,11 @@ public: return v; } + inline void + undo_pop(int index = 1) + { + undo.push(new UndoTokenPop(this, index)); + } inline Type & peek(int index = 1) @@ -58,41 +102,6 @@ public: } }; -template -class UndoTokenPush : public UndoToken { - ValueStack *stack; - - Type value; - int index; - -public: - UndoTokenPush(ValueStack &_stack, Type _value, int _index = 1) - : stack(&_stack), value(_value), index(_index) {} - - void - run(void) - { - stack->push(value, index); - } -}; - -template -class UndoTokenPop : public UndoToken { - ValueStack *stack; - - int index; - -public: - UndoTokenPop(ValueStack &_stack, int _index = 1) - : stack(&_stack), index(_index) {} - - void - run(void) - { - stack->pop(index); - } -}; - /* * Arithmetic expression stacks */ -- cgit v1.2.3