2
0
mirror of https://github.com/boostorg/lambda2.git synced 2026-01-19 16:22:20 +00:00

Compare commits

...

5 Commits

Author SHA1 Message Date
Peter Dimov
f3a3810be4 Add compound assignment operators 2021-06-27 18:41:35 +03:00
Peter Dimov
04367ac3e5 Add operator[] to placeholders 2021-06-27 17:05:54 +03:00
Peter Dimov
07c965a088 Add operator() to placeholders 2021-06-27 12:16:17 +03:00
Peter Dimov
bfc742d854 Add operator++, operator-- 2021-06-27 11:30:02 +03:00
Peter Dimov
a01d4473ab Rename BOOST_LAMBDA2_PREFIX_FN to BOOST_LAMBDA2_UNARY_FN 2021-06-27 11:22:06 +03:00
6 changed files with 185 additions and 3 deletions

View File

@@ -8,6 +8,8 @@
#include <functional>
#include <type_traits>
#include <utility>
#include <tuple>
#include <cstddef>
// Same format as BOOST_VERSION:
// major * 100000 + minor * 100 + patch
@@ -18,10 +20,32 @@ namespace boost
namespace lambda2
{
namespace lambda2_detail
{
struct subscript
{
template<class T1, class T2> decltype(auto) operator()(T1&& t1, T2&& t2) const
{
return std::forward<T1>(t1)[ std::forward<T2>(t2) ];
}
};
} // namespace lambda2_detail
// placeholders
template<int I> struct lambda2_arg
{
template<class... A> decltype(auto) operator()( A&&... a ) const noexcept
{
return std::get<std::size_t{I-1}>( std::tuple<A&&...>( std::forward<A>(a)... ) );
}
template<class T> auto operator[]( T&& t ) const
{
return std::bind( lambda2_detail::subscript(), *this, std::forward<T>( t ) );
}
};
#if defined(__cpp_inline_variables) && __cpp_inline_variables >= 201606L
@@ -64,7 +88,7 @@ namespace lambda2_detail
// additional function objects
#define BOOST_LAMBDA2_PREFIX_FN(op, fn) \
#define BOOST_LAMBDA2_UNARY_FN(op, fn) \
struct fn \
{ \
template<class T> decltype(auto) operator()(T&& t) const \
@@ -94,8 +118,25 @@ namespace lambda2_detail
BOOST_LAMBDA2_BINARY_FN(<<, left_shift)
BOOST_LAMBDA2_BINARY_FN(>>, right_shift)
BOOST_LAMBDA2_PREFIX_FN(+, unary_plus)
BOOST_LAMBDA2_PREFIX_FN(*, dereference)
BOOST_LAMBDA2_UNARY_FN(+, unary_plus)
BOOST_LAMBDA2_UNARY_FN(*, dereference)
BOOST_LAMBDA2_UNARY_FN(++, increment)
BOOST_LAMBDA2_UNARY_FN(--, decrement)
BOOST_LAMBDA2_POSTFIX_FN(++, postfix_increment)
BOOST_LAMBDA2_POSTFIX_FN(--, postfix_decrement)
BOOST_LAMBDA2_BINARY_FN(+=, plus_equal)
BOOST_LAMBDA2_BINARY_FN(-=, minus_equal)
BOOST_LAMBDA2_BINARY_FN(*=, multiplies_equal)
BOOST_LAMBDA2_BINARY_FN(/=, divides_equal)
BOOST_LAMBDA2_BINARY_FN(%=, modulus_equal)
BOOST_LAMBDA2_BINARY_FN(&=, bit_and_equal)
BOOST_LAMBDA2_BINARY_FN(|=, bit_or_equal)
BOOST_LAMBDA2_BINARY_FN(^=, bit_xor_equal)
BOOST_LAMBDA2_BINARY_FN(<<=, left_shift_equal)
BOOST_LAMBDA2_BINARY_FN(>>=, right_shift_equal)
// operators
@@ -119,6 +160,13 @@ template<class A, class B> using enable_binary_lambda =
return std::bind( fn(), std::forward<A>(a) ); \
}
#define BOOST_LAMBDA2_POSTFIX_LAMBDA(op, fn) \
template<class A, class = lambda2_detail::enable_unary_lambda<A>> \
auto operator op( A&& a, int ) \
{ \
return std::bind( fn(), std::forward<A>(a) ); \
}
#define BOOST_LAMBDA2_BINARY_LAMBDA(op, fn) \
template<class A, class B, class = lambda2_detail::enable_binary_lambda<A, B>> \
auto operator op( A&& a, B&& b ) \
@@ -159,6 +207,25 @@ BOOST_LAMBDA2_BINARY_LAMBDA(>>, lambda2_detail::right_shift)
BOOST_LAMBDA2_UNARY_LAMBDA(+, lambda2_detail::unary_plus)
BOOST_LAMBDA2_UNARY_LAMBDA(*, lambda2_detail::dereference)
BOOST_LAMBDA2_UNARY_LAMBDA(++, lambda2_detail::increment)
BOOST_LAMBDA2_UNARY_LAMBDA(--, lambda2_detail::decrement)
BOOST_LAMBDA2_POSTFIX_LAMBDA(++, lambda2_detail::postfix_increment)
BOOST_LAMBDA2_POSTFIX_LAMBDA(--, lambda2_detail::postfix_decrement)
// compound assignment
BOOST_LAMBDA2_BINARY_LAMBDA(+=, lambda2_detail::plus_equal)
BOOST_LAMBDA2_BINARY_LAMBDA(-=, lambda2_detail::minus_equal)
BOOST_LAMBDA2_BINARY_LAMBDA(*=, lambda2_detail::multiplies_equal)
BOOST_LAMBDA2_BINARY_LAMBDA(/=, lambda2_detail::divides_equal)
BOOST_LAMBDA2_BINARY_LAMBDA(%=, lambda2_detail::modulus_equal)
BOOST_LAMBDA2_BINARY_LAMBDA(&=, lambda2_detail::bit_and_equal)
BOOST_LAMBDA2_BINARY_LAMBDA(|=, lambda2_detail::bit_or_equal)
BOOST_LAMBDA2_BINARY_LAMBDA(^=, lambda2_detail::bit_xor_equal)
BOOST_LAMBDA2_BINARY_LAMBDA(<<=, lambda2_detail::left_shift_equal)
BOOST_LAMBDA2_BINARY_LAMBDA(>>=, lambda2_detail::right_shift_equal)
} // namespace lambda2
} // namespace boost

