Ticket #6728: function_property_map.hpp

File function_property_map.hpp, 2.0 KB (added by Jeremiah Willcock, 11 years ago)
Line 
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_H
14#define BOOST_PROPERTY_MAP_FUNCTION_PROPERTY_MAP_H
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
24namespace boost {
25
26template<typename Func, typename Key, typename Ret = typename boost::result_of<Func(Key)>::type>
27class 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
42 function_property_map(Func f = Func()) : f(f) {}
43
44 reference operator[](const Key& k) const {
45 return f(k);
46 }
47
48 reference operator[](const Key& k) {
49 return f(k);
50 }
51
52 private:
53 Func f;
54};
55
56template<typename Key, typename Func>
57function_property_map<Func, Key>
58make_function_property_map(const Func& f) {
59 return function_property_map<Func, Key>(f);
60}
61
62template<typename Key, typename Ret, typename Func>
63function_property_map<Func, Key, Ret>
64make_function_property_map(const Func& f) {
65 return function_property_map<Func, Key, Ret>(f);
66}
67
68} // boost
69
70#endif /* BOOST_PROPERTY_MAP_FUNCTION_PROPERTY_MAP_H */