mirror of
https://github.com/ToruNiina/toml11.git
synced 2025-09-17 00:38:08 +08:00
add conversion members to result
This commit is contained in:
155
toml/result.hpp
155
toml/result.hpp
@@ -8,6 +8,19 @@
|
||||
namespace toml
|
||||
{
|
||||
|
||||
#if __cplusplus >= 201703L
|
||||
|
||||
template<typename F, typename ... Args>
|
||||
using return_type_of_t = std::invoke_result_t<F, Args...>;
|
||||
|
||||
#else
|
||||
// result_of is deprecated after C++17
|
||||
template<typename F, typename ... Args>
|
||||
using return_type_of_t = typename std::result_of<F(Args...)>::type;
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
template<typename T>
|
||||
struct success
|
||||
{
|
||||
@@ -390,6 +403,142 @@ struct result
|
||||
error_type const& as_err() const& noexcept {return this->fail.value;}
|
||||
error_type&& as_err() && noexcept {return std::move(this->fail.value);}
|
||||
|
||||
|
||||
// prerequisities
|
||||
// F: T -> U
|
||||
// retval: result<U, E>
|
||||
template<typename F>
|
||||
result<return_type_of_t<F, value_type&>, error_type>
|
||||
map(F&& f) &
|
||||
{
|
||||
if(this->is_ok()){return ok(f(this->as_ok()));}
|
||||
return err(this->as_err());
|
||||
}
|
||||
template<typename F>
|
||||
result<return_type_of_t<F, value_type const&>, error_type>
|
||||
map(F&& f) const&
|
||||
{
|
||||
if(this->is_ok()){return ok(f(this->as_ok()));}
|
||||
return err(this->as_err());
|
||||
}
|
||||
template<typename F>
|
||||
result<return_type_of_t<F, value_type &&>, error_type>
|
||||
map(F&& f) &&
|
||||
{
|
||||
if(this->is_ok()){return ok(f(std::move(this->as_ok())));}
|
||||
return err(std::move(this->as_err()));
|
||||
}
|
||||
|
||||
// prerequisities
|
||||
// F: E -> F
|
||||
// retval: result<T, F>
|
||||
template<typename F>
|
||||
result<value_type, return_type_of_t<F, error_type&>>
|
||||
map_err(F&& f) &
|
||||
{
|
||||
if(this->is_err()){return err(f(this->as_err()));}
|
||||
return ok(this->as_ok());
|
||||
}
|
||||
template<typename F>
|
||||
result<value_type, return_type_of_t<F, error_type const&>>
|
||||
map_err(F&& f) const&
|
||||
{
|
||||
if(this->is_err()){return err(f(this->as_err()));}
|
||||
return ok(this->as_ok());
|
||||
}
|
||||
template<typename F>
|
||||
result<value_type, return_type_of_t<F, error_type&&>>
|
||||
map_err(F&& f) &&
|
||||
{
|
||||
if(this->is_err()){return err(f(std::move(this->as_err())));}
|
||||
return ok(std::move(this->as_ok()));
|
||||
}
|
||||
|
||||
// prerequisities
|
||||
// F: T -> U
|
||||
// retval: U
|
||||
template<typename F, typename U>
|
||||
return_type_of_t<F, value_type&>
|
||||
map_or_else(F&& f, U&& opt) &
|
||||
{
|
||||
if(this->is_err()){return std::forward<U>(opt);}
|
||||
return f(this->as_ok());
|
||||
}
|
||||
template<typename F, typename U>
|
||||
return_type_of_t<F, value_type const&>
|
||||
map_or_else(F&& f, U&& opt) const&
|
||||
{
|
||||
if(this->is_err()){return std::forward<U>(opt);}
|
||||
return f(this->as_ok());
|
||||
}
|
||||
template<typename F, typename U>
|
||||
return_type_of_t<F, value_type&&>
|
||||
map_or_else(F&& f, U&& opt) &&
|
||||
{
|
||||
if(this->is_err()){return std::forward<U>(opt);}
|
||||
return f(std::move(this->as_ok()));
|
||||
}
|
||||
|
||||
// prerequisities:
|
||||
// F: func T -> U
|
||||
// toml::err(error_type) should be convertible to U.
|
||||
// normally, type U is another result<S, F> and E is convertible to F
|
||||
template<typename F>
|
||||
return_type_of_t<F, value_type&>
|
||||
and_then(F&& f) &
|
||||
{
|
||||
if(this->is_ok()){return f(this->as_ok());}
|
||||
return err(this->as_err());
|
||||
}
|
||||
template<typename F>
|
||||
return_type_of_t<F, value_type const&>
|
||||
and_then(F&& f) const&
|
||||
{
|
||||
if(this->is_ok()){return f(this->as_ok());}
|
||||
return err(this->as_err());
|
||||
}
|
||||
template<typename F>
|
||||
return_type_of_t<F, value_type&&>
|
||||
and_then(F&& f) &&
|
||||
{
|
||||
if(this->is_ok()){return f(std::move(this->as_ok()));}
|
||||
return err(std::move(this->as_err()));
|
||||
}
|
||||
|
||||
// prerequisities:
|
||||
// F: func E -> U
|
||||
// toml::ok(value_type) should be convertible to U.
|
||||
// normally, type U is another result<S, F> and T is convertible to S
|
||||
template<typename F>
|
||||
return_type_of_t<F, error_type&>
|
||||
or_else(F&& f) &
|
||||
{
|
||||
if(this->is_err()){return f(this->as_err());}
|
||||
return ok(this->as_ok());
|
||||
}
|
||||
template<typename F>
|
||||
return_type_of_t<F, error_type const&>
|
||||
or_else(F&& f) const&
|
||||
{
|
||||
if(this->is_err()){return f(this->as_err());}
|
||||
return ok(this->as_ok());
|
||||
}
|
||||
template<typename F>
|
||||
return_type_of_t<F, error_type&&>
|
||||
or_else(F&& f) &&
|
||||
{
|
||||
if(this->is_err()){return f(std::move(this->as_err()));}
|
||||
return ok(std::move(this->as_ok()));
|
||||
}
|
||||
|
||||
void swap(result<T, E>& other)
|
||||
{
|
||||
result<T, E> tmp(std::move(*this));
|
||||
*this = std::move(other);
|
||||
other = std::move(tmp);
|
||||
return ;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
void cleanup() noexcept
|
||||
@@ -409,6 +558,12 @@ struct result
|
||||
};
|
||||
};
|
||||
|
||||
template<typename T, typename E>
|
||||
void swap(result<T, E>& lhs, result<T, E>& rhs)
|
||||
{
|
||||
lhs.swap(rhs);
|
||||
return;
|
||||
}
|
||||
|
||||
} // toml11
|
||||
#endif// TOML11_RESULT_H
|
||||
|
Reference in New Issue
Block a user