A toolkit for working with phylogenetic data.
v0.24.0
edge.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_TREE_TREE_EDGE_H_
2 #define GENESIS_TREE_TREE_EDGE_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 TreeLink;
52 class TreeNode;
53 
54 class BaseEdgeData;
55 
56 // =================================================================================================
57 // Tree Edge
58 // =================================================================================================
59 
60 class TreeEdge
61 {
62 public:
63 
64  // ---------------------------------------------------------------------
65  // Typedefs and Enums
66  // ---------------------------------------------------------------------
67 
68  friend class Tree;
69 
70  // ---------------------------------------------------------------------
71  // Constructor and Rule of Five
72  // ---------------------------------------------------------------------
73 
75  : index_( 0 )
76  , link_p_( nullptr )
77  , link_s_( nullptr )
78  , data_( nullptr )
79  {}
80 
82  : index_( index )
83  , link_p_( primary_link )
84  , link_s_( secondary_link )
85  , data_( nullptr )
86  {}
87 
88  ~TreeEdge() = default;
89 
90  // avoid copy constructor and assignment operator.
91  // creating copies is maintained by Tree only.
92 
93  TreeEdge( TreeEdge const& ) = delete;
94  TreeEdge( TreeEdge&& ) = delete;
95 
96  TreeEdge& operator= ( TreeEdge const& ) = delete;
97  TreeEdge& operator= ( TreeEdge&& ) = delete;
98 
99  // ---------------------------------------------------------------------
100  // Accessors
101  // ---------------------------------------------------------------------
102 
106  size_t index() const
107  {
108  return index_;
109  }
110 
115  {
116  return *link_p_;
117  }
118 
122  TreeLink const& primary_link() const
123  {
124  return *link_p_;
125  }
126 
131  {
132  return *link_s_;
133  }
134 
138  TreeLink const& secondary_link() const
139  {
140  return *link_s_;
141  }
142 
147  {
148  return link_p_->node();
149  }
150 
154  TreeNode const& primary_node() const
155  {
156  return link_p_->node();
157  }
158 
163  {
164  return link_s_->node();
165  }
166 
170  TreeNode const& secondary_node() const
171  {
172  return link_s_->node();
173  }
174 
175  // ---------------------------------------------------------------------
176  // Data Accessors
177  // ---------------------------------------------------------------------
178 
182  bool has_data() const
183  {
184  return data_.get() != nullptr;
185  }
186 
191  template< class EdgeDataType >
192  bool data_is( bool allow_null = false ) const
193  {
194  if( data_.get() == nullptr ) {
195  return allow_null;
196  }
197  assert( data_.get() );
198  auto const& ref = *data_.get();
199  return ( typeid( ref ) == typeid( EdgeDataType ));
200  }
201 
206  template< class EdgeDataType >
207  bool data_is_derived_from( bool allow_null = false ) const
208  {
209  if( data_.get() == nullptr ) {
210  return allow_null;
211  }
212  assert( data_.get() );
213  return ( dynamic_cast< EdgeDataType const* >( data_.get() ) != nullptr );
214  }
215 
216  template< class EdgeDataType >
217  EdgeDataType& data()
218  {
219  return dynamic_cast< EdgeDataType& >( *data_ );
220  }
221 
222  template< class EdgeDataType >
223  EdgeDataType const& data() const
224  {
225  return dynamic_cast< EdgeDataType const& >( *data_ );
226  }
227 
228  template< class EdgeDataType >
229  EdgeDataType* data_cast()
230  {
231  return dynamic_cast< EdgeDataType* >( data_.get() );
232  }
233 
234  template< class EdgeDataType >
235  EdgeDataType const* data_cast() const
236  {
237  return dynamic_cast< EdgeDataType const* >( data_.get() );
238  }
239 
247  {
248  return data_.get();
249  }
250 
257  BaseEdgeData const* data_ptr() const
258  {
259  return data_.get();
260  }
261 
262  // ---------------------------------------------------------------------
263  // Modifiers
264  // ---------------------------------------------------------------------
265 
276  TreeEdge& reset_index( size_t val )
277  {
278  index_ = val;
279  return *this;
280  }
281 
291  {
292  link_p_ = val;
293  return *this;
294  }
295 
305  {
306  link_s_ = val;
307  return *this;
308  }
309 
318  TreeEdge& reset_data( std::unique_ptr< BaseEdgeData > data )
319  {
320  data_ = std::move( data );
321  return *this;
322  }
323 
324  // ---------------------------------------------------------------------
325  // Member Variables
326  // ---------------------------------------------------------------------
327 
328 private:
329 
330  size_t index_;
331 
332  TreeLink* link_p_;
333  TreeLink* link_s_;
334 
335  std::unique_ptr< BaseEdgeData > data_;
336 };
337 
338 } // namespace tree
339 } // namespace genesis
340 
341 #endif // include guard
TreeLink & secondary_link()
Return the TreeLink of this TreeEdge that points away from the root.
Definition: edge.hpp:130
bool has_data() const
Return true if the TreeEdge has a data object assigned to it.
Definition: edge.hpp:182
TreeEdge & operator=(TreeEdge const &)=delete
TreeLink const & primary_link() const
Return the TreeLink of this TreeEdge that points towards the root.
Definition: edge.hpp:122
TreeNode const & primary_node() const
Return the TreeNode of this TreeEdge that points towards the root.
Definition: edge.hpp:154
size_t index() const
Return the index of this Edge.
Definition: edge.hpp:106
TreeNode const & secondary_node() const
Return the TreeNode of this TreeEdge that points away from the root.
Definition: edge.hpp:170
TreeNode & secondary_node()
Return the TreeNode of this TreeEdge that points away from the root.
Definition: edge.hpp:162
TreeEdge & reset_data(std::unique_ptr< BaseEdgeData > data)
Reset the data pointer of this TreeEdge.
Definition: edge.hpp:318
Container namespace for all symbols of genesis in order to keep them separate when used as a library...
Provides some valuable additions to STD.
bool data_is(bool allow_null=false) const
Return true iff the TreeEdge has a data object assigned to it that is of a particular given data type...
Definition: edge.hpp:192
TreeNode & primary_node()
Return the TreeNode of this TreeEdge that points towards the root.
Definition: edge.hpp:146
bool data_is_derived_from(bool allow_null=false) const
Return true iff the TreeEdge has a data object assigned to it that is derived from a particular given...
Definition: edge.hpp:207
BaseEdgeData * data_ptr()
Return a pointer to the data.
Definition: edge.hpp:246
Class for representing phylogenetic trees.
Definition: tree/tree.hpp:97
TreeEdge & reset_index(size_t val)
Reset the internal index of this TreeEdge.
Definition: edge.hpp:276
BaseEdgeData const * data_ptr() const
Return a const pointer to the data.
Definition: edge.hpp:257
TreeEdge & reset_primary_link(TreeLink *val)
Reset the internal pointer to the primary TreeLink of this TreeEdge.
Definition: edge.hpp:290
TreeLink const & secondary_link() const
Return the TreeLink of this TreeEdge that points away from the root.
Definition: edge.hpp:138
EdgeDataType const & data() const
Definition: edge.hpp:223
EdgeDataType const * data_cast() const
Definition: edge.hpp:235
TreeLink & primary_link()
Return the TreeLink of this TreeEdge that points towards the root.
Definition: edge.hpp:114
Base class for storing data on Edges of a Tree.
Definition: edge_data.hpp:66
TreeEdge(size_t index, TreeLink *primary_link, TreeLink *secondary_link)
Definition: edge.hpp:81
EdgeDataType & data()
Definition: edge.hpp:217
EdgeDataType * data_cast()
Definition: edge.hpp:229
TreeEdge & reset_secondary_link(TreeLink *val)
Reset the internal pointer to the secondary TreeLink of this TreeEdge.
Definition: edge.hpp:304