opensurgsim
TreeNode.h
1 // This file is a part of the OpenSurgSim project.
2 // Copyright 2013, SimQuest Solutions Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef SURGSIM_DATASTRUCTURES_TREENODE_H
17 #define SURGSIM_DATASTRUCTURES_TREENODE_H
18 
19 #include <vector>
20 #include <memory>
21 
22 #include "SurgSim/DataStructures/TreeVisitor.h"
23 
24 namespace SurgSim
25 {
26 
27 namespace DataStructures
28 {
29 
30 class TreeData;
31 
32 
36 class TreeNode
37 {
38 public:
40  TreeNode();
41 
43  virtual ~TreeNode();
44 
47  void setData(std::shared_ptr<TreeData> data);
48 
50  const std::shared_ptr<TreeData>& getData() const;
51 
53  size_t getNumChildren() const;
54 
58  std::shared_ptr<TreeNode>& getChild(size_t index);
59 
62  void accept(TreeVisitor* visitor);
63 
69  bool operator==(const TreeNode& node) const;
70 
76  bool operator!=(const TreeNode& node) const;
77 
78 protected:
79 
85  virtual bool isEqual(const TreeNode& node) const;
86 
91  virtual bool doAccept(TreeVisitor* visitor) = 0;
92 
96  void setNumChildren(size_t numChildren);
97 
100  void addChild(const std::shared_ptr<TreeNode>& node);
101 
104  void addChild(const std::shared_ptr<TreeNode>&& node);
105 
109  void setChild(size_t index, const std::shared_ptr<TreeNode>& node);
110 
111 private:
112 
114  std::vector<std::shared_ptr<TreeNode>> m_children;
115 
117  std::shared_ptr<TreeData> m_data;
118 };
119 
120 }; // namespace DataStructures
121 
122 }; // namespace SurgSim
123 
124 #endif // SURGSIM_DATASTRUCTURES_TREENODE_H
Wraps glewInit() to separate the glew opengl definitions from the osg opengl definitions only imgui n...
Definition: AddRandomSphereBehavior.cpp:36
virtual bool isEqual(const TreeNode &node) const
Returns true if the nodes are equal; otherwise, returns false.
Definition: TreeNode.cpp:44
Abstract Class for visitors, this needs to be extended for other tree nodes when necessary return fal...
Definition: TreeVisitor.h:31
const std::shared_ptr< TreeData > & getData() const
Definition: TreeNode.cpp:62
Basic tree node structure.
Definition: TreeNode.h:36
virtual bool doAccept(TreeVisitor *visitor)=0
Private function for use with the visitor pattern, this needs to be implemented to make the correct d...
void setNumChildren(size_t numChildren)
Sets the number of children of this node.
Definition: TreeNode.cpp:67
void accept(TreeVisitor *visitor)
Public entry point for visitor, currently this performs pre-order traversal of the tree...
Definition: TreeNode.cpp:99
TreeNode()
Constructor. After construction, the node has no children, and the data is null.
Definition: TreeNode.cpp:27
size_t getNumChildren() const
Definition: TreeNode.cpp:72
bool operator==(const TreeNode &node) const
Returns true if the nodes are equal; otherwise, returns false.
Definition: TreeNode.cpp:34
void setData(std::shared_ptr< TreeData > data)
Sets the data of this node.
Definition: TreeNode.cpp:57
void setChild(size_t index, const std::shared_ptr< TreeNode > &node)
Set a specific child of this node.
Definition: TreeNode.cpp:87
void addChild(const std::shared_ptr< TreeNode > &node)
Add a child to this node.
Definition: TreeNode.cpp:77
bool operator!=(const TreeNode &node) const
Returns true if the nodes are not equal; otherwise, returns false.
Definition: TreeNode.cpp:39
virtual ~TreeNode()
Destructor.
Definition: TreeNode.cpp:30
std::shared_ptr< TreeNode > & getChild(size_t index)
Returns the specified child of this node.
Definition: TreeNode.cpp:93