A library for working with phylogenetic and population genetic data.
v0.27.0
tree/tree/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-2020 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 
36 
38 
39 #include <memory>
40 #include <string>
41 #include <typeinfo>
42 
43 namespace genesis {
44 namespace tree {
45 
46 // =================================================================================================
47 // Forward declarations
48 // =================================================================================================
49 
50 class Tree;
51 class TreeEdge;
52 class TreeLink;
53 
54 // =================================================================================================
55 // Tree Node
56 // =================================================================================================
57 
58 class TreeNode
59 {
60 public:
61 
62  // ---------------------------------------------------------------------
63  // Typedefs and Enums
64  // ---------------------------------------------------------------------
65 
66  friend class Tree;
67 
68  // ---------------------------------------------------------------------
69  // Constructor and Rule of Five
70  // ---------------------------------------------------------------------
71 
73  : index_( 0 )
74  , link_( nullptr )
75  , data_( nullptr )
76  {}
77 
79  : index_( index )
80  , link_( primary_link )
81  , data_( nullptr )
82  {}
83 
84  ~TreeNode() = default;
85 
86  // avoid copy constructor and assignment operator.
87  // creating copies is maintained by Tree only.
88 
89  TreeNode( TreeNode const& ) = delete;
90  TreeNode( TreeNode&& ) = delete;
91 
92  TreeNode& operator= ( TreeNode const& ) = delete;
93  TreeNode& operator= ( TreeNode&& ) = delete;
94 
95  // ---------------------------------------------------------------------
96  // Accessors
97  // ---------------------------------------------------------------------
98 
102  size_t index() const
103  {
104  return index_;
105  }
106 
111  {
112  return *link_;
113  }
114 
118  TreeLink const& primary_link() const
119  {
120  return *link_;
121  }
122 
130  {
131  return *link_;
132  }
133 
140  TreeLink const& link() const
141  {
142  return *link_;
143  }
144 
149  {
150  return link_->edge();
151  }
152 
156  TreeEdge const& primary_edge() const
157  {
158  return link_->edge();
159  }
160 
161  // ---------------------------------------------------------------------
162  // Data Accessors
163  // ---------------------------------------------------------------------
164 
168  bool has_data() const
169  {
170  return data_.get() != nullptr;
171  }
172 
177  template< class NodeDataType >
178  bool data_is( bool allow_null = false ) const
179  {
180  if( data_.get() == nullptr ) {
181  return allow_null;
182  }
183  assert( data_.get() );
184  auto const& ref = *data_.get();
185  return ( typeid( ref ) == typeid( NodeDataType ));
186  }
187 
192  template< class NodeDataType >
193  bool data_is_derived_from( bool allow_null = false ) const
194  {
195  if( data_.get() == nullptr ) {
196  return allow_null;
197  }
198  assert( data_.get() );
199  return ( dynamic_cast< NodeDataType const* >( data_.get() ) != nullptr );
200  }
201 
202  template< class NodeDataType >
203  NodeDataType& data()
204  {
205  return dynamic_cast< NodeDataType& >( *data_ );
206  }
207 
208  template< class NodeDataType >
209  NodeDataType const& data() const
210  {
211  return dynamic_cast< NodeDataType const& >( *data_ );
212  }
213 
214  template< class NodeDataType >
215  NodeDataType* data_cast()
216  {
217  return dynamic_cast< NodeDataType* >( data_.get() );
218  }
219 
220  template< class NodeDataType >
221  NodeDataType const* data_cast() const
222  {
223  return dynamic_cast< NodeDataType const* >( data_.get() );
224  }
225 
233  {
234  return data_.get();
235  }
236 
243  BaseNodeData const* data_ptr() const
244  {
245  return data_.get();
246  }
247 
248  // ---------------------------------------------------------------------
249  // Modifiers
250  // ---------------------------------------------------------------------
251 
262  TreeNode& reset_index( size_t val )
263  {
264  index_ = val;
265  return *this;
266  }
267 
277  {
278  link_ = val;
279  return *this;
280  }
281 
290  TreeNode& reset_data( std::unique_ptr< BaseNodeData > data )
291  {
292  data_ = std::move( data );
293  return *this;
294  }
295 
296  // ---------------------------------------------------------------------
297  // Member Variables
298  // ---------------------------------------------------------------------
299 
300 private:
301 
302  size_t index_;
303  TreeLink* link_;
304 
305  std::unique_ptr< BaseNodeData > data_;
306 
307 };
308 
309 } // namespace tree
310 } // namespace genesis
311 
312 #endif // include guard
genesis::tree::TreeNode::reset_primary_link
TreeNode & reset_primary_link(TreeLink *val)
Reset the internal pointer to the TreeLink of this TreeNode.
Definition: tree/tree/node.hpp:276
genesis::tree::TreeNode::TreeNode
TreeNode(size_t index, TreeLink *primary_link)
Definition: tree/tree/node.hpp:78
genesis::tree::TreeNode::TreeNode
TreeNode()
Definition: tree/tree/node.hpp:72
genesis::tree::TreeNode::primary_link
TreeLink const & primary_link() const
Return the TreeLink that points towards the root.
Definition: tree/tree/node.hpp:118
node_data.hpp
std.hpp
Provides some valuable additions to STD.
genesis::tree::TreeNode::operator=
TreeNode & operator=(TreeNode const &)=delete
genesis::tree::TreeNode::primary_link
TreeLink & primary_link()
Return the TreeLink that points towards the root.
Definition: tree/tree/node.hpp:110
genesis::tree::TreeNode::data_ptr
BaseNodeData const * data_ptr() const
Return a const pointer to the data.
Definition: tree/tree/node.hpp:243
genesis::tree::Tree
Class for representing phylogenetic trees.
Definition: tree/tree.hpp:97
genesis::tree::TreeNode::has_data
bool has_data() const
Return true if the TreeNode has a data object assigned to it.
Definition: tree/tree/node.hpp:168
genesis::tree::TreeNode::data
NodeDataType const & data() const
Definition: tree/tree/node.hpp:209
genesis::tree::TreeNode::data_cast
NodeDataType const * data_cast() const
Definition: tree/tree/node.hpp:221
genesis::tree::TreeNode::data_is
bool data_is(bool allow_null=false) const
Return true iff the TreeNode has a data object assigned to it that is of a particular given data type...
Definition: tree/tree/node.hpp:178
genesis::tree::TreeEdge
Definition: edge.hpp:60
genesis::tree::TreeNode
Definition: tree/tree/node.hpp:58
genesis
Container namespace for all symbols of genesis in order to keep them separate when used as a library.
Definition: placement/formats/edge_color.cpp:42
genesis::tree::TreeNode::link
TreeLink & link()
Return the TreeLink that points towards the root.
Definition: tree/tree/node.hpp:129
genesis::tree::BaseNodeData
Base class for storing data on Nodes of a Tree.
Definition: node_data.hpp:66
genesis::tree::TreeNode::data_is_derived_from
bool data_is_derived_from(bool allow_null=false) const
Return true iff the TreeNode has a data object assigned to it that is derived from a particular given...
Definition: tree/tree/node.hpp:193
genesis::tree::TreeNode::~TreeNode
~TreeNode()=default
genesis::tree::TreeNode::reset_data
TreeNode & reset_data(std::unique_ptr< BaseNodeData > data)
Reset the data pointer of this TreeNode.
Definition: tree/tree/node.hpp:290
genesis::tree::TreeNode::primary_edge
TreeEdge & primary_edge()
Return the TreeEdge that points towards the root.
Definition: tree/tree/node.hpp:148
genesis::tree::TreeNode::index
size_t index() const
Return the index of this Node.
Definition: tree/tree/node.hpp:102
genesis::tree::TreeNode::data
NodeDataType & data()
Definition: tree/tree/node.hpp:203
genesis::tree::TreeNode::primary_edge
TreeEdge const & primary_edge() const
Return the TreeEdge that points towards the root.
Definition: tree/tree/node.hpp:156
genesis::tree::TreeNode::data_ptr
BaseNodeData * data_ptr()
Return a pointer to the data.
Definition: tree/tree/node.hpp:232
genesis::tree::TreeNode::data_cast
NodeDataType * data_cast()
Definition: tree/tree/node.hpp:215
genesis::tree::TreeNode::reset_index
TreeNode & reset_index(size_t val)
Reset the internal index of this TreeNode.
Definition: tree/tree/node.hpp:262
genesis::tree::TreeNode::link
TreeLink const & link() const
Return the TreeLink that points towards the root.
Definition: tree/tree/node.hpp:140