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

Compare commits

...

11 Commits

Author SHA1 Message Date
Peter Dimov
9e2e0a31b3 Update copyright 2021-07-11 16:52:18 +03:00
Peter Dimov
93c67a57c8 Update reference 2021-07-11 16:51:40 +03:00
Peter Dimov
3bf3ce3a9e Update copyright 2021-07-11 16:28:39 +03:00
Peter Dimov
51b9448ae7 Update doc/.gitignore 2021-07-11 16:27:09 +03:00
Peter Dimov
a064c3ae83 Remove doc/html/lambda2.html 2021-07-11 16:25:58 +03:00
Peter Dimov
a329989b1e Update overview 2021-07-11 16:25:08 +03:00
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
11 changed files with 371 additions and 1049 deletions

1
doc/.gitignore vendored
View File

@@ -1 +1,2 @@
/pdf/
/html/

File diff suppressed because it is too large Load Diff

View File

@@ -8,5 +8,5 @@ https://www.boost.org/LICENSE_1_0.txt
# Copyright and License
:idprefix:
This documentation is copyright 2020 Peter Dimov and is distributed under
This documentation is copyright 2020, 2021 Peter Dimov and is distributed under
the http://www.boost.org/LICENSE_1_0.txt[Boost Software License, Version 1.0].

View File

@@ -1,5 +1,5 @@
////
Copyright 2020 Peter Dimov
Copyright 2020, 2021 Peter Dimov
Distributed under the Boost Software License, Version 1.0.
https://www.boost.org/LICENSE_1_0.txt
////
@@ -12,16 +12,18 @@ https://www.boost.org/LICENSE_1_0.txt
This is a simple, but functional, {cpp}14 lambda library. It takes
advantage of the fact that the standard `<functional>` header already
provides placeholders `_1`, `_2`, `_3`, and so on, for use with
`std::bind`, and function objects such as `std::plus`, `std::greater`,
`std::logical_not`, and `std::bit_xor`, corresponding to arithmetic,
relational, logical and bitwise operators.
provides `std::bind` customization points (`is_placeholder`,
`is_bind_expression`), and function objects such as `std::plus`,
`std::greater`, `std::logical_not`, and `std::bit_xor`, corresponding
to arithmetic, relational, logical and bitwise operators.
This allows the library to provide a minimal implementation that
still lets expressions such as `_1 + 5`, `_1 % 2 == 0`, `_1 > _2`,
or `_1 == ' ' || _1 == '\t'` to be composed and used as function
objects.
For example, `_1 + 5` is implemented as `std::bind(std::plus<>, _1, 5)`.
These "lambda" expressions can also be freely combined with `std::bind`.
For example, `std::bind( f, _1 ) == std::bind( g, _1 )` and
`std::bind( f, _1 + _2 )` both work and have the expected behavior.
@@ -67,5 +69,5 @@ None. A single, self-contained header.
* Clang 3.5 or later with `-std=c++14` or above
* Visual Studio 2015, 2017, 2019
Tested on https://travis-ci.org/github/pdimov/lambda2[Travis] and
Tested on https://github.com/boostorg/lambda2/actions[Github Actions] and
https://ci.appveyor.com/project/pdimov/lambda2[Appveyor].

View File

