From 733cb5c8bef84a1724fed7ac876c5da06ec57ff1 Mon Sep 17 00:00:00 2001 From: Sam Fredrickson Date: Sun, 11 Jul 2010 17:39:03 -0700 Subject: [PATCH] Code cleanup & factoring of commands module. --- src/Calculator.h | 4 ++++ src/Commands.cpp | 58 +++++++++++++++++++++++++++----------------------------- 2 files changed, 32 insertions(+), 30 deletions(-) diff --git a/src/Calculator.h b/src/Calculator.h index 219c4cd..10b44b0 100644 --- a/src/Calculator.h +++ b/src/Calculator.h @@ -66,6 +66,8 @@ namespace RPN void printHelp (std::vector&); //! Copies the top stack and pushes it onto the history. void pushHistory (std::vector&); + //! The generic method to print the history. + void printHistoryGeneric (void (*)(Value)); //! The command to print the history. void printHistory (std::vector&); //! The command to print the history in detail. @@ -74,6 +76,8 @@ namespace RPN void printStack (std::vector&); //! The command to print the stack in detail. void printStackDetailed (std::vector&); + //! The generic method to print the variables. + void printVariablesGeneric (void (*)(Value)); //! The command to print the variables. void printVariables (std::vector&); //! The command to print the variables in detail. diff --git a/src/Commands.cpp b/src/Commands.cpp index 6857eaa..5ad1c72 100644 --- a/src/Commands.cpp +++ b/src/Commands.cpp @@ -101,26 +101,25 @@ void Calculator::printHelp(vector&) printHelpItems(helpItems); } -void Calculator::printHistory(vector&) +void Calculator::printHistoryGeneric(void (*printer)(Value)) { Print('['); BOOST_FOREACH(Stack& item, history) { - printAnyList(item, printValue); + printAnyList(item, printer); Print(", "); } Print("]\n"); } +void Calculator::printHistory(vector&) +{ + printHistoryGeneric(printValue); +} + void Calculator::printHistoryDetailed(vector&) { - Print('['); - BOOST_FOREACH(Stack& item, history) - { - printAnyList(item, printValueDetailed); - Print(", "); - } - Print("]\n"); + printHistoryGeneric(printValueDetailed); } void Calculator::printStack(vector&) @@ -141,30 +140,27 @@ void Calculator::printStackDetailed(vector&) } } -void Calculator::printVariables(vector&) +void Calculator::printVariablesGeneric(void (*printer)(Value)) { Print("[ "); BOOST_FOREACH(Variables::value_type& v, variables) { Print(v.first); Print(" = "); - Print(v.second); + printer(v.second); Print(", "); } Print("]\n"); } +void Calculator::printVariables(vector&) +{ + printVariablesGeneric(printValue); +} + void Calculator::printVariablesDetailed(vector&) { - Print("[ "); - BOOST_FOREACH(Variables::value_type& v, variables) - { - Print(v.first); - Print(" = "); - PrintDetailed(v.second); - Print(", "); - } - Print("]\n"); + printVariablesGeneric(printValueDetailed); } void Calculator::printVersion(vector&) @@ -192,12 +188,13 @@ void Calculator::sqrtTop(vector&) { if(HasStack() && StackSize() > 0) { - Value top = CurrentStack().front(); - CurrentStack().pop_front(); + Stack& stack = CurrentStack(); + Value top = stack.front(); + stack.pop_front(); #ifdef RPN_DOUBLE - CurrentStack().push_front(sqrt(top)); + stack.push_front(sqrt(top)); #elif RPN_LONG_DOUBLE - CurrentStack().push_front(sqrtl(top)); + stack.push_front(sqrtl(top)); #endif } } @@ -206,13 +203,14 @@ void Calculator::swap(vector&) { if(HasStack() && StackSize() > 1) { + Stack& stack = CurrentStack(); Value a, b; - a = CurrentStack().front(); - CurrentStack().pop_front(); - b = CurrentStack().front(); - CurrentStack().pop_front(); - CurrentStack().push_front(a); - CurrentStack().push_front(b); + a = stack.front(); + stack.pop_front(); + b = stack.front(); + stack.pop_front(); + stack.push_front(a); + stack.push_front(b); } } -- 2.11.4.GIT