A library for working with phylogenetic and population genetic data.
v0.27.0
taxon.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_TAXONOMY_TAXON_H_
2 #define GENESIS_TAXONOMY_TAXON_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2019 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 
37 #include <memory>
38 #include <string>
39 
40 namespace genesis {
41 namespace taxonomy {
42 
43 // =================================================================================================
44 // Forward Declarations
45 // =================================================================================================
46 
47 class Taxon;
48 void swap( Taxon& lhs, Taxon& rhs );
49 
50 // =================================================================================================
51 // Taxon
52 // =================================================================================================
53 
76 class Taxon : public Taxonomy
77 {
78 public:
79 
80  // -------------------------------------------------------------------------
81  // Typedefs and Enums
82  // -------------------------------------------------------------------------
83 
87  friend Taxonomy;
88 
89  // -------------------------------------------------------------------------
90  // Constructors and Rule of Five
91  // -------------------------------------------------------------------------
92 
96  Taxon();
97 
101  explicit Taxon( std::string const& name, std::string const& rank = "", std::string const& id = "" );
102 
103  virtual ~Taxon() override = default;
104 
118  Taxon( Taxon const& );
119 
126  Taxon( Taxon&& );
127 
136  Taxon& operator= ( Taxon const& );
137 
144  Taxon& operator= ( Taxon&& );
145 
149  friend void swap( Taxon& lhs, Taxon& rhs );
150 
151  // -------------------------------------------------------------------------
152  // Properties
153  // -------------------------------------------------------------------------
154 
158  std::string const& name() const;
159 
163  void name( std::string const& value );
164 
168  std::string const& rank() const;
169  void rank( std::string const& value );
170 
174  std::string const& id() const;
175  void id( std::string const& value );
176 
180  Taxon const* parent () const;
181 
185  Taxon* parent ();
186 
187  // -------------------------------------------------------------------------
188  // Data
189  // -------------------------------------------------------------------------
190 
194  bool has_data() const;
195 
196  template< class TaxonDataType >
197  TaxonDataType& data()
198  {
199  return dynamic_cast< TaxonDataType& >( *data_ );
200  }
201 
202  template< class TaxonDataType >
203  TaxonDataType const& data() const
204  {
205  return dynamic_cast< TaxonDataType const& >( *data_ );
206  }
207 
208  template< class TaxonDataType >
209  TaxonDataType* data_cast()
210  {
211  return dynamic_cast< TaxonDataType* >( data_.get() );
212  }
213 
214  template< class TaxonDataType >
215  TaxonDataType const* data_cast() const
216  {
217  return dynamic_cast< TaxonDataType const* >( data_.get() );
218  }
219 
227 
234  BaseTaxonData const* data_ptr() const;
235 
244  Taxon& reset_data( std::unique_ptr< BaseTaxonData > data );
245 
246  // -------------------------------------------------------------------------
247  // Protected Implementation Details
248  // -------------------------------------------------------------------------
249 
250 protected:
251 
258  Taxon& add_child_( Taxon const& child, bool merge_duplicates ) override;
259 
260  // -------------------------------------------------------------------------
261  // Data Members
262  // -------------------------------------------------------------------------
263 
264 private:
265 
266  std::string name_;
267  std::string rank_;
268  std::string id_;
269 
270  Taxon* parent_;
271 
272  std::unique_ptr< BaseTaxonData > data_;
273 
274 };
275 
276 } // namespace taxonomy
277 } // namespace genesis
278 
279 #endif // include guard
genesis::taxonomy::Taxon::operator=
Taxon & operator=(Taxon const &)
Copy assignment operator.
Definition: taxon.cpp:85
genesis::taxonomy::Taxon::parent
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:173
genesis::taxonomy::BaseTaxonData
Base class for storing data on Taxa of a Taxonomy.
Definition: taxon_data.hpp:56
genesis::taxonomy::Taxon::~Taxon
virtual ~Taxon() override=default
genesis::taxonomy::Taxon::Taxonomy
friend Taxonomy
Taxonomy is a friend, as it needs access to parent_ in order to ensure consistency.
Definition: taxon.hpp:87
genesis::taxonomy::Taxon::add_child_
Taxon & add_child_(Taxon const &child, bool merge_duplicates) override
Virtual implementation function for adding a child taxon.
Definition: taxon.cpp:212
genesis::taxonomy::Taxon::Taxon
Taxon()
Default constructor. Does nothing. Yields an empty Taxon.
Definition: taxon.cpp:43
genesis::taxonomy::Taxon
Store a Taxon, i.e., an element in a Taxonomy, with its name, rank, ID and sub-taxa.
Definition: taxon.hpp:76
taxonomy.hpp
taxon_data.hpp
genesis::taxonomy::Taxon::rank
std::string const & rank() const
Return the rank of this taxon.
Definition: taxon.cpp:145
genesis::taxonomy::Taxon::data_cast
TaxonDataType const * data_cast() const
Definition: taxon.hpp:215
genesis::taxonomy::swap
void swap(Taxon &lhs, Taxon &rhs)
Definition: taxon.cpp:111
genesis::taxonomy::Taxon::name
std::string const & name() const
Return the name of this taxon.
Definition: taxon.cpp:131
genesis::taxonomy::Taxon::data_ptr
BaseTaxonData * data_ptr()
Return a pointer to the data.
Definition: taxon.cpp:192
genesis::taxonomy::Taxon::data_cast
TaxonDataType * data_cast()
Definition: taxon.hpp:209
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::taxonomy::Taxonomy
Store a Taxonomy, i.e., a nested hierarchy of Taxa.
Definition: taxonomy/taxonomy.hpp:96
genesis::placement::merge_duplicates
void merge_duplicates(Sample &smp)
Look for Pqueries with the same name and merge them.
Definition: placement/function/functions.cpp:569
genesis::taxonomy::Taxon::reset_data
Taxon & reset_data(std::unique_ptr< BaseTaxonData > data)
Reset the data pointer of this Taxon.
Definition: taxon.cpp:202
genesis::taxonomy::Taxon::data
TaxonDataType & data()
Definition: taxon.hpp:197
genesis::taxonomy::Taxon::data
TaxonDataType const & data() const
Definition: taxon.hpp:203
genesis::taxonomy::Taxon::has_data
bool has_data() const
Return true if the Taxon has a data object assigned to it.
Definition: taxon.cpp:187
genesis::taxonomy::Taxon::swap
friend void swap(Taxon &lhs, Taxon &rhs)
Swapperator for Taxon.
Definition: taxon.cpp:111
genesis::taxonomy::Taxon::id
std::string const & id() const
Set the ID of this taxon.
Definition: taxon.cpp:159