Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:51:16

0001 //  Boost string_algo library collection_traits.hpp header file  -------------//
0002 
0003 //  Copyright Pavol Droba 2002-2003. Use, modification and
0004 //  distribution is subject to the Boost Software License, Version
0005 //  1.0. (See accompanying file LICENSE_1_0.txt or copy at
0006 //  http://www.boost.org/LICENSE_1_0.txt)
0007 
0008 // (C) Copyright Thorsten Ottosen 2002-2003. Use, modification and
0009 //  distribution is subject to the Boost Software License, Version
0010 //  1.0. (See accompanying file LICENSE_1_0.txt or copy at
0011 //  http://www.boost.org/LICENSE_1_0.txt)
0012 
0013 // (C) Copyright Jeremy Siek 2001. Use, modification and
0014 //  distribution is subject to the Boost Software License, Version
0015 //  1.0. (See accompanying file LICENSE_1_0.txt or copy at
0016 //  http://www.boost.org/LICENSE_1_0.txt)
0017 
0018 //  Original idea of container traits was proposed by Jeremy Siek and
0019 //  Thorsten Ottosen. This implementation is lightweighted version
0020 //  of container_traits adapter for usage with string_algo library
0021 
0022 #ifndef BOOST_RANGE_STRING_COLLECTION_TRAITS_HPP
0023 #define BOOST_RANGE_STRING_COLLECTION_TRAITS_HPP
0024 
0025 #include <boost/type_traits/is_array.hpp>
0026 #include <boost/type_traits/is_pointer.hpp>
0027 #include <boost/mpl/eval_if.hpp>
0028 
0029 // Implementation
0030 #include <boost/range/detail/collection_traits_detail.hpp>
0031 
0032 /*! \file
0033     Defines collection_traits class and related free-standing functions.
0034     This facility is used to unify the access to different types of collections.
0035     It allows the algorithms in the library to work with STL collections, c-style
0036     array, null-terminated c-strings (and more) using the same interface.
0037 */
0038 
0039 namespace boost {
0040     namespace algorithm {
0041 
0042 //  collection_traits template class -----------------------------------------//
0043         
0044         //! collection_traits class
0045         /*!
0046             Collection traits provide uniform access to different types of 
0047             collections. This functionality allows to write generic algorithms
0048             which work with several different kinds of collections.
0049 
0050             Currently following collection types are supported:
0051                 - containers with STL compatible container interface ( see ContainerConcept )
0052                     ( i.e. \c std::vector<>, \c std::list<>, \c std::string<> ... )
0053                 - c-style array 
0054                    ( \c char[10], \c int[15] ... )
0055                 - null-terminated c-strings
0056                     ( \c char*, \c wchar_T* )
0057                 - std::pair of iterators 
0058                     ( i.e \c std::pair<vector<int>::iterator,vector<int>::iterator> )
0059 
0060             Collection traits provide an external collection interface operations.
0061             All are accessible using free-standing functions.
0062 
0063             The following operations are supported:
0064                 - \c size()
0065                 - \c empty()
0066                 - \c begin()
0067                 - \c end()
0068 
0069             Container traits have somewhat limited functionality on compilers not
0070             supporting partial template specialization and partial template ordering.
0071         */
0072         template< typename T >
0073         struct collection_traits
0074         {
0075         private:
0076             typedef typename ::boost::mpl::eval_if<
0077                     ::boost::algorithm::detail::is_pair<T>, 
0078                         detail::pair_container_traits_selector<T>,
0079                         typename ::boost::mpl::eval_if<
0080                         ::boost::is_array<T>, 
0081                             detail::array_container_traits_selector<T>,
0082                             typename ::boost::mpl::eval_if<
0083                             ::boost::is_pointer<T>,
0084                                 detail::pointer_container_traits_selector<T>,
0085                                 detail::default_container_traits_selector<T>
0086                             >
0087                         > 
0088                 >::type container_helper_type;
0089         public:
0090             //! Function type       
0091             typedef container_helper_type function_type;        
0092             //! Value type
0093             typedef typename
0094                 container_helper_type::value_type value_type;
0095             //! Size type
0096             typedef typename
0097                 container_helper_type::size_type size_type;
0098             //! Iterator type
0099             typedef typename
0100                 container_helper_type::iterator iterator;
0101             //! Const iterator type
0102             typedef typename
0103                 container_helper_type::const_iterator const_iterator;
0104             //! Result iterator type ( iterator of const_iterator, depending on the constness of the container )
0105             typedef typename
0106                 container_helper_type::result_iterator result_iterator;
0107             //! Difference type
0108             typedef typename
0109                 container_helper_type::difference_type difference_type;
0110 
0111         }; // 'collection_traits'
0112 
0113 //  collection_traits metafunctions -----------------------------------------//
0114 
0115         //! Container value_type trait
0116         /*!
0117             Extract the type of elements contained in a container
0118         */
0119         template< typename C >
0120         struct value_type_of
0121         {
0122             typedef typename collection_traits<C>::value_type type;
0123         };
0124         
0125         //! Container difference trait
0126         /*!
0127             Extract the container's difference type
0128         */
0129         template< typename C >
0130         struct difference_type_of
0131         {
0132             typedef typename collection_traits<C>::difference_type type;
0133         };
0134 
0135         //! Container iterator trait
0136         /*!
0137             Extract the container's iterator type
0138         */
0139         template< typename C >
0140         struct iterator_of
0141         {
0142             typedef typename collection_traits<C>::iterator type;
0143         };
0144 
0145         //! Container const_iterator trait
0146         /*!
0147             Extract the container's const_iterator type
0148         */
0149         template< typename C >
0150         struct const_iterator_of
0151         {
0152             typedef typename collection_traits<C>::const_iterator type;
0153         };
0154 
0155 
0156         //! Container result_iterator
0157         /*!
0158             Extract the container's result_iterator type. This type maps to \c C::iterator
0159             for mutable container and \c C::const_iterator for const containers.
0160         */
0161         template< typename C >
0162         struct result_iterator_of
0163         {
0164             typedef typename collection_traits<C>::result_iterator type;
0165         };
0166 
0167 //  collection_traits related functions -----------------------------------------//
0168 
0169         //! Free-standing size() function
0170         /*!
0171             Get the size of the container. Uses collection_traits.
0172         */
0173         template< typename C >
0174         inline typename collection_traits<C>::size_type
0175         size( const C& c )
0176         {
0177             return collection_traits<C>::function_type::size( c ); 
0178         }
0179 
0180         //! Free-standing empty() function
0181         /*!
0182             Check whether the container is empty. Uses container traits.
0183         */
0184         template< typename C >
0185         inline bool empty( const C& c )
0186         {
0187             return collection_traits<C>::function_type::empty( c );
0188         }
0189 
0190         //! Free-standing begin() function
0191         /*!
0192             Get the begin iterator of the container. Uses collection_traits.
0193         */
0194         template< typename C >
0195         inline typename collection_traits<C>::iterator
0196         begin( C& c )
0197         {
0198             return collection_traits<C>::function_type::begin( c ); 
0199         }
0200 
0201         //! Free-standing begin() function
0202         /*!
0203             \overload
0204         */
0205         template< typename C >
0206         inline typename collection_traits<C>::const_iterator
0207         begin( const C& c )
0208         {
0209             return collection_traits<C>::function_type::begin( c ); 
0210         }
0211 
0212         //! Free-standing end() function
0213         /*!
0214             Get the begin iterator of the container. Uses collection_traits.
0215         */
0216         template< typename C >
0217         inline typename collection_traits<C>::iterator
0218         end( C& c )
0219         {
0220             return collection_traits<C>::function_type::end( c );
0221         }
0222 
0223         //! Free-standing end() function
0224         /*!
0225             \overload           
0226         */
0227         template< typename C >
0228         inline typename collection_traits<C>::const_iterator
0229         end( const C& c )
0230         {
0231             return collection_traits<C>::function_type::end( c );
0232         }
0233 
0234     } // namespace algorithm
0235 } // namespace boost
0236 
0237 #endif // BOOST_STRING_COLLECTION_TRAITS_HPP