transform_value_property_map.hpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. //
  2. //=======================================================================
  3. // Author: Philipp Moeller
  4. //
  5. // Copyright 2012, Philipp Moeller
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See
  8. // accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. //=======================================================================
  11. //
  12. #ifndef BOOST_PROPERTY_MAP_TRANSFORM_VALUE_PROPERTY_MAP_HPP
  13. #define BOOST_PROPERTY_MAP_TRANSFORM_VALUE_PROPERTY_MAP_HPP
  14. #include <boost/config.hpp>
  15. #include <boost/property_map/property_map.hpp>
  16. #include <boost/type_traits.hpp>
  17. #include <boost/utility/result_of.hpp>
  18. #include <boost/mpl/and.hpp>
  19. #include <boost/mpl/not.hpp>
  20. #include <utility>
  21. namespace boost {
  22. template<typename Func, typename PM, typename Ret = typename boost::result_of<const Func(typename property_traits<PM>::reference)>::type>
  23. class transform_value_property_map: public put_get_helper<Ret, transform_value_property_map<Func, PM, Ret> > {
  24. public:
  25. typedef typename property_traits<PM>::key_type key_type;
  26. typedef Ret reference;
  27. typedef typename boost::remove_cv<typename boost::remove_reference<Ret>::type>::type value_type;
  28. typedef typename boost::mpl::if_<
  29. boost::mpl::and_<
  30. boost::is_reference<Ret>,
  31. boost::mpl::not_<boost::is_const<Ret> >
  32. >,
  33. boost::lvalue_property_map_tag,
  34. boost::readable_property_map_tag>::type
  35. category;
  36. transform_value_property_map(Func f, PM pm) : f(f), pm(pm) {}
  37. reference operator[](const key_type& k) const {
  38. return f(get(pm, k));
  39. }
  40. private:
  41. Func f;
  42. PM pm;
  43. };
  44. template<typename PM, typename Func>
  45. transform_value_property_map<Func, PM>
  46. make_transform_value_property_map(const Func& f, const PM& pm) {
  47. return transform_value_property_map<Func, PM>(f, pm);
  48. }
  49. template<typename Ret, typename PM, typename Func>
  50. transform_value_property_map<Func, PM, Ret>
  51. make_transform_value_property_map(const Func& f, const PM& pm) {
  52. return transform_value_property_map<Func, PM, Ret>(f, pm);
  53. }
  54. } // boost
  55. #endif /* BOOST_PROPERTY_MAP_TRANSFORM_VALUE_PROPERTY_MAP_HPP */