| 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 | |
| 13 | #ifndef BOOST_PROPERTY_MAP_TRANSFORM_VALUE_PROPERTY_MAP_HPP |
| 14 | #define BOOST_PROPERTY_MAP_TRANSFORM_VALUE_PROPERTY_MAP_HPP |
| 15 | |
| 16 | #include <boost/config.hpp> |
| 17 | #include <boost/property_map/property_map.hpp> |
| 18 | #include <boost/type_traits.hpp> |
| 19 | #include <boost/utility/result_of.hpp> |
| 20 | #include <boost/mpl/and.hpp> |
| 21 | #include <boost/mpl/not.hpp> |
| 22 | #include <utility> |
| 23 | |
| 24 | namespace boost { |
| 25 | |
| 26 | template<typename Func, typename PM, typename Ret = typename boost::result_of<const Func(typename property_traits<PM>::reference)>::type> |
| 27 | class transform_value_property_map: public put_get_helper<Ret, transform_value_property_map<Func, PM, Ret> > { |
| 28 | public: |
| 29 | typedef typename property_traits<PM>::key_type key_type; |
| 30 | typedef Ret reference; |
| 31 | typedef typename boost::remove_cv<typename boost::remove_reference<Ret>::type>::type value_type; |
| 32 | |
| 33 | typedef typename boost::mpl::if_< |
| 34 | boost::mpl::and_< |
| 35 | boost::is_reference<Ret>, |
| 36 | boost::mpl::not_<boost::is_const<Ret> > |
| 37 | >, |
| 38 | boost::lvalue_property_map_tag, |
| 39 | boost::readable_property_map_tag>::type |
| 40 | category; |
| 41 | |
| 42 | transform_value_property_map(Func f, PM pm) : f(f), pm(pm) {} |
| 43 | |
| 44 | reference operator[](const key_type& k) const { |
| 45 | return f(get(pm, k)); |
| 46 | } |
| 47 | |
| 48 | private: |
| 49 | Func f; |
| 50 | PM pm; |
| 51 | }; |
| 52 | |
| 53 | template<typename PM, typename Func> |
| 54 | transform_value_property_map<Func, PM> |
| 55 | make_transform_value_property_map(const Func& f, const PM& pm) { |
| 56 | return transform_value_property_map<Func, PM>(f, pm); |
| 57 | } |
| 58 | |
| 59 | template<typename Ret, typename PM, typename Func> |
| 60 | transform_value_property_map<Func, PM, Ret> |
| 61 | make_transform_value_property_map(const Func& f, const PM& pm) { |
| 62 | return transform_value_property_map<Func, PM, Ret>(f, pm); |
| 63 | } |
| 64 | |
| 65 | } // boost |
| 66 | |
| 67 | #endif /* BOOST_PROPERTY_MAP_TRANSFORM_VALUE_PROPERTY_MAP_HPP */ |
| 68 | |