Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:29:37

0001 // Boost.Bimap
0002 //
0003 // Copyright (c) 2006-2007 Matias Capeletto
0004 //
0005 // Distributed under the Boost Software License, Version 1.0.
0006 // (See accompanying file LICENSE_1_0.txt or copy at
0007 // http://www.boost.org/LICENSE_1_0.txt)
0008 
0009 /// \file container_adaptor/container_adaptor.hpp
0010 /// \brief Container adaptor to build a type that is compliant to the concept of a container.
0011 
0012 #ifndef BOOST_BIMAP_CONTAINER_ADAPTOR_CONTAINER_ADAPTOR_HPP
0013 #define BOOST_BIMAP_CONTAINER_ADAPTOR_CONTAINER_ADAPTOR_HPP
0014 
0015 #if defined(_MSC_VER)
0016 #pragma once
0017 #endif
0018 
0019 #include <boost/config.hpp>
0020 
0021 #include <utility>
0022 
0023 #include <boost/mpl/if.hpp>
0024 #include <boost/mpl/aux_/na.hpp>
0025 #include <boost/bimap/container_adaptor/detail/identity_converters.hpp>
0026 #include <boost/iterator/iterator_traits.hpp>
0027 
0028 #include <boost/bimap/container_adaptor/detail/functor_bag.hpp>
0029 #include <boost/mpl/vector.hpp>
0030 #include <boost/mpl/copy.hpp>
0031 #include <boost/mpl/front_inserter.hpp>
0032 #include <boost/call_traits.hpp>
0033 
0034 
0035 
0036 namespace boost {
0037 namespace bimaps {
0038 
0039 /// \brief Container Adaptor toolbox, easy way to build new containers from existing ones.
0040 
0041 namespace container_adaptor {
0042 
0043 /// \brief Container adaptor to build a type that is compliant to the concept of a container.
0044 
0045 template
0046 <
0047     class Base,
0048 
0049     class Iterator,
0050     class ConstIterator,
0051 
0052     class IteratorToBaseConverter   = ::boost::mpl::na,
0053     class IteratorFromBaseConverter = ::boost::mpl::na,
0054     class ValueToBaseConverter      = ::boost::mpl::na,
0055     class ValueFromBaseConverter    = ::boost::mpl::na,
0056 
0057     class FunctorsFromDerivedClasses = mpl::vector<>
0058 >
0059 class container_adaptor
0060 {
0061     // MetaData -------------------------------------------------------------
0062 
0063     public:
0064 
0065     typedef Iterator iterator;
0066     typedef ConstIterator const_iterator;
0067 
0068     typedef BOOST_DEDUCED_TYPENAME iterator_value    <       iterator >::type value_type;
0069     typedef BOOST_DEDUCED_TYPENAME iterator_pointer  <       iterator >::type pointer;
0070     typedef BOOST_DEDUCED_TYPENAME iterator_reference<       iterator >::type reference;
0071     typedef BOOST_DEDUCED_TYPENAME iterator_reference< const_iterator >::type const_reference;
0072 
0073     typedef BOOST_DEDUCED_TYPENAME Base::size_type size_type;
0074     typedef BOOST_DEDUCED_TYPENAME Base::difference_type difference_type;
0075 
0076     typedef BOOST_DEDUCED_TYPENAME mpl::if_< ::boost::mpl::is_na<IteratorToBaseConverter>,
0077         // {
0078                 ::boost::bimaps::container_adaptor::detail::
0079                     iterator_to_base_identity
0080                 <
0081                     BOOST_DEDUCED_TYPENAME Base::iterator                , iterator,
0082                     BOOST_DEDUCED_TYPENAME Base::const_iterator          , const_iterator
0083                 >,
0084         // }
0085         // else
0086         // {
0087                 IteratorToBaseConverter
0088         // }
0089 
0090         >::type iterator_to_base;
0091 
0092     typedef BOOST_DEDUCED_TYPENAME mpl::if_< ::boost::mpl::is_na<IteratorFromBaseConverter>,
0093         // {
0094                 ::boost::bimaps::container_adaptor::detail::
0095                     iterator_from_base_identity
0096                 <
0097                     BOOST_DEDUCED_TYPENAME Base::iterator                , iterator,
0098                     BOOST_DEDUCED_TYPENAME Base::const_iterator          , const_iterator
0099                 >,
0100         // }
0101         // else
0102         // {
0103                 IteratorFromBaseConverter
0104         // }
0105 
0106         >::type iterator_from_base;
0107 
0108     typedef BOOST_DEDUCED_TYPENAME mpl::if_< ::boost::mpl::is_na<ValueToBaseConverter>,
0109         // {
0110                 ::boost::bimaps::container_adaptor::detail::
0111                     value_to_base_identity
0112                 <
0113                     BOOST_DEDUCED_TYPENAME Base::value_type,
0114                     value_type
0115                 >,
0116         // }
0117         // else
0118         // {
0119                 ValueToBaseConverter
0120         // }
0121 
0122         >::type value_to_base;
0123 
0124     typedef BOOST_DEDUCED_TYPENAME mpl::if_< ::boost::mpl::is_na<ValueFromBaseConverter>,
0125         // {
0126                 ::boost::bimaps::container_adaptor::detail::
0127                     value_from_base_identity
0128                 <
0129                     BOOST_DEDUCED_TYPENAME Base::value_type,
0130                     value_type
0131                 >,
0132         // }
0133         // else
0134         // {
0135                 ValueFromBaseConverter
0136         // }
0137 
0138         >::type value_from_base;
0139 
0140     // ACCESS -----------------------------------------------------------------
0141 
0142     public:
0143 
0144     explicit container_adaptor(Base & c) : dwfb(c) {}
0145 
0146     protected:
0147 
0148     typedef Base base_type;
0149 
0150     typedef container_adaptor container_adaptor_;
0151 
0152     const Base & base() const { return dwfb.data; }
0153           Base & base()       { return dwfb.data; }
0154 
0155     // Interface --------------------------------------------------------------
0156 
0157     public:
0158 
0159     size_type size() const                    { return base().size();         }
0160     size_type max_size() const                { return base().max_size();     }
0161     bool empty() const                        { return base().empty();        }
0162 
0163     iterator begin()
0164     {
0165         return this->template functor<iterator_from_base>()( base().begin() );
0166     }
0167 
0168     iterator end()
0169     {
0170         return this->template functor<iterator_from_base>()( base().end() );
0171     }
0172 
0173     const_iterator begin() const
0174     {
0175         return this->template functor<iterator_from_base>()( base().begin() );
0176     }
0177 
0178     const_iterator end() const
0179     {
0180         return this->template functor<iterator_from_base>()( base().end() );
0181     }
0182 
0183 
0184     iterator erase(iterator pos)
0185     {
0186         return this->template functor<iterator_from_base>()(
0187             base().erase(this->template functor<iterator_to_base>()(pos))
0188         );
0189     }
0190 
0191     iterator erase(iterator first, iterator last)
0192     {
0193         return this->template functor<iterator_from_base>()(
0194             base().erase(
0195                 this->template functor<iterator_to_base>()(first),
0196                 this->template functor<iterator_to_base>()(last)
0197             )
0198         );
0199     }
0200 
0201     void clear()
0202     {
0203         base().clear();
0204     }
0205 
0206     template< class InputIterator >
0207     void insert(InputIterator iterBegin, InputIterator iterEnd)
0208     {
0209         for( ; iterBegin != iterEnd ; ++iterBegin )
0210         {
0211             base().insert( this->template
0212                 functor<value_to_base>()( *iterBegin )
0213             );
0214         }
0215     }
0216 
0217     std::pair<iterator, bool> insert(
0218         BOOST_DEDUCED_TYPENAME ::boost::call_traits< value_type >::param_type x)
0219     {
0220         std::pair< BOOST_DEDUCED_TYPENAME Base::iterator, bool > r(
0221             base().insert( this->template functor<value_to_base>()(x) )
0222         );
0223 
0224         return std::pair<iterator, bool>( this->template
0225                     functor<iterator_from_base>()(r.first),r.second
0226                );
0227     }
0228 
0229     iterator insert(iterator pos,
0230                     BOOST_DEDUCED_TYPENAME ::boost::call_traits< value_type >::param_type x)
0231     {
0232         return this->template functor<iterator_from_base>()(
0233             base().insert(
0234                 this->template functor<iterator_to_base>()(pos),
0235                 this->template functor<value_to_base>()(x))
0236         );
0237     }
0238 
0239     void swap( container_adaptor & c )
0240     {
0241         base().swap( c.base() );
0242     }
0243 
0244     // Access to functors ----------------------------------------------------
0245 
0246     protected:
0247 
0248     template< class Functor >
0249     Functor & functor()
0250     {
0251         return dwfb.template functor<Functor>();
0252     }
0253 
0254     template< class Functor >
0255     Functor const & functor() const
0256     {
0257         return dwfb.template functor<Functor>();
0258     }
0259 
0260     // Data ------------------------------------------------------------------
0261 
0262     private:
0263 
0264     ::boost::bimaps::container_adaptor::detail::data_with_functor_bag
0265     <
0266         Base &,
0267 
0268         BOOST_DEDUCED_TYPENAME mpl::copy
0269         <
0270             mpl::vector
0271             <
0272                 iterator_to_base,
0273                 iterator_from_base,
0274                 value_to_base,
0275                 value_from_base
0276             >,
0277 
0278             mpl::front_inserter< FunctorsFromDerivedClasses >
0279 
0280         >::type
0281 
0282     > dwfb;
0283 };
0284 
0285 
0286 } // namespace container_adaptor
0287 } // namespace bimaps
0288 } // namespace boost
0289 
0290 
0291 #endif // BOOST_BIMAP_CONTAINER_ADAPTOR_CONTAINER_ADAPTOR_HPP