#include <genesis/taxonomy/taxon.hpp>
Inherits Taxonomy.
Store a Taxon, i.e., an element in a Taxonomy, with its name, rank, ID and sub-taxa.
This class models a taxon within the hierarchy of a Taxonomy. Each such taxon can have a name assigned to it, a taxonomic rank, an ID, and an arbitrarily nested set of sub-taxa. The name, rank, and ID are free-form strings which can have any value.
The name is one part of a taxonomic path string. For example, given the string
Animalia;Vertebrata;Mammalia;Carnivora
a Taxon could have the name "Mammalia", and contain a Taxon with the name "Carnivora" as child. See Taxonomy for details, see Taxopath for the taxonomic path string format.
Each taxon is itself also a Taxonomy, in terms of class inheritance. This also makes some biological sense, as a taxon can be seen as the taxonomy of its sub-taxa. We however only consider the Taxonomy as the top level of the hierarchy. For more information, see there. The class inheritance is mainly due to software design descisions, in order to make working with Taxonomies and Taxa as easy and straight forward as possible.
Public Member Functions | |
Taxon () | |
Default constructor. Does nothing. Yields an empty Taxon. More... | |
Taxon (std::string const &name, std::string const &rank="", std::string const &id="") | |
Constructor that uses the given name for the Taxon. More... | |
Taxon (Taxon &&) | |
Move constructor. More... | |
Taxon (Taxon const &) | |
Copy constructor. More... | |
virtual | ~Taxon () override=default |
template<class TaxonDataType > | |
TaxonDataType & | data () |
template<class TaxonDataType > | |
TaxonDataType const & | data () const |
template<class TaxonDataType > | |
TaxonDataType * | data_cast () |
template<class TaxonDataType > | |
TaxonDataType const * | data_cast () const |
BaseTaxonData * | data_ptr () |
Return a pointer to the data. More... | |
BaseTaxonData const * | data_ptr () const |
Return a const pointer to the data. More... | |
bool | has_data () const |
Return true if the Taxon has a data object assigned to it. More... | |
std::string const & | id () const |
Set the ID of this taxon. More... | |
void | id (std::string const &value) |
std::string const & | name () const |
Return the name of this taxon. More... | |
void | name (std::string const &value) |
Set the name of this taxon. More... | |
Taxon & | operator= (Taxon &&) |
Move assignment operator. More... | |
Taxon & | operator= (Taxon const &) |
Copy assignment operator. More... | |
Taxon * | parent () |
Return a pointer to the parent of this taxon, or a nullptr if this is the top level taxon. More... | |
Taxon const * | parent () const |
Return a pointer to the parent of this taxon, or a nullptr if this is the top level taxon. More... | |
std::string const & | rank () const |
Return the rank of this taxon. More... | |
void | rank (std::string const &value) |
Taxon & | reset_data (std::unique_ptr< BaseTaxonData > data) |
Reset the data pointer of this Taxon. More... | |
Public Member Functions inherited from Taxonomy | |
Taxonomy ()=default | |
Taxonomy (Taxonomy &&) | |
Move constructor. More... | |
Taxonomy (Taxonomy const &) | |
Copy constructor. More... | |
virtual | ~Taxonomy ()=default |
Taxon & | add_child (std::string const &name, bool merge_duplicates=true) |
Add a child Taxon by creating a new one with the given name and return it. More... | |
Taxon & | add_child (Taxon const &child, bool merge_duplicates=true) |
Add a child Taxon as a copy of a given Taxon and return it. More... | |
Taxon & | at (size_t index) |
Return the child Taxon at the given index. More... | |
Taxon const & | at (size_t index) const |
Return the child Taxon at the given index. More... | |
iterator | begin () |
Return an iterator to the beginning of the child taxa. More... | |
const_iterator | begin () const |
Return a const iterator to the beginning of the child taxa. More... | |
const_iterator | cbegin () const |
Return a const iterator to the beginning of the child taxa. More... | |
const_iterator | cend () const |
Return a const iterator to the end of the child taxa. More... | |
void | clear_children () |
Remove all children. More... | |
iterator | end () |
Return an iterator to the end of the child taxa. More... | |
const_iterator | end () const |
Return a const iterator to the end of the child taxa. More... | |
Taxon & | get_child (std::string name) |
Return the child Taxon with a given name if it exists, or throw otherwise. More... | |
Taxon const & | get_child (std::string name) const |
Return the child Taxon with a given name if it exists, or throw otherwise. More... | |
bool | has_child (std::string name) const |
Return whether an immediate child Taxon with the given name exists. More... | |
size_t | index_of (std::string const &name) const |
Taxonomy & | operator= (Taxonomy &&) |
Move assignment operator. More... | |
Taxonomy & | operator= (Taxonomy const &) |
Copy assignment operator. More... | |
Taxon & | operator[] (size_t index) |
Return the child Taxon at the given index. More... | |
Taxon const & | operator[] (size_t index) const |
Return the child Taxon at the given index. More... | |
Taxon & | operator[] (std::string name) |
Return the child Taxon with a given name if it exists, or throw otherwise. More... | |
Taxon const & | operator[] (std::string name) const |
Return the child Taxon with a given name if it exists, or throw otherwise. More... | |
void | remove_at (size_t index) |
Remove a child Taxon at a certain index. More... | |
void | remove_child (std::string const &name) |
Remove a child Taxon with a certain name. More... | |
size_t | size () const |
Return the number of immediate child Taxa. More... | |
template<class Compare > | |
void | sort (Compare comp) |
Sort the taxonomy according to some compare criterion. More... | |
Public Attributes | |
friend | Taxonomy |
Taxonomy is a friend, as it needs access to parent_ in order to ensure consistency. More... | |
Protected Member Functions | |
Taxon & | add_child_ (Taxon const &child, bool merge_duplicates) override |
Virtual implementation function for adding a child taxon. More... | |
Protected Member Functions inherited from Taxonomy | |
void | reset_parent_pointers_ (Taxon *parent) |
Internal helper function that resets the parent pointer of all stored Taxa. More... | |
Friends | |
void | swap (Taxon &lhs, Taxon &rhs) |
Swapperator for Taxon. More... | |
Additional Inherited Members | |
Public Types inherited from Taxonomy | |
typedef std::list< Taxon >::const_iterator | const_iterator |
typedef std::list< Taxon >::iterator | iterator |
Taxon | ( | ) |
|
explicit |
|
overridevirtualdefault |
Copy constructor.
We need a custom version of this in order to set the Taxon::parent() pointers of all children correctly when copying and to copy the data.
Copying first sets the parent() pointer to nullptr
. This is because we might want to get a 'blank' copy, i.e., a Taxon that is not attached to a Taxonomy. This way, the functions expecting a parent pointer don't get a potentially invalid pointer.
If however we copy a Taxon into a Taxonomy (or into some other Taxon), reset_parent_pointers_() is called later anyway, which then sets the parent pointer to the correct value.
Move constructor.
We need a custom version of this in order to set the Taxon::parent() pointers of all children correctly, and to treat the data correctlty when copying.
Virtual implementation function for adding a child taxon.
See Taxonomy::add_child_() for details. In addition to the base class implementation, this function also sets the parent pointer of the Taxon.
Reimplemented from Taxonomy.
BaseTaxonData const * data_ptr | ( | ) |
BaseTaxonData const* data_ptr | ( | ) | const |
Return a const pointer to the data.
In most cases, using data<>() is more convenient. However, in some cases, this function might be necessary.
bool has_data | ( | ) | const |
std::string const & id | ( | ) | const |
std::string const & name | ( | ) | const |
void name | ( | std::string const & | value | ) |
Move assignment operator.
We need a custom version of this in order to set the Taxon::parent() pointers of all children correctly, and to treat the data correctlty when copying.
Copy assignment operator.
We need a custom version of this in order to set the Taxon::parent() pointers of all children correctly, and to treat the data correctlty when copying.
See the move constructor for details.
Taxon* parent | ( | ) |
Return a pointer to the parent of this taxon, or a nullptr
if this is the top level taxon.
Taxon * parent | ( | ) | const |
std::string const & rank | ( | ) | const |
Taxon & reset_data | ( | std::unique_ptr< BaseTaxonData > | data | ) |
Reset the data pointer of this Taxon.
Using this function, a Taxon can be assigend new data. It is also possible to change the data type completely (as long as it derives from BaseTaxonData). Be however aware that many functions that work with a Taxonomy expect a certain data type. Thus, changing it might break those functions and lead to exceptions and other errors.
friend Taxonomy |