Code cleanup & factoring of commands module.
[rpn.git] / src / Calculator.h
blob10b44b09450ebd44b70da14945292d822095e501
1 /*******************************************************************************
2 * Reverse Polish Notation calculator. *
3 * Copyright (c) 2007-2009, Samuel Fredrickson <kinghajj@gmail.com> *
4 * All rights reserved. *
5 * *
6 * Redistribution and use in source and binary forms, with or without *
7 * modification, are permitted provided that the following conditions are met: *
8 * * Redistributions of source code must retain the above copyright *
9 * notice, this list of conditions and the following disclaimer. *
10 * * Redistributions in binary form must reproduce the above copyright *
11 * notice, this list of conditions and the following disclaimer in the *
12 * documentation and/or other materials provided with the distribution. *
13 * *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ``AS IS'' AND ANY EXPRESS *
15 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED *
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE *
17 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY *
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES *
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR *
20 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER *
21 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT *
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY *
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH *
24 * DAMAGE. *
25 ******************************************************************************/
27 /*******************************************************************************
28 * Calculator.h - header for the Calculator class. *
29 ******************************************************************************/
31 #ifndef RPN_CALCULATOR_H
32 #define RPN_CALCULATOR_H
34 #include <string>
35 #include <vector>
36 #include "typedefs.h"
38 namespace RPN
40 //! The main class for the program.
41 class Calculator
43 //! Holds the calculator's status, i.e. whether it's running or not.
44 enum Status
46 Continue,
47 Stop
50 Commands commands;
51 HelpItems helpItems;
52 History history;
53 Operators operators;
54 Status status;
55 Variables variables;
57 //! The command to duplicate the top item of the stack.
58 void dup (std::vector<std::string>&);
59 //! The command to exit the calculator.
60 void exit (std::vector<std::string>&);
61 //! Pops the topmost item from the stack.
62 void pop (std::vector<std::string>&);
63 //! Removes the top stack as long as there will be at least one left.
64 void popHistory (std::vector<std::string>&);
65 //! The command to print the help list.
66 void printHelp (std::vector<std::string>&);
67 //! Copies the top stack and pushes it onto the history.
68 void pushHistory (std::vector<std::string>&);
69 //! The generic method to print the history.
70 void printHistoryGeneric (void (*)(Value));
71 //! The command to print the history.
72 void printHistory (std::vector<std::string>&);
73 //! The command to print the history in detail.
74 void printHistoryDetailed(std::vector<std::string>&);
75 //! The command to print the stack.
76 void printStack (std::vector<std::string>&);
77 //! The command to print the stack in detail.
78 void printStackDetailed (std::vector<std::string>&);
79 //! The generic method to print the variables.
80 void printVariablesGeneric (void (*)(Value));
81 //! The command to print the variables.
82 void printVariables (std::vector<std::string>&);
83 //! The command to print the variables in detail.
84 void printVariablesDetailed(std::vector<std::string>&);
85 void printVersion (std::vector<std::string>&);
86 //! Pops the top item, then pushes its square root.
87 void sqrtTop (std::vector<std::string>&);
88 //! Swaps the top two items of the stack.
89 void swap (std::vector<std::string>&);
90 //! Unsets a previously set variable.
91 void unset (std::vector<std::string>&);
93 //! Returns true if there is at least one stack.
94 bool HasStack() const { return history.size() != 0; }
96 //! Returns a reference to the current stack.
97 Stack& CurrentStack() { return history.front(); }
98 const Stack& CurrentStack() const { return history.front(); }
100 //! Returns the current stack's size.
101 size_t StackSize() const
103 return HasStack() ? CurrentStack().size() : 0;
106 //! Returns the topmost item of the current stack.
107 Value TopmostItem() const
109 return HasStack() && StackSize() > 0 ? CurrentStack().front() : 0;
112 public:
114 //! The default and only constructor.
115 Calculator()
116 : commands (defaultCommands()),
117 helpItems (defaultHelpItems()),
118 history (defaultHistory()),
119 operators (defaultOperators()),
120 status (Continue),
121 variables (defaultVariables())
125 //! Evaluates a string.
126 void Eval(std::string input);
128 //! Returns true if the calculator is running.
129 bool IsRunning() const { return status == Continue; }
131 //! Returns a map of the default commands.
132 Commands defaultCommands();
134 //! Displays the top item of the stack.
135 void Display() const;
139 #endif