A toolkit for working with phylogenetic data.
v0.19.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
tree/default/operators.cpp
Go to the documentation of this file.
1 /*
2  Genesis - A toolkit for working with phylogenetic data.
3  Copyright (C) 2014-2017 Lucas Czech
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  Contact:
19  Lucas Czech <lucas.czech@h-its.org>
20  Exelixis Lab, Heidelberg Institute for Theoretical Studies
21  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
22 */
23 
32 
35 #include "genesis/tree/tree.hpp"
36 
37 namespace genesis {
38 namespace tree {
39 
40 // =================================================================================================
41 // Conversion
42 // =================================================================================================
43 
45 {
46  // In both converter functions, we first cast to default data, in order to make sure that we
47  // actually have data that is derived from default data. If not, those casts will throw.
48  // Then, we explicitly call functions of the default data classes, i.e., we break the virtual
49  // call on purpose, by calling `.DefaultNodeData::...`. This makes sure that we actually get
50  // default data and not the most derived one.
51 
52  auto node_data_converter = [] ( tree::BaseNodeData const& source_node ) {
53  return dynamic_cast< DefaultNodeData const& >( source_node ).DefaultNodeData::clone();
54  };
55 
56  auto edge_data_converter = [] ( tree::BaseEdgeData const& source_edge ) {
57  return dynamic_cast< DefaultEdgeData const& >( source_edge ).DefaultEdgeData::clone();
58  };
59 
60  return tree::convert(
61  source_tree,
62  node_data_converter,
63  edge_data_converter
64  );
65 }
66 
67 } // namespace tree
68 } // namespace genesis
virtual std::unique_ptr< BaseEdgeData > clone() const override
Polymorphically copy an instance of this class. Use instead of copy constructor.
Tree convert(Tree const &source, std::function< std::unique_ptr< BaseNodeData >(BaseNodeData const &node_data)> node_data_converter, std::function< std::unique_ptr< BaseEdgeData >(BaseEdgeData const &edge_data)> edge_data_converter)
Create a tree with the same topology as the source tree, while converting its data.
DefaultTree convert_to_default_tree(Tree const &source_tree)
Convert a Tree to a DefaultTree with DefaultNodeData and DefaultEdgeData.
Base class for storing data on Nodes of a Tree.
Definition: node_data.hpp:66
Tree operator functions.
Default class containing the commonly needed data for tree nodes.
Default class containing the commonly needed data for tree edges.
virtual std::unique_ptr< BaseNodeData > clone() const override
Polymorphically copy an instance of this class. Use instead of copy constructor.
Class for representing phylogenetic trees.
Definition: tree/tree.hpp:95
Header of Tree class.
Base class for storing data on Edges of a Tree.
Definition: edge_data.hpp:66