Skip to content

Commit

Permalink
Add operator&( result, unary-returning-value )
Browse files Browse the repository at this point in the history
  • Loading branch information
pdimov committed Oct 29, 2023
1 parent 47a08cb commit 194b84e
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 0 deletions.
34 changes: 34 additions & 0 deletions include/boost/system/result.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1046,6 +1046,40 @@ template<class E, class F,
}
}

// result & unary-returning-value

template<class T, class E, class F,
class U = decltype( std::declval<F>()( std::declval<T const&>() ) ),
class En = typename std::enable_if<!detail::is_result<U>::value>::type
>
result<U, E> operator&( result<T, E> const& r, F&& f )
{
if( r )
{
return std::forward<F>( f )( *r );
}
else
{
return r.error();
}
}

template<class T, class E, class F,
class U = decltype( std::declval<F>()( std::declval<T>() ) ),
class En = typename std::enable_if<!detail::is_result<U>::value>::type
>
result<U, E> operator&( result<T, E>&& r, F&& f )
{
if( r )
{
return std::forward<F>( f )( *std::move( r ) );
}
else
{
return r.error();
}
}

} // namespace system
} // namespace boost

Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,4 @@ boost_test(TYPE compile-fail SOURCES result_or_value_fail.cpp)
boost_test(TYPE compile-fail SOURCES result_or_value_fail2.cpp)
boost_test(TYPE run SOURCES result_or_fn0v.cpp)
boost_test(TYPE run SOURCES result_or_fn0r.cpp)
boost_test(TYPE run SOURCES result_and_fn0v.cpp)
1 change: 1 addition & 0 deletions test/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,4 @@ compile-fail result_or_value_fail.cpp : $(CPP11) ;
compile-fail result_or_value_fail2.cpp : $(CPP11) ;
run result_or_fn0v.cpp : : : $(CPP11) ;
run result_or_fn0r.cpp : : : $(CPP11) ;
run result_and_fn0v.cpp : : : $(CPP11) ;
158 changes: 158 additions & 0 deletions test/result_and_fn0v.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
// Copyright 2017, 2021, 2022 Peter Dimov.
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#include <boost/system/result.hpp>
#include <boost/core/lightweight_test.hpp>

using namespace boost::system;

struct X
{
int v_;
};

struct Y
{
int v_;

explicit Y( int v ): v_( v ) {}
Y( X x ): v_( x.v_) {}

Y( Y const& ) = delete;
Y& operator=( Y const& ) = delete;

Y( Y&& r ): v_( r.v_ )
{
r.v_ = 0;
}

Y& operator=( Y&& ) = delete;
};

struct E
{
};

int f( int x )
{
return x * 2 + 1;
}

X g( Y y )
{
return X{ y.v_ * 2 + 1 };
}

int& h( int& )
{
static int x = 2;
return x;
}

int main()
{
{
result<int> r( 1 );
result<int> r2 = r & f;

BOOST_TEST( r2.has_value() ) && BOOST_TEST_EQ( *r2, 3 );
}

{
result<int> const r( 1 );
result<int> r2 = r & f;

BOOST_TEST( r2.has_value() ) && BOOST_TEST_EQ( *r2, 3 );
}

{
result<int> r2 = result<int>( 1 ) & f;

BOOST_TEST( r2.has_value() ) && BOOST_TEST_EQ( *r2, 3 );
}

{
result<int, E> r( in_place_error );
result<int, E> r2 = r & f;

BOOST_TEST( r2.has_error() );
}

{
result<int, E> const r( in_place_error );
result<int, E> r2 = r & f;

BOOST_TEST( r2.has_error() );
}

{
result<int, E> r2 = result<int, E>( in_place_error ) & f;

BOOST_TEST( r2.has_error() );
}

{
result<X> r2 = result<Y>( in_place_value, 1 ) & g;

BOOST_TEST( r2.has_value() ) && BOOST_TEST_EQ( r2->v_, 3 );
}

{
result<X, E> r2 = result<Y, E>( in_place_error ) & g;

BOOST_TEST( r2.has_error() );
}

{
int x1 = 1;

result<int&> r( x1 );

result<int&> r2 = r & h;

BOOST_TEST( r2.has_value() ) && BOOST_TEST_EQ( &*r2, &h( x1 ) );
}

{
int x1 = 1;

result<int&> const r( x1 );

result<int&> r2 = r & h;

BOOST_TEST( r2.has_value() ) && BOOST_TEST_EQ( &*r2, &h( x1 ) );
}

{
int x1 = 1;

result<int&> r2 = result<int&>( x1 ) & h;

BOOST_TEST( r2.has_value() ) && BOOST_TEST_EQ( &*r2, &h( x1 ) );
}

{
result<int&, E> r( in_place_error );

result<int&, E> r2 = r & h;

BOOST_TEST( r2.has_error() );
}

{
result<int&, E> const r( in_place_error );

result<int&, E> r2 = r & h;

BOOST_TEST( r2.has_error() );
}

{
result<int&, E> r2 = result<int&, E>( in_place_error ) & h;

BOOST_TEST( r2.has_error() );
}

return boost::report_errors();
}

0 comments on commit 194b84e

Please sign in to comment.