mirror of
				https://github.com/ethereum/solidity
				synced 2023-10-03 13:03:40 +00:00 
			
		
		
		
	Simplifies Result<T> and prevents undefined behaviour.
This commit is contained in:
		
							parent
							
								
									05e74d096e
								
							
						
					
					
						commit
						e3accc6aa6
					
				| @ -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; | ||||
| }; | ||||
|  | ||||
| @ -611,14 +611,14 @@ TypeResult IntegerType::unaryOperatorResult(Token _operator) const | ||||
| { | ||||
| 	// "delete" is ok for all integer types
 | ||||
| 	if (_operator == Token::Delete) | ||||
| 		return TypeResult::Ok(make_shared<TupleType>()); | ||||
| 		return TypeResult{make_shared<TupleType>()}; | ||||
| 	// we allow +, -, ++ and --
 | ||||
| 	else if (_operator == Token::Add || _operator == Token::Sub || | ||||
| 			_operator == Token::Inc || _operator == Token::Dec || | ||||
| 			_operator == Token::BitNot) | ||||
| 		return TypeResult::Ok(shared_from_this()); | ||||
| 		return TypeResult{shared_from_this()}; | ||||
| 	else | ||||
| 		return TypeResult::Err(); | ||||
| 		return TypeResult{""}; | ||||
| } | ||||
| 
 | ||||
| bool IntegerType::operator==(Type const& _other) const | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user