Skip to content

Commit

Permalink
Support fn.contains(f) where f is a function. Fixes #46.
Browse files Browse the repository at this point in the history
  • Loading branch information
pdimov committed Sep 3, 2023
1 parent 4ecf3e8 commit 7ca2310
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
17 changes: 16 additions & 1 deletion include/boost/function/function_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <boost/type_traits/alignment_of.hpp>
#include <boost/type_traits/enable_if.hpp>
#include <boost/type_traits/integral_constant.hpp>
#include <boost/type_traits/is_function.hpp>
#include <boost/assert.hpp>
#include <boost/config.hpp>
#include <boost/config/workaround.hpp>
Expand Down Expand Up @@ -652,7 +653,8 @@ class function_base
}

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>())
{
Expand All @@ -662,6 +664,19 @@ class function_base
}
}

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
// GCC 3.3 and newer cannot copy with the global operator==, due to
// problems with instantiation of function return types before it
Expand Down
3 changes: 3 additions & 0 deletions test/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -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 : : : <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 contains3_test.cpp ;
run contains3_test.cpp : : : <rtti>off <toolset>gcc-4.4,<cxxstd>0x:<build>no : contains3_test_no_rtti ;
33 changes: 33 additions & 0 deletions test/contains3_test.cpp
Original file line number Diff line number Diff line change
@@ -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();
}

0 comments on commit 7ca2310

Please sign in to comment.