Added contains() and function_equal()

[SVN r22483]
This commit is contained in:
Douglas Gregor
2004-03-12 03:38:20 +00:00
parent e80a00545c
commit 2c0e633307
5 changed files with 111 additions and 15 deletions

View File

@ -106,6 +106,18 @@
<throws><simpara>Will not throw.</simpara></throws> <throws><simpara>Will not throw.</simpara></throws>
</overloaded-method> </overloaded-method>
<method name="contains" cv="const">
<template>
<template-type-parameter name="Functor"/>
</template>
<type>bool</type>
<parameter name="f">
<paramtype>const Functor&amp;</paramtype>
</parameter>
<returns><simpara><code>true</code> if <code>this-&gt;<methodname>target</methodname>&lt;Functor&gt;()</code> is non-NULL and <code><functionname>function_equal</functionname>(*(this-&gt;target&lt;Functor&gt;()), f)</code></simpara></returns>
</method>
</method-group> </method-group>
</class> </class>
@ -262,6 +274,18 @@
<throws><simpara>Will not throw.</simpara></throws> <throws><simpara>Will not throw.</simpara></throws>
</overloaded-method> </overloaded-method>
<method name="contains" cv="const">
<template>
<template-type-parameter name="Functor"/>
</template>
<type>bool</type>
<parameter name="f">
<paramtype>const Functor&amp;</paramtype>
</parameter>
<returns><simpara><code>true</code> if <code>this-&gt;<methodname>target</methodname>&lt;Functor&gt;()</code> is non-NULL and <code><functionname>function_equal</functionname>(*(this-&gt;target&lt;Functor&gt;()), f)</code></simpara></returns>
</method>
</method-group> </method-group>
<method-group name="invocation"> <method-group name="invocation">
@ -377,8 +401,9 @@
<listitem><simpara><code>g</code> is not of type <listitem><simpara><code>g</code> is not of type
<code><classname>reference_wrapper</classname>&lt;Functor&gt;</code> <code><classname>reference_wrapper</classname>&lt;Functor&gt;</code>
and <code>*(f.target&lt;Functor&gt;()) == and
g</code>.</simpara></listitem> <code><functionname>function_equal</functionname>(*(f.target&lt;Functor&gt;()),
g)</code>.</simpara></listitem>
</itemizedlist> </itemizedlist>
</simpara></returns> </simpara></returns>
@ -479,8 +504,7 @@
<listitem><simpara><code>g</code> is not of type <listitem><simpara><code>g</code> is not of type
<code><classname>reference_wrapper</classname>&lt;Functor&gt;</code> <code><classname>reference_wrapper</classname>&lt;Functor&gt;</code>
and <code>*(f.target&lt;Functor&gt;()) != and <code>!<functionname>function_equal</functionname>(*(f.target&lt;Functor&gt;()), g)</code>.</simpara></listitem>
g</code>.</simpara></listitem>
</itemizedlist> </itemizedlist>
</simpara></returns> </simpara></returns>
@ -672,6 +696,18 @@
pointer.</simpara></returns> pointer.</simpara></returns>
<throws><simpara>Will not throw.</simpara></throws> <throws><simpara>Will not throw.</simpara></throws>
</overloaded-method> </overloaded-method>
<method name="contains" cv="const">
<template>
<template-type-parameter name="Functor"/>
</template>
<type>bool</type>
<parameter name="f">
<paramtype>const Functor&amp;</paramtype>
</parameter>
<returns><simpara><code>true</code> if <code>this-&gt;<methodname>target</methodname>&lt;Functor&gt;()</code> is non-NULL and <code><functionname>function_equal</functionname>(*(this-&gt;target&lt;Functor&gt;()), f)</code></simpara></returns>
</method>
</method-group> </method-group>
<method-group name="invocation"> <method-group name="invocation">
@ -766,8 +802,7 @@
<listitem><simpara><code>g</code> is not of type <listitem><simpara><code>g</code> is not of type
<code><classname>reference_wrapper</classname>&lt;Functor&gt;</code> <code><classname>reference_wrapper</classname>&lt;Functor&gt;</code>
and <code>*(f.target&lt;Functor&gt;()) == and <code><functionname>function_equals</functionname>(*(f.target&lt;Functor&gt;()), g)</code>.</simpara></listitem>
g</code>.</simpara></listitem>
</itemizedlist> </itemizedlist>
</simpara></returns> </simpara></returns>
@ -850,8 +885,7 @@
<listitem><simpara><code>g</code> is not of type <listitem><simpara><code>g</code> is not of type
<code><classname>reference_wrapper</classname>&lt;Functor&gt;</code> <code><classname>reference_wrapper</classname>&lt;Functor&gt;</code>
and <code>*(f.target&lt;Functor&gt;()) != and <code>!<functionname>function_equals</functionname>(*(f.target&lt;Functor&gt;()), g)</code>.</simpara></listitem>
g</code>.</simpara></listitem>
</itemizedlist> </itemizedlist>
</simpara></returns> </simpara></returns>
@ -871,4 +905,25 @@
</class> </class>
</namespace> </namespace>
</header> </header>
<header name="boost/function_equal.hpp">
<namespace name="boost">
<function name="function_equal">
<template>
<template-type-parameter name="F"/>
<template-type-parameter name="G"/>
</template>
<type>bool</type>
<parameter name="f">
<paramtype>const F&amp;</paramtype>
</parameter>
<parameter name="g">
<paramtype>const G&amp;</paramtype>
</parameter>
<purpose><simpara>Compare two function objects for equality.</simpara></purpose>
<returns><simpara><code>f == g</code>.</simpara></returns>
<throws><simpara>Only if <code>f == g</code> throws.</simpara></throws>
</function>
</namespace>
</header>
</library-reference> </library-reference>

