diff --git a/src/chatbot.cpp b/src/chatbot.cpp index 41d1f0c1f..15bfdcdb3 100644 --- a/src/chatbot.cpp +++ b/src/chatbot.cpp @@ -45,6 +45,65 @@ ChatBot::~ChatBot() //// STUDENT CODE //// +ChatBot::ChatBot(const ChatBot &source) { + std::cout << "ChatBot Copy Constructor" << std::endl; + + // image is owned, so we allocate the memory and copy content + _image = new wxBitmap; + *_image = *source._image; + + // not-owned, so we just copy pointers + _currentNode = source._currentNode; + _rootNode = source._rootNode; + _chatLogic = source._chatLogic; +} + +ChatBot& ChatBot::operator=(const ChatBot &source) { + std::cout << "ChatBot Copy Assignment" << std::endl; + if (this == &source) { + return *this; + } + + delete _image; + _image = new wxBitmap; + *_image = *source._image; + + _currentNode = source._currentNode; + _rootNode = source._rootNode; + _chatLogic = source._chatLogic; + + return *this; +} + +ChatBot::ChatBot(ChatBot &&source) noexcept { + std::cout << "ChatBot Move Constructor" << std::endl; + _image = source._image; + _currentNode = source._currentNode; + _rootNode = source._rootNode; + _chatLogic = source._chatLogic; + + source._image = nullptr; + source._currentNode = nullptr; + source._rootNode = nullptr; + source._chatLogic = nullptr; +} + +ChatBot& ChatBot::operator=(ChatBot &&source) noexcept { + std::cout << "ChatBot Move Assignment" << std::endl; + if (this == &source) { + return *this; + } + + delete _image; + _image = new wxBitmap; + *_image = *source._image; + + _currentNode = source._currentNode; + _rootNode = source._rootNode; + _chatLogic = source._chatLogic; + + return *this; +} //// //// EOF STUDENT CODE diff --git a/src/chatbot.h b/src/chatbot.h index 0367a93f8..f5ec03f5f 100644 --- a/src/chatbot.h +++ b/src/chatbot.h @@ -29,7 +29,11 @@ class ChatBot //// STUDENT CODE //// - + ChatBot(const ChatBot &source); + ChatBot& operator=(const ChatBot &source); + ChatBot(ChatBot &&source) noexcept; + ChatBot& operator=(ChatBot &&source) noexcept; + //// //// EOF STUDENT CODE diff --git a/src/chatgui.cpp b/src/chatgui.cpp index 6637e562b..ad6556d63 100644 --- a/src/chatgui.cpp +++ b/src/chatgui.cpp @@ -118,7 +118,7 @@ ChatBotPanelDialog::ChatBotPanelDialog(wxWindow *parent, wxWindowID id) //// // create chat logic instance - _chatLogic = new ChatLogic(); + _chatLogic = std::make_unique(); // pass pointer to chatbot dialog so answers can be displayed in GUI _chatLogic->SetPanelDialogHandle(this); @@ -135,7 +135,7 @@ ChatBotPanelDialog::~ChatBotPanelDialog() //// STUDENT CODE //// - delete _chatLogic; + _chatLogic.reset(); //// //// EOF STUDENT CODE diff --git a/src/chatgui.h b/src/chatgui.h index 503c59790..9b7ad1fab 100644 --- a/src/chatgui.h +++ b/src/chatgui.h @@ -2,6 +2,7 @@ #define CHATGUI_H_ #include +#include class ChatLogic; // forward declaration @@ -16,8 +17,7 @@ class ChatBotPanelDialog : public wxScrolledWindow //// STUDENT CODE //// - ChatLogic *_chatLogic; - + std::unique_ptr _chatLogic; //// //// EOF STUDENT CODE @@ -27,7 +27,7 @@ class ChatBotPanelDialog : public wxScrolledWindow ~ChatBotPanelDialog(); // getter / setter - ChatLogic *GetChatLogicHandle() { return _chatLogic; } + ChatLogic *GetChatLogicHandle() { return _chatLogic.get(); } // events void paintEvent(wxPaintEvent &evt); diff --git a/src/chatlogic.cpp b/src/chatlogic.cpp index 79c58ef41..9162c3fd4 100644 --- a/src/chatlogic.cpp +++ b/src/chatlogic.cpp @@ -17,12 +17,6 @@ ChatLogic::ChatLogic() //// STUDENT CODE //// - // create instance of chatbot - _chatBot = new ChatBot("../images/chatbot.png"); - - // add pointer to chatlogic so that chatbot answers can be passed on to the GUI - _chatBot->SetChatLogicHandle(this); - //// //// EOF STUDENT CODE } @@ -32,21 +26,6 @@ ChatLogic::~ChatLogic() //// STUDENT CODE //// - // delete chatbot instance - delete _chatBot; - - // delete all nodes - for (auto it = std::begin(_nodes); it != std::end(_nodes); ++it) - { - delete *it; - } - - // delete all edges - for (auto it = std::begin(_edges); it != std::end(_edges); ++it) - { - delete *it; - } - //// //// EOF STUDENT CODE } @@ -127,16 +106,16 @@ void ChatLogic::LoadAnswerGraphFromFile(std::string filename) //// // check if node with this ID exists already - auto newNode = std::find_if(_nodes.begin(), _nodes.end(), [&id](GraphNode *node) { return node->GetID() == id; }); + auto newNode = std::find_if(_nodes.begin(), _nodes.end(), [&id](const std::unique_ptr& node) { return node->GetID() == id; }); // create new element if ID does not yet exist if (newNode == _nodes.end()) { - _nodes.emplace_back(new GraphNode(id)); + _nodes.emplace_back(std::make_unique(id)); newNode = _nodes.end() - 1; // get iterator to last element // add all answers to current node - AddAllTokensToElement("ANSWER", tokens, **newNode); + AddAllTokensToElement("ANSWER", tokens, *((*newNode).get())); } //// @@ -156,21 +135,20 @@ void ChatLogic::LoadAnswerGraphFromFile(std::string filename) if (parentToken != tokens.end() && childToken != tokens.end()) { // get iterator on incoming and outgoing node via ID search - auto parentNode = std::find_if(_nodes.begin(), _nodes.end(), [&parentToken](GraphNode *node) { return node->GetID() == std::stoi(parentToken->second); }); - auto childNode = std::find_if(_nodes.begin(), _nodes.end(), [&childToken](GraphNode *node) { return node->GetID() == std::stoi(childToken->second); }); + auto parentNode = std::find_if(_nodes.begin(), _nodes.end(), [&parentToken](const std::unique_ptr& node) { return node->GetID() == std::stoi(parentToken->second); }); + auto childNode = std::find_if(_nodes.begin(), _nodes.end(), [&childToken](const std::unique_ptr& node) { return node->GetID() == std::stoi(childToken->second); }); // create new edge - GraphEdge *edge = new GraphEdge(id); - edge->SetChildNode(*childNode); - edge->SetParentNode(*parentNode); - _edges.push_back(edge); + auto edge = std::make_shared(id); + edge->SetChildNode((*childNode).get()); + edge->SetParentNode((*parentNode).get()); // find all keywords for current node AddAllTokensToElement("KEYWORD", tokens, *edge); // store reference in child node and parent node (*childNode)->AddEdgeToParentNode(edge); - (*parentNode)->AddEdgeToChildNode(edge); + (*parentNode)->AddEdgeToChildNode(std::move(edge)); } //// @@ -206,7 +184,7 @@ void ChatLogic::LoadAnswerGraphFromFile(std::string filename) if (rootNode == nullptr) { - rootNode = *it; // assign current node to root + rootNode = (*it).get(); // assign current node to root } else { @@ -215,9 +193,10 @@ void ChatLogic::LoadAnswerGraphFromFile(std::string filename) } } - // add chatbot to graph root node - _chatBot->SetRootNode(rootNode); - rootNode->MoveChatbotHere(_chatBot); + ChatBot lChatBot("../images/chatbot.png"); + lChatBot.SetChatLogicHandle(this); + lChatBot.SetRootNode(rootNode); + rootNode->MoveChatbotHere(std::move(lChatBot)); //// //// EOF STUDENT CODE diff --git a/src/chatlogic.h b/src/chatlogic.h index e70b0713e..2f8c0f3a4 100644 --- a/src/chatlogic.h +++ b/src/chatlogic.h @@ -4,6 +4,7 @@ #include #include #include "chatgui.h" +#include // forward declarations class ChatBot; @@ -17,8 +18,7 @@ class ChatLogic //// // data handles (owned) - std::vector _nodes; - std::vector _edges; + std::vector> _nodes; //// //// EOF STUDENT CODE diff --git a/src/graphnode.cpp b/src/graphnode.cpp index 65f56060b..ed5964f26 100644 --- a/src/graphnode.cpp +++ b/src/graphnode.cpp @@ -1,6 +1,6 @@ #include "graphedge.h" #include "graphnode.h" - +#include GraphNode::GraphNode(int id) { _id = id; @@ -11,8 +11,6 @@ GraphNode::~GraphNode() //// STUDENT CODE //// - delete _chatBot; - //// //// EOF STUDENT CODE } @@ -22,28 +20,29 @@ void GraphNode::AddToken(std::string token) _answers.push_back(token); } -void GraphNode::AddEdgeToParentNode(GraphEdge *edge) +void GraphNode::AddEdgeToParentNode(const std::shared_ptr &edge) { _parentEdges.push_back(edge); } -void GraphNode::AddEdgeToChildNode(GraphEdge *edge) +void GraphNode::AddEdgeToChildNode(std::shared_ptr edge) { - _childEdges.push_back(edge); + _childEdges.push_back(std::move(edge)); } //// STUDENT CODE //// -void GraphNode::MoveChatbotHere(ChatBot *chatbot) +void GraphNode::MoveChatbotHere(ChatBot chatbot) { - _chatBot = chatbot; - _chatBot->SetCurrentNode(this); + _chatBot = std::move(chatbot); + _chatBot.GetChatLogicHandle()->SetChatbotHandle(&_chatBot); + _chatBot.SetCurrentNode(this); + } void GraphNode::MoveChatbotToNewNode(GraphNode *newNode) { - newNode->MoveChatbotHere(_chatBot); - _chatBot = nullptr; // invalidate pointer at source + newNode->MoveChatbotHere(std::move(_chatBot)); } //// //// EOF STUDENT CODE @@ -53,7 +52,7 @@ GraphEdge *GraphNode::GetChildEdgeAtIndex(int index) //// STUDENT CODE //// - return _childEdges[index]; + return _childEdges[index].get(); //// //// EOF STUDENT CODE diff --git a/src/graphnode.h b/src/graphnode.h index ba3910d20..450884a65 100644 --- a/src/graphnode.h +++ b/src/graphnode.h @@ -4,7 +4,8 @@ #include #include #include "chatbot.h" - +#include "chatlogic.h" +#include // forward declarations class GraphEdge; @@ -16,11 +17,11 @@ class GraphNode //// // data handles (owned) - std::vector _childEdges; // edges to subsequent nodes + std::vector> _childEdges; // edges to subsequent nodes // data handles (not owned) - std::vector _parentEdges; // edges to preceding nodes - ChatBot *_chatBot; + std::vector> _parentEdges; // edges to preceding nodes + ChatBot _chatBot; //// //// EOF STUDENT CODE @@ -43,13 +44,13 @@ class GraphNode // proprietary functions void AddToken(std::string token); // add answers to list - void AddEdgeToParentNode(GraphEdge *edge); - void AddEdgeToChildNode(GraphEdge *edge); + void AddEdgeToParentNode(const std::shared_ptr& edge); + void AddEdgeToChildNode(std::shared_ptr edge); //// STUDENT CODE //// - void MoveChatbotHere(ChatBot *chatbot); + void MoveChatbotHere(ChatBot chatbot); //// //// EOF STUDENT CODE