aboutsummaryrefslogtreecommitdiffstats
path: root/libdevcore
diff options
context:
space:
mode:
authorErik Kundt <bitshift@posteo.org>2018-12-04 00:59:21 +0800
committerErik Kundt <bitshift@posteo.org>2018-12-05 17:33:34 +0800
commite3accc6aa6857c8ae7c6f075ff50d9a17989cd0d (patch)
tree61f195edac6d80164098f98b3ec0aa640c59b388 /libdevcore
parent05e74d096ed5a891ae2c1c0d916727ebaca1109f (diff)
downloaddexon-solidity-e3accc6aa6857c8ae7c6f075ff50d9a17989cd0d.tar.gz
dexon-solidity-e3accc6aa6857c8ae7c6f075ff50d9a17989cd0d.tar.zst
dexon-solidity-e3accc6aa6857c8ae7c6f075ff50d9a17989cd0d.zip
Simplifies Result<T> and prevents undefined behaviour.
Diffstat (limited to 'libdevcore')
-rw-r--r--libdevcore/Result.h35
1 files changed, 9 insertions, 26 deletions
diff --git a/libdevcore/Result.h b/libdevcore/Result.h
index c1abf380..cdff67c6 100644
--- a/libdevcore/Result.h
+++ b/libdevcore/Result.h
@@ -28,44 +28,22 @@ namespace dev
/// Result<bool> check()
/// {
/// if (false)
-/// return Result<bool>(false, "Error message.")
+/// return Result<bool>("Error message.")
/// return true;
/// }
///
-/// A result can also be instantiated using one of the factory methods it provides:
-///
-/// using BoolResult = Result<bool>;
-/// BoolResult check()
-/// {
-/// if (false)
-/// return BoolResult::failure("Error message");
-/// return BoolResult::success(true);
-/// }
-///
-///
-struct ResultError { std::string reason; };
template <class ResultType>
class Result
{
public:
- /// @{
- /// @name Factory functions
- /// Factory functions that provide a verbose way to create a result
- static Result<ResultType> Ok(ResultType _value) { return Result(std::move(_value)); }
- static Result<ResultType> Err() { return Result(ResultError{std::string()}); }
- static Result<ResultType> Err(std::string _error) { return Result(ResultError{std::move(_error)}); }
- /// @}
-
- Result(ResultType _value): m_value(std::move(_value)) {}
- Result(ResultError _error): m_error(std::move(_error.reason)) {}
- Result(ResultType _value, ResultError _error): m_value(std::move(_value)), m_error(std::move(_error.reason)) {}
+ Result(ResultType _value): Result(_value, std::string{}) {}
+ Result(std::string _error): Result(ResultType{}, _error) {}
/// @{
/// @name Wrapper functions
/// Wrapper functions that provide implicit conversions to and explicit retrieval of
/// the value this result holds.
- /// If the result is an error, accessing the value results in undefined behaviour.
operator ResultType const&() const { return m_value; }
ResultType& operator*() const { return m_value; }
ResultType const& get() const { return m_value; }
@@ -73,9 +51,14 @@ public:
/// @}
/// @returns the error message (can be empty).
- std::string const& err() const { return m_error; }
+ std::string const& error() const { return m_error; }
private:
+ explicit Result(ResultType _value, std::string _error):
+ m_value(std::move(_value)),
+ m_error(std::move(_error))
+ {}
+
ResultType m_value;
std::string m_error;
};