Calculator
Extensible stack-based calculator primarily in library form
StackManipulator.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 STACK_MANIPULATOR_H
15 #define STACK_MANIPULATOR_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 Stack Operations
24  *
25  * Support multiple operations distinguished by the Operation parameter.
26  *
27  * Operations support separate stack-element manipulations
28  */
30  public:
31  /** \enum Operation Operations supported */
32  enum class Operation {
33  /** Pop the top value off and forget it */
34  POP,
35 
36  /** Duplicate the top value */
37  DUP,
38 
39  /** Swap the top two elements */
40  SWAP,
41 
42  /** Clear the entire stack and set of variables */
43  RESET,
44 
45  /** Clear the entire stack */
46  POP_ALL
47  };
48 
49  /** Create to perform operation.
50  *
51  * @param operation to perform
52  *
53  * @return StackManipulator created
54  */
55  static StackManipulator::Ptr create(Operation operation);
56 
58 
59  private:
60  /** Create to perform operation.
61  *
62  * @param operation to perform
63  */
64  StackManipulator(Operation operation);
65 
66  StackManipulator(const StackManipulator&) = delete;
68  StackManipulator& operator=(const StackManipulator&) = delete;
69  StackManipulator& operator=(StackManipulator&&) = delete;
70 
71  public:
72  virtual Result operator()(Stack& stack, StackOperator::Ptr ofThis);
73 
74  private:
75  const Operation op;
76  };
77 
78  /** Create StackManipulator from a string */
80  public:
81  virtual ~StackManipulatorCreator();
82 
83  virtual std::string getHelp() const;
84  virtual StackOperator::Ptr create(const std::string& str);
85  };
86 } // namespace Calculator
87 
88 #endif // STACK_MANIPULATOR_H
Collection of Result information for an operation.
Definition: Result.h:30
Stack of values to process from.
Definition: Stack.h:208
Create StackManipulator from a string.
virtual StackOperator::Ptr create(const std::string &str)
virtual std::string getHelp() const
Stack Modifier for Stack Operations.
static StackManipulator::Ptr create(Operation operation)
Create to perform operation.
virtual Result operator()(Stack &stack, StackOperator::Ptr ofThis)
Execute this operator on stack.
Operation
Operations supported.
@ SWAP
Swap the top two elements.
@ POP_ALL
Clear the entire stack.
@ RESET
Clear the entire stack and set of variables.
@ POP
Pop the top value off and forget it.
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.