Calculator
Extensible stack-based calculator primarily in library form
Result.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 RESULT_H
15 #define RESULT_H
16 
17 #include <vector>
18 
19 namespace Calculator {
20 
21  /** Collection of Result information for an operation.
22  *
23  * Contains non-positional message strings for things like "Ok". May be used
24  * by driving code to pass around commands like "quit".
25  *
26  * Contains positional messages of stack position (0 is top) and message to
27  * indicate what failed, such as Stack Underflow or conversion error on a
28  * position in the stack.
29  */
30  class Result {
31  public:
32  typedef std::string Message;
33  typedef std::vector<Message> Messages;
34 
35  /** For convenience, we use a pair for the line and message for a position */
36  typedef std::pair<unsigned int, std::string> PositionMessage;
37  typedef std::vector<PositionMessage> PositionMessages;
38 
39  Result() = default;
40 
41  /** Create with all messages in theMessage */
42  Result(std::initializer_list<Message> theMessages);
43 
44  /** Create with all the positional messages in thePositionMessages */
45  Result(std::initializer_list<PositionMessage> thePositionMessages);
46 
47  /** Create with all messages in theMessage and all the positional messages
48  * in thePositionMessages.
49  */
50  Result(std::initializer_list<Message> theMessages,
51  std::initializer_list<PositionMessage> thePositionMessages);
52 
53  /** @return all the messages for iteration */
54  const Messages& getMessages() const;
55 
56  /** Convenience to test with message has been set
57  *
58  * @param message to test
59  *
60  * @return true if the message is present, otherwise false
61  */
62  bool hasMessage(const std::string& message) const;
63 
64  /** Add message.
65  *
66  * @param message to add
67  */
68  void addMessage(const std::string& message);
69 
70  /** @return all the positional messages for iteration */
72 
73  /** Convenience to test if the positional message has been set at the position
74  *
75  * @param position to look for message in
76  * @param message to look for in position
77  *
78  * @return true if the message is present at position, otherwise false
79  */
80  bool hasPositionMessage(const unsigned int position, const std::string& message) const;
81 
82  /** Add the positional message
83  *
84  * @param position the message belongs to
85  * @param message to be added at position
86  */
87  void addPositionMessage(const unsigned int position, const std::string& message);
88 
89  /** @return a string representation of this Result */
90  std::string toString() const;
91 
92  /** Create a string representation of this Result with prefix at the start
93  * of each line.
94  *
95  * @param prefix to put before each line
96  *
97  * @return string representation
98  */
99  std::string toString(const std::string& prefix) const;
100 
101  private:
102  /** All messages */
103  Messages messages;
104 
105  /** All positional messages */
106  PositionMessages positionMessages;
107  };
108 
109 } // namespace Calculator
110 
111 #endif // RESULT_H
Collection of Result information for an operation.
Definition: Result.h:30
const Messages & getMessages() const
Definition: Result.cpp:39
std::pair< unsigned int, std::string > PositionMessage
For convenience, we use a pair for the line and message for a position.
Definition: Result.h:36
bool hasPositionMessage(const unsigned int position, const std::string &message) const
Convenience to test if the positional message has been set at the position.
Definition: Result.cpp:56
std::vector< Message > Messages
Definition: Result.h:33
const PositionMessages & getPositionMessages() const
Definition: Result.cpp:52
void addPositionMessage(const unsigned int position, const std::string &message)
Add the positional message.
Definition: Result.cpp:61
std::vector< PositionMessage > PositionMessages
Definition: Result.h:37
void addMessage(const std::string &message)
Add message.
Definition: Result.cpp:48
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
std::string Message
Definition: Result.h:32
Container of Calculator resources.