From 122b632970bc25c53f0e8e092e9914ae1462f993 Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Wed, 18 Jul 2018 10:06:32 +0200 Subject: [PATCH] document some more classes --- alib2std/src/extensions/ptr_tuple.hpp | 2 +- alib2std/src/extensions/tree.hpp | 10 +- alib2std/src/extensions/trie.hpp | 339 +++++++++++++++++++++++- alib2std/src/extensions/tuple.hpp | 155 ++++++++++- alib2std/src/extensions/type_traits.hpp | 76 ++++-- alib2std/src/extensions/typeindex.h | 44 +++ alib2std/src/extensions/typeinfo.hpp | 41 +++ 7 files changed, 627 insertions(+), 40 deletions(-) diff --git a/alib2std/src/extensions/ptr_tuple.hpp b/alib2std/src/extensions/ptr_tuple.hpp index 276cff2394..be819956f8 100644 --- a/alib2std/src/extensions/ptr_tuple.hpp +++ b/alib2std/src/extensions/ptr_tuple.hpp @@ -472,7 +472,7 @@ namespace ext { * \brief * Helper of pointer tuple construction. The tuple is constructed from values pack, types are deduced. * - * \tparam the Types of pointer tuple values + * \tparam _Elements of types inside the tuple * * \param __args the pointer tuple content * diff --git a/alib2std/src/extensions/tree.hpp b/alib2std/src/extensions/tree.hpp index 03bb538cf7..c8a158722b 100644 --- a/alib2std/src/extensions/tree.hpp +++ b/alib2std/src/extensions/tree.hpp @@ -666,7 +666,7 @@ public: /** * \brief - * Insert helper for insertion specified by position in children and inserted subtrees given by range of iterators. + * Inserts subtrees given by range of values at specified position. * * \param position the specification of position in children of the node where to add subtrees * \param begin the start of the range of subtrees @@ -1021,7 +1021,7 @@ public: /** * \brief - * Access value given indexes of chindren allong the selection path + * Access value given indexes of chindren allong the selection path. * * \tparam Indexes ... types of child indexes * @@ -1036,7 +1036,7 @@ public: /** * \brief - * Access value given indexes of chindren allong the selection path + * Access value given indexes of chindren allong the selection path. * * \param indexes actual values of child indexes * @@ -1120,7 +1120,7 @@ public: /** * \brief - * Swap method of two forward trees + * Swap method of two trees * * \param first the first forward tree to swap * \param second the second forward tree to swap @@ -1239,7 +1239,7 @@ public: /** * \brief - * Internal method of printing a tree into a stream + * Method of printing a tree into a stream * * The tree is printed hierarchically. * diff --git a/alib2std/src/extensions/trie.hpp b/alib2std/src/extensions/trie.hpp index 27d9001474..6d951348da 100644 --- a/alib2std/src/extensions/trie.hpp +++ b/alib2std/src/extensions/trie.hpp @@ -42,25 +42,25 @@ namespace ext { * * The trie is a hierarchical structure of nodes with parent child relationship, where the children are accessible by their keys. * - * \tparam T the type of values inside nodes of the tree + * \tparam T the type of values inside nodes of the trie */ template < class Key, class Value > class trie { /** * \brief - * The value in the root node of the tree + * The value in the root node of the trie */ Value m_data; /** * \brief - * Pointer to the parent of the node. Null pointer for root of a tree. + * Pointer to the parent of the node. Null pointer for root of a trie. */ trie * m_parent; /** * \brief - * Container of children of the root node of the tree. + * Container of children of the root node of the trie. * * The children are tries themselves, hence the subtrie of a trie is itself a trie. */ @@ -141,6 +141,15 @@ public: // ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + /** + * \brief + * Inserts a subtrie into a trie. + * + * \param key the associating key for the inserted subtrie + * \param value a subtrie to insert + * + * \return updated position iterator pointing to the first node inserted + */ const_children_iterator insert ( Key key, trie < Key, Value > && value ) { ext::map < Key, trie > & children = const_cast < ext::map < Key, trie > & > ( getChildren ( ) ); @@ -149,10 +158,26 @@ public: return iter; } + /** + * \brief + * Inserts a subtrie into a trie. + * + * \param key the associating key for the inserted subtrie + * \param value a subtrie to insert + * + * \return updated position iterator pointing to the first node inserted + */ const_children_iterator insert ( Key key, const trie < Key, Value > & value ) { return insert ( std::move ( key ), trie < Key, Value > ( value ) ); } + /** + * \brief + * Inserts subtries given by range of key-value pairs at specified position. + * + * \param begin the start of the range of subtries + * \param end the end of the range of subtries + */ void insert ( const_children_iterator begin, const_children_iterator end ) { ext::map < Key, trie > & children = const_cast < ext::map < Key, trie > & > ( getChildren ( ) ); @@ -162,42 +187,114 @@ public: // ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + /** + * \brief + * Constructor of the trie from value to be stored in the root node and children tries. + * + * \param data the value to be stored in the root + * \param children map of subtries + */ trie ( Value && data, ext::map < Key, trie > && children ) : m_data ( std::move ( data ) ), m_parent ( nullptr ), m_children ( std::move ( children ) ) { for ( std::pair < const Key, trie > & child : m_children ) child.second.m_parent = this; } - trie ( const Value & data, const ext::map < Key, trie > & subtrees ) : trie ( Value ( data ), ext::map < Key, trie > ( subtrees ) ) { + /** + * \brief + * Constructor of the trie from value to be stored in the root node and children tries. + * + * \param data the value to be stored in the root + * \param children map of subtries + */ + trie ( const Value & data, const ext::map < Key, trie > & subtries ) : trie ( Value ( data ), ext::map < Key, trie > ( subtries ) ) { } + /** + * \brief + * Constructor of the trie from value to be stored in the root node and pack of children tries. + * + * \tparam Types ... pack of types convertible to trie + * + * \param data the value to be stored in the root + * \param subtries ... pack of subtries + */ template < typename ... Types > - trie ( const Value & data, Types ... subtrees ) : trie ( data, ext::map < Key, trie > { subtrees ... } ) { + trie ( const Value & data, Types ... subtries ) : trie ( data, ext::map < Key, trie > { subtries ... } ) { } + /** + * \brief + * Constructor of the trie from value to be stored in the root node and pack of children tries. + * + * \tparam Types ... pack of types convertible to trie + * + * \param data the value to be stored in the root + * \param subtries ... pack of subtries + */ template < typename ... Types > - trie ( Value && data, Types ... subtrees ) : trie ( std::move ( data ), ext::map < Key, trie > { subtrees ... } ) { + trie ( Value && data, Types ... subtries ) : trie ( std::move ( data ), ext::map < Key, trie > { subtries ... } ) { } + /** + * \brief + * Constructor of the trie from value to be stored in the root node and range of key-value pairs to construct nullary nodes. + * + * \param data the value to be stored in the root + * \param begin the iterator to the begining of key-value pairs range + * \param end the iterator to the end of key-value pairs range + */ trie ( const Value & data, const_children_iterator begin, const_children_iterator end ) : trie ( data, ext::map < Key, trie > ( begin, end ) ) { } + /** + * \brief + * Dectructor of the trie + */ ~trie ( ) noexcept { } + /** + * \brief + * Copy constructor of the trie. + * + * \param other the other instace of the trie + */ trie ( const trie & other ) : m_data ( other.m_data ), m_parent ( other.m_parent ), m_children ( other.m_children ) { for ( std::pair < const Key, trie > & child : m_children ) child.second.m_parent = this; } + /** + * \brief + * Move constructor of the trie. + * + * \param other the other instace of the trie + */ trie ( trie && other ) noexcept : m_data ( std::move ( other.m_data ) ), m_parent ( other.m_parent ), m_children ( std::move ( other.m_children ) ) { for ( std::pair < const Key, trie > & child : m_children ) child.second.m_parent = this; } + /** + * \brief + * Copy operator of assignment of the trie. + * + * \param other the other instace of the trie + * + * \return the assigned to instance + */ trie & operator =( const trie & node ) { return this->operator =( trie ( node ) ); } + /** + * \brief + * Move operator of assignment of the trie. + * + * \param other the other instace of the trie + * + * \return the assigned to instance + */ trie & operator =( trie && node ) noexcept { m_data = std::move ( node.m_data ); m_children = std::move ( node.m_children ); @@ -210,24 +307,54 @@ public: // ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + /** + * \brief + * Getter of a children iterator to the begining of children + * + * \return iterator to the first child + */ const_children_iterator begin ( ) const { return m_children.begin ( ); } + /** + * \brief + * Getter of a children iterator to the begining of children + * + * \return iterator to the first child + */ children_iterator begin ( ) { return m_children.begin ( ); } + /** + * \brief + * Getter of a children iterator one after the last child + * + * \return iterator one after the last child + */ const_children_iterator end ( ) const { return m_children.end ( ); } + /** + * \brief + * Getter of a children iterator one after the last child + * + * \return iterator one after the last child + */ children_iterator end ( ) { return m_children.end ( ); } // ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + /** + * \brief + * Erases a subtrie from a trie on given by \p position + * + * \param position the specification of position in children where to erase the subtrie + */ const_children_iterator erase ( const_children_iterator position ) { ext::map < Key, trie > & children = const_cast < ext::map < Key, trie > & > ( getChildren ( ) ); @@ -236,11 +363,29 @@ public: // ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + /** + * \brief + * Access value given indexes to chindren allong the selection path. + * + * \tparam Indexes ... types of child keys + * + * \param indexes actual keys + * + * \return the value in the selected node + */ template < class ... Indexes > const Value & operator ()( Indexes ... indexes ) const { return this->operator ()( { ( Key ) indexes ... } ); } + /** + * \brief + * Access value given key to chindren allong the selection path. + * + * \param indexes list of actual keys + * + * \return the value in the selected node + */ const Value & operator ()( std::initializer_list < Key > indexes ) const { const trie * node = this; @@ -251,11 +396,29 @@ public: return node->getData ( ); } + /** + * \brief + * Access value given indexes to chindren allong the selection path. + * + * \tparam Indexes ... types of child keys + * + * \param indexes actual keys + * + * \return the value in the selected node + */ template < class ... Indexes > Value & operator ()( Indexes ... indexes ) { return this->operator ()( { ( Key ) indexes ... } ); } + /** + * \brief + * Access value given key to chindren allong the selection path. + * + * \param indexes list of actual keys + * + * \return the value in the selected node + */ Value & operator ()( std::initializer_list < Key > indexes ) { trie * node = this; @@ -268,6 +431,14 @@ public: // ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + /** + * \brief + * Helper method to traverse the trie and check all parent references are set correctly. + * + * \param node the node to test + * + * \return true if the parent child relationship of nodes is sound + */ bool checkStructure ( const trie & node ) const { bool sign = true; @@ -277,12 +448,27 @@ public: return sign; } + /** + * \brief + * Helper method to traverse the trie and check all parent references are set correctly. Starts at root node and recursively tests all its child nodes. + * + * \param node the node to test + * + * \return true if the parent child relationship of nodes is sound + */ bool checkStructure ( ) const { return m_parent == nullptr && checkStructure ( * this ); } // ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + /** + * \brief + * Swap method of two tries + * + * \param first the first forward trie to swap + * \param second the second forward trie to swap + */ friend void swap ( trie & first, trie & second ) { swap ( first.m_data, second.m_data ); swap ( first.m_children, second.m_children ); @@ -291,6 +477,16 @@ public: // ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + /** + * \brief + * Three-way comparison method of forward tries. + * + * \param other the other instance to compare against + * + * \return negative if this instance is smaller than \p other instance + * zero if this is equal to \p other instance + * positive if this instance is bigger than \p other instance + */ int compare ( const trie & other ) const { auto first = ext::tie ( this->getData ( ), this->getChildren ( ) ); auto second = ext::tie ( other.getData ( ), other.getChildren ( ) ); @@ -300,32 +496,102 @@ public: return comp ( first, second ); } + /** + * \brief + * Equality comparison operator. + * + * \param other the other instance to compare with + * + * \return true if this instance is equal to other instance, false othervise + */ bool operator ==( const trie & other ) { return compare ( other ) == 0; } + /** + * \brief + * Non-equality comparison operator. + * + * \param other the other instance to compare with + * + * \return true if this instance is not equal to other instance, false othervise + */ bool operator !=( const trie & other ) { return compare ( other ) != 0; } + /** + * \brief + * Less comparison operator. + * + * \param other the other instance to compare with + * + * \return true if this instance is smaller than other instance, false othervise + */ bool operator <( const trie & other ) { return compare ( other ) < 0; } + /** + * \brief + * Less or equal comparison operator. + * + * \param other the other instance to compare with + * + * \return true if this instance is smaller or equal than other instance, false othervise + */ bool operator <=( const trie & other ) { return compare ( other ) <= 0; } + /** + * \brief + * Greater comparison operator. + * + * \param other the other instance to compare with + * + * \return true if this instance is greater than other instance, false othervise + */ bool operator >( const trie & other ) { return compare ( other ) > 0; } + /** + * \brief + * Greater or equal comparison operator. + * + * \param other the other instance to compare with + * + * \return true if this instance is greater or equal than other instance, false othervise + */ bool operator >=( const trie & other ) { return compare ( other ) >= 0; } // ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + /** + * \brief + * Internal method of printing a trie into a stream + * + * The trie is printed hierarchically. Key-value pair components are printed separated by colon. + * + * Example trie a ( k:b ( m:c ), l:b ( n:c ) ) would be printed like + * + * \-a + * | + * |-k:b + * | | + * | \-m:c + * | + * \-l:b + * | + * \-n:c + * + * \param os the stream to print to + * + * \return the changed output stream + */ std::ostream & nicePrint ( std::ostream & os ) const { os << "\\-"; std::string prefix ( " " ); @@ -342,6 +608,28 @@ public: } private: + /** + * \brief + * Method of printing a trie into a stream + * + * The trie is printed hierarchically. Key-value pair components are printed separated by colon. + * + * Example trie a ( k:b ( m:c ), l:b ( n:c ) ) would be printed like + * + * \-a + * | + * |-k:b + * | | + * | \-m:c + * | + * \-l:b + * | + * \-n:c + * + * \param os the stream to print to + * + * \return the changed output stream + */ static void nicePrint ( std::ostream & os, const std::pair < const Key, trie < Key, Value > > & data, std::string prefix, const bool last ) { os << prefix; @@ -365,6 +653,17 @@ private: }; +/** + * \brief + * Operator to print the trie to the output stream. + * + * \param out the output stream + * \param trie the trie to print + * + * \tparam T the type of values inside the trie + * + * \return the output stream from the \p out + */ template < class Key, class Value > std::ostream & operator <<( std::ostream & out, const trie < Key, Value > & t ) { out << "["; @@ -383,14 +682,40 @@ std::ostream & operator <<( std::ostream & out, const trie < Key, Value > & t ) return out; } +/** + * \brief + * Specialisation of the compare structure implementing the three-way comparison. + * + * \tparam T the type of values inside the trie + */ template < class Key, class Value > struct compare < ext::trie < Key, Value > > { + + /** + * \brief + * Implementation of the three-way comparison. + * + * \param first the left operand of the comparison + * \param second the right operand of the comparison + * + * \return negative value of left < right, positive value if left > right, zero if left == right + */ int operator ()( const ext::trie < Key, Value > & first, const ext::trie < Key, Value > & second ) const { return first.compare ( second ); } }; +/** + * \brief + * Overload of to_string function. + * + * \param value the trie to be converted to string + * + * \tparam T the type of values inside the trie + * + * \return string representation + */ template < class Key, class Value > std::string to_string ( const ext::trie < Key, Value > & value ) { std::stringstream ss; diff --git a/alib2std/src/extensions/tuple.hpp b/alib2std/src/extensions/tuple.hpp index 6ba76b7d6e..f58359c797 100644 --- a/alib2std/src/extensions/tuple.hpp +++ b/alib2std/src/extensions/tuple.hpp @@ -1,6 +1,22 @@ /* * tuple.hpp * + * This file is part of Algorithms library toolkit. + * Copyright (C) 2017 Jan Travnicek (jan.travnicek@fit.cvut.cz) + + * Algorithms library toolkit is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + + * Algorithms library toolkit is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with Algorithms library toolkit. If not, see <http://www.gnu.org/licenses/>. + * * Created on: Apr 1, 2013 * Author: Jan Travnicek */ @@ -18,50 +34,105 @@ namespace ext { +/** + * \brief + * Class extending the tuple class from the standard library. Original reason is to allow printing of the container with overloaded operator <<. + * + * The class mimics the behavior of the tuple from the standatd library. + * + * \tparam Types ... the types inside the tuple + */ template < typename ... Ts > class tuple : public std::tuple < Ts ... > { public: + /** + * Inherit constructors of the standard set + */ using std::tuple< Ts ... >::tuple; + + /** + * Inherit operator = of the standard set + */ using std::tuple< Ts ... >::operator =; #ifndef __clang__ + + /** + * Default constructor needed by g++ since it is not inherited + */ tuple ( ) = default; + /** + * Copy constructor needed by g++ since it is not inherited + */ tuple ( const tuple & other ) = default; + /** + * Move constructor needed by g++ since it is not inherited + */ tuple ( tuple && other ) = default; + /** + * Copy operator = needed by g++ since it is not inherited + */ tuple & operator = ( tuple && other ) = default; + /** + * Move operator = needed by g++ since it is not inherited + */ tuple & operator = ( const tuple & other ) = default; #endif + /** + * \brief + * Inherited behavior of get for non-const instance. + * + * \tparam I the index to retrieve + * + * \return reference to the value at index I + */ template < std::size_t I > auto & get ( ) & { return std::get < I > ( * this ); } + /** + * \brief + * New behavior of get for rvalue referenced instance. + * + * \tparam I the index to retrieve + * + * \return rvalue reference to the value at index I + */ template < std::size_t I > auto && get ( ) && { return std::get < I > ( std::move ( * this ) ); } + /** + * \brief + * Inherited behavior of get fro const instance. + * + * \tparam I the index to retrieve + * + * \return const reference to the value at index I + */ template < std::size_t I > const auto & get ( ) const & { return std::get < I > ( * this ); } }; -template<typename... _Elements> -constexpr tuple < typename std::__decay_and_strip < _Elements >::__type... > make_tuple ( _Elements && ... __args ) { - typedef tuple < typename std::__decay_and_strip < _Elements >::__type... > __result_type; - return __result_type ( std::forward < _Elements > ( __args ) ... ); -} - -template<typename ... _Elements > -constexpr tuple < _Elements & ... > tie ( _Elements & ... __args ) noexcept { - return tuple < _Elements & ... > ( __args ... ); -} - +/** + * \brief + * Operator to print the tuple to the output stream. + * + * \param out the output stream + * \param tuple the tuple to print + * + * \tparam Ts ... the pack of value type of the tuple + * + * \return the output stream from the \p out + */ template< class... Ts> std::ostream& operator<<(std::ostream& out, const ext::tuple<Ts...>& tuple) { out << "("; @@ -81,6 +152,12 @@ std::ostream& operator<<(std::ostream& out, const ext::tuple<Ts...>& tuple) { return out; } +/** + * \brief + * Specialisation of the compare structure implementing the three-way comparison. + * + * \tparam Ts ... the pack of value type of the tuple + */ template < typename ... Ts > struct compare < ext::tuple < Ts ... > > { int operator ()( const ext::tuple < Ts ... > & first, const ext::tuple < Ts ... > & second ) const { @@ -89,6 +166,16 @@ struct compare < ext::tuple < Ts ... > > { }; +/** + * \brief + * Overload of to_string function. + * + * \param value the tuple to be converted to string + * + * \tparam Ts ... the pack of value type of the tuple + * + * \return string representation + */ template < class ... Ts > std::string to_string ( const ext::tuple < Ts ... > & value ) { std::stringstream ss; @@ -96,9 +183,55 @@ std::string to_string ( const ext::tuple < Ts ... > & value ) { return ss.str(); } +/** + * \brief + * Helper of extended tuple construction. The tuple is constructed from values pack, types are deduced. + * + * \tparam _Elements of types inside the tuple + * + * \param __args the tuple content + * + * \result tuple containing values from arguments + */ +template < typename ... _Elements > +constexpr tuple < typename std::__decay_and_strip < _Elements >::__type... > make_tuple ( _Elements && ... __args ) { + typedef tuple < typename std::__decay_and_strip < _Elements >::__type... > __result_type; + return __result_type ( std::forward < _Elements > ( __args ) ... ); +} + +/** + * \brief + * Helper of extended tuple of references construction. The tuple is constructed to reffer to values in the parameter pack, types are deduced. + * + * \tparam _Elements of types of references inside the tuple + * + * \param __args values to refer to + * + * \result tuple containing references to values from arguments + */ +template<typename ... _Elements > +constexpr tuple < _Elements & ... > tie ( _Elements & ... __args ) noexcept { + return tuple < _Elements & ... > ( __args ... ); +} + +/** + * \brief + * Specialisation of tuple_size for extended tuple. + * + * \tparam Types the types of tuple values + */ template < class ... Types > class tuple_size < ext::tuple < Types ... > > : public std::integral_constant < std::size_t, sizeof...( Types ) > { }; +/** + * \brief + * Specialisation of tuple_element for extended tuple. + * + * The class provides type field representing selected type. + * + * \tparam I the index into tuple types + * \tparam ... Types the pack of tuple types + */ template < std::size_t I, class... Types > struct tuple_element < I, ext::tuple < Types... > > { typedef typename std::tuple_element < I, std::tuple < Types ... > >::type type; diff --git a/alib2std/src/extensions/type_traits.hpp b/alib2std/src/extensions/type_traits.hpp index 046f23c7bf..7161c28094 100644 --- a/alib2std/src/extensions/type_traits.hpp +++ b/alib2std/src/extensions/type_traits.hpp @@ -1,6 +1,22 @@ /* * type_traits.hpp * + * This file is part of Algorithms library toolkit. + * Copyright (C) 2017 Jan Travnicek (jan.travnicek@fit.cvut.cz) + + * Algorithms library toolkit is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + + * Algorithms library toolkit is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with Algorithms library toolkit. If not, see <http://www.gnu.org/licenses/>. + * * Created on: Feb 28, 2014 * Author: Jan Travnicek */ @@ -14,43 +30,71 @@ namespace ext { + /** + * \brief + * Type trait to determine existence of clone method. A boolean field namd value is set to true if provided class (possibly cv-qualified and via reference) has clone method. + * + * \tparam the type to examine + */ template<class T> struct has_clone { + private: typedef char (&Yes)[1]; typedef char (&No)[2]; template<class U> static Yes test(U * data, typename std::enable_if< std::is_pointer<decltype(data->clone())>::value>::type * = 0); static No test(...); - static const bool value = sizeof(Yes) == sizeof(has_clone::test((typename std::remove_reference<T>::type*)0)); - }; - -// ---------------------------------------------------------------------------------------------------- - - template < size_t N, typename ... T > - struct get_type_pack_element; - - template < typename T, typename ... Ts> - struct get_type_pack_element < 0, T, Ts ... > { - typedef T type; - }; - - template < size_t N, typename T, typename ... Ts > - struct get_type_pack_element < N, T, Ts ... > { - typedef typename get_type_pack_element < N - 1, Ts ... >::type type; + public: + /** + * \brief + * True if the type decayed type T has clone method. + */ + static const bool value = sizeof(Yes) == sizeof(has_clone::test((typename std::decay<T>::type*)0)); }; // ---------------------------------------------------------------------------------------------------- + /** + * \brief + * Trait to test whether type T is in a types pack. The trait provides field value set to true if the type T is in pack of types Ts ..., false othervise. + * + * \tparam T the type to look for + * \tparam Ts ... the types pack to look in + */ template < typename T, typename ... Ts > struct is_in; + /** + * \brief + * Trait to test whether type T is in a types pack. The trait provides field value set to true if the type T is in pack of types Ts ..., false othervise. + * + * Specialisation for empty pack, in which case the field is set to false. + * + * \tparam T the type to look for + */ template < typename T > struct is_in < T > : std::false_type { }; + /** + * \brief + * Trait to test whether type T is in a types pack. The trait provides field value set to true if the type T is in pack of types Ts ..., false othervise. + * + * Specialisation for non-empty pack where the first type in the pack is different from the tested type, in which case the field is set to result of the recursive test on shorter pack. + * + * \tparam T the type to look for + */ template < typename T, typename U, typename ... Ts > struct is_in < T, U, Ts ... > : is_in < T, Ts ... > { }; + /** + * \brief + * Trait to test whether type T is in a types pack. The trait provides field value set to true if the type T is in pack of types Ts ..., false othervise. + * + * Specialisation for non-empty pack where the first type in the pack is the same as the tested type, in which case the field is set to true. + * + * \tparam T the type to look for + */ template < typename T, typename ... Ts > struct is_in < T, T, Ts ... > : std::true_type { }; diff --git a/alib2std/src/extensions/typeindex.h b/alib2std/src/extensions/typeindex.h index 061834b7fe..f565cc2ac3 100644 --- a/alib2std/src/extensions/typeindex.h +++ b/alib2std/src/extensions/typeindex.h @@ -1,6 +1,22 @@ /* * typeindex.h * + * This file is part of Algorithms library toolkit. + * Copyright (C) 2017 Jan Travnicek (jan.travnicek@fit.cvut.cz) + + * Algorithms library toolkit is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + + * Algorithms library toolkit is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with Algorithms library toolkit. If not, see <http://www.gnu.org/licenses/>. + * * Created on: Apr 1, 2013 * Author: Jan Travnicek */ @@ -15,18 +31,46 @@ namespace ext { +/** + * Class extending the type_index class from the standard library. Original reason is to allow printing of the container with overloaded operator <<. + * + * The class mimics the behavior of the type_index from the standatd library. + */ class type_index : public std::type_index { public: using std::type_index::type_index; using std::type_index::operator =; }; +/** + * \brief + * Implementation of three way comparison helper. + * + * \return -1 if the first is smaller than the second, 0 if they are the same, 1 if the the first is bigger than the second + */ inline int operator -( const ext::type_index & first, const ext::type_index & second ) { return ( first < second ) ? -1 : ( first > second ) ? 1 : 0; } +/** + * \brief + * Operator to print the type_index to the output stream. + * + * \param out the output stream + * \param type the type_index to print + * + * \return the output stream from the \p out + */ std::ostream & operator << ( std::ostream & os, const ext::type_index & type ); +/** + * \brief + * Overload of to_string function. + * + * \param value the type_index to be converted to string + * + * \return string representation + */ std::string to_string ( const ext::type_index & type ); } /* namespace ext */ diff --git a/alib2std/src/extensions/typeinfo.hpp b/alib2std/src/extensions/typeinfo.hpp index a8b1253c45..bac002c306 100644 --- a/alib2std/src/extensions/typeinfo.hpp +++ b/alib2std/src/extensions/typeinfo.hpp @@ -1,6 +1,22 @@ /* * typeinfo.hpp * + * This file is part of Algorithms library toolkit. + * Copyright (C) 2017 Jan Travnicek (jan.travnicek@fit.cvut.cz) + + * Algorithms library toolkit is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + + * Algorithms library toolkit is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with Algorithms library toolkit. If not, see <http://www.gnu.org/licenses/>. + * * Created on: Apr 1, 2013 * Author: Jan Travnicek */ @@ -15,6 +31,16 @@ namespace ext { +/** + * \brief + * Overload of to_string function. For type specified by template parameter. + * + * Specialisation for class types. Can handle incomplete types. + * + * \param value the type_index to be converted to string + * + * \return string representation + */ template < class T, typename std::enable_if < std::is_class < T >::value >::type * = nullptr > std::string to_string ( ) { std::string res = ext::to_string ( ext::type_index ( typeid ( T * ) ) ); // handle even incomplete class types @@ -22,11 +48,26 @@ std::string to_string ( ) { return res; } +/** + * \brief + * Overload of to_string function. For type specified by template parameter. + * + * \param value the type_index to be converted to string + * + * \return string representation + */ template < class T, typename std::enable_if < ! std::is_class < T >::value >::type * = nullptr > std::string to_string ( ) { return ext::to_string ( ext::type_index ( typeid ( T ) ) ); } +/** + * \brief + * Compares two types specified by their string names not looking at template params and unspecified segments. + * + * Example + * foo < aaa >::bb::cc is equal to foo::::cc + */ bool is_same_type ( const std::string & first, const std::string & second ); bool are_same_types ( const std::vector < std::string > & first, const std::vector < std::string > & second ); -- GitLab