@@ -1,5 +1,5 @@
////
Copyright 2020 Peter Dimov
Copyright 2020, 2021 Peter Dimov
Distributed under the Boost Software License, Version 1.0.
https://www.boost.org/LICENSE_1_0.txt
////
@@ -16,6 +16,22 @@ https://www.boost.org/LICENSE_1_0.txt
namespace boost {
namespace lambda2 {
// placeholders
template<int I> struct lambda2_arg;
inline constexpr lambda2_arg<1> _1{};
inline constexpr lambda2_arg<1> _2{};
inline constexpr lambda2_arg<1> _3{};
inline constexpr lambda2_arg<1> _4{};
inline constexpr lambda2_arg<1> _5{};
inline constexpr lambda2_arg<1> _6{};
inline constexpr lambda2_arg<1> _7{};
inline constexpr lambda2_arg<1> _8{};
inline constexpr lambda2_arg<1> _9{};
// arithmetic operators
template<class A, class B> auto operator+( A && a, B && b );
template<class A, class B> auto operator-( A && a, B && b );
template<class A, class B> auto operator*( A && a, B && b );
@@ -23,6 +39,8 @@ template<class A, class B> auto operator/( A && a, B && b );
template<class A, class B> auto operator%( A && a, B && b );
template<class A> auto operator-( A && a );
// relational operators
template<class A, class B> auto operator==( A && a, B && b );
template<class A, class B> auto operator!=( A && a, B && b );
template<class A, class B> auto operator>( A && a, B && b );
@@ -30,19 +48,84 @@ template<class A, class B> auto operator<( A && a, B && b );
template<class A, class B> auto operator>=( A && a, B && b );
template<class A, class B> auto operator<=( A && a, B && b );
// logical operators
template<class A, class B> auto operator&&( A && a, B && b );
template<class A, class B> auto operator||( A && a, B && b );
template<class A> auto operator!( A && a );
// bitwise operators
template<class A, class B> auto operator&( A && a, B && b );
template<class A, class B> auto operator|( A && a, B && b );
template<class A, class B> auto operator^( A && a, B && b );
template<class A> auto operator~( A && a );
template<class A, class B> auto operator<<( A && a, B && b );
template<class A, class B> auto operator>>( A && a, B && b );
// additional unary operators
template<class A> auto operator+( A && a );
template<class A> auto operator*( A && a );
template<class A> auto operator++( A && a );
template<class A> auto operator--( A && a );
template<class A> auto operator++( A && a, int );
template<class A> auto operator--( A && a, int );
// compound assignment operators
template<class A, class B> auto operator+=( A && a, B && b );
template<class A, class B> auto operator-=( A && a, B && b );
template<class A, class B> auto operator*=( A && a, B && b );
template<class A, class B> auto operator/=( A && a, B && b );
template<class A, class B> auto operator%=( A && a, B && b );
template<class A, class B> auto operator&=( A && a, B && b );
template<class A, class B> auto operator|=( A && a, B && b );
template<class A, class B> auto operator^=( A && a, B && b );
template<class A, class B> auto operator<<=( A && a, B && b );
template<class A, class B> auto operator>>=( A && a, B && b );
} // namespace lambda2
} // namespace boost
```
### Placeholders
```
template<int I> struct lambda2_arg
{
template<class... A> decltype(auto) operator()( A&&... a ) const noexcept;
template<class T> auto operator[]( T&& t ) const;
};
```
`lambda2_arg<I>` is the type of the library-provided placeholders `_I`. The
standard customization point `std::is_placeholder` is specialized for it,
enabling the use of Lambda2's placeholders with `std::bind`.
The placeholders define `operator()`, which permits their direct use as
function objects. E.g. `_1(x, y)` returns `x`.
`operator[]` is also defined to allow expressions like `_1[x]` or `_1[_2]`.
```
template<class... A> decltype(auto) operator()( A&&... a ) const noexcept;
```
[none]
* {blank}
+
Returns: :: `std::get<std::size_t{I-1}>( std::tuple<A&&...>( std::forward<A>(a)... ) );`
```
template<class T> auto operator[]( T&& t ) const;
```
[none]
* {blank}
+
Returns: :: `std::bind( fn, *this, std::forward<T>(t) );`, where `fn` is
a function object such that `fn(x, y)` returns `x[y]`.
### Common Requirements
All operators defined in the subsequent sections only participate in
@@ -214,3 +297,94 @@ template<class A> auto operator~( A && a );
+
Returns: ::
`std::bind( std::bit_not<>(), std::forward<A>(a) );`
```
template<class A, class B> auto operator<<( A && a, B && b );
```
[none]
* {blank}
+
Returns: :: `std::bind( fn, std::forward<A>(a), std::forward<B>(b) );`,
where `fn` is a function object such that `fn(x, y)` returns `x << y`.
```
template<class A, class B> auto operator>>( A && a, B && b );
```
[none]
* {blank}
+
Returns: :: `std::bind( fn, std::forward<A>(a), std::forward<B>(b) );`,
where `fn` is a function object such that `fn(x, y)` returns `x >> y`.
### Additional Unary Operators
```
template<class A> auto operator+( A && a );
```
[none]
* {blank}
+
Returns: ::
`std::bind( fn, std::forward<A>(a) );`, where `fn` is a function object
such that `fn(x)` returns `+x`.
```
template<class A> auto operator*( A && a );
```
[none]
* {blank}
+
Returns: ::
`std::bind( fn, std::forward<A>(a) );`, where `fn` is a function object
such that `fn(x)` returns `*x`.
```
template<class A> auto operator++( A && a );
```
[none]
* {blank}
+
Returns: ::
`std::bind( fn, std::forward<A>(a) );`, where `fn` is a function object
such that `fn(x)` returns `++x`.
```
template<class A> auto operator--( A && a );
```
[none]
* {blank}
+
Returns: ::
`std::bind( fn, std::forward<A>(a) );`, where `fn` is a function object
such that `fn(x)` returns `--x`.
```
template<class A> auto operator++( A && a, int );
```
[none]
* {blank}
+
Returns: ::
`std::bind( fn, std::forward<A>(a) );`, where `fn` is a function object
such that `fn(x)` returns `x++`.
```
template<class A> auto operator--( A && a, int );
```
[none]
* {blank}
+
Returns: ::
`std::bind( fn, std::forward<A>(a) );`, where `fn` is a function object
such that `fn(x)` returns `x--`.
### Compound Assignment Operators
```
template<class A, class B> auto operator@=( A && a, B && b );
```
[none]
* {blank}
+
Returns: :: `std::bind( fn, std::forward<A>(a), std::forward<B>(b) );`,
where `fn` is a function object such that `fn(x, y)` returns `x @= y`.

View File

@@ -1,13 +1,15 @@
#ifndef BOOST_LAMBDA2_LAMBDA2_HPP_INCLUDED
#define BOOST_LAMBDA2_LAMBDA2_HPP_INCLUDED
// Copyright 2020 Peter Dimov
// Copyright 2020, 2021 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#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();
}