From a6e5e717fc2cb2dea03ba8105c07ded91daa57b5 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Thu, 21 Mar 2024 19:37:07 +0200 Subject: [PATCH] Add boost::compat::invoke --- include/boost/compat/detail/returns.hpp | 10 +++++++ include/boost/compat/invoke.hpp | 26 ++++++++++++++++++ include/boost/compat/mem_fn.hpp | 18 +++++++++++++ test/Jamfile | 3 +++ test/invoke_fn_test.cpp | 36 +++++++++++++++++++++++++ 5 files changed, 93 insertions(+) create mode 100644 include/boost/compat/detail/returns.hpp create mode 100644 include/boost/compat/invoke.hpp create mode 100644 include/boost/compat/mem_fn.hpp create mode 100644 test/invoke_fn_test.cpp diff --git a/include/boost/compat/detail/returns.hpp b/include/boost/compat/detail/returns.hpp new file mode 100644 index 0000000..4e44974 --- /dev/null +++ b/include/boost/compat/detail/returns.hpp @@ -0,0 +1,10 @@ +#ifndef BOOST_COMPAT_DETAIL_RETURNS_HPP_INCLUDED +#define BOOST_COMPAT_DETAIL_RETURNS_HPP_INCLUDED + +// Copyright 2024 Peter Dimov +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#define BOOST_COMPAT_RETURNS(...) noexcept(noexcept(__VA_ARGS__)) -> decltype(__VA_ARGS__) { return __VA_ARGS__; } + +#endif // BOOST_COMPAT_DETAIL_RETURNS_HPP_INCLUDED diff --git a/include/boost/compat/invoke.hpp b/include/boost/compat/invoke.hpp new file mode 100644 index 0000000..7a67095 --- /dev/null +++ b/include/boost/compat/invoke.hpp @@ -0,0 +1,26 @@ +#ifndef BOOST_COMPAT_INVOKE_HPP_INCLUDED +#define BOOST_COMPAT_INVOKE_HPP_INCLUDED + +// Copyright 2024 Peter Dimov +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#include +#include +#include + +namespace boost { +namespace compat { + +template +auto invoke( F&& f, A&&... a ) +BOOST_COMPAT_RETURNS( std::forward(f)(std::forward(a)...) ) + +template +auto invoke( M T::* pm, A&&... a ) +BOOST_COMPAT_RETURNS( mem_fn(pm)(std::forward(a)...) ) + +} // namespace compat +} // namespace boost + +#endif // BOOST_COMPAT_INVOKE_HPP_INCLUDED diff --git a/include/boost/compat/mem_fn.hpp b/include/boost/compat/mem_fn.hpp new file mode 100644 index 0000000..3044a67 --- /dev/null +++ b/include/boost/compat/mem_fn.hpp @@ -0,0 +1,18 @@ +#ifndef BOOST_COMPAT_MEM_FN_HPP_INCLUDED +#define BOOST_COMPAT_MEM_FN_HPP_INCLUDED + +// Copyright 2024 Peter Dimov +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#include + +namespace boost { +namespace compat { + +using std::mem_fn; + +} // namespace compat +} // namespace boost + +#endif // BOOST_COMPAT_MEM_FN_HPP_INCLUDED diff --git a/test/Jamfile b/test/Jamfile index feb3106..f4fb748 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -25,5 +25,8 @@ project run quick.cpp ; run latch_test.cpp ; + run shared_lock_test.cpp ; run shared_lock_test.cpp : : : off : shared_lock_test_nx ; + +run invoke_fn_test.cpp ; diff --git a/test/invoke_fn_test.cpp b/test/invoke_fn_test.cpp new file mode 100644 index 0000000..3cbc78e --- /dev/null +++ b/test/invoke_fn_test.cpp @@ -0,0 +1,36 @@ +// Copyright 2024 Peter Dimov +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#include +#include + +int f0() +{ + return -1; +} + +int f1( int x1 ) +{ + return x1; +} + +int f2( int x1, int x2 ) +{ + return 10*x1+x2; +} + +int f3( int x1, int x2, int x3 ) +{ + return 100*x1 + 10*x2 + x3; +} + +int main() +{ + BOOST_TEST_EQ( boost::compat::invoke( f0 ), -1 ); + BOOST_TEST_EQ( boost::compat::invoke( f1, 1 ), 1 ); + BOOST_TEST_EQ( boost::compat::invoke( f2, 1, 2 ), 12 ); + BOOST_TEST_EQ( boost::compat::invoke( f3, 1, 2, 3 ), 123 ); + + return boost::report_errors(); +}