From 7ca2310b15e387258e97e4cd16887f5ee4906b28 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Sun, 3 Sep 2023 17:55:50 +0300 Subject: [PATCH] Support fn.contains(f) where f is a function. Fixes #46. --- include/boost/function/function_base.hpp | 17 +++++++++++- test/Jamfile.v2 | 3 +++ test/contains3_test.cpp | 33 ++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 test/contains3_test.cpp diff --git a/include/boost/function/function_base.hpp b/include/boost/function/function_base.hpp index 5693e11e..00c7ce8e 100644 --- a/include/boost/function/function_base.hpp +++ b/include/boost/function/function_base.hpp @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -652,7 +653,8 @@ class function_base } template - bool contains(const F& f) const + typename boost::enable_if_< !boost::is_function::value, bool >::type + contains(const F& f) const { if (const F* fp = this->template target()) { @@ -662,6 +664,19 @@ class function_base } } + template + typename boost::enable_if_< boost::is_function::value, bool >::type + contains(Fn& f) const + { + typedef Fn* F; + if (const F* fp = this->template target()) + { + return function_equal(*fp, &f); + } else { + return false; + } + } + #if defined(__GNUC__) && __GNUC__ == 3 && __GNUC_MINOR__ <= 3 // GCC 3.3 and newer cannot copy with the global operator==, due to // problems with instantiation of function return types before it diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 15d401ea..7be87a11 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -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 run contains_test.cpp : : : off gcc-4.4,0x:no : contains_test_no_rtti ; run contains2_test.cpp : : : off gcc-4.4,0x:no : contains2_test_no_rtti ; + +run contains3_test.cpp ; +run contains3_test.cpp : : : off gcc-4.4,0x:no : contains3_test_no_rtti ; diff --git a/test/contains3_test.cpp b/test/contains3_test.cpp new file mode 100644 index 00000000..e6130bbe --- /dev/null +++ b/test/contains3_test.cpp @@ -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 +#include + +static int f() +{ + return 1; +} + +static int g() +{ + return 2; +} + +int main() +{ + { + boost::function fn; + BOOST_TEST( !fn.contains( f ) ); + BOOST_TEST( !fn.contains( g ) ); + } + + { + boost::function fn( f ); + BOOST_TEST( fn.contains( f ) ); + BOOST_TEST( !fn.contains( g ) ); + } + + return boost::report_errors(); +}