Skip to content

Commit

Permalink
Add bind_back
Browse files Browse the repository at this point in the history
  • Loading branch information
pdimov committed Mar 29, 2024
1 parent 28e7155 commit 9a58caf
Show file tree
Hide file tree
Showing 7 changed files with 645 additions and 0 deletions.
72 changes: 72 additions & 0 deletions include/boost/compat/bind_back.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#ifndef BOOST_COMPAT_BIND_BACK_HPP_INCLUDED
#define BOOST_COMPAT_BIND_BACK_HPP_INCLUDED

// Copyright 2024 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#include <boost/compat/invoke.hpp>
#include <boost/compat/type_traits.hpp>
#include <boost/compat/integer_sequence.hpp>
#include <boost/compat/detail/returns.hpp>
#include <boost/config.hpp>
#include <boost/config/workaround.hpp>
#include <tuple>
#include <utility>

namespace boost {
namespace compat {

namespace detail {

#if BOOST_WORKAROUND(BOOST_MSVC, < 1910)
# pragma warning(push)
# pragma warning(disable: 4100) // 'a': unreferenced formal parameter
#endif

template<class F, class A, class... B, std::size_t... I>
static constexpr auto invoke_bind_back_( F&& f, A&& a, index_sequence<I...>, B&&... b )
BOOST_COMPAT_RETURNS( compat::invoke( std::forward<F>(f), std::forward<B>(b)..., std::get<I>( std::forward<A>(a) )... ) )

#if BOOST_WORKAROUND(BOOST_MSVC, < 1910)
# pragma warning(pop)
#endif

template<class F, class... A> class bind_back_
{
private:

F f_;
std::tuple<A...> a_;

public:

template<class F2, class... A2>
constexpr bind_back_( F2&& f2, A2&&... a2 ): f_( std::forward<F2>(f2) ), a_( std::forward<A2>(a2)... ) {}

public:

template<class... B> BOOST_CXX14_CONSTEXPR auto operator()( B&&... b ) &
BOOST_COMPAT_RETURNS( detail::invoke_bind_back_( f_, a_, make_index_sequence<sizeof...(A)>(), std::forward<B>(b)... ) )

template<class... B> constexpr auto operator()( B&&... b ) const &
BOOST_COMPAT_RETURNS( detail::invoke_bind_back_( f_, a_, make_index_sequence<sizeof...(A)>(), std::forward<B>(b)... ) )

template<class... B> BOOST_CXX14_CONSTEXPR auto operator()( B&&... b ) &&
BOOST_COMPAT_RETURNS( detail::invoke_bind_back_( std::move(f_), std::move(a_), make_index_sequence<sizeof...(A)>(), std::forward<B>(b)... ) )

template<class... B> constexpr auto operator()( B&&... b ) const &&
BOOST_COMPAT_RETURNS( detail::invoke_bind_back_( std::move(f_), std::move(a_), make_index_sequence<sizeof...(A)>(), std::forward<B>(b)... ) )
};

} // namespace detail

template<class F, class... A> constexpr auto bind_back( F&& f, A&&... a ) -> detail::bind_back_< decay_t<F>, decay_t<A>... >
{
return { std::forward<F>(f), std::forward<A>(a)... };
}

} // namespace compat
} // namespace boost

#endif // BOOST_COMPAT_BIND_BACK_HPP_INCLUDED
5 changes: 5 additions & 0 deletions test/Jamfile
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,8 @@ compile bind_front_mfn_constexpr_test.cpp ;
compile bind_front_md_constexpr_test.cpp ;

run integer_sequence_test.cpp ;

run bind_back_fn_test.cpp ;
run bind_back_obj_test.cpp ;
run bind_back_mfn_test.cpp ;
run bind_back_md_test.cpp ;
57 changes: 57 additions & 0 deletions test/bind_back_fn_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2024 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#if defined(__GNUC__) && __GNUC__ == 7
# pragma GCC diagnostic ignored "-Wnoexcept-type"
#endif

#include <boost/compat/bind_back.hpp>
#include <boost/core/lightweight_test.hpp>
#include <memory>

int f0()
{
return -1;
}

int f1( int x1 ) noexcept
{
return x1;
}

int f2( int x1, int x2 )
{
return 10*x1+x2;
}

int f3( int x1, int x2, int x3 ) noexcept
{
return 100*x1 + 10*x2 + x3;
}

