Skip to content

Commit

Permalink
Add operator|=(result&, fn0r)
Browse files Browse the repository at this point in the history
  • Loading branch information
pdimov committed Feb 1, 2024
1 parent f18edb3 commit 310bf1d
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 0 deletions.
18 changes: 18 additions & 0 deletions include/boost/system/result.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,24 @@ result<T, E>& operator|=( result<T, E>& r, F&& f )
return r;
}

// result |= nullary-returning-result

template<class T, class E, class F,
class U = decltype( std::declval<F>()() ),
class En1 = typename std::enable_if<detail::is_result<U>::value>::type,
class En2 = typename std::enable_if<detail::is_value_convertible_to<typename U::value_type, T>::value>::type,
class En3 = typename std::enable_if<std::is_convertible<typename U::error_type, E>::value>::type
>
result<T, E>& operator|=( result<T, E>& r, F&& f )
{
if( !r )
{
r = std::forward<F>( f )();
}

return r;
}

// operator&

// result & unary-returning-value
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,4 @@ boost_test(TYPE run SOURCES result_and_eq_fn1r.cpp)
boost_test(TYPE run SOURCES result_in_place_use.cpp)
boost_test(TYPE run SOURCES result_or_eq_value.cpp)
boost_test(TYPE run SOURCES result_or_eq_fn0v.cpp)
boost_test(TYPE run SOURCES result_or_eq_fn0r.cpp)
1 change: 1 addition & 0 deletions test/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,4 @@ run result_and_eq_fn1r.cpp ;
run result_in_place_use.cpp ;
run result_or_eq_value.cpp ;
run result_or_eq_fn0v.cpp ;
run result_or_eq_fn0r.cpp ;
160 changes: 160 additions & 0 deletions test/result_or_eq_fn0r.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
// Copyright 2017, 2021-2024 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&& r )
{
if( &r != this )
{
v_ = r.v_;
r.v_ = 0;
}

return *this;
}
};

struct E
{
};

struct E2
{
E2() {}
E2( E ) {}
};

result<int, E> fi()
{
return 2;
}

result<int, E2> fi2()
{
return E2();
}

result<X, E> fy()
{
return X{ 2 };
}

result<Y, E2> fy2()
{
return E2();
}

result<int&, E> fri()
{
static int x = 2;
return x;
}

result<int&, E2> fri2()
{
return E2();
}

int main()
{
{
result<int, E2> r( 1 );

r |= fi;

BOOST_TEST( r.has_value() ) && BOOST_TEST_EQ( *r, 1 );

r |= fi2;

BOOST_TEST( r.has_value() ) && BOOST_TEST_EQ( *r, 1 );
}

{
result<int, E2> r( in_place_error );

r |= fi2;

BOOST_TEST( r.has_error() );

r |= fi;

BOOST_TEST( r.has_value() ) && BOOST_TEST_EQ( *r, 2 );
}

{
result<Y, E2> r( in_place_value, 1 );

r |= fy;

BOOST_TEST( r.has_value() ) && BOOST_TEST_EQ( r->v_, 1 );

r |= fy2;

BOOST_TEST( r.has_value() ) && BOOST_TEST_EQ( r->v_, 1 );
}

{
result<Y, E2> r( in_place_error );

r |= fy2;

BOOST_TEST( r.has_error() );

r |= fy;

BOOST_TEST( r.has_value() ) && BOOST_TEST_EQ( r->v_, 2 );
}

{
int x1 = 1;

result<int&, E2> r( x1 );

r |= fri;

BOOST_TEST( r.has_value() ) && BOOST_TEST_EQ( &*r, &x1 );

r |= fri2;

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

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

r |= fri2;

BOOST_TEST( r.has_error() );

r |= fri;

BOOST_TEST( r.has_value() ) && BOOST_TEST_EQ( &*r, &*fri() );
}

return boost::report_errors();
}

0 comments on commit 310bf1d

Please sign in to comment.