Support fn.contains(f) where f is a function. Fixes #46.

This commit is contained in:
Peter Dimov
2023-09-03 17:55:50 +03:00
parent 4ecf3e8ad5
commit 7ca2310b15
3 changed files with 52 additions and 1 deletions

View File

@ -25,6 +25,7 @@
#include <boost/type_traits/alignment_of.hpp> #include <boost/type_traits/alignment_of.hpp>
#include <boost/type_traits/enable_if.hpp> #include <boost/type_traits/enable_if.hpp>
#include <boost/type_traits/integral_constant.hpp> #include <boost/type_traits/integral_constant.hpp>
#include <boost/type_traits/is_function.hpp>
#include <boost/assert.hpp> #include <boost/assert.hpp>
#include <boost/config.hpp> #include <boost/config.hpp>
#include <boost/config/workaround.hpp> #include <boost/config/workaround.hpp>
@ -652,7 +653,8 @@ public:
} }
template<typename F> template<typename F>
bool contains(const F& f) const typename boost::enable_if_< !boost::is_function<F>::value, bool >::type
contains(const F& f) const
{ {
if (const F* fp = this->template target<F>()) if (const F* fp = this->template target<F>())
{ {
@ -662,6 +664,19 @@ public:
} }
} }
template<typename Fn>
typename boost::enable_if_< boost::is_function<Fn>::value, bool >::type
contains(Fn& f) const
{
typedef Fn* F;
if (const F* fp = this->template target<F>())
{
return function_equal(*fp, &f);
} else {
return false;
}
}
#if defined(__GNUC__) && __GNUC__ == 3 && __GNUC_MINOR__ <= 3 #if defined(__GNUC__) && __GNUC__ == 3 && __GNUC_MINOR__ <= 3
// GCC 3.3 and newer cannot copy with the global operator==, due to // GCC 3.3 and newer cannot copy with the global operator==, due to
// problems with instantiation of function return types before it // problems with instantiation of function return types before it

View File

@ -90,3 +90,6 @@ run fn_eq_bind_test.cpp ;
# /usr/include/c++/4.4/bits/shared_ptr.h:146: error: cannot use typeid with -fno-rtti # /usr/include/c++/4.4/bits/shared_ptr.h:146: error: cannot use typeid with -fno-rtti
run contains_test.cpp : : : <rtti>off <toolset>gcc-4.4,<cxxstd>0x:<build>no : contains_test_no_rtti ; run contains_test.cpp : : : <rtti>off <toolset>gcc-4.4,<cxxstd>0x:<build>no : contains_test_no_rtti ;
run contains2_test.cpp : : : <rtti>off <toolset>gcc-4.4,<cxxstd>0x:<build>no : contains2_test_no_rtti ; run contains2_test.cpp : : : <rtti>off <toolset>gcc-4.4,<cxxstd>0x:<build>no : contains2_test_no_rtti ;
run contains3_test.cpp ;
run contains3_test.cpp : : : <rtti>off <toolset>gcc-4.4,<cxxstd>0x:<build>no : contains3_test_no_rtti ;

33
test/contains3_test.cpp Normal file
View File

@ -0,0 +1,33 @@
// Copyright 2023 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/function.hpp>
#include <boost/core/lightweight_test.hpp>
static int f()
{
return 1;
}
static int g()
{
return 2;
}
int main()
{
{
boost::function<int()> fn;
BOOST_TEST( !fn.contains( f ) );
BOOST_TEST( !fn.contains( g ) );
}
{
boost::function<int()> fn( f );
BOOST_TEST( fn.contains( f ) );
BOOST_TEST( !fn.contains( g ) );
}
return boost::report_errors();
}