3 // Copyright (c) 2006-2007 Matias Capeletto
5 // Distributed under the Boost Software License, Version 1.0.
6 // (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
9 /// \file container_adaptor/container_adaptor.hpp
10 /// \brief Container adaptor to build a type that is compliant to the concept of a container.
12 #ifndef BOOST_BIMAP_CONTAINER_ADAPTOR_CONTAINER_ADAPTOR_HPP
13 #define BOOST_BIMAP_CONTAINER_ADAPTOR_CONTAINER_ADAPTOR_HPP
15 #if defined(_MSC_VER) && (_MSC_VER>=1200)
19 #include <boost/config.hpp>
23 #include <boost/mpl/if.hpp>
24 #include <boost/mpl/aux_/na.hpp>
25 #include <boost/bimap/container_adaptor/detail/identity_converters.hpp>
26 #include <boost/iterator/iterator_traits.hpp>
28 #include <boost/bimap/container_adaptor/detail/functor_bag.hpp>
29 #include <boost/mpl/vector.hpp>
30 #include <boost/mpl/copy.hpp>
31 #include <boost/mpl/front_inserter.hpp>
32 #include <boost/call_traits.hpp>
39 /// \brief Container Adaptor toolbox, easy way to build new containers from existing ones.
41 namespace container_adaptor {
43 /// \brief Container adaptor to build a type that is compliant to the concept of a container.
52 class IteratorToBaseConverter = ::boost::mpl::na,
53 class IteratorFromBaseConverter = ::boost::mpl::na,
54 class ValueToBaseConverter = ::boost::mpl::na,
55 class ValueFromBaseConverter = ::boost::mpl::na,
57 class FunctorsFromDerivedClasses = mpl::vector<>
59 class container_adaptor
61 // MetaData -------------------------------------------------------------
65 typedef Iterator iterator;
66 typedef ConstIterator const_iterator;
68 typedef BOOST_DEDUCED_TYPENAME iterator_value < iterator >::type value_type;
69 typedef BOOST_DEDUCED_TYPENAME iterator_pointer < iterator >::type pointer;
70 typedef BOOST_DEDUCED_TYPENAME iterator_reference< iterator >::type reference;
71 typedef BOOST_DEDUCED_TYPENAME iterator_reference< const_iterator >::type const_reference;
73 typedef BOOST_DEDUCED_TYPENAME Base::size_type size_type;
74 typedef BOOST_DEDUCED_TYPENAME Base::difference_type difference_type;
76 typedef BOOST_DEDUCED_TYPENAME mpl::if_< ::boost::mpl::is_na<IteratorToBaseConverter>,
78 ::boost::bimaps::container_adaptor::detail::
79 iterator_to_base_identity
81 BOOST_DEDUCED_TYPENAME Base::iterator , iterator,
82 BOOST_DEDUCED_TYPENAME Base::const_iterator , const_iterator
87 IteratorToBaseConverter
90 >::type iterator_to_base;
92 typedef BOOST_DEDUCED_TYPENAME mpl::if_< ::boost::mpl::is_na<IteratorFromBaseConverter>,
94 ::boost::bimaps::container_adaptor::detail::
95 iterator_from_base_identity
97 BOOST_DEDUCED_TYPENAME Base::iterator , iterator,
98 BOOST_DEDUCED_TYPENAME Base::const_iterator , const_iterator
103 IteratorFromBaseConverter
106 >::type iterator_from_base;
108 typedef BOOST_DEDUCED_TYPENAME mpl::if_< ::boost::mpl::is_na<ValueToBaseConverter>,
110 ::boost::bimaps::container_adaptor::detail::
111 value_to_base_identity
113 BOOST_DEDUCED_TYPENAME Base::value_type,
122 >::type value_to_base;
124 typedef BOOST_DEDUCED_TYPENAME mpl::if_< ::boost::mpl::is_na<ValueFromBaseConverter>,
126 ::boost::bimaps::container_adaptor::detail::
127 value_from_base_identity
129 BOOST_DEDUCED_TYPENAME Base::value_type,
135 ValueFromBaseConverter
138 >::type value_from_base;
140 // ACCESS -----------------------------------------------------------------
144 explicit container_adaptor(Base & c) : dwfb(c) {}
148 typedef Base base_type;
150 typedef container_adaptor container_adaptor_;
152 const Base & base() const { return dwfb.data; }
153 Base & base() { return dwfb.data; }
155 // Interface --------------------------------------------------------------
159 size_type size() const { return base().size(); }
160 size_type max_size() const { return base().max_size(); }
161 bool empty() const { return base().empty(); }
165 return this->template functor<iterator_from_base>()( base().begin() );
170 return this->template functor<iterator_from_base>()( base().end() );
173 const_iterator begin() const
175 return this->template functor<iterator_from_base>()( base().begin() );
178 const_iterator end() const
180 return this->template functor<iterator_from_base>()( base().end() );
184 iterator erase(iterator pos)
186 return this->template functor<iterator_from_base>()(
187 base().erase(this->template functor<iterator_to_base>()(pos))
191 iterator erase(iterator first, iterator last)
193 return this->template functor<iterator_from_base>()(
195 this->template functor<iterator_to_base>()(first),
196 this->template functor<iterator_to_base>()(last)
206 template< class InputIterator >
207 void insert(InputIterator iterBegin, InputIterator iterEnd)
209 for( ; iterBegin != iterEnd ; ++iterBegin )
211 base().insert( this->template
212 functor<value_to_base>()( *iterBegin )
217 std::pair<iterator, bool> insert(
218 BOOST_DEDUCED_TYPENAME ::boost::call_traits< value_type >::param_type x)
220 std::pair< BOOST_DEDUCED_TYPENAME Base::iterator, bool > r(
221 base().insert( this->template functor<value_to_base>()(x) )
224 return std::pair<iterator, bool>( this->template
225 functor<iterator_from_base>()(r.first),r.second
229 iterator insert(iterator pos,
230 BOOST_DEDUCED_TYPENAME ::boost::call_traits< value_type >::param_type x)
232 return this->template functor<iterator_from_base>()(
234 this->template functor<iterator_to_base>()(pos),
235 this->template functor<value_to_base>()(x))
239 void swap( container_adaptor & c )
241 base().swap( c.base() );
244 // Access to functors ----------------------------------------------------
248 template< class Functor >
251 return dwfb.template functor<Functor>();
254 template< class Functor >
255 Functor const & functor() const
257 return dwfb.template functor<Functor>();
260 // Data ------------------------------------------------------------------
264 ::boost::bimaps::container_adaptor::detail::data_with_functor_bag
268 BOOST_DEDUCED_TYPENAME mpl::copy
278 mpl::front_inserter< FunctorsFromDerivedClasses >
286 } // namespace container_adaptor
287 } // namespace bimaps
291 #endif // BOOST_BIMAP_CONTAINER_ADAPTOR_CONTAINER_ADAPTOR_HPP