diff options
-rw-r--r-- | expressions.cpp | 8 | ||||
-rw-r--r-- | expressions.h | 79 |
2 files changed, 48 insertions, 39 deletions
diff --git a/expressions.cpp b/expressions.cpp index 38af2fc..6999312 100644 --- a/expressions.cpp +++ b/expressions.cpp @@ -28,7 +28,7 @@ Expressions::push(gint64 number) push(OP_NUMBER); - undo.push(new UndoTokenPop<gint64>(numbers)); + numbers.undo_pop(); return numbers.push(number); } @@ -40,7 +40,7 @@ Expressions::pop_num(int index) pop_op(); if (numbers.items() > 0) { n = numbers.pop(index); - undo.push(new UndoTokenPush<gint64>(numbers, n, index)); + numbers.undo_push(n, index); } return n; @@ -67,7 +67,7 @@ Expressions::add_digit(gchar digit) Expressions::Operator Expressions::push(Expressions::Operator op) { - undo.push(new UndoTokenPop<Operator>(operators)); + operators.undo_pop(); return operators.push(op); } @@ -90,7 +90,7 @@ Expressions::pop_op(int index) if (operators.items() > 0) { op = operators.pop(index); - undo.push(new UndoTokenPush<Operator>(operators, op, index)); + operators.undo_push(op, index); } return op; 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 */ |