|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r77506 - in trunk: boost/property_map libs/property_map/doc libs/property_map/test
From: jewillco_at_[hidden]
Date: 2012-03-23 15:35:53
Author: jewillco
Date: 2012-03-23 15:35:52 EDT (Fri, 23 Mar 2012)
New Revision: 77506
URL: http://svn.boost.org/trac/boost/changeset/77506
Log:
Added function_property_map based on code from Philipp Moeller; fixes #6728
Added:
trunk/boost/property_map/function_property_map.hpp (contents, props changed)
trunk/libs/property_map/doc/function_property_map.html (contents, props changed)
trunk/libs/property_map/test/function_property_map_test.cpp (contents, props changed)
Text files modified:
trunk/libs/property_map/doc/property_map.html | 5 +++--
trunk/libs/property_map/test/Jamfile.v2 | 1 +
2 files changed, 4 insertions(+), 2 deletions(-)
Added: trunk/boost/property_map/function_property_map.hpp
==============================================================================
--- (empty file)
+++ trunk/boost/property_map/function_property_map.hpp 2012-03-23 15:35:52 EDT (Fri, 23 Mar 2012)
@@ -0,0 +1,70 @@
+//
+//=======================================================================
+// Author: Philipp Moeller
+//
+// Copyright 2012, Philipp Moeller
+//
+// Distributed under the Boost Software License, Version 1.0. (See
+// accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt)
+//=======================================================================
+//
+
+#ifndef BOOST_PROPERTY_MAP_FUNCTION_PROPERTY_MAP_H
+#define BOOST_PROPERTY_MAP_FUNCTION_PROPERTY_MAP_H
+
+#include <boost/config.hpp>
+#include <boost/property_map/property_map.hpp>
+#include <boost/type_traits.hpp>
+#include <boost/utility/result_of.hpp>
+#include <boost/mpl/and.hpp>
+#include <boost/mpl/not.hpp>
+#include <utility>
+
+namespace boost {
+
+template<typename Func, typename Key, typename Ret = typename boost::result_of<Func(Key)>::type>
+class function_property_map: public put_get_helper<Ret, function_property_map<Func, Key, Ret> > {
+ public:
+ typedef Key key_type;
+ typedef Ret reference;
+ typedef typename boost::remove_cv<typename boost::remove_reference<Ret>::type>::type value_type;
+
+ typedef typename boost::mpl::if_<
+ boost::mpl::and_<
+ boost::is_reference<Ret>,
+ boost::mpl::not_<boost::is_const<Ret> >
+ >,
+ boost::lvalue_property_map_tag,
+ boost::readable_property_map_tag>::type
+ category;
+
+ function_property_map(Func f = Func()) : f(f) {}
+
+ reference operator[](const Key& k) const {
+ return f(k);
+ }
+
+ reference operator[](const Key& k) {
+ return f(k);
+ }
+
+ private:
+ Func f;
+};
+
+template<typename Key, typename Func>
+function_property_map<Func, Key>
+make_function_property_map(const Func& f) {
+ return function_property_map<Func, Key>(f);
+}
+
+template<typename Key, typename Ret, typename Func>
+function_property_map<Func, Key, Ret>
+make_function_property_map(const Func& f) {
+ return function_property_map<Func, Key, Ret>(f);
+}
+
+} // boost
+
+#endif /* BOOST_PROPERTY_MAP_FUNCTION_PROPERTY_MAP_H */
Added: trunk/libs/property_map/doc/function_property_map.html
==============================================================================
--- (empty file)
+++ trunk/libs/property_map/doc/function_property_map.html 2012-03-23 15:35:52 EDT (Fri, 23 Mar 2012)
@@ -0,0 +1,131 @@
+<HTML>
+<!--
+ Copyright (c) 2012 Trustees of Indiana University
+
+ Distributed under the Boost Software License, Version 1.0.
+ (See accompanying file LICENSE_1_0.txt or copy at
+ http://www.boost.org/LICENSE_1_0.txt)
+ -->
+<Head>
+<Title>Function Property Map Adaptor</Title>
+<BODY BGCOLOR="#ffffff" LINK="#0000ee" TEXT="#000000" VLINK="#551a8b"
+ ALINK="#ff0000">
+<IMG SRC="../../../boost.png"
+ ALT="C++ Boost" width="277" height="86">
+
+<BR Clear>
+
+
+<H2><A NAME="sec:function-property-map"></A>
+</h2>
+<PRE>
+function_property_map<UnaryFunction, Key, Ref>
+</PRE>
+
+<P>
+This property map is an adaptor that converts a function object into either a Readable Property Map or a Lvalue Property Map. The category of the property map is based on whether the function's return type (as given by <tt>Ref</tt>) is a non-const reference type.
+
+<P>
+
+<!-- <h3>Example</h3>
+
+<pre>
+</pre> -->
+
+<H3>Where Defined</H3>
+
+<P>
+boost/property_map/function_property_map.hpp
+
+<p>
+<H3>Model Of</H3>
+
+Readable Property Map or Lvalue Property Map
+
+<P>
+
+<H3>Template Parameters</H3>
+
+<P>
+
+<TABLE border>
+<TR>
+<th>Parameter</th><th>Description</th><th>Default</th>
+</tr>
+
+
+<TR>
+<TD><TT>UnaryFunction</TT></TD>
+<TD>Must be a model of Unary Function that accepts an object of type <tt>Key</tt> as an argument and returns a result of type <tt>Ref</tt>.</TD>
+<TD> </td>
+</tr>
+
+<TR>
+<TD><TT>Key</TT></TD> <TD>The key type of the property map.
+</TD> <TD> </TD>
+</TR>
+
+<TR>
+<TD><TT>Ref</TT></TD>
+<TD>The result type of the function.</TD>
+<TD><TT>boost::result_of<UnaryFunction(Key)>::type</TT></TD>
+</TR>
+
+
+</TABLE>
+<P>
+
+<H3>Members</H3>
+
+<P>
+In addition to the methods and functions required by <a
+href="./ReadablePropertyMap.html">Readable Property Map</a> or Lvalue Property Map, this
+class has the following members:
+
+<hr>
+
+<pre>
+property_traits<function_property_map>::value_type
+</pre>
+The type <tt>Ref</tt> with any reference or cv-qualifiers removed.
+
+<hr>
+
+<pre>
+function_property_map(const UnaryFunction& f = UnaryFunction())
+</pre>
+Constructor.
+
+<hr>
+
+<h3>Non-Member functions</h3>
+
+<hr>
+
+<pre>
+ template <class Key, class UnaryFunction>
+ function_property_map<UnaryFunction, Key>
+ make_function_property_map(const UnaryFunction& f);
+</pre>
+Returns a <tt>function_property_map</tt> using the given function and key type.
+
+<hr>
+
+<pre>
+ template <class Key, class Ref, class UnaryFunction>
+ function_property_map<UnaryFunction, Key, Ref>
+ make_function_property_map(const UnaryFunction& f);
+</pre>
+Returns a <tt>function_property_map</tt> using the given function and key and reference types.
+
+<hr>
+
+<br>
+<HR>
+<TABLE>
+<TR valign=top>
+<TD nowrap>Copyright © 2012</TD><TD>Trustees of Indiana University
+</TD></TR></TABLE>
+
+</BODY>
+</HTML>
Modified: trunk/libs/property_map/doc/property_map.html
==============================================================================
--- trunk/libs/property_map/doc/property_map.html (original)
+++ trunk/libs/property_map/doc/property_map.html 2012-03-23 15:35:52 EDT (Fri, 23 Mar 2012)
@@ -283,7 +283,8 @@
}</PRE>
</li>
- <li>identity_property_map </li>
+ <li>identity_property_map and typed_identity_property_map </li>
+ <li>function_property_map </li>
<li>iterator_property_map</li>
<li>shared_array_property_map</li>
<li>associative_property_map</li>
@@ -319,7 +320,7 @@
<HR>
<TABLE>
<TR valign=top>
-<TD nowrap>Copyright © 2000-2002</TD><TD>
+<TD nowrap>Copyright © 2000-2002</TD><TD>
<a HREF="http://www.boost.org/people/jeremy_siek.htm">Jeremy Siek</a>, Indiana University (<A HREF="mailto:jsiek_at_[hidden]">jsiek_at_[hidden]</A>)
</TD></TR></TABLE>
Modified: trunk/libs/property_map/test/Jamfile.v2
==============================================================================
--- trunk/libs/property_map/test/Jamfile.v2 (original)
+++ trunk/libs/property_map/test/Jamfile.v2 2012-03-23 15:35:52 EDT (Fri, 23 Mar 2012)
@@ -13,4 +13,5 @@
test-suite property_map
: [ compile property_map_cc.cpp ]
[ run dynamic_properties_test.cpp ]
+ [ run function_property_map_test.cpp ]
;
Added: trunk/libs/property_map/test/function_property_map_test.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/property_map/test/function_property_map_test.cpp 2012-03-23 15:35:52 EDT (Fri, 23 Mar 2012)
@@ -0,0 +1,66 @@
+//
+//=======================================================================
+// Author: Jeremiah Willcock
+//
+// Copyright 2012, Trustees of Indiana University
+//
+// Distributed under the Boost Software License, Version 1.0. (See
+// accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt)
+//=======================================================================
+//
+
+#include <boost/property_map/function_property_map.hpp>
+#include <boost/concept_check.hpp>
+#include <boost/property_map/property_map.hpp>
+#include <boost/test/minimal.hpp>
+#include <boost/static_assert.hpp>
+
+template <typename T>
+struct add1 {typedef T result_type; T operator()(const T& x) const {return x + 1;}};
+
+template <typename T>
+struct add1_val {typedef T result_type; T operator()(T x) const {return x + 1;}};
+
+template <typename T>
+struct return_fixed_ref {
+ int* ptr;
+ return_fixed_ref(int* ptr): ptr(ptr) {}
+ typedef int& result_type;
+ int& operator()(const T&) const {return *ptr;}
+};
+
+int test_main(int, char**) {
+ using namespace boost;
+ function_requires<ReadablePropertyMapConcept<function_property_map<add1<int>, int>, int> >();
+ function_requires<ReadablePropertyMapConcept<function_property_map<add1<int>, int, double>, int> >();
+ function_requires<ReadablePropertyMapConcept<function_property_map<add1_val<int>, int>, int> >();
+ function_requires<ReadablePropertyMapConcept<function_property_map<add1_val<int>, int, double>, int> >();
+ function_requires<ReadablePropertyMapConcept<function_property_map<return_fixed_ref<int>, int>, int> >();
+ function_requires<WritablePropertyMapConcept<function_property_map<return_fixed_ref<int>, int>, int> >();
+ function_requires<ReadWritePropertyMapConcept<function_property_map<return_fixed_ref<int>, int>, int> >();
+ function_requires<LvaluePropertyMapConcept<function_property_map<return_fixed_ref<int>, int>, int> >();
+
+ BOOST_STATIC_ASSERT((boost::is_same<typename boost::property_traits<function_property_map<add1<int>, int> >::category, boost::readable_property_map_tag>::value));
+ BOOST_STATIC_ASSERT((boost::is_same<typename boost::property_traits<function_property_map<add1_val<int>, int> >::category, boost::readable_property_map_tag>::value));
+ BOOST_STATIC_ASSERT((boost::is_same<typename boost::property_traits<function_property_map<return_fixed_ref<int>, int> >::category, boost::lvalue_property_map_tag>::value));
+
+ BOOST_CHECK(get(function_property_map<add1<int>, int>(), 3) == 4);
+ BOOST_CHECK(get(function_property_map<add1<int>, int>(add1<int>()), 4) == 5);
+ BOOST_CHECK(get(make_function_property_map<int>(add1<int>()), 5) == 6);
+ BOOST_CHECK(get(function_property_map<add1_val<int>, int>(), 3) == 4);
+ BOOST_CHECK(get(function_property_map<add1_val<int>, int>(add1_val<int>()), 4) == 5);
+ BOOST_CHECK(get(make_function_property_map<int>(add1_val<int>()), 5) == 6);
+ int val;
+ const function_property_map<return_fixed_ref<int>, int> pm = return_fixed_ref<int>((&val));
+ put(pm, 1, 6);
+ BOOST_CHECK(get(pm, 2) == 6);
+ BOOST_CHECK((get(pm, 3) = 7) == 7);
+ BOOST_CHECK(get(pm, 4) == 7);
+ const function_property_map<return_fixed_ref<int>, int> pm2 = pm; // Check shallow copying
+ BOOST_CHECK(get(pm2, 5) == 7);
+ put(pm2, 3, 1);
+ BOOST_CHECK(get(pm, 1) == 1);
+
+ return 0;
+}
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk