Calculator
Extensible stack-based calculator primarily in library form
Executive.cpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2013, Komodo Does Inc
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6 
7 - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8 - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
9 - Neither the name of the Komodo Does Inc nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10 
11 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
12 */
13 
14 #include <memory>
15 #include <sstream>
16 #include <string>
17 #include <vector>
18 
19 using namespace std;
20 
21 #include "Executive.h"
22 #include "Stack.h"
23 #include "StackOperatorFactory.h"
24 #include "Error.h"
25 
26 namespace Calculator {
27 
28 Executive::Executive(Stack& theStack)
29  : stack(theStack), operationCount(0)
30 {
31 }
32 
33 void Executive::process(StackOperatorFactory& factory, std::istream& input, std::ostream& output) {
34  output << toString() << endl;
35  while(true) {
36  std::string command;
37  input >> command;
38  if(!input) {
39  break;
40  }
41 
42  Result result = process(factory, command);
43  if(result.hasMessage("quit")) {
44  output << "User requested quit..." << endl;
45  return;
46  }
47 
48  output << result.toString(std::string(">> ") + std::to_string(operationCount));
49  output << toString() << endl;
50  }
51 }
52 
53 std::string Executive::toString() const {
54  return std::string("Stack: ") + stack.toString();
55 }
56 
57 Result Executive::process(StackOperatorFactory& factory, const std::string& input) {
58  if(0 == input.compare("quit")) {
59  return Result({Error::Ok, "quit"});
60  }
61 
62  if(0 == input.compare("help")) {
63  std::ostringstream os;
64  doHelp(factory, os);
65 
66  return Result({Error::Ok, os.str()});
67  }
68 
69  StackOperator::Ptr op = factory.create(input);
70  if(!op) {
71  return Result({std::string("Ignoring invalid input: \"") + input + "\""});
72  }
73 
74  return process(op);
75 }
76 
78  if(!oper) {
79  return Result({"Internal Error: Invalid Operator"});
80  }
81 
82  Result results = (*oper)(stack, oper);
83 
84  ++operationCount;
85 
86  return results;
87 }
88 
89 unsigned int Executive::getOperationCount() const {
90  return operationCount;
91 }
92 
93  void Executive::doHelp(const StackOperatorFactory& factory, ostream& output) const {
94  output << "Stack-based calculator." << endl;
95  output << "Commands:" << endl;
96  output << "\tquit -- exit the program" << endl;
97  output << "\thelp -- help for the program" << endl;
98 
99  output << endl;
100  output << factory.getHelp();
101 }
102 
104  : executive(theStack), factory(theFactory)
105 {
106 }
107 
108 void FixedOperatorExecutive::doHelp(std::ostream& output) const {
109  executive.doHelp(factory, output);
110 }
111 
112 void FixedOperatorExecutive::process(std::istream& input, std::ostream& output) {
113  executive.process(factory, input, output);
114 }
115 
116 Result FixedOperatorExecutive::process(const std::string& input) {
117  return executive.process(factory, input);
118 }
119 
120 } // namespace Caclulator
std::string toString() const
Definition: Executive.cpp:53
unsigned int getOperationCount() const
Definition: Executive.cpp:89
void doHelp(const StackOperatorFactory &factory, std::ostream &output) const
Generate help info for this executive to output.
Definition: Executive.cpp:93
void process(StackOperatorFactory &factory, std::istream &input, std::ostream &output)
Process commands from input and pushing generated info to output.
Definition: Executive.cpp:33
FixedOperatorExecutive(StackOperatorFactory &theFactory, Stack &theStack)
Definition: Executive.cpp:103
void process(std::istream &input, std::ostream &output)
Process commands from input and pushing generated info to output.
Definition: Executive.cpp:112
void doHelp(std::ostream &output) const
Definition: Executive.cpp:108
Collection of Result information for an operation.
Definition: Result.h:30
std::string toString() const
Definition: Result.cpp:66
bool hasMessage(const std::string &message) const
Convenience to test with message has been set.
Definition: Result.cpp:43
Stack of values to process from.
Definition: Stack.h:208
std::string toString() const
Create string representation of the current contents of the Stack.
Definition: Stack.cpp:245
Factory to create StackOperators and help from StackOperatorCreators.
std::shared_ptr< StackOperator > create(const std::string &str) const
Try to create a StackOperator for the given str.
std::shared_ptr< StackOperator > Ptr
Definition: StackOperator.h:31
const std::string Ok
Operation was successful.
Definition: Error.h:23
Container of Calculator resources.