A toolkit for working with phylogenetic data.
v0.19.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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-2017 Lucas Czech
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 TreeLink;
50 class TreeNode;
51 
52 class BaseEdgeData;
53 
54 // =================================================================================================
55 // Tree Edge
56 // =================================================================================================
57 
58 class TreeEdge
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_p_( nullptr )
75  , link_s_( nullptr )
76  , data_( nullptr )
77  {}
78 
80  : index_( index )
81  , link_p_( primary_link )
82  , link_s_( secondary_link )
83  , data_( nullptr )
84  {}
85 
86  ~TreeEdge() = default;
87 
88  // avoid copy constructor and assignment operator.
89  // creating copies is maintained by Tree only.
90 
91  TreeEdge( TreeEdge const& ) = delete;
92  TreeEdge( TreeEdge&& ) = delete;
93 
94  TreeEdge& operator= ( TreeEdge const& ) = delete;
95  TreeEdge& operator= ( TreeEdge&& ) = delete;
96 
97  // ---------------------------------------------------------------------
98  // Accessors
99  // ---------------------------------------------------------------------
100 
101  size_t index() const;
102 
104  TreeLink const& primary_link() const;
105 
107  TreeLink const& secondary_link() const;
108 
110  TreeNode const& primary_node() const;
111 
113  TreeNode const& secondary_node() const;
114 
115  bool has_data() const;
116 
117  template< class EdgeDataType >
118  EdgeDataType& data()
119  {
120  return dynamic_cast< EdgeDataType& >( *data_ );
121  }
122 
123  template< class EdgeDataType >
124  EdgeDataType const& data() const
125  {
126  return dynamic_cast< EdgeDataType const& >( *data_ );
127  }
128 
129  template< class EdgeDataType >
130  EdgeDataType* data_cast()
131  {
132  return dynamic_cast< EdgeDataType* >( data_.get() );
133  }
134 
135  template< class EdgeDataType >
136  EdgeDataType const* data_cast() const
137  {
138  return dynamic_cast< EdgeDataType const* >( data_.get() );
139  }
140 
142  BaseEdgeData const* data_ptr() const;
143 
144  // ---------------------------------------------------------------------
145  // Modifiers
146  // ---------------------------------------------------------------------
147 
148  TreeEdge& reset_index( size_t val );
149 
152 
153  TreeEdge& reset_data( std::unique_ptr< BaseEdgeData > data );
154 
155  // ---------------------------------------------------------------------
156  // Member Functions
157  // ---------------------------------------------------------------------
158 
159  std::string dump() const;
160 
161  // ---------------------------------------------------------------------
162  // Member Variables
163  // ---------------------------------------------------------------------
164 
165 private:
166 
167  size_t index_;
168 
169  TreeLink* link_p_;
170  TreeLink* link_s_;
171 
172  std::unique_ptr< BaseEdgeData > data_;
173 };
174 
175 } // namespace tree
176 } // namespace genesis
177 
178 #endif // include guard
size_t index() const
Return the index of this Edge.
Definition: edge.cpp:45
TreeLink & secondary_link()
Return the TreeLink of this TreeEdge that points away from the root.
Definition: edge.cpp:69
TreeEdge & operator=(TreeEdge const &)=delete
BaseEdgeData * data_ptr()
Return a pointer to the data.
Definition: edge.cpp:128
TreeEdge & reset_data(std::unique_ptr< BaseEdgeData > data)
Reset the data pointer of this TreeEdge.
Definition: edge.cpp:200
Provides some valuable additions to STD.
TreeLink & primary_link()
Return the TreeLink of this TreeEdge that points towards the root.
Definition: edge.cpp:53
Class for representing phylogenetic trees.
Definition: tree/tree.hpp:95
bool has_data() const
Return true if the TreeEdge has a data object assigned to it.
Definition: edge.cpp:117
TreeEdge & reset_secondary_link(TreeLink *val)
Reset the internal pointer to the secondary TreeLink of this TreeEdge.
Definition: edge.cpp:186
TreeNode & secondary_node()
Return the TreeNode of this TreeEdge that points away from the root.
Definition: edge.cpp:101
TreeEdge & reset_index(size_t val)
Reset the internal index of this TreeEdge.
Definition: edge.cpp:158
std::string dump() const
Returns a one-line dump summary of the data of this edge.
Definition: edge.cpp:215
TreeNode & primary_node()
Return the TreeNode of this TreeEdge that points towards the root.
Definition: edge.cpp:85
EdgeDataType const & data() const
Definition: edge.hpp:124
EdgeDataType const * data_cast() const
Definition: edge.hpp:136
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:79
EdgeDataType & data()
Definition: edge.hpp:118
EdgeDataType * data_cast()
Definition: edge.hpp:130
TreeEdge & reset_primary_link(TreeLink *val)
Reset the internal pointer to the primary TreeLink of this TreeEdge.
Definition: edge.cpp:172