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_FUNCTION_PROPERTY_MAP_HPP
14 #define BOOST_PROPERTY_MAP_FUNCTION_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 Key, typename Ret = typename boost::result_of<const Func(const Key&)>::type>
27 class function_property_map: public put_get_helper<Ret, function_property_map<Func, Key, Ret> > {
28 public:
29 typedef Key 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
function_property_map(Func f=Func ())42 function_property_map(Func f = Func()) : f(f) {}
43
operator [](const Key & k) const44 reference operator[](const Key& k) const {
45 return f(k);
46 }
47
48 private:
49 Func f;
50 };
51
52 template<typename Key, typename Func>
53 function_property_map<Func, Key>
make_function_property_map(const Func & f)54 make_function_property_map(const Func& f) {
55 return function_property_map<Func, Key>(f);
56 }
57
58 template<typename Key, typename Ret, typename Func>
59 function_property_map<Func, Key, Ret>
make_function_property_map(const Func & f)60 make_function_property_map(const Func& f) {
61 return function_property_map<Func, Key, Ret>(f);
62 }
63
64 } // boost
65
66 #endif /* BOOST_PROPERTY_MAP_FUNCTION_PROPERTY_MAP_HPP */
67