diff options
author | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2012-11-07 20:12:18 +0100 |
---|---|---|
committer | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2012-11-07 20:12:18 +0100 |
commit | 2b99d5f24d891b887338af863f78a84a7231a7c1 (patch) | |
tree | a9084d9d786e38d27f588e62728bb27c3c18a2cb /expressions.h | |
parent | 99c8e92238986d00557ded56adc8495086d3c631 (diff) | |
download | sciteco-2b99d5f24d891b887338af863f78a84a7231a7c1.tar.gz |
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
Diffstat (limited to 'expressions.h')
-rw-r--r-- | expressions.h | 79 |
1 files changed, 44 insertions, 35 deletions
diff --git a/expressions.h b/expressions.h index 0f4c9eb..1f1637f 100644 --- a/expressions.h +++ b/expressions.h @@ -7,6 +7,40 @@ template <typename Type> class ValueStack { + class UndoTokenPush : public UndoToken { + ValueStack<Type> *stack; + + Type value; + int index; + + public: + UndoTokenPush(ValueStack<Type> *_stack, + Type _value, int _index = 1) + : stack(_stack), value(_value), index(_index) {} + + void + run(void) + { + stack->push(value, index); + } + }; + + class UndoTokenPop : public UndoToken { + ValueStack<Type> *stack; + + int index; + + public: + UndoTokenPop(ValueStack<Type> *_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 <typename Type> -class UndoTokenPush : public UndoToken { - ValueStack<Type> *stack; - - Type value; - int index; - -public: - UndoTokenPush(ValueStack<Type> &_stack, Type _value, int _index = 1) - : stack(&_stack), value(_value), index(_index) {} - - void - run(void) - { - stack->push(value, index); - } -}; - -template <typename Type> -class UndoTokenPop : public UndoToken { - ValueStack<Type> *stack; - - int index; - -public: - UndoTokenPop(ValueStack<Type> &_stack, int _index = 1) - : stack(&_stack), index(_index) {} - - void - run(void) - { - stack->pop(index); - } -}; - /* * Arithmetic expression stacks */ |