Calculator
Extensible stack-based calculator primarily in library form
BinaryMathOperator.h
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 #ifndef BINARY_MATH_OPERATOR_H
15 #define BINARY_MATH_OPERATOR_H
16 
17 #ifndef STACK_OPERATOR_H
18 #include "StackOperator.h"
19 #endif // STACK_OPERATOR_H
20 
21 namespace Calculator {
22 
23  /** Stack Modifier for Binary Math Operations.
24  *
25  * Support multiple operations distinguished by the Operation parameter.
26  *
27  * All Operations pop 2 items, caclulate formerTop (operator) formerSecond
28  * and push the result back onto the stack.
29  */
31  public:
32  typedef std::shared_ptr<BinaryMathOperator> Ptr;
33 
34  /** \enum Operation Operations supported */
35  enum class Operation {
36  /** Add the two values */
37  ADD,
38 
39  /** Subtract the two values */
40  SUBTRACT,
41 
42  /** Multiply the two values */
43  MULTIPLY,
44 
45  /** Divide the two values */
46  DIVIDE,
47 
48  /** Raise top to the exponent second */
49  EXPONENT
50  };
51 
52  /** Create to perform operation.
53  *
54  * @param op to perform
55  *
56  * @return BinaryMathOperator created
57  */
59 
61 
62  protected:
63  /** Create to perform operation.
64  *
65  * @param op to perform
66  */
68 
73 
74  public:
75  virtual Result operator()(Stack& stack, StackOperator::Ptr ofThis);
76 
77  private:
78  const Operation op;
79  };
80 
81  /** Create BinaryMathOperation from a string */
83  public:
84  virtual ~BinaryMathCreator();
85 
86  virtual std::string getHelp() const;
87  virtual StackOperator::Ptr create(const std::string& str);
88  };
89 } // namespace Calculator
90 
91 #endif // BINARY_MATH_OPERATOR_H
Create BinaryMathOperation from a string.
virtual std::string getHelp() const
virtual StackOperator::Ptr create(const std::string &str)
Stack Modifier for Binary Math Operations.
BinaryMathOperator & operator=(BinaryMathOperator &&)=delete
BinaryMathOperator(const BinaryMathOperator &)=delete
@ MULTIPLY
Multiply the two values.
@ SUBTRACT
Subtract the two values.
@ EXPONENT
Raise top to the exponent second.
BinaryMathOperator(Operation op)
Create to perform operation.
std::shared_ptr< BinaryMathOperator > Ptr
BinaryMathOperator & operator=(const BinaryMathOperator &)=delete
BinaryMathOperator(BinaryMathOperator &&)=delete
static BinaryMathOperator::Ptr create(Operation op)
Create to perform operation.
virtual Result operator()(Stack &stack, StackOperator::Ptr ofThis)
Execute this operator on stack.
Collection of Result information for an operation.
Definition: Result.h:30
Stack of values to process from.
Definition: Stack.h:208
Factory creators of StackOperator::Ptrs.
Definition: StackOperator.h:57
Base class for all operators.
Definition: StackOperator.h:29
std::shared_ptr< StackOperator > Ptr
Definition: StackOperator.h:31
Container of Calculator resources.