• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Boost.Bimap
2 //
3 // Copyright (c) 2006-2007 Matias Capeletto
4 //
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)
8 
9 /// \file views/map_view.hpp
10 /// \brief View of a side of a bimap that is signature compatible with std::map.
11 
12 #ifndef BOOST_BIMAP_VIEWS_MAP_VIEW_HPP
13 #define BOOST_BIMAP_VIEWS_MAP_VIEW_HPP
14 
15 #if defined(_MSC_VER)
16 #pragma once
17 #endif
18 
19 #include <boost/config.hpp>
20 
21 #include <boost/bimap/container_adaptor/map_adaptor.hpp>
22 #include <boost/bimap/detail/map_view_base.hpp>
23 
24 namespace boost {
25 namespace bimaps {
26 namespace views {
27 
28 /// \brief View of a side of a bimap that is signature compatible with std::map.
29 /**
30 
31 This class uses container_adaptor and iterator_adaptor to wrapped a index of the
32 multi_index bimap core so it can be used as a std::map.
33 
34 See also const_map_view.
35                                                                             **/
36 
37 template< class Tag, class BimapType >
38 class map_view
39 :
40     public BOOST_BIMAP_MAP_VIEW_CONTAINER_ADAPTOR(
41         map_adaptor,
42         Tag,BimapType,
43         reverse_map_view_iterator,const_reverse_map_view_iterator
44     ),
45     public ::boost::bimaps::detail::
46         map_view_base< map_view<Tag,BimapType>,Tag,BimapType >,
47     public ::boost::bimaps::detail::
48         unique_map_view_access< map_view<Tag,BimapType>, Tag,  BimapType>::type
49 {
50     typedef BOOST_BIMAP_MAP_VIEW_CONTAINER_ADAPTOR(
51         map_adaptor,
52         Tag,BimapType,
53         reverse_map_view_iterator,const_reverse_map_view_iterator
54 
55     ) base_;
56 
57     BOOST_BIMAP_MAP_VIEW_BASE_FRIEND(map_view,Tag,BimapType)
58 
59     typedef BOOST_DEDUCED_TYPENAME ::boost::bimaps::detail::
60         unique_map_view_access<
61             map_view<Tag,BimapType>, Tag,  BimapType
62 
63         >::type unique_map_view_access_;
64 
65     public:
66 
67     typedef BOOST_DEDUCED_TYPENAME base_::value_type::info_type info_type;
68 
map_view(BOOST_DEDUCED_TYPENAME base_::base_type & c)69     map_view(BOOST_DEDUCED_TYPENAME base_::base_type & c) : base_(c) {}
70 
71     using unique_map_view_access_::at;
72     using unique_map_view_access_::operator[];
73 
BOOST_BIMAP_MAP_VIEW_RANGE_IMPLEMENTATION(base_)74     BOOST_BIMAP_MAP_VIEW_RANGE_IMPLEMENTATION(base_)
75 
76     map_view & operator=(const map_view & v)
77     {
78         this->base() = v.base();
79         return *this;
80     }
81 
82     // It can be used enable_if here but the error message when there
83     // is no info is very clear like this
84 
85     template< class CompatibleKey >
info_at(const CompatibleKey & k) const86     const info_type & info_at(const CompatibleKey& k) const
87     {
88         BOOST_DEDUCED_TYPENAME base_::const_iterator iter = this->find(k);
89         if( iter == this->end() )
90         {
91             ::boost::throw_exception(
92                 std::out_of_range("bimap<>: invalid key")
93             );
94         }
95         return iter->info;
96     }
97 
98     template< class CompatibleKey >
info_at(const CompatibleKey & k)99     info_type & info_at(const CompatibleKey& k)
100     {
101         BOOST_DEDUCED_TYPENAME base_::iterator iter = this->find(k);
102         if( iter == this->end() )
103         {
104             ::boost::throw_exception(
105                 std::out_of_range("bimap<>: invalid key")
106             );
107         }
108         return iter->info;
109     }
110 };
111 
112 } // namespace views
113 
114 /*===========================================================================*/
115 #define BOOST_BIMAP_MAP_VIEW_EXTRA_TYPEDEF(MAP_VIEW,SIDE,TYPENAME)            \
116 typedef BOOST_DEDUCED_TYPENAME MAP_VIEW::TYPENAME                             \
117     BOOST_PP_CAT(SIDE,BOOST_PP_CAT(_,TYPENAME));
118 /*===========================================================================*/
119 
120 /*===========================================================================*/
121 #define BOOST_BIMAP_MAP_VIEW_EXTRA_TYPEDEFS_BODY(MAP_VIEW,SIDE)               \
122     BOOST_BIMAP_MAP_VIEW_EXTRA_TYPEDEF(MAP_VIEW,SIDE,reverse_iterator)        \
123     BOOST_BIMAP_MAP_VIEW_EXTRA_TYPEDEF(MAP_VIEW,SIDE,const_reverse_iterator)  \
124     BOOST_BIMAP_MAP_VIEW_EXTRA_TYPEDEF(MAP_VIEW,SIDE,range_type)              \
125     BOOST_BIMAP_MAP_VIEW_EXTRA_TYPEDEF(MAP_VIEW,SIDE,const_range_type)        \
126     BOOST_BIMAP_MAP_VIEW_EXTRA_TYPEDEF(MAP_VIEW,SIDE,key_compare)
127 /*===========================================================================*/
128 
129 namespace detail {
130 
131 template< class Tag, class BimapType >
132 struct left_map_view_extra_typedefs< ::boost::bimaps::views::map_view<Tag,BimapType> >
133 {
134     private: typedef ::boost::bimaps::views::map_view<Tag,BimapType> map_view_;
135     public : BOOST_BIMAP_MAP_VIEW_EXTRA_TYPEDEFS_BODY(map_view_,left)
136 };
137 
138 template< class Tag, class BimapType >
139 struct right_map_view_extra_typedefs< ::boost::bimaps::views::map_view<Tag,BimapType> >
140 {
141     private: typedef ::boost::bimaps::views::map_view<Tag,BimapType> map_view_;
142     public : BOOST_BIMAP_MAP_VIEW_EXTRA_TYPEDEFS_BODY(map_view_,right)
143 };
144 
145 } // namespace detail
146 
147 /*===========================================================================*/
148 #undef BOOST_BIMAP_MAP_VIEW_EXTRA_TYPEDEF
149 #undef BOOST_BIMAP_MAP_VIEW_EXTRA_TYPEDEFS_BODY
150 /*===========================================================================*/
151 
152 } // namespace bimaps
153 } // namespace boost
154 
155 #endif // BOOST_BIMAP_VIEWS_MAP_VIEW_HPP
156 
157