View File

@ -1,6 +1,6 @@
// Boost.Function library // Boost.Function library
// Copyright Doug Gregor 2001-2003. Use, modification and // Copyright Doug Gregor 2001-2004. Use, modification and
// distribution is subject to the Boost Software License, Version // distribution is subject to the Boost Software License, Version
// 1.0. (See accompanying file LICENSE_1_0.txt or copy at // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt) // http://www.boost.org/LICENSE_1_0.txt)
@ -28,6 +28,7 @@
#else #else
# include "boost/mpl/bool.hpp" # include "boost/mpl/bool.hpp"
#endif #endif
#include <boost/function_equal.hpp>
// Borrowed from Boost.Python library: determines the cases where we // Borrowed from Boost.Python library: determines the cases where we
// need to use std::type_info::name to compare instead of operator==. // need to use std::type_info::name to compare instead of operator==.
@ -336,7 +337,7 @@ namespace boost {
mpl::bool_<false>) mpl::bool_<false>)
{ {
if (const Functor* fp = f.template target<Functor>()) if (const Functor* fp = f.template target<Functor>())
return *fp == g; return function_equal(*fp, g);
else return false; else return false;
} }
@ -356,7 +357,7 @@ namespace boost {
mpl::bool_<false>) mpl::bool_<false>)
{ {
if (const Functor* fp = f.template target<Functor>()) if (const Functor* fp = f.template target<Functor>())
return *fp != g; return !function_equal(*fp, g);
else return true; else return true;
} }
@ -421,6 +422,16 @@ public:
} }
} }
template<typename F>
bool contains(const F& f) const
{
if (const F* fp = this->template target<F>()) {
return function_equal(*fp, f);
} else {
return false;
}
}
public: // should be protected, but GCC 2.95.3 will fail to allow access public: // should be protected, but GCC 2.95.3 will fail to allow access
detail::function::any_pointer (*manager)( detail::function::any_pointer (*manager)(
detail::function::any_pointer, detail::function::any_pointer,

View File

@ -648,7 +648,8 @@ template<typename R BOOST_FUNCTION_COMMA BOOST_FUNCTION_TEMPLATE_PARMS ,
Allocator>& f, Allocator>& f,
Functor g) Functor g)
{ {
if (const Functor* fp = f.template target<Functor>()) return *fp == g; if (const Functor* fp = f.template target<Functor>())
return function_equal(*fp, g);
else return false; else return false;
} }
@ -661,7 +662,8 @@ template<typename R BOOST_FUNCTION_COMMA BOOST_FUNCTION_TEMPLATE_PARMS ,
BOOST_FUNCTION_TEMPLATE_ARGS , BOOST_FUNCTION_TEMPLATE_ARGS ,
Allocator>& f) Allocator>& f)
{ {
if (const Functor* fp = f.template target<Functor>()) return g == *fp; if (const Functor* fp = f.template target<Functor>())
return function_equal(g, *fp);
else return false; else return false;
} }
@ -674,7 +676,8 @@ template<typename R BOOST_FUNCTION_COMMA BOOST_FUNCTION_TEMPLATE_PARMS ,
Allocator>& f, Allocator>& f,
Functor g) Functor g)
{ {
if (const Functor* fp = f.template target<Functor>()) return *fp != g; if (const Functor* fp = f.template target<Functor>())
return !function_equal(*fp, g);
else return true; else return true;
} }
@ -687,7 +690,8 @@ template<typename R BOOST_FUNCTION_COMMA BOOST_FUNCTION_TEMPLATE_PARMS ,
BOOST_FUNCTION_TEMPLATE_ARGS , BOOST_FUNCTION_TEMPLATE_ARGS ,
Allocator>& f) Allocator>& f)
{ {
if (const Functor* fp = f.template target<Functor>()) return g != *fp; if (const Functor* fp = f.template target<Functor>())
return !function_equal(g, *fp);
else return true; else return true;
} }

