map_view.hpp 5.0 KB

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