Calculator
Extensible stack-based calculator primarily in library form
Result.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 #include <algorithm>
19 
20 #include "Result.h"
21 
22 namespace Calculator {
23 
24  Result::Result(std::initializer_list<Message> theMessages) {
25  messages.insert(messages.end(), theMessages.begin(), theMessages.end());
26  }
27 
28  Result::Result(std::initializer_list<PositionMessage> thePositionMessages) {
29  positionMessages.insert(positionMessages.end(), thePositionMessages.begin(), thePositionMessages.end());
30  }
31 
32  Result::Result(std::initializer_list<Message> theMessages,
33  std::initializer_list<PositionMessage> thePositionMessages
34  ) {
35  messages.insert(messages.end(), theMessages.begin(), theMessages.end());
36 
37  }
38 
40  return messages;
41  }
42 
43  bool Result::hasMessage(const std::string& message) const {
44  return std::any_of(messages.begin(), messages.end(),
45  [=](const std::string& str) { return 0 == message.compare(str); });
46  }
47 
48  void Result::addMessage(const std::string& message) {
49  messages.push_back(message);
50  }
51 
53  return positionMessages;
54  }
55 
56  bool Result::hasPositionMessage(const unsigned int position, const std::string& message) const {
57  return std::any_of(positionMessages.begin(), positionMessages.end(),
58  [=](const PositionMessage& pm) { return position == pm.first && 0 == message.compare(pm.second); });
59  }
60 
61  void Result::addPositionMessage(const unsigned int position, const std::string& message) {
62  positionMessages.push_back(PositionMessage(position, message));
63  std::sort(positionMessages.begin(), positionMessages.end());
64  }
65 
66  std::string Result::toString() const {
67  return toString(">> ");
68  }
69 
70  std::string Result::toString(const std::string& prefix) const {
71  std::ostringstream os;
72  for(auto message : messages) {
73  os << prefix << " " << message << std::endl ;
74  }
75  for(auto positionMessage : positionMessages) {
76  os << prefix << " @" << positionMessage.first << ": " << positionMessage.second << std::endl ;
77  }
78  return os.str();
79  }
80 
81 } // namespace Calculator
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
Container of Calculator resources.