diff --git a/.gitignore b/.gitignore index c129b08a5..44f5f7c83 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ .github/** +.vscode/** +build/** + diff --git a/src/chatbot.cpp b/src/chatbot.cpp index 41d1f0c1f..be7116375 100644 --- a/src/chatbot.cpp +++ b/src/chatbot.cpp @@ -44,8 +44,62 @@ ChatBot::~ChatBot() //// STUDENT CODE //// +ChatBot::ChatBot(const ChatBot &source) +{ + std::cout << "ChatBot Copy Constructor" << std::endl; + if(source._image != nullptr) _image = new wxBitmap(*source._image); + else _image = nullptr; + + _chatLogic = source._chatLogic; + _rootNode = source._rootNode; + _currentNode = source._currentNode; +} -//// +ChatBot &ChatBot::operator=(const ChatBot &source) +{ + std::cout << "ChatBot Copy Assignment Operator" << std::endl; + + if(this == &source) return *this; + if(_image != nullptr) {delete _image; _image = nullptr;} + if(source._image != nullptr) _image = new wxBitmap(*source._image); + else _image = nullptr; + + _chatLogic = source._chatLogic; + _rootNode = source._rootNode; + _currentNode = source._currentNode; + return *this; +} + +ChatBot::ChatBot(ChatBot &&source) +{ + std::cout << "ChatBot Move Constructor" << std::endl; + _image = source._image; + _chatLogic = source._chatLogic; + _rootNode = source._rootNode; + _currentNode = source._currentNode; + _chatLogic->SetChatbotHandle(this); + source._image = nullptr; + source._chatLogic = nullptr; + source._rootNode = nullptr; + source._currentNode = nullptr; +} + +ChatBot &ChatBot::operator=(ChatBot &&source) +{ + std::cout << "ChatBot Move Assignment Operator" << std::endl; + if(this == &source) return *this; + if(_image != nullptr) {delete _image; _image = nullptr;} + _image = source._image; + _chatLogic = source._chatLogic; + _rootNode = source._rootNode; + _currentNode = source._currentNode; + _chatLogic->SetChatbotHandle(this); + source._image = nullptr; + source._chatLogic = nullptr; + source._rootNode = nullptr; + source._currentNode = nullptr; + return *this; +} //// EOF STUDENT CODE void ChatBot::ReceiveMessageFromUser(std::string message) diff --git a/src/chatbot.h b/src/chatbot.h index 0367a93f8..9cf6ec1a5 100644 --- a/src/chatbot.h +++ b/src/chatbot.h @@ -29,7 +29,10 @@ class ChatBot //// STUDENT CODE //// - + ChatBot(const ChatBot &source); + ChatBot &operator=(const ChatBot &source); + ChatBot(ChatBot &&source); + ChatBot &operator=(ChatBot &&source); //// //// EOF STUDENT CODE diff --git a/src/chatgui.cpp b/src/chatgui.cpp index 6637e562b..ff04a5dc5 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,8 +135,6 @@ ChatBotPanelDialog::~ChatBotPanelDialog() //// STUDENT CODE //// - delete _chatLogic; - //// //// EOF STUDENT CODE } diff --git a/src/chatgui.h b/src/chatgui.h index 503c59790..e71bb2879 100644 --- a/src/chatgui.h +++ b/src/chatgui.h @@ -1,6 +1,6 @@ #ifndef CHATGUI_H_ #define CHATGUI_H_ - +#include #include class ChatLogic; // forward declaration @@ -16,7 +16,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..087918be0 100644 --- a/src/chatlogic.cpp +++ b/src/chatlogic.cpp @@ -18,10 +18,10 @@ ChatLogic::ChatLogic() //// // create instance of chatbot - _chatBot = new ChatBot("../images/chatbot.png"); + //_chatBot = new ChatBot("../images/chatbot.png"); // add pointer to chatlogic so that chatbot answers can be passed on to the GUI - _chatBot->SetChatLogicHandle(this); + //_chatBot->SetChatLogicHandle(this); //// //// EOF STUDENT CODE @@ -33,19 +33,10 @@ ChatLogic::~ChatLogic() //// // delete chatbot instance - delete _chatBot; + //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; - } + // No need to manually delete edges now. + // The child nodes' unique_ptr will handle that. //// //// EOF STUDENT CODE @@ -127,12 +118,19 @@ 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](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 @@ -156,21 +154,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](std::unique_ptr &node) { return node->GetID() == std::stoi(parentToken->second); }); + auto childNode = std::find_if(_nodes.begin(), _nodes.end(), [&childToken](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_unique(id); + // _edges.push_back(edge); + 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); + (*childNode)->AddEdgeToParentNode(edge.get()); + (*parentNode)->AddEdgeToChildNode(std::move(edge)); } //// @@ -206,7 +203,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 { @@ -214,10 +211,12 @@ void ChatLogic::LoadAnswerGraphFromFile(std::string filename) } } } - + ChatBot chatbot("../images/chatbot.png"); + SetChatbotHandle(&chatbot); // add chatbot to graph root node - _chatBot->SetRootNode(rootNode); - rootNode->MoveChatbotHere(_chatBot); + chatbot.SetChatLogicHandle(this); + chatbot.SetRootNode(rootNode); + rootNode->MoveChatbotHere(std::move(chatbot)); //// //// EOF STUDENT CODE diff --git a/src/chatlogic.h b/src/chatlogic.h index e70b0713e..787f59cf2 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,8 @@ class ChatLogic //// // data handles (owned) - std::vector _nodes; - std::vector _edges; + std::vector> _nodes; + // std::vector _edges; //// //// EOF STUDENT CODE diff --git a/src/graphnode.cpp b/src/graphnode.cpp index 65f56060b..ee215a131 100644 --- a/src/graphnode.cpp +++ b/src/graphnode.cpp @@ -11,7 +11,8 @@ GraphNode::~GraphNode() //// STUDENT CODE //// - delete _chatBot; + // We do NOT delete _parentEdges here, since they are non‐owning! + // The unique_ptr in _childEdges will automatically free the GraphEdges. //// //// EOF STUDENT CODE @@ -27,23 +28,23 @@ void GraphNode::AddEdgeToParentNode(GraphEdge *edge) _parentEdges.push_back(edge); } -void GraphNode::AddEdgeToChildNode(GraphEdge *edge) +void GraphNode::AddEdgeToChildNode(std::unique_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.SetCurrentNode(this); } void GraphNode::MoveChatbotToNewNode(GraphNode *newNode) { - newNode->MoveChatbotHere(_chatBot); - _chatBot = nullptr; // invalidate pointer at source + newNode->MoveChatbotHere(std::move(_chatBot)); + //_chatBot = nullptr; // invalidate pointer at source } //// //// EOF STUDENT CODE @@ -53,7 +54,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..612f8c8e2 100644 --- a/src/graphnode.h +++ b/src/graphnode.h @@ -4,7 +4,7 @@ #include #include #include "chatbot.h" - +#include // forward declarations class GraphEdge; @@ -16,11 +16,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; + ChatBot _chatBot; //// //// EOF STUDENT CODE @@ -37,19 +37,19 @@ class GraphNode // getter / setter int GetID() { return _id; } int GetNumberOfChildEdges() { return _childEdges.size(); } + int GetNumberOfParents() { return _parentEdges.size(); } GraphEdge *GetChildEdgeAtIndex(int index); std::vector GetAnswers() { return _answers; } - int GetNumberOfParents() { return _parentEdges.size(); } // proprietary functions void AddToken(std::string token); // add answers to list void AddEdgeToParentNode(GraphEdge *edge); - void AddEdgeToChildNode(GraphEdge *edge); + void AddEdgeToChildNode(std::unique_ptr edge); //// STUDENT CODE //// - void MoveChatbotHere(ChatBot *chatbot); + void MoveChatbotHere(ChatBot chatbot); //// //// EOF STUDENT CODE