Calculator
Extensible stack-based calculator primarily in library form
StackOperator.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_OPERATOR_H
15 #define STACK_OPERATOR_H
16 
17 #ifndef RESULT_H
18 #include "Result.h"
19 #endif // STACK_ERRROR_H
20 
21 namespace Calculator {
22 
23  class Stack;
24 
25  /** \brief Base class for all operators.
26  *
27  * StackOperatorCreators and the StackFactory will return StackOperator::Ptr.
28  */
29  class StackOperator {
30  public:
31  typedef std::shared_ptr<StackOperator> Ptr;
32 
33  virtual ~StackOperator() { }
34 
35  protected:
36  StackOperator() = default;
37 
38  StackOperator(const StackOperator&) = delete;
42 
43  public:
44  /** Execute this operator on stack
45  *
46  * @param stack to operate on
47  * @param ofThis is this in a copied StackOperator::Ptr to maintain
48  * integrity of the shared_ptrs.
49  *
50  * @return Result of execution
51  */
52  virtual Result operator()(Stack& stack, StackOperator::Ptr ofThis) = 0;
53  };
54 
55  /** \brief Factory creators of StackOperator::Ptrs
56  */
58  public:
59  typedef std::shared_ptr<StackOperatorCreator> Ptr;
60 
61  virtual ~StackOperatorCreator() { }
62 
63  /** @return Help text for StackOperator::Ptrs this creates */
64  virtual std::string getHelp() const = 0;
65 
66  /** @return StackOperator::Ptr of the StackOperator created from str, or
67  * it will be empty to indicate str cannot be used to create an item of
68  * this type.
69  */
70  virtual StackOperator::Ptr create(const std::string& str) = 0;
71  };
72 
73 } // namespace Calculator
74 
75 #endif // STACK_OPERATOR_H
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
virtual std::string getHelp() const =0
std::shared_ptr< StackOperatorCreator > Ptr
Definition: StackOperator.h:59
virtual StackOperator::Ptr create(const std::string &str)=0
Base class for all operators.
Definition: StackOperator.h:29
StackOperator(const StackOperator &)=delete
StackOperator & operator=(const StackOperator &)=delete
std::shared_ptr< StackOperator > Ptr
Definition: StackOperator.h:31
StackOperator & operator=(StackOperator &&)=delete
virtual Result operator()(Stack &stack, StackOperator::Ptr ofThis)=0
Execute this operator on stack.
StackOperator(StackOperator &&)=delete
Container of Calculator resources.