int g( std::unique_ptr<int> p, std::unique_ptr<int> q )
{
return 10 * *p + *q;
}

int main()
{
BOOST_TEST_EQ( boost::compat::bind_back( f0 )(), -1 );
BOOST_TEST_EQ( boost::compat::bind_back( f1 )( 1 ), 1 );
BOOST_TEST_EQ( boost::compat::bind_back( f2 )( 1, 2 ), 12 );
BOOST_TEST_EQ( boost::compat::bind_back( f3 )( 1, 2, 3 ), 123 );

BOOST_TEST_EQ( boost::compat::bind_back( f1, 1 )(), 1 );
BOOST_TEST_EQ( boost::compat::bind_back( f2, 1 )( 2 ), 21 );
BOOST_TEST_EQ( boost::compat::bind_back( f3, 1 )( 2, 3 ), 231 );

BOOST_TEST_EQ( boost::compat::bind_back( f2, 1, 2 )(), 12 );
BOOST_TEST_EQ( boost::compat::bind_back( f3, 1, 2 )( 3 ), 312 );

BOOST_TEST_EQ( boost::compat::bind_back( f3, 1, 2, 3 )(), 123 );

BOOST_TEST_EQ( boost::compat::bind_back( g, std::unique_ptr<int>( new int(1) ) )( std::unique_ptr<int>( new int(2) ) ), 21 );

return boost::report_errors();
}
101 changes: 101 additions & 0 deletions test/bind_back_md_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright 2024 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#include <boost/compat/bind_back.hpp>
#include <boost/core/lightweight_test.hpp>
#include <functional>

struct X
{
int m = -1;
};

struct Y: public virtual X
{
};

int main()
{
{
BOOST_TEST_EQ( boost::compat::bind_back( &X::m, X() )(), -1 );
}

{
BOOST_TEST_EQ( boost::compat::bind_back( &X::m, Y() )(), -1 );
}

{
X x;

BOOST_TEST_EQ( boost::compat::bind_back( &X::m, x )(), -1 );
}

{
X x;

BOOST_TEST_EQ( boost::compat::bind_back( &X::m, &x )(), -1 );
boost::compat::bind_back( &X::m, &x )() = +1;
BOOST_TEST_EQ( boost::compat::bind_back( &X::m, &x )(), +1 );
}

{
X x;

BOOST_TEST_EQ( boost::compat::bind_back( &X::m, std::ref(x) )(), -1 );
boost::compat::bind_back( &X::m, std::ref(x) )() = +1;
BOOST_TEST_EQ( boost::compat::bind_back( &X::m, std::ref(x) )(), +1 );
}

{
X x;

BOOST_TEST_EQ( boost::compat::bind_back( &X::m, std::cref(x) )(), -1 );
}

{
Y y;

BOOST_TEST_EQ( boost::compat::bind_back( &X::m, y )(), -1 );
}

{
Y y;

BOOST_TEST_EQ( boost::compat::bind_back( &X::m, &y )(), -1 );
boost::compat::bind_back( &X::m, &y )() = +1;
BOOST_TEST_EQ( boost::compat::bind_back( &X::m, &y )(), +1 );
}

{
Y y;

BOOST_TEST_EQ( boost::compat::bind_back( &X::m, std::ref(y) )(), -1 );
boost::compat::bind_back( &X::m, std::ref(y) )() = +1;
BOOST_TEST_EQ( boost::compat::bind_back( &X::m, std::ref(y) )(), +1 );
}

{
Y y;

BOOST_TEST_EQ( boost::compat::bind_back( &X::m, std::cref(y) )(), -1 );
}

{
X const x = {};

BOOST_TEST_EQ( boost::compat::bind_back( &X::m, x )(), -1 );
BOOST_TEST_EQ( boost::compat::bind_back( &X::m, &x )(), -1 );
BOOST_TEST_EQ( boost::compat::bind_back( &X::m, std::ref(x) )(), -1 );
}

{
Y const y = {};

BOOST_TEST_EQ( boost::compat::bind_back( &X::m, y )(), -1 );
BOOST_TEST_EQ( boost::compat::bind_back( &X::m, &y )(), -1 );
BOOST_TEST_EQ( boost::compat::bind_back( &X::m, std::ref(y) )(), -1 );
}

return boost::report_errors();
}
Loading

0 comments on commit 9a58caf

Please sign in to comment.