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 detail/map_view_base.hpp
10 /// \brief Helper base for the construction of the bimap views types.
12 #ifndef BOOST_BIMAP_DETAIL_MAP_VIEW_BASE_HPP
13 #define BOOST_BIMAP_DETAIL_MAP_VIEW_BASE_HPP
19 #include <boost/config.hpp>
24 #include <boost/throw_exception.hpp>
25 #include <boost/type_traits/is_const.hpp>
26 #include <boost/mpl/if.hpp>
28 #include <boost/bimap/relation/support/get_pair_functor.hpp>
29 #include <boost/bimap/relation/detail/to_mutable_relation_functor.hpp>
30 #include <boost/bimap/container_adaptor/support/iterator_facade_converters.hpp>
31 #include <boost/bimap/relation/support/data_extractor.hpp>
32 #include <boost/bimap/relation/support/opposite_tag.hpp>
33 #include <boost/bimap/relation/support/pair_type_by.hpp>
34 //#include <boost/bimap/support/iterator_type_by.hpp>
35 #include <boost/bimap/support/key_type_by.hpp>
36 #include <boost/bimap/support/data_type_by.hpp>
37 #include <boost/bimap/support/value_type_by.hpp>
38 #include <boost/bimap/detail/modifier_adaptor.hpp>
39 #include <boost/bimap/detail/debug/static_error.hpp>
40 #include <boost/bimap/detail/map_view_iterator.hpp>
48 // The next macro can be converted in a metafunctor to gain code robustness.
49 /*===========================================================================*/
50 #define BOOST_BIMAP_MAP_VIEW_CONTAINER_ADAPTOR( \
51 CONTAINER_ADAPTOR, TAG, BIMAP, OTHER_ITER, CONST_OTHER_ITER \
53 ::boost::bimaps::container_adaptor::CONTAINER_ADAPTOR \
55 BOOST_DEDUCED_TYPENAME BIMAP::core_type:: \
56 BOOST_NESTED_TEMPLATE index<TAG>::type, \
57 ::boost::bimaps::detail:: map_view_iterator<TAG,BIMAP>, \
58 ::boost::bimaps::detail::const_map_view_iterator<TAG,BIMAP>, \
59 ::boost::bimaps::detail:: OTHER_ITER<TAG,BIMAP>, \
60 ::boost::bimaps::detail::CONST_OTHER_ITER<TAG,BIMAP>, \
61 ::boost::bimaps::container_adaptor::support::iterator_facade_to_base \
63 ::boost::bimaps::detail:: map_view_iterator<TAG,BIMAP>, \
64 ::boost::bimaps::detail::const_map_view_iterator<TAG,BIMAP> \
68 ::boost::bimaps::relation::detail:: \
69 pair_to_relation_functor<TAG,BOOST_DEDUCED_TYPENAME BIMAP::relation>, \
70 ::boost::bimaps::relation::support:: \
71 get_pair_functor<TAG, BOOST_DEDUCED_TYPENAME BIMAP::relation > \
73 /*===========================================================================*/
76 #if defined(BOOST_MSVC)
77 /*===========================================================================*/
78 #define BOOST_BIMAP_MAP_VIEW_BASE_FRIEND(TYPE,TAG,BIMAP) \
79 typedef ::boost::bimaps::detail::map_view_base< \
80 TYPE<TAG,BIMAP>,TAG,BIMAP > friend_map_view_base; \
81 friend class friend_map_view_base;
82 /*===========================================================================*/
84 /*===========================================================================*/
85 #define BOOST_BIMAP_MAP_VIEW_BASE_FRIEND(TYPE,TAG,BIMAP) \
86 friend class ::boost::bimaps::detail::map_view_base< \
87 TYPE<TAG,BIMAP>,TAG,BIMAP >;
88 /*===========================================================================*/
92 /// \brief Common base for map views.
94 template< class Derived, class Tag, class BimapType>
97 typedef ::boost::bimaps::container_adaptor::support::
98 iterator_facade_to_base<
99 ::boost::bimaps::detail:: map_view_iterator<Tag,BimapType>,
100 ::boost::bimaps::detail::const_map_view_iterator<Tag,BimapType>
103 typedef ::boost::bimaps::relation::detail::
104 pair_to_relation_functor<Tag,
105 BOOST_DEDUCED_TYPENAME BimapType::relation> value_to_base_;
107 typedef BOOST_DEDUCED_TYPENAME ::boost::bimaps::support::
108 key_type_by<Tag,BimapType>::type key_type_;
110 typedef BOOST_DEDUCED_TYPENAME ::boost::bimaps::support::
111 data_type_by<Tag,BimapType>::type data_type_;
113 typedef BOOST_DEDUCED_TYPENAME ::boost::bimaps::relation::support::
115 BOOST_DEDUCED_TYPENAME BimapType::relation>::type value_type_;
118 ::boost::bimaps::detail::map_view_iterator<Tag,BimapType> iterator_;
122 bool replace(iterator_ position, const value_type_ & x)
124 return derived().base().replace(
125 derived().template functor<iterator_to_base_>()(position),
126 derived().template functor<value_to_base_>()(x)
130 template< class CompatibleKey >
131 bool replace_key(iterator_ position, const CompatibleKey & k)
133 return derived().base().replace(
134 derived().template functor<iterator_to_base_>()(position),
135 derived().template functor<value_to_base_>()(
136 ::boost::bimaps::relation::detail::
137 copy_with_first_replaced(*position,k)
142 template< class CompatibleData >
143 bool replace_data(iterator_ position, const CompatibleData & d)
145 return derived().base().replace(
146 derived().template functor<iterator_to_base_>()(position),
147 derived().template functor<value_to_base_>()(
148 ::boost::bimaps::relation::detail::
149 copy_with_second_replaced(*position,d)
154 /* This function may be provided in the future
156 template< class Modifier >
157 bool modify(iterator_ position, Modifier mod)
159 return derived().base().modify(
161 derived().template functor<iterator_to_base_>()(position),
163 ::boost::bimaps::detail::relation_modifier_adaptor
166 BOOST_DEDUCED_TYPENAME BimapType::relation,
167 BOOST_DEDUCED_TYPENAME ::boost::bimaps::relation::support::
170 Tag, BOOST_DEDUCED_TYPENAME BimapType::relation
173 BOOST_DEDUCED_TYPENAME ::boost::bimaps::relation::support::
176 BOOST_DEDUCED_TYPENAME ::boost::bimaps::relation::support::
177 opossite_tag<Tag,BimapType>::type,
178 BOOST_DEDUCED_TYPENAME BimapType::relation
187 template< class Modifier >
188 bool modify_key(iterator_ position, Modifier mod)
190 return derived().base().modify_key(
191 derived().template functor<iterator_to_base_>()(position), mod
195 template< class Modifier >
196 bool modify_data(iterator_ position, Modifier mod)
198 typedef BOOST_DEDUCED_TYPENAME ::boost::bimaps::relation::support::
201 BOOST_DEDUCED_TYPENAME ::boost::bimaps::relation::support::
202 opossite_tag<Tag,BimapType>::type,
203 BOOST_DEDUCED_TYPENAME BimapType::relation
205 >::type data_extractor_;
207 return derived().base().modify(
209 derived().template functor<iterator_to_base_>()(position),
211 // this may be replaced later by
212 // ::boost::bind( mod, ::boost::bind(data_extractor_(),_1) )
214 ::boost::bimaps::detail::unary_modifier_adaptor
217 BOOST_DEDUCED_TYPENAME BimapType::relation,
226 typedef map_view_base map_view_base_;
230 // Curiously Recurring Template interface.
234 return *static_cast<Derived*>(this);
237 Derived const& derived() const
239 return *static_cast<Derived const*>(this);
246 template< class Derived, class Tag, class BimapType>
247 class mutable_data_unique_map_view_access
249 typedef BOOST_DEDUCED_TYPENAME ::boost::bimaps::support::
250 data_type_by<Tag,BimapType>::type data_type_;
254 template< class CompatibleKey >
255 data_type_ & at(const CompatibleKey& k)
257 typedef ::boost::bimaps::detail::
258 map_view_iterator<Tag,BimapType> iterator;
260 iterator iter = derived().find(k);
261 if( iter == derived().end() )
263 ::boost::throw_exception(
264 std::out_of_range("bimap<>: invalid key")
270 template< class CompatibleKey >
271 const data_type_ & at(const CompatibleKey& k) const
273 typedef ::boost::bimaps::detail::
274 const_map_view_iterator<Tag,BimapType> const_iterator;
276 const_iterator iter = derived().find(k);
277 if( iter == derived().end() )
279 ::boost::throw_exception(
280 std::out_of_range("bimap<>: invalid key")
286 template< class CompatibleKey >
287 data_type_ & operator[](const CompatibleKey& k)
289 typedef ::boost::bimaps::detail::
290 map_view_iterator<Tag,BimapType> iterator;
292 typedef BOOST_DEDUCED_TYPENAME ::boost::bimaps::support::
293 value_type_by<Tag,BimapType>::type value_type;
295 iterator iter = derived().find(k);
296 if( iter == derived().end() )
298 iter = derived().insert( value_type(k,data_type_()) ).first;
305 typedef mutable_data_unique_map_view_access
306 mutable_data_unique_map_view_access_;
310 // Curiously Recurring Template interface.
314 return *static_cast<Derived*>(this);
317 Derived const& derived() const
319 return *static_cast<Derived const*>(this);
324 template< class Derived, class Tag, class BimapType>
325 class non_mutable_data_unique_map_view_access
327 typedef BOOST_DEDUCED_TYPENAME ::boost::bimaps::support::
328 data_type_by<Tag,BimapType>::type data_type_;
332 template< class CompatibleKey >
333 const data_type_ & at(const CompatibleKey& k) const
335 typedef ::boost::bimaps::detail::
336 const_map_view_iterator<Tag,BimapType> const_iterator;
338 const_iterator iter = derived().find(k);
339 if( iter == derived().end() )
341 ::boost::throw_exception(
342 std::out_of_range("bimap<>: invalid key")
348 template< class CompatibleKey >
349 data_type_ & operator[](const CompatibleKey&)
351 BOOST_BIMAP_STATIC_ERROR( OPERATOR_BRACKET_IS_NOT_SUPPORTED, (Derived));
356 typedef non_mutable_data_unique_map_view_access
357 non_mutable_data_unique_map_view_access_;
361 // Curiously Recurring Template interface.
365 return *static_cast<Derived*>(this);
368 Derived const& derived() const
370 return *static_cast<Derived const*>(this);
375 template< class Derived, class Tag, class BimapType>
376 struct unique_map_view_access
379 typedef BOOST_DEDUCED_TYPENAME ::boost::bimaps::support::
380 value_type_by<Tag,BimapType>::type value_type;
383 typedef BOOST_DEDUCED_TYPENAME ::boost::mpl::if_
385 typename ::boost::is_const<
386 BOOST_DEDUCED_TYPENAME value_type::second_type >::type,
388 non_mutable_data_unique_map_view_access<Derived,Tag,BimapType>,
389 mutable_data_unique_map_view_access<Derived,Tag,BimapType>
394 // Map views specialize the following structs to provide to the bimap class
395 // the extra side typedefs (i.e. left_local_iterator for unordered_maps,
396 // right_range_type for maps)
398 template< class MapView >
399 struct left_map_view_extra_typedefs {};
401 template< class MapView >
402 struct right_map_view_extra_typedefs {};
404 } // namespace detail
406 // This function is already part of Boost.Lambda.
407 // They may be moved to Boost.Utility.
409 template <class T> inline const T& make_const(const T& t) { return t; }
411 } // namespace bimaps
415 // The following macros avoids code duplication in map views
416 // Maybe this can be changed in the future using a scheme similar to
417 // the one used with map_view_base.
419 /*===========================================================================*/
420 #define BOOST_BIMAP_MAP_VIEW_RANGE_IMPLEMENTATION(BASE) \
423 BOOST_DEDUCED_TYPENAME base_::iterator, \
424 BOOST_DEDUCED_TYPENAME base_::iterator> range_type; \
427 BOOST_DEDUCED_TYPENAME base_::const_iterator, \
428 BOOST_DEDUCED_TYPENAME base_::const_iterator> const_range_type; \
431 template< class LowerBounder, class UpperBounder> \
432 range_type range(LowerBounder lower,UpperBounder upper) \
436 BOOST_DEDUCED_TYPENAME BASE::base_type::iterator, \
437 BOOST_DEDUCED_TYPENAME BASE::base_type::iterator \
439 > r( this->base().range(lower,upper) ); \
442 this->template functor< \
443 BOOST_DEDUCED_TYPENAME BASE::iterator_from_base \
445 this->template functor< \
446 BOOST_DEDUCED_TYPENAME BASE::iterator_from_base \
451 template< class LowerBounder, class UpperBounder> \
452 const_range_type range(LowerBounder lower,UpperBounder upper) const \
456 BOOST_DEDUCED_TYPENAME BASE::base_type::const_iterator, \
457 BOOST_DEDUCED_TYPENAME BASE::base_type::const_iterator \
459 > r( this->base().range(lower,upper) ); \
461 return const_range_type( \
462 this->template functor< \
463 BOOST_DEDUCED_TYPENAME BASE::iterator_from_base \
465 this->template functor< \
466 BOOST_DEDUCED_TYPENAME BASE::iterator_from_base \
470 /*===========================================================================*/
473 /*===========================================================================*/
474 #define BOOST_BIMAP_VIEW_ASSIGN_IMPLEMENTATION(BASE) \
476 template< class InputIterator > \
477 void assign(InputIterator first,InputIterator last) \
480 this->insert(this->end(),first,last); \
483 void assign(BOOST_DEDUCED_TYPENAME BASE::size_type n, \
484 const BOOST_DEDUCED_TYPENAME BASE::value_type& v) \
487 for(BOOST_DEDUCED_TYPENAME BASE::size_type i = 0 ; i < n ; ++i) \
489 this->push_back(v); \
492 /*===========================================================================*/
495 /*===========================================================================*/
496 #define BOOST_BIMAP_VIEW_FRONT_BACK_IMPLEMENTATION(BASE) \
498 BOOST_DEDUCED_TYPENAME BASE::reference front() \
500 return this->template functor< \
501 BOOST_DEDUCED_TYPENAME base_::value_from_base>() \
505 BOOST_DEDUCED_TYPENAME BASE::base_type::value_type & \
507 > ( this->base().front() ) \
511 BOOST_DEDUCED_TYPENAME BASE::reference back() \
513 return this->template functor< \
514 BOOST_DEDUCED_TYPENAME base_::value_from_base>() \
518 BOOST_DEDUCED_TYPENAME BASE::base_type::value_type & \
520 >( this->base().back() ) \
524 BOOST_DEDUCED_TYPENAME BASE::const_reference front() const \
526 return this->template functor< \
527 BOOST_DEDUCED_TYPENAME BASE::value_from_base>() \
529 this->base().front() \
533 BOOST_DEDUCED_TYPENAME BASE::const_reference back() const \
535 return this->template functor< \
536 BOOST_DEDUCED_TYPENAME BASE::value_from_base>() \
538 this->base().back() \
541 /*===========================================================================*/
544 #endif // BOOST_BIMAP_DETAIL_MAP_VIEW_BASE_HPP