A toolkit for working with phylogenetic data.
v0.20.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
node.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_TREE_TREE_NODE_H_
2 #define GENESIS_TREE_TREE_NODE_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2018 Lucas Czech and HITS gGmbH
7 
8  This program is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 
21  Contact:
22  Lucas Czech <lucas.czech@h-its.org>
23  Exelixis Lab, Heidelberg Institute for Theoretical Studies
24  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
25 */
26 
35 
37 
38 #include <memory>
39 #include <string>
40 
41 namespace genesis {
42 namespace tree {
43 
44 // =================================================================================================
45 // Forward declarations
46 // =================================================================================================
47 
48 class Tree;
49 class TreeEdge;
50 class TreeLink;
51 
52 // =================================================================================================
53 // Tree Node
54 // =================================================================================================
55 
56 class TreeNode
57 {
58 public:
59 
60  // ---------------------------------------------------------------------
61  // Typedefs and Enums
62  // ---------------------------------------------------------------------
63 
64  friend class Tree;
65 
66  // ---------------------------------------------------------------------
67  // Constructor and Rule of Five
68  // ---------------------------------------------------------------------
69 
71  : index_( 0 )
72  , link_( nullptr )
73  , data_( nullptr )
74  {}
75 
77  : index_( index )
78  , link_( primary_link )
79  , data_( nullptr )
80  {}
81 
82  ~TreeNode() = default;
83 
84  // avoid copy constructor and assignment operator.
85  // creating copies is maintained by Tree only.
86 
87  TreeNode( TreeNode const& ) = delete;
88  TreeNode( TreeNode&& ) = delete;
89 
90  TreeNode& operator= ( TreeNode const& ) = delete;
91  TreeNode& operator= ( TreeNode&& ) = delete;
92 
93  // ---------------------------------------------------------------------
94  // Accessors
95  // ---------------------------------------------------------------------
96 
100  size_t index() const
101  {
102  return index_;
103  }
104 
109  {
110  return *link_;
111  }
112 
116  TreeLink const& primary_link() const
117  {
118  return *link_;
119  }
120 
128  {
129  return *link_;
130  }
131 
138  TreeLink const& link() const
139  {
140  return *link_;
141  }
142 
146  bool has_data() const
147  {
148  return data_.get() != nullptr;
149  }
150 
151  template< class NodeDataType >
152  NodeDataType& data()
153  {
154  return dynamic_cast< NodeDataType& >( *data_ );
155  }
156 
157  template< class NodeDataType >
158  NodeDataType const& data() const
159  {
160  return dynamic_cast< NodeDataType const& >( *data_ );
161  }
162 
163  template< class NodeDataType >
164  NodeDataType* data_cast()
165  {
166  return dynamic_cast< NodeDataType* >( data_.get() );
167  }
168 
169  template< class NodeDataType >
170  NodeDataType const* data_cast() const
171  {
172  return dynamic_cast< NodeDataType const* >( data_.get() );
173  }
174 
182  {
183  return data_.get();
184  }
185 
192  BaseNodeData const* data_ptr() const
193  {
194  return data_.get();
195  }
196 
197  // ---------------------------------------------------------------------
198  // Modifiers
199  // ---------------------------------------------------------------------
200 
211  TreeNode& reset_index( size_t val )
212  {
213  index_ = val;
214  return *this;
215  }
216 
226  {
227  link_ = val;
228  return *this;
229  }
230 
239  TreeNode& reset_data( std::unique_ptr< BaseNodeData > data )
240  {
241  data_ = std::move( data );
242  return *this;
243  }
244 
245  // ---------------------------------------------------------------------
246  // Member Variables
247  // ---------------------------------------------------------------------
248 
249 private:
250 
251  size_t index_;
252  TreeLink* link_;
253 
254  std::unique_ptr< BaseNodeData > data_;
255 
256 };
257 
258 } // namespace tree
259 } // namespace genesis
260 
261 #endif // include guard
TreeNode & reset_primary_link(TreeLink *val)
Reset the internal pointer to the TreeLink of this TreeNode.
Definition: node.hpp:225
TreeNode(size_t index, TreeLink *primary_link)
Definition: node.hpp:76
TreeLink const & link() const
Return the TreeLink that points towards the root.
Definition: node.hpp:138
TreeNode & operator=(TreeNode const &)=delete
Base class for storing data on Nodes of a Tree.
Definition: node_data.hpp:66
TreeLink & primary_link()
Return the TreeLink that points towards the root.
Definition: node.hpp:108
size_t index() const
Return the index of this Node.
Definition: node.hpp:100
Provides some valuable additions to STD.
TreeLink const & primary_link() const
Return the TreeLink that points towards the root.
Definition: node.hpp:116
NodeDataType const * data_cast() const
Definition: node.hpp:170
Class for representing phylogenetic trees.
Definition: tree/tree.hpp:95
TreeLink & link()
Return the TreeLink that points towards the root.
Definition: node.hpp:127
NodeDataType const & data() const
Definition: node.hpp:158
BaseNodeData const * data_ptr() const
Return a const pointer to the data.
Definition: node.hpp:192
NodeDataType & data()
Definition: node.hpp:152
bool has_data() const
Return true if the TreeNode has a data object assigned to it.
Definition: node.hpp:146
TreeNode & reset_data(std::unique_ptr< BaseNodeData > data)
Reset the data pointer of this TreeNode.
Definition: node.hpp:239
TreeNode & reset_index(size_t val)
Reset the internal index of this TreeNode.
Definition: node.hpp:211
BaseNodeData * data_ptr()
Return a pointer to the data.
Definition: node.hpp:181
NodeDataType * data_cast()
Definition: node.hpp:164