From 9b6d925e2db66f4a3b8177fa46f60f725c61ce4a Mon Sep 17 00:00:00 2001 From: Antoine Beauchamp Date: Thu, 30 Dec 2021 09:01:54 -0500 Subject: [PATCH] Deleted Node class. For issue #102. --- include/shellanything/Node.h | 184 ------------------- src/CMakeLists.txt | 2 - src/Node.cpp | 202 --------------------- test/CMakeLists.txt | 2 - test/TestNode.cpp | 333 ----------------------------------- test/TestNode.h | 42 ----- 6 files changed, 765 deletions(-) delete mode 100644 include/shellanything/Node.h delete mode 100644 src/Node.cpp delete mode 100644 test/TestNode.cpp delete mode 100644 test/TestNode.h diff --git a/include/shellanything/Node.h b/include/shellanything/Node.h deleted file mode 100644 index 202d050..0000000 --- a/include/shellanything/Node.h +++ /dev/null @@ -1,184 +0,0 @@ -/********************************************************************************** - * MIT License - * - * Copyright (c) 2018 Antoine Beauchamp - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - *********************************************************************************/ - -#ifndef SA_NODE_H -#define SA_NODE_H - -#include -#include - -namespace shellanything -{ - /// - /// A Node class for defining an object hierarchy - /// - class Node - { - public: - /// - /// A list of Node pointer - /// - typedef std::vector NodePtrList; - - Node(); - - /// - /// Constructor for create a new Node instance with a 'type' node-type. - /// - Node(const std::string & type); - virtual ~Node(); - - private: - // Disable copy constructor and copy operator - Node(const Node&); - Node& operator=(const Node&); - public: - - /// - /// Getter for the 'node-type' parameter. - /// - const std::string & GetNodeType() const; - - /// - /// Get the parent of this node. - /// - /// Returns a valid Node pointer if the node have a parent. Returns NULL otherwise. - Node * GetParent() const; - - /// - /// Add a new subnode to this node. The node takes ownership of the given child node. - /// The given node instance must not already be the child of another node. - /// - /// A node instance to add as a subnode. - /// Returns a valid Node pointer if the node was inserted as a child node. Returns NULL otherwise. - Node * AddChild(Node * child); - - /// - /// Getter list of all subnode of this node. - /// - NodePtrList GetChildren() const; - - /// - /// Returns the number of child this node have. - /// - size_t GetNumChildren() const; - - /// - /// Searches the subnodes for a Node with a node-type 'type'. - /// - /// The given type of node to search for. - /// Returns a list of Node pointer that matches the given node type. - NodePtrList FindChildren(const std::string & type) const; - - /// - /// Searches for the first node with a node-type 'type'. - /// - /// The given type of node to search for. - /// Returns the first Node pointer that matches the given node type. - Node * FindFirst(const std::string & type) const; - - /// - /// Returns the nth subnode of this node. - /// - /// The given index of the child node. - /// Returns the node pointer that matches the nth subnode of this node. Returns NULL if index is invalid or out of bounds. - Node * GetChild(size_t index) const; - - /// - /// Deletes the nth subnode of this node. - /// - /// The given index of the child node. - /// Returns true if the subnode was deleted. Returns false otherwise. - bool RemoveChild(size_t index); - - /// - /// Deletes the subnode matching the given node. - /// - /// The given node to delete. - /// Returns true if the subnode was deleted. Returns false otherwise. - bool RemoveChild(Node * child); - - /// - /// Deletes all subnode of the node. - /// - /// Returns true if all the subnode were deleted. Returns false otherwise. - bool RemoveChildren(); - - /// - /// Deletes all subnode of the node that are of type 'type'. - /// - /// Returns true if subnodes were deleted. Returns false otherwise. - bool RemoveChildren(const std::string & type); - - /// - /// Returns the depth of this node based on the root node. The root node have a depth of 0. - /// - /// Returns the depth of this node based on the root node. - size_t Depth() const; - - /// - /// Returns true if this node have no subnodes. - /// - /// Returns true if this node have no subnodes. Returns false otherwise. - bool IsLeaf() const; - - /// - /// Returns true if this node have no parent node. - /// - /// Returns true if this node have no parent node. Returns false otherwise. - bool IsRoot() const; - - /// - /// Returns the number of nodes (including itself) in this node's hierarchy. - /// A leaf node have a size() of 1. - /// - /// Returns the number of nodes (including itself) in this node's hierarchy. - size_t Size() const; - -protected: - std::string mNodeType; - Node * mParent; - NodePtrList mChildren; - }; - - /// - /// Utility fonctions for converting Node::NodePtrList to a vector of type T - /// - template - std::vector FilterNodes(Node::NodePtrList & nodes) - { - std::vector output; - for(size_t i=0; i(node); - if (out) - output.push_back(out); - } - return output; - } - -} //namespace shellanything - -#endif //SA_NODE_H diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d792363..7bc2d6d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -43,7 +43,6 @@ set(SHELLANYTHING_HEADER_FILES "" ${CMAKE_SOURCE_DIR}/include/shellanything/ListsBody.inc ${CMAKE_SOURCE_DIR}/include/shellanything/ListsDeclaration.inc ${CMAKE_SOURCE_DIR}/include/shellanything/Menu.h - ${CMAKE_SOURCE_DIR}/include/shellanything/Node.h ${CMAKE_SOURCE_DIR}/include/shellanything/Validator.h ) @@ -72,7 +71,6 @@ add_library(shellanything STATIC InputBox.h InputBox.cpp Menu.cpp - Node.cpp ObjectFactory.h ObjectFactory.cpp Unicode.h diff --git a/src/Node.cpp b/src/Node.cpp deleted file mode 100644 index 3c1e477..0000000 --- a/src/Node.cpp +++ /dev/null @@ -1,202 +0,0 @@ -/********************************************************************************** - * MIT License - * - * Copyright (c) 2018 Antoine Beauchamp - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - *********************************************************************************/ - -#include "shellanything/Node.h" -#include -#include - -namespace shellanything -{ - - Node::Node() : - mParent(NULL) - { - } - - Node::Node(const std::string & type) : - mParent(NULL) - { - mNodeType = type; - } - - Node::~Node() - { - //delete children - for(size_t i=0; imParent != NULL) - { - throw std::runtime_error("Node already in another tree."); - } - - child->mParent = this; - mChildren.push_back(child); - - return child; - } - - bool Node::RemoveChild(Node * child) - { - Node::NodePtrList::iterator it = std::find(mChildren.begin(), mChildren.end(), child); - if (it != mChildren.end()) - { - //found! - Node * node = (*it); - mChildren.erase(it); - delete node; - return true; - } - return false; - } - - Node::NodePtrList Node::GetChildren() const - { - return mChildren; - } - - Node::NodePtrList Node::FindChildren(const std::string & type) const - { - Node::NodePtrList nodes; - for(size_t i=0; imNodeType == type) - nodes.push_back(n); - } - return nodes; - } - - Node * Node::FindFirst(const std::string & type) const - { - for(size_t i=0; imNodeType == type) - return n; - } - return NULL; - } - - size_t Node::GetNumChildren() const - { - return mChildren.size(); - } - - Node * Node::GetChild(size_t index) const - { - if (index < mChildren.size()) - { - Node * node = mChildren[index]; - return node; - } - return NULL; - } - - bool Node::RemoveChild(size_t index) - { - if (index < mChildren.size()) - { - Node * node = mChildren[index]; - mChildren.erase(mChildren.begin() + index); - delete node; - return true; - } - return false; - } - - bool Node::RemoveChildren() - { - bool success = true; - while(mChildren.size() > 0) - { - success = success && RemoveChild((size_t)0); - } - return success; - } - - bool Node::RemoveChildren(const std::string & type) - { - bool success = true; - Node * node = FindFirst(type); - while(node != NULL) - { - success = success && RemoveChild(node); - node = FindFirst(type); - } - return success; - } - - size_t Node::Depth() const - { - size_t depth = 0; - Node * ancestor = mParent; - for (depth = 0; ancestor != NULL; depth++) - { - ancestor = ancestor->mParent; - } - return depth; - } - - bool Node::IsLeaf() const - { - return (mChildren.size() == 0); - } - - bool Node::IsRoot() const - { - return (mParent == NULL); - } - - size_t Node::Size() const - { - size_t total = 1; - for(size_t i=0; iSize(); - total += node_size; - } - return total; - } - -} //namespace shellanything diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index e296dd4..3905a58 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -82,8 +82,6 @@ add_executable(shellanything_unittest TestLibEval.h TestMenu.cpp TestMenu.h - TestNode.cpp - TestNode.h TestObjectFactory.cpp TestObjectFactory.h TestWin32Registry.cpp diff --git a/test/TestNode.cpp b/test/TestNode.cpp deleted file mode 100644 index 6165598..0000000 --- a/test/TestNode.cpp +++ /dev/null @@ -1,333 +0,0 @@ -/********************************************************************************** - * MIT License - * - * Copyright (c) 2018 Antoine Beauchamp - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - *********************************************************************************/ - -#include "TestNode.h" -#include "shellanything/Node.h" - -namespace shellanything { namespace test -{ - - //-------------------------------------------------------------------------------------------------- - void TestNode::SetUp() - { - } - //-------------------------------------------------------------------------------------------------- - void TestNode::TearDown() - { - } - //-------------------------------------------------------------------------------------------------- - TEST_F(TestNode, testGetParent) - { - Node root("root"); - Node * child1 = (new Node("child1")); - - ASSERT_TRUE( root.GetParent() == NULL ); - - root.AddChild(child1); - - ASSERT_TRUE( root.GetParent() == NULL ); - ASSERT_TRUE( child1->GetParent() != NULL ); - } - //-------------------------------------------------------------------------------------------------- - TEST_F(TestNode, testGetChildren) - { - Node root("root"); - Node * child1 = (new Node("child1")); - Node * child2 = (new Node("child2")); - - //no children yet - ASSERT_EQ(0, root.GetChildren().size()); - - root.AddChild(child1); - root.AddChild(child2); - - //assert 2 children - ASSERT_EQ(2, root.GetChildren().size()); - } - //-------------------------------------------------------------------------------------------------- - TEST_F(TestNode, testFindChildren) - { - Node root("html"); - Node * body = (new Node("body")); - Node * child1 = (new Node("h1")); - Node * child2 = (new Node("p")); - Node * child3 = (new Node("p")); - - //no children yet - ASSERT_EQ(0, root.FindChildren("").size()); - - //build tree - root.AddChild(body); - body->AddChild(child1); - body->AddChild(child2); - body->AddChild(child3); - - //search for a node that is not found - ASSERT_EQ(0, body->FindChildren("a").size()); - - //search for multiple nodes - ASSERT_EQ(2, body->FindChildren("p").size()); - } - //-------------------------------------------------------------------------------------------------- - TEST_F(TestNode, testFindFirst) - { - Node root("html"); - Node * body = (new Node("body")); - Node * child1 = (new Node("h1")); - Node * child2 = (new Node("p")); - Node * child3 = (new Node("p")); - - //no children yet - ASSERT_TRUE( root.FindFirst("") == NULL ); - - //build tree - root.AddChild(body); - body->AddChild(child1); - body->AddChild(child2); - body->AddChild(child3); - - //search for a node that is not found - ASSERT_EQ( NULL, body->FindFirst("a") ); - - //search for multiple nodes - ASSERT_EQ(child2, body->FindFirst("p") ); - } - //-------------------------------------------------------------------------------------------------- - TEST_F(TestNode, testGetNumChildren) - { - Node root("root"); - Node * child1 = (new Node("child1")); - Node * child2 = (new Node("child2")); - - //no children yet - ASSERT_EQ(0, root.GetNumChildren()); - - root.AddChild(child1); - root.AddChild(child2); - - //assert 2 children - ASSERT_EQ(2, root.GetNumChildren()); - } - //-------------------------------------------------------------------------------------------------- - TEST_F(TestNode, testGetChild) - { - Node root("html"); - Node * body = (new Node("body")); - Node * child1 = (new Node("h1")); - Node * child2 = (new Node("p")); - Node * child3 = (new Node("p")); - - //no children yet - ASSERT_TRUE( root.GetChild(0) == NULL ); - - //build tree - root.AddChild(body); - body->AddChild(child1); - body->AddChild(child2); - body->AddChild(child3); - - //search for a node that is not found - ASSERT_EQ( NULL, body->GetChild(999) ); - - //search specific nodes - ASSERT_EQ(child1, body->GetChild(0) ); - ASSERT_EQ(child2, body->GetChild(1) ); - ASSERT_EQ(child3, body->GetChild(2) ); - } - //-------------------------------------------------------------------------------------------------- - TEST_F(TestNode, testDepth) - { - Node root("html"); - Node * body = (new Node("body")); - Node * child1 = (new Node("h1")); - Node * child2 = (new Node("p")); - Node * child3 = (new Node("p")); - - //no children yet - ASSERT_EQ( 0, root.Depth() ); - - //build tree - root.AddChild(body); - body->AddChild(child1); - body->AddChild(child2); - body->AddChild(child3); - - //assert - ASSERT_EQ( 0, root.Depth() ); - ASSERT_EQ( 1, body->Depth() ); - ASSERT_EQ( 2, child1->Depth() ); - ASSERT_EQ( 2, child2->Depth() ); - ASSERT_EQ( 2, child3->Depth() ); - } - //-------------------------------------------------------------------------------------------------- - TEST_F(TestNode, testLeaf) - { - Node root("html"); - Node * body = (new Node("body")); - Node * child1 = (new Node("h1")); - Node * child2 = (new Node("p")); - Node * child3 = (new Node("p")); - - //no children yet - ASSERT_TRUE( root.IsLeaf() ); - - //build tree - root.AddChild(body); - body->AddChild(child1); - body->AddChild(child2); - body->AddChild(child3); - - //assert - ASSERT_FALSE( root.IsLeaf() ); - ASSERT_FALSE( body->IsLeaf() ); - ASSERT_TRUE ( child1->IsLeaf() ); - ASSERT_TRUE ( child2->IsLeaf() ); - ASSERT_TRUE ( child3->IsLeaf() ); - } - //-------------------------------------------------------------------------------------------------- - TEST_F(TestNode, testRoot) - { - Node root("html"); - Node * body = (new Node("body")); - Node * child1 = (new Node("h1")); - Node * child2 = (new Node("p")); - Node * child3 = (new Node("p")); - - //no children yet - ASSERT_TRUE( root.IsRoot() ); - - //build tree - root.AddChild(body); - body->AddChild(child1); - body->AddChild(child2); - body->AddChild(child3); - - //assert - ASSERT_TRUE ( root.IsRoot() ); - ASSERT_FALSE( body->IsRoot() ); - ASSERT_FALSE( child1->IsRoot() ); - ASSERT_FALSE( child2->IsRoot() ); - ASSERT_FALSE( child3->IsRoot() ); - } - //-------------------------------------------------------------------------------------------------- - TEST_F(TestNode, testSize) - { - Node root("html"); - Node * body = (new Node("body")); - Node * child1 = (new Node("h1")); - Node * child2 = (new Node("p")); - Node * child3 = (new Node("p")); - - //no children yet - ASSERT_EQ( 1, root.Size() ); - - //build tree - root.AddChild(body); - body->AddChild(child1); - body->AddChild(child2); - body->AddChild(child3); - - //assert - ASSERT_EQ( 5, root.Size() ); - ASSERT_EQ( 4, body->Size() ); - ASSERT_EQ( 1, child1->Size() ); - ASSERT_EQ( 1, child2->Size() ); - ASSERT_EQ( 1, child3->Size() ); - } - //-------------------------------------------------------------------------------------------------- - TEST_F(TestNode, testRemoveChild) - { - Node root("html"); - Node * body = (new Node("body")); - Node * child1 = (new Node("h1")); - Node * child2 = (new Node("p")); - Node * child3 = (new Node("p")); - - //invalid values - ASSERT_FALSE( root.RemoveChild( (Node*)NULL ) ); - ASSERT_FALSE( root.RemoveChild( child3 ) ); - ASSERT_FALSE( root.RemoveChild(9999) ); //out of bounds - - //build tree - root.AddChild(body); - body->AddChild(child1); - body->AddChild(child2); - body->AddChild(child3); - - //assert - ASSERT_EQ( 5, root.Size() ); - ASSERT_TRUE( body->RemoveChild(child3) ); - ASSERT_EQ( 4, root.Size() ); - ASSERT_TRUE( body->RemoveChild(1) ); //matches child2 - ASSERT_EQ( 3, root.Size() ); - } - //-------------------------------------------------------------------------------------------------- - TEST_F(TestNode, testRemoveChildren) - { - Node root("html"); - Node * body = (new Node("body")); - Node * child1 = (new Node("h1")); - Node * child2 = (new Node("p")); - Node * child3 = (new Node("p")); - - //removeChildren empty nodes - ASSERT_TRUE( root.RemoveChildren() ); - - //build tree - root.AddChild(body); - body->AddChild(child1); - body->AddChild(child2); - body->AddChild(child3); - - //assert - ASSERT_TRUE( body->RemoveChildren() ); - ASSERT_EQ( 1, body->Size() ); //body should be a leaf since we removed all children - } - //-------------------------------------------------------------------------------------------------- - TEST_F(TestNode, testRemoveChildrenType) - { - Node root("html"); - Node * body = (new Node("body")); - Node * child1 = (new Node("h1")); - Node * child2 = (new Node("p")); - Node * child3 = (new Node("p")); - - //removeChildren empty nodes - ASSERT_TRUE( root.RemoveChildren() ); - - //build tree - root.AddChild(body); - body->AddChild(child1); - body->AddChild(child2); - body->AddChild(child3); - - //assert - ASSERT_TRUE( body->RemoveChildren("p") ); - ASSERT_EQ( 2, body->Size() ); //body should only contain child1 of type "h1" - ASSERT_EQ( child1, body->GetChildren()[0] ); - } - //-------------------------------------------------------------------------------------------------- - -} //namespace test -} //namespace shellanything diff --git a/test/TestNode.h b/test/TestNode.h deleted file mode 100644 index e881647..0000000 --- a/test/TestNode.h +++ /dev/null @@ -1,42 +0,0 @@ -/********************************************************************************** - * MIT License - * - * Copyright (c) 2018 Antoine Beauchamp - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - *********************************************************************************/ - -#ifndef TEST_SA_NODE_H -#define TEST_SA_NODE_H - -#include - -namespace shellanything { namespace test -{ - class TestNode : public ::testing::Test - { - public: - virtual void SetUp(); - virtual void TearDown(); - }; - -} //namespace test -} //namespace shellanything - -#endif //TEST_SA_NODE_H