View File

@ -0,0 +1,24 @@
// Copyright Doug Gregor 2004. Use, modification and
// distribution is subject to 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)
// For more information, see http://www.boost.org
#ifndef BOOST_FUNCTION_EQUAL_HPP
#define BOOST_FUNCTION_EQUAL_HPP
namespace boost {
namespace detail {
template<typename F, typename G>
bool function_equal_impl(const F& f, const G& g, long)
{ return f == g; }
} // end namespace boost::function
template<typename F, typename G>
bool function_equal(const F& f, const G& g)
{ return ::boost::detail::function_equal_impl(f, g, 0); }
} // end namespace boost
#endif // BOOST_FUNCTION_EQUAL_HPP

View File

@ -59,6 +59,7 @@ static void equal_test()
BOOST_TEST(&forty_two == f); BOOST_TEST(&forty_two == f);
BOOST_TEST(f != ReturnInt(17)); BOOST_TEST(f != ReturnInt(17));
BOOST_TEST(ReturnInt(17) != f); BOOST_TEST(ReturnInt(17) != f);
BOOST_TEST(f.contains(&forty_two));
f = ReturnInt(17); f = ReturnInt(17);
BOOST_TEST(f != &forty_two); BOOST_TEST(f != &forty_two);
@ -67,6 +68,7 @@ static void equal_test()
BOOST_TEST(ReturnInt(17) == f); BOOST_TEST(ReturnInt(17) == f);
BOOST_TEST(f != ReturnInt(16)); BOOST_TEST(f != ReturnInt(16));
BOOST_TEST(ReturnInt(16) != f); BOOST_TEST(ReturnInt(16) != f);
BOOST_TEST(f.contains(ReturnInt(17)));
#if !defined(BOOST_FUNCTION_NO_FUNCTION_TYPE_SYNTAX) #if !defined(BOOST_FUNCTION_NO_FUNCTION_TYPE_SYNTAX)
boost::function<int(void)> g; boost::function<int(void)> g;