Chris@16: // Boost string_algo library collection_traits.hpp header file -------------// Chris@16: Chris@16: // Copyright Pavol Droba 2002-2003. Use, modification and Chris@16: // distribution is subject to the Boost Software License, Version Chris@16: // 1.0. (See accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: // (C) Copyright Thorsten Ottosen 2002-2003. Use, modification and Chris@16: // distribution is subject to the Boost Software License, Version Chris@16: // 1.0. (See accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: // (C) Copyright Jeremy Siek 2001. Use, modification and Chris@16: // distribution is subject to the Boost Software License, Version Chris@16: // 1.0. (See accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: // Original idea of container traits was proposed by Jeremy Siek and Chris@16: // Thorsten Ottosen. This implementation is lightweighted version Chris@16: // of container_traits adapter for usage with string_algo library Chris@16: Chris@16: #ifndef BOOST_RANGE_STRING_COLLECTION_TRAITS_HPP Chris@16: #define BOOST_RANGE_STRING_COLLECTION_TRAITS_HPP Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: // Implementation Chris@16: #include Chris@16: Chris@16: /*! \file Chris@16: Defines collection_traits class and related free-standing functions. Chris@16: This facility is used to unify the access to different types of collections. Chris@16: It allows the algorithms in the library to work with STL collections, c-style Chris@16: array, null-terminated c-strings (and more) using the same interface. Chris@16: */ Chris@16: Chris@16: namespace boost { Chris@16: namespace algorithm { Chris@16: Chris@16: // collection_traits template class -----------------------------------------// Chris@16: Chris@16: //! collection_traits class Chris@16: /*! Chris@16: Collection traits provide uniform access to different types of Chris@16: collections. This functionality allows to write generic algorithms Chris@16: which work with several different kinds of collections. Chris@16: Chris@16: Currently following collection types are supported: Chris@16: - containers with STL compatible container interface ( see ContainerConcept ) Chris@16: ( i.e. \c std::vector<>, \c std::list<>, \c std::string<> ... ) Chris@16: - c-style array Chris@16: ( \c char[10], \c int[15] ... ) Chris@16: - null-terminated c-strings Chris@16: ( \c char*, \c wchar_T* ) Chris@16: - std::pair of iterators Chris@16: ( i.e \c std::pair::iterator,vector::iterator> ) Chris@16: Chris@16: Collection traits provide an external collection interface operations. Chris@16: All are accessible using free-standing functions. Chris@16: Chris@16: The following operations are supported: Chris@16: - \c size() Chris@16: - \c empty() Chris@16: - \c begin() Chris@16: - \c end() Chris@16: Chris@16: Container traits have somewhat limited functionality on compilers not Chris@16: supporting partial template specialization and partial template ordering. Chris@16: */ Chris@16: template< typename T > Chris@16: struct collection_traits Chris@16: { Chris@16: private: Chris@101: typedef typename ::boost::mpl::eval_if< Chris@16: ::boost::algorithm::detail::is_pair, Chris@16: detail::pair_container_traits_selector, Chris@101: typename ::boost::mpl::eval_if< Chris@16: ::boost::is_array, Chris@16: detail::array_container_traits_selector, Chris@101: typename ::boost::mpl::eval_if< Chris@16: ::boost::is_pointer, Chris@16: detail::pointer_container_traits_selector, Chris@16: detail::default_container_traits_selector Chris@16: > Chris@16: > Chris@16: >::type container_helper_type; Chris@16: public: Chris@16: //! Function type Chris@16: typedef container_helper_type function_type; Chris@16: //! Value type Chris@101: typedef typename Chris@16: container_helper_type::value_type value_type; Chris@16: //! Size type Chris@101: typedef typename Chris@16: container_helper_type::size_type size_type; Chris@16: //! Iterator type Chris@101: typedef typename Chris@16: container_helper_type::iterator iterator; Chris@16: //! Const iterator type Chris@101: typedef typename Chris@16: container_helper_type::const_iterator const_iterator; Chris@16: //! Result iterator type ( iterator of const_iterator, depending on the constness of the container ) Chris@101: typedef typename Chris@16: container_helper_type::result_iterator result_iterator; Chris@16: //! Difference type Chris@101: typedef typename Chris@16: container_helper_type::difference_type difference_type; Chris@16: Chris@16: }; // 'collection_traits' Chris@16: Chris@16: // collection_traits metafunctions -----------------------------------------// Chris@16: Chris@16: //! Container value_type trait Chris@16: /*! Chris@16: Extract the type of elements contained in a container Chris@16: */ Chris@16: template< typename C > Chris@16: struct value_type_of Chris@16: { Chris@101: typedef typename collection_traits::value_type type; Chris@16: }; Chris@16: Chris@16: //! Container difference trait Chris@16: /*! Chris@16: Extract the container's difference type Chris@16: */ Chris@16: template< typename C > Chris@16: struct difference_type_of Chris@16: { Chris@101: typedef typename collection_traits::difference_type type; Chris@16: }; Chris@16: Chris@16: //! Container iterator trait Chris@16: /*! Chris@16: Extract the container's iterator type Chris@16: */ Chris@16: template< typename C > Chris@16: struct iterator_of Chris@16: { Chris@101: typedef typename collection_traits::iterator type; Chris@16: }; Chris@16: Chris@16: //! Container const_iterator trait Chris@16: /*! Chris@16: Extract the container's const_iterator type Chris@16: */ Chris@16: template< typename C > Chris@16: struct const_iterator_of Chris@16: { Chris@101: typedef typename collection_traits::const_iterator type; Chris@16: }; Chris@16: Chris@16: Chris@16: //! Container result_iterator Chris@16: /*! Chris@16: Extract the container's result_iterator type. This type maps to \c C::iterator Chris@16: for mutable container and \c C::const_iterator for const containers. Chris@16: */ Chris@16: template< typename C > Chris@16: struct result_iterator_of Chris@16: { Chris@101: typedef typename collection_traits::result_iterator type; Chris@16: }; Chris@16: Chris@16: // collection_traits related functions -----------------------------------------// Chris@16: Chris@16: //! Free-standing size() function Chris@16: /*! Chris@16: Get the size of the container. Uses collection_traits. Chris@16: */ Chris@16: template< typename C > Chris@101: inline typename collection_traits::size_type Chris@16: size( const C& c ) Chris@16: { Chris@16: return collection_traits::function_type::size( c ); Chris@16: } Chris@16: Chris@16: //! Free-standing empty() function Chris@16: /*! Chris@16: Check whether the container is empty. Uses container traits. Chris@16: */ Chris@16: template< typename C > Chris@16: inline bool empty( const C& c ) Chris@16: { Chris@16: return collection_traits::function_type::empty( c ); Chris@16: } Chris@16: Chris@16: #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING Chris@16: Chris@16: //! Free-standing begin() function Chris@16: /*! Chris@16: Get the begin iterator of the container. Uses collection_traits. Chris@16: */ Chris@16: template< typename C > Chris@101: inline typename collection_traits::iterator Chris@16: begin( C& c ) Chris@16: { Chris@16: return collection_traits::function_type::begin( c ); Chris@16: } Chris@16: Chris@16: //! Free-standing begin() function Chris@16: /*! Chris@16: \overload Chris@16: */ Chris@16: template< typename C > Chris@101: inline typename collection_traits::const_iterator Chris@16: begin( const C& c ) Chris@16: { Chris@16: return collection_traits::function_type::begin( c ); Chris@16: } Chris@16: Chris@16: //! Free-standing end() function Chris@16: /*! Chris@16: Get the begin iterator of the container. Uses collection_traits. Chris@16: */ Chris@16: template< typename C > Chris@101: inline typename collection_traits::iterator Chris@16: end( C& c ) Chris@16: { Chris@16: return collection_traits::function_type::end( c ); Chris@16: } Chris@16: Chris@16: //! Free-standing end() function Chris@16: /*! Chris@16: \overload Chris@16: */ Chris@16: template< typename C > Chris@101: inline typename collection_traits::const_iterator Chris@16: end( const C& c ) Chris@16: { Chris@16: return collection_traits::function_type::end( c ); Chris@16: } Chris@16: Chris@16: #else // BOOST_NO_FUNCTION_TEMPLATE_ORDERING Chris@16: Chris@16: //! Free-standing begin() function Chris@16: /*! Chris@16: \overload Chris@16: */ Chris@16: template< typename C > Chris@101: inline typename collection_traits::result_iterator Chris@16: begin( C& c ) Chris@16: { Chris@16: return collection_traits::function_type::begin( c ); Chris@16: } Chris@16: Chris@16: //! Free-standing end() function Chris@16: /*! Chris@16: \overload Chris@16: */ Chris@16: template< typename C > Chris@101: inline typename collection_traits::result_iterator Chris@16: end( C& c ) Chris@16: { Chris@16: return collection_traits::function_type::end( c ); Chris@16: } Chris@16: Chris@16: #endif // BOOST_NO_FUNCTION_TEMPLATE_ORDERING Chris@16: Chris@16: } // namespace algorithm Chris@16: } // namespace boost Chris@16: Chris@16: #endif // BOOST_STRING_COLLECTION_TRAITS_HPP