compose_property_map.hpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright (C) 2013 Eurodecision
  2. // Authors: Guillaume Pinot
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // See http://www.boost.org/libs/property_map for documentation.
  9. #ifndef BOOST_PROPERTY_MAP_COMPOSE_PROPERTY_MAP_HPP
  10. #define BOOST_PROPERTY_MAP_COMPOSE_PROPERTY_MAP_HPP
  11. #include <boost/property_map/property_map.hpp>
  12. #include <boost/type_traits.hpp>
  13. namespace boost {
  14. // A compose property map: make_compose_property_map(f, g)[x] == f[g[x]]
  15. //
  16. // g must be a readable property map.
  17. // The category of compose_property_map(f, g) is the category of f.
  18. template <typename FPMap, typename GPMap>
  19. class compose_property_map
  20. {
  21. public:
  22. typedef typename boost::property_traits<FPMap>::category category;
  23. typedef typename boost::property_traits<GPMap>::key_type key_type;
  24. typedef typename boost::property_traits<FPMap>::value_type value_type;
  25. typedef typename boost::property_traits<FPMap>::reference reference;
  26. inline compose_property_map(const FPMap &f_p, const GPMap &g_p):
  27. f(f_p), g(g_p)
  28. {}
  29. inline compose_property_map() {}
  30. inline reference
  31. operator[](const key_type &v) const {
  32. return f[get(g, v)];
  33. }
  34. // return type of get():
  35. // if (reference is not a ref)
  36. // value_type
  37. // else if (reference is const)
  38. // reference
  39. // else
  40. // const value_type&
  41. inline friend typename boost::mpl::if_<
  42. boost::mpl::not_< boost::is_reference<reference> >,
  43. value_type,
  44. typename boost::mpl::if_<
  45. boost::is_const<reference>,
  46. reference,
  47. const value_type&
  48. >::type
  49. >::type
  50. get(const compose_property_map &m, const key_type &k) {
  51. return get(m.f, get(m.g, k));
  52. }
  53. inline friend void
  54. put(const compose_property_map &m, const key_type &k, const value_type &v) {
  55. put(m.f, get(m.g, k), v);
  56. }
  57. private:
  58. FPMap f;
  59. GPMap g;
  60. };
  61. template <class FPMap, class GPMap>
  62. inline compose_property_map<FPMap, GPMap>
  63. make_compose_property_map(const FPMap &f, const GPMap &g) {
  64. return compose_property_map<FPMap, GPMap>(f, g);
  65. }
  66. } // namespace boost
  67. #endif // BOOST_PROPERTY_MAP_COMPOSE_PROPERTY_MAP_HPP