function_property_map.hpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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_FUNCTION_PROPERTY_MAP_HPP
  13. #define BOOST_PROPERTY_MAP_FUNCTION_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 Key, typename Ret = typename boost::result_of<const Func(const Key&)>::type>
  23. class function_property_map: public put_get_helper<Ret, function_property_map<Func, Key, Ret> > {
  24. public:
  25. typedef Key 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. function_property_map(Func f = Func()) : f(f) {}
  37. reference operator[](const Key& k) const {
  38. return f(k);
  39. }
  40. private:
  41. Func f;
  42. };
  43. template<typename Key, typename Func>
  44. function_property_map<Func, Key>
  45. make_function_property_map(const Func& f) {
  46. return function_property_map<Func, Key>(f);
  47. }
  48. template<typename Key, typename Ret, typename Func>
  49. function_property_map<Func, Key, Ret>
  50. make_function_property_map(const Func& f) {
  51. return function_property_map<Func, Key, Ret>(f);
  52. }
  53. } // boost
  54. #endif /* BOOST_PROPERTY_MAP_FUNCTION_PROPERTY_MAP_HPP */