A toolkit for working with phylogenetic data.
v0.18.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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-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 
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 
93  Taxon();
94  Taxon( std::string const& name );
95 
96  virtual ~Taxon() = default;
97 
98  Taxon( Taxon const& );
99  Taxon( Taxon&& );
100 
101  Taxon& operator= ( Taxon const& );
102  Taxon& operator= ( Taxon&& );
103 
104  friend void swap( Taxon& lhs, Taxon& rhs );
105 
106  // -------------------------------------------------------------------------
107  // Properties
108  // -------------------------------------------------------------------------
109 
110  std::string const& name() const;
111  void name( std::string const& value );
112 
113  std::string const& rank() const;
114  void rank( std::string const& value );
115 
116  Taxon const* parent () const;
117  Taxon* parent ();
118 
119  // -------------------------------------------------------------------------
120  // Data
121  // -------------------------------------------------------------------------
122 
123  bool has_data() const;
124 
125  template< class TaxonDataType >
126  TaxonDataType& data()
127  {
128  return dynamic_cast< TaxonDataType& >( *data_ );
129  }
130 
131  template< class TaxonDataType >
132  TaxonDataType const& data() const
133  {
134  return dynamic_cast< TaxonDataType const& >( *data_ );
135  }
136 
137  template< class TaxonDataType >
138  TaxonDataType* data_cast()
139  {
140  return dynamic_cast< TaxonDataType* >( data_.get() );
141  }
142 
143  template< class TaxonDataType >
144  TaxonDataType const* data_cast() const
145  {
146  return dynamic_cast< TaxonDataType const* >( data_.get() );
147  }
148 
150  BaseTaxonData const* data_ptr() const;
151 
152  Taxon& reset_data( std::unique_ptr< BaseTaxonData > data );
153 
154  // -------------------------------------------------------------------------
155  // Protected Implementation Details
156  // -------------------------------------------------------------------------
157 
158 protected:
159 
160  Taxon& add_child_( Taxon const& child ) override;
161 
162  // -------------------------------------------------------------------------
163  // Data Members
164  // -------------------------------------------------------------------------
165 
166 private:
167 
168  std::string name_;
169  std::string rank_;
170 
171  Taxon* parent_;
172 
173  std::unique_ptr< BaseTaxonData > data_;
174 
175 };
176 
177 } // namespace taxonomy
178 } // namespace genesis
179 
180 #endif // include guard
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
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
TaxonDataType const & data() const
Definition: taxon.hpp:132
virtual ~Taxon()=default
friend Taxonomy
Taxonomy is a friend, as it needs access to parent_ in order to ensure consistency.
Definition: taxon.hpp:87
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
TaxonDataType const * data_cast() const
Definition: taxon.hpp:144
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
TaxonDataType * data_cast()
Definition: taxon.hpp:138
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
friend void swap(Taxon &lhs, Taxon &rhs)
Swapperator for Taxon.
Definition: taxon.cpp:147
TaxonDataType & data()
Definition: taxon.hpp:126
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