forked from boostorg/system
Add operator|( result, value )
This commit is contained in:
@ -885,6 +885,56 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// operator|
|
||||||
|
|
||||||
|
namespace detail
|
||||||
|
{
|
||||||
|
|
||||||
|
template<class T, class U> struct is_value_convertible_to: std::is_convertible<T, U>
|
||||||
|
{
|
||||||
|
};
|
||||||
|
|
||||||
|
template<class T, class U> struct is_value_convertible_to<T, U&>:
|
||||||
|
std::integral_constant<bool,
|
||||||
|
std::is_lvalue_reference<T>::value &&
|
||||||
|
std::is_convertible<std::remove_reference_t<T>*, U*>::value>
|
||||||
|
{
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace detail
|
||||||
|
|
||||||
|
// result | value
|
||||||
|
|
||||||
|
template<class T, class E, class U,
|
||||||
|
class En = typename std::enable_if<detail::is_value_convertible_to<U, T>::value>::type
|
||||||
|
>
|
||||||
|
T operator|( result<T, E> const& r, U&& u )
|
||||||
|
{
|
||||||
|
if( r )
|
||||||
|
{
|
||||||
|
return *r;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return std::forward<U>( u );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T, class E, class U,
|
||||||
|
class En = typename std::enable_if<detail::is_value_convertible_to<U, T>::value>::type
|
||||||
|
>
|
||||||
|
T operator|( result<T, E>&& r, U&& u )
|
||||||
|
{
|
||||||
|
if( r )
|
||||||
|
{
|
||||||
|
return *std::move( r );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return std::forward<U>( u );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace system
|
} // namespace system
|
||||||
} // namespace boost
|
} // namespace boost
|
||||||
|
|
||||||
|
@ -167,3 +167,6 @@ boost_test(TYPE run SOURCES result_error_move.cpp)
|
|||||||
boost_test(TYPE run SOURCES result_value_construct6.cpp)
|
boost_test(TYPE run SOURCES result_value_construct6.cpp)
|
||||||
boost_test(TYPE run SOURCES result_value_construct7.cpp)
|
boost_test(TYPE run SOURCES result_value_construct7.cpp)
|
||||||
boost_test(TYPE run SOURCES result_error_construct5.cpp)
|
boost_test(TYPE run SOURCES result_error_construct5.cpp)
|
||||||
|
boost_test(TYPE run SOURCES result_or_value.cpp)
|
||||||
|
boost_test(TYPE compile-fail SOURCES result_or_value_fail.cpp)
|
||||||
|
boost_test(TYPE compile-fail SOURCES result_or_value_fail2.cpp)
|
||||||
|
@ -197,3 +197,6 @@ run result_error_move.cpp : : : $(CPP11) ;
|
|||||||
run result_value_construct6.cpp : : : $(CPP11) ;
|
run result_value_construct6.cpp : : : $(CPP11) ;
|
||||||
run result_value_construct7.cpp : : : $(CPP11) ;
|
run result_value_construct7.cpp : : : $(CPP11) ;
|
||||||
run result_error_construct5.cpp : : : $(CPP11) ;
|
run result_error_construct5.cpp : : : $(CPP11) ;
|
||||||
|
run result_or_value.cpp : : : $(CPP11) ;
|
||||||
|
compile-fail result_or_value_fail.cpp : $(CPP11) ;
|
||||||
|
compile-fail result_or_value_fail2.cpp : $(CPP11) ;
|
||||||
|
167
test/result_or_value.cpp
Normal file
167
test/result_or_value.cpp
Normal file
@ -0,0 +1,167 @@
|
|||||||
|
// 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 main()
|
||||||
|
{
|
||||||
|
{
|
||||||
|
result<int> r( 1 );
|
||||||
|
|
||||||
|
int x = r | 2;
|
||||||
|
|
||||||
|
BOOST_TEST_EQ( x, 1 );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
result<int> const r( 1 );
|
||||||
|
|
||||||
|
int x = r | 2;
|
||||||
|
|
||||||
|
BOOST_TEST_EQ( x, 1 );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
int x = result<int>( 1 ) | 2;
|
||||||
|
|
||||||
|
BOOST_TEST_EQ( x, 1 );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
result<int, E> r( in_place_error );
|
||||||
|
|
||||||
|
int x = r | 2;
|
||||||
|
|
||||||
|
BOOST_TEST_EQ( x, 2 );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
result<int, E> const r( in_place_error );
|
||||||
|
|
||||||
|
int x = r | 2;
|
||||||
|
|
||||||
|
BOOST_TEST_EQ( x, 2 );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
int x = result<int, E>( in_place_error ) | 2;
|
||||||
|
|
||||||
|
BOOST_TEST_EQ( x, 2 );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
Y y = result<Y>( in_place_value, 1 ) | Y{2};
|
||||||
|
|
||||||
|
BOOST_TEST_EQ( y.v_, 1 );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
Y y = result<Y, E>( in_place_error ) | Y{2};
|
||||||
|
|
||||||
|
BOOST_TEST_EQ( y.v_, 2 );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
Y y = result<Y>( in_place_value, 1 ) | X{2};
|
||||||
|
|
||||||
|
BOOST_TEST_EQ( y.v_, 1 );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
Y y = result<Y, E>( in_place_error ) | X{2};
|
||||||
|
|
||||||
|
BOOST_TEST_EQ( y.v_, 2 );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
int x1 = 1;
|
||||||
|
int x2 = 2;
|
||||||
|
|
||||||
|
result<int&> r( x1 );
|
||||||
|
|
||||||
|
int& x = r | x2;
|
||||||
|
|
||||||
|
BOOST_TEST_EQ( &x, &x1 );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
int x1 = 1;
|
||||||
|
int x2 = 2;
|
||||||
|
|
||||||
|
result<int&> const r( x1 );
|
||||||
|
|
||||||
|
int& x = r | x2;
|
||||||
|
|
||||||
|
BOOST_TEST_EQ( &x, &x1 );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
int x1 = 1;
|
||||||
|
int x2 = 2;
|
||||||
|
|
||||||
|
int& x = result<int&>( x1 ) | x2;
|
||||||
|
|
||||||
|
BOOST_TEST_EQ( &x, &x1 );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
int x2 = 2;
|
||||||
|
|
||||||
|
result<int&, E> r( in_place_error );
|
||||||
|
|
||||||
|
int& x = r | x2;
|
||||||
|
|
||||||
|
BOOST_TEST_EQ( &x, &x2 );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
int x2 = 2;
|
||||||
|
|
||||||
|
result<int&, E> const r( in_place_error );
|
||||||
|
|
||||||
|
int& x = r | x2;
|
||||||
|
|
||||||
|
BOOST_TEST_EQ( &x, &x2 );
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
int x2 = 2;
|
||||||
|
|
||||||
|
int& x = result<int&, E>( in_place_error ) | x2;
|
||||||
|
|
||||||
|
BOOST_TEST_EQ( &x, &x2 );
|
||||||
|
}
|
||||||
|
|
||||||
|
return boost::report_errors();
|
||||||
|
}
|
14
test/result_or_value_fail.cpp
Normal file
14
test/result_or_value_fail.cpp
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
// 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>
|
||||||
|
|
||||||
|
using namespace boost::system;
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int x = 1;
|
||||||
|
result<int const&> r( x );
|
||||||
|
r | 2;
|
||||||
|
}
|
13
test/result_or_value_fail2.cpp
Normal file
13
test/result_or_value_fail2.cpp
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
// 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>
|
||||||
|
|
||||||
|
using namespace boost::system;
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int x = 1;
|
||||||
|
result<int const&>( x ) | 2;
|
||||||
|
}
|
Reference in New Issue
Block a user