A toolkit for working with phylogenetic data.
v0.19.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
taxon.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 
33 #include <algorithm>
34 #include <stdexcept>
35 
36 namespace genesis {
37 namespace taxonomy {
38 
39 // ================================================================================================
40 // Constructors and Rule of Five
41 // ================================================================================================
42 
47  : Taxonomy()
48  , name_()
49  , rank_()
50  , parent_( nullptr )
51  , data_( nullptr )
52 {}
53 
57 Taxon::Taxon( std::string const& name )
58  : Taxonomy()
59  , name_( name )
60  , rank_()
61  , parent_( nullptr )
62  , data_( nullptr )
63 {}
64 
78 Taxon::Taxon( Taxon const& other )
79  : Taxonomy( other )
80  , name_( other.name_ )
81  , rank_( other.rank_ )
82  , parent_( nullptr )
83 {
84  if( other.has_data() ) {
85  reset_data( other.data_->clone() );
86  }
87  reset_parent_pointers_( this );
88 }
89 
96 Taxon::Taxon( Taxon&& other )
97  : Taxonomy( std::move( other ))
98  , name_( std::move( other.name_ ))
99  , rank_( std::move( other.rank_ ))
100  , parent_( other.parent_ )
101  , data_( std::move( other.data_ ))
102 {
103  reset_parent_pointers_( this );
104 }
105 
114 Taxon& Taxon::operator= ( Taxon const& other )
115 {
116  Taxonomy::operator=( static_cast< Taxonomy const& >( other ));
117  name_ = other.name_;
118  rank_ = other.rank_;
119  parent_ = nullptr;
120  if( other.has_data() ) {
121  reset_data( other.data_->clone() );
122  }
123  reset_parent_pointers_( this );
124  return *this;
125 }
126 
134 {
135  Taxonomy::operator=( static_cast< Taxonomy&& >( std::move( other )));
136  name_ = std::move( other.name_ );
137  rank_ = std::move( other.rank_ );
138  parent_ = other.parent_;
139  data_ = std::move( other.data_ );
140  reset_parent_pointers_( this );
141  return *this;
142 }
143 
147 void swap( Taxon& lhs, Taxon& rhs )
148 {
149  using std::swap;
150  swap( static_cast< Taxonomy& >( lhs ), static_cast< Taxonomy& >( rhs ) );
151 
152  swap( lhs.name_, rhs.name_ );
153  swap( lhs.rank_, rhs.rank_ );
154  swap( lhs.parent_, rhs.parent_ );
155  swap( lhs.data_, rhs.data_ );
156 }
157 
158 // ================================================================================================
159 // Properties
160 // ================================================================================================
161 
162 // -----------------------------------------------------
163 // Name
164 // -----------------------------------------------------
165 
169 std::string const& Taxon::name() const
170 {
171  return name_;
172 }
173 
177 void Taxon::name( std::string const& value )
178 {
179  name_ = value;
180 }
181 
182 // -----------------------------------------------------
183 // Rank
184 // -----------------------------------------------------
185 
189 std::string const& Taxon::rank() const
190 {
191  return rank_;
192 }
193 
197 void Taxon::rank( std::string const& value )
198 {
199  rank_ = value;
200 }
201 
202 // -----------------------------------------------------
203 // Parent
204 // -----------------------------------------------------
205 
209 Taxon const* Taxon::parent () const
210 {
211  return parent_;
212 }
213 
218 {
219  return parent_;
220 }
221 
222 // ================================================================================================
223 // Data
224 // ================================================================================================
225 
229 bool Taxon::has_data() const
230 {
231  return data_.get() != nullptr;
232 }
233 
241 {
242  return data_.get();
243 }
244 
252 {
253  return data_.get();
254 }
255 
264 Taxon& Taxon::reset_data( std::unique_ptr< BaseTaxonData > data )
265 {
266  data_ = std::move( data );
267  return *this;
268 }
269 
270 // ================================================================================================
271 // Protected Implementation Details
272 // ================================================================================================
273 
280 Taxon& Taxon::add_child_( Taxon const& child )
281 {
282  auto& c = Taxonomy::add_child_( child );
283  // c.parent_ = this;
284 
285  // We added to the container. This might have caused relocation of the contant.
286  // Need to update parent pointers!
287  reset_parent_pointers_( this );
288  return c;
289 }
290 
291 } // namespace taxonomy
292 } // namespace genesis
Taxon()
Default constructor. Does nothing.
Definition: taxon.cpp:46
Taxon & add_child_(Taxon const &child) override
Virtual implementation function for adding a child taxon.
Definition: taxon.cpp:280
Taxonomy & operator=(Taxonomy const &)
Copy assigment operator.
Definition: taxonomy.cpp:75
std::string const & name() const
Return the name of this taxon.
Definition: taxon.cpp:169
Taxon & operator=(Taxon const &)
Copy assignment operator.
Definition: taxon.cpp:114
bool has_data() const
Return true if the Taxon has a data object assigned to it.
Definition: taxon.cpp:229
void reset_parent_pointers_(Taxon *parent)
Internal helper function that resets the parent pointer of all stored Taxa.
Definition: taxonomy.cpp:388
Store a Taxonomy, i.e., a nested hierarchy of Taxa.
Store a Taxon, i.e., an element in a Taxonomy, with its name, rank and sub-taxa.
Definition: taxon.hpp:76
void swap(Taxon &lhs, Taxon &rhs)
Swapperator for Taxon.
Definition: taxon.cpp:147
Taxon & reset_data(std::unique_ptr< BaseTaxonData > data)
Reset the data pointer of this Taxon.
Definition: taxon.cpp:264
BaseTaxonData * data_ptr()
Return a pointer to the data.
Definition: taxon.cpp:240
Base class for storing data on Taxa of a Taxonomy.
Definition: taxon_data.hpp:56
Taxon const * parent() const
Return a pointer to the parent of this taxon, or a nullptr if this is the top level taxon...
Definition: taxon.cpp:209
std::string const & rank() const
Return the rank of this taxon.
Definition: taxon.cpp:189
virtual Taxon & add_child_(Taxon const &child)
Virtual implementation function for adding a child Taxon.
Definition: taxonomy.cpp:352