View File

@@ -21,3 +21,6 @@ run version.cpp ;
run lookup_problem.cpp ;
run dereference.cpp ;
run placeholders.cpp ;
run increment.cpp ;
run subscript.cpp ;
run compound.cpp ;

32
test/compound.cpp Normal file
View File

@@ -0,0 +1,32 @@
// Copyright 2021 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/lambda2.hpp>
#include <boost/core/lightweight_test.hpp>
#include <functional>
#define TEST_COMPOUND(Op) \
BOOST_TEST_EQ( (_1 Op 1)(x1), y1 Op 1 ); \
BOOST_TEST_EQ( (_1 Op _2)(x1, x2), y1 Op y2 ); \
BOOST_TEST_EQ( ((_1 Op _2) Op _3)(x1, x2, x3), (y1 Op y2) Op y3 );
int main()
{
using namespace boost::lambda2;
int x1 = 1, x2 = 2, x3 = 3, y1 = 1, y2 = 2, y3 = 3;
TEST_COMPOUND(+=)
TEST_COMPOUND(-=)
TEST_COMPOUND(*=)
TEST_COMPOUND(/=)
TEST_COMPOUND(%=)
TEST_COMPOUND(&=)
TEST_COMPOUND(|=)
TEST_COMPOUND(^=)
TEST_COMPOUND(<<=)
TEST_COMPOUND(>>=)
return boost::report_errors();
}

34
test/increment.cpp Normal file
View File

