-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
645 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
Oops, something went wrong.