property_map_iterator.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // (C) Copyright Jeremy Siek, 2001.
  2. // Distributed under the Boost Software License, Version 1.0. (See
  3. // accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // See http://www.boost.org/libs/property_map for documentation.
  6. #ifndef BOOST_PROPERTY_MAP_ITERATOR_HPP
  7. #define BOOST_PROPERTY_MAP_ITERATOR_HPP
  8. #include <boost/property_map/property_map.hpp>
  9. #include <boost/iterator/iterator_adaptor.hpp>
  10. #include <boost/mpl/if.hpp>
  11. #include <boost/type_traits/is_same.hpp>
  12. namespace boost {
  13. //======================================================================
  14. // property iterator, generalized from ideas by Francois Faure
  15. namespace detail {
  16. template <class Iterator, class LvaluePropertyMap>
  17. class lvalue_pmap_iter
  18. : public iterator_adaptor< lvalue_pmap_iter< Iterator, LvaluePropertyMap >,
  19. Iterator,
  20. typename property_traits<LvaluePropertyMap>::value_type,
  21. use_default,
  22. typename property_traits<LvaluePropertyMap>::reference>
  23. {
  24. friend class boost::iterator_core_access;
  25. typedef iterator_adaptor< lvalue_pmap_iter< Iterator, LvaluePropertyMap >,
  26. Iterator,
  27. typename property_traits<LvaluePropertyMap>::value_type,
  28. use_default,
  29. typename property_traits<LvaluePropertyMap>::reference> super_t;
  30. public:
  31. lvalue_pmap_iter() { }
  32. lvalue_pmap_iter(Iterator const& it,
  33. LvaluePropertyMap m)
  34. : super_t(it),
  35. m_map(m) {}
  36. private:
  37. typename super_t::reference
  38. dereference() const
  39. {
  40. return m_map[*(this->base_reference())];
  41. }
  42. LvaluePropertyMap m_map;
  43. };
  44. template <class Iterator, class ReadablePropertyMap>
  45. class readable_pmap_iter :
  46. public iterator_adaptor< readable_pmap_iter< Iterator, ReadablePropertyMap >,
  47. Iterator,
  48. typename property_traits<ReadablePropertyMap>::value_type,
  49. use_default,
  50. typename property_traits<ReadablePropertyMap>::value_type>
  51. {
  52. friend class boost::iterator_core_access;
  53. typedef iterator_adaptor< readable_pmap_iter< Iterator, ReadablePropertyMap >,
  54. Iterator,
  55. typename property_traits<ReadablePropertyMap>::value_type,
  56. use_default,
  57. typename property_traits<ReadablePropertyMap>::value_type> super_t;
  58. public:
  59. readable_pmap_iter() { }
  60. readable_pmap_iter(Iterator const& it,
  61. ReadablePropertyMap m)
  62. : super_t(it),
  63. m_map(m) {}
  64. private:
  65. typename super_t::reference
  66. dereference() const
  67. {
  68. return get(m_map, *(this->base_reference()));
  69. }
  70. ReadablePropertyMap m_map;
  71. };
  72. } // namespace detail
  73. template <class PropertyMap, class Iterator>
  74. struct property_map_iterator_generator :
  75. mpl::if_< is_same< typename property_traits<PropertyMap>::category, lvalue_property_map_tag>,
  76. detail::lvalue_pmap_iter<Iterator, PropertyMap>,
  77. detail::readable_pmap_iter<Iterator, PropertyMap> >
  78. {};
  79. template <class PropertyMap, class Iterator>
  80. typename property_map_iterator_generator<PropertyMap, Iterator>::type
  81. make_property_map_iterator(PropertyMap pmap, Iterator iter)
  82. {
  83. typedef typename property_map_iterator_generator<PropertyMap,
  84. Iterator>::type Iter;
  85. return Iter(iter, pmap);
  86. }
  87. } // namespace boost
  88. #endif // BOOST_PROPERTY_MAP_ITERATOR_HPP