@@ -0,0 +1,34 @@
// Copyright 2021 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/lambda2.hpp>
#include <boost/core/lightweight_test.hpp>
#include <functional>
int main()
{
using namespace boost::lambda2;
int x1 = 1, x2 = 2, y1 = 1, y2 = 2;
BOOST_TEST_EQ( (_1++)(x1), y1++ );
BOOST_TEST_EQ( (++_1)(x1), ++y1 );
BOOST_TEST_EQ( (_1--)(x1), y1-- );
BOOST_TEST_EQ( (--_1)(x1), --y1 );
BOOST_TEST_EQ( (_1++ + _2++)(x1, x2), y1++ + y2++ );
BOOST_TEST_EQ( (++_1 + ++_2)(x1, x2), ++y1 + ++y2 );
BOOST_TEST_EQ( (_1-- + _2--)(x1, x2), y1-- + y2-- );
BOOST_TEST_EQ( (--_1 + --_2)(x1, x2), --y1 + --y2 );
BOOST_TEST_EQ( std::bind(_1 + _2, _1++, _2++)(x1, x2), y1++ + y2++ );
BOOST_TEST_EQ( std::bind(_1 + _2, ++_1, ++_2)(x1, x2), ++y1 + ++y2 );
BOOST_TEST_EQ( std::bind(_1 + _2, _1--, _2--)(x1, x2), y1-- + y2-- );
BOOST_TEST_EQ( std::bind(_1 + _2, --_1, --_2)(x1, x2), --y1 + --y2 );
return boost::report_errors();
}

View File

@@ -25,5 +25,24 @@ int main()
BOOST_TEST_EQ( std::bind(f, _8)( 1, 2, 3, 4, 5, 6, 7, 8 ), 8 );
BOOST_TEST_EQ( std::bind(f, _9)( 1, 2, 3, 4, 5, 6, 7, 8, 9 ), 9 );
BOOST_TEST_EQ( _1( 1 ), 1 );
BOOST_TEST_EQ( _2( 1, 2 ), 2 );
BOOST_TEST_EQ( _3( 1, 2, 3 ), 3 );
BOOST_TEST_EQ( _4( 1, 2, 3, 4 ), 4 );
BOOST_TEST_EQ( _5( 1, 2, 3, 4, 5 ), 5 );
BOOST_TEST_EQ( _6( 1, 2, 3, 4, 5, 6 ), 6 );
BOOST_TEST_EQ( _7( 1, 2, 3, 4, 5, 6, 7 ), 7 );
BOOST_TEST_EQ( _8( 1, 2, 3, 4, 5, 6, 7, 8 ), 8 );
BOOST_TEST_EQ( _9( 1, 2, 3, 4, 5, 6, 7, 8, 9 ), 9 );
BOOST_TEST_EQ( _1( 1, 2, 3, 4, 5, 6, 7, 8, 9 ), 1 );
BOOST_TEST_EQ( _2( 1, 2, 3, 4, 5, 6, 7, 8, 9 ), 2 );
BOOST_TEST_EQ( _3( 1, 2, 3, 4, 5, 6, 7, 8, 9 ), 3 );
BOOST_TEST_EQ( _4( 1, 2, 3, 4, 5, 6, 7, 8, 9 ), 4 );
BOOST_TEST_EQ( _5( 1, 2, 3, 4, 5, 6, 7, 8, 9 ), 5 );
BOOST_TEST_EQ( _6( 1, 2, 3, 4, 5, 6, 7, 8, 9 ), 6 );
BOOST_TEST_EQ( _7( 1, 2, 3, 4, 5, 6, 7, 8, 9 ), 7 );
BOOST_TEST_EQ( _8( 1, 2, 3, 4, 5, 6, 7, 8, 9 ), 8 );
return boost::report_errors();
}

27
test/subscript.cpp Normal file
View File

@@ -0,0 +1,27 @@
// Copyright 2021 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/lambda2.hpp>
#include <boost/core/lightweight_test.hpp>
#include <functional>
int main()
{
using namespace boost::lambda2;
int x[] = { 1, 2, 3, 4 };
BOOST_TEST_EQ( _1[0](x), x[0] );
BOOST_TEST_EQ( _1[_2](x, 1), x[1] );
BOOST_TEST_EQ( (_1[_2] + _3[_4])(x, 2, x, 3), x[2] + x[3] );
BOOST_TEST_EQ( std::bind(_1 + _2, _1[_2], _3[_4])(x, 2, x, 3), x[2] + x[3] );
_1[0](x) = 7;
BOOST_TEST_EQ( x[0], 7 );
_1[_2](x, 1) = 8;
BOOST_TEST_EQ( x[1], 8 );
return boost::report_errors();
}