mirror of
https://github.com/arcan1s/result.git
synced 2025-04-24 15:37:18 +00:00
add onSuccess and recover methods
This commit is contained in:
parent
fafc01123d
commit
f1247cd010
19
README.md
19
README.md
@ -94,6 +94,25 @@ template <class T> Result::Content print_example(IResult<T> r)
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Construction like `r.match(...)` is also allowed.
|
||||||
|
|
||||||
|
### `Result::recover`, `Result::onSuccess`
|
||||||
|
|
||||||
|
`Result::recover` can be called in case if you want to set default value in case
|
||||||
|
of error or do another action. `Result::onSuccess` can be used to convert your
|
||||||
|
value to another one (including doing action if success):
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
auto convert = [](const int value) { return "str("s + std::to_string(value) + ")"s; };
|
||||||
|
|
||||||
|
std::string get_string(IResult<int> r)
|
||||||
|
{
|
||||||
|
return r
|
||||||
|
.onSuccess<std::string, decltype(convert)>(convert)
|
||||||
|
.recover([](IError) { return ""s; });
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### DBus serialization example
|
### DBus serialization example
|
||||||
|
|
||||||
This code example is copied from [queued](http://github.com/arcan1s/queued) and
|
This code example is copied from [queued](http://github.com/arcan1s/queued) and
|
||||||
|
41
result.hpp
41
result.hpp
@ -190,6 +190,45 @@ public:
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
/**
|
||||||
|
* @brief do action in case if no errors found
|
||||||
|
* @tparam U
|
||||||
|
* new value class name
|
||||||
|
* @tparam UnaryFunctionValue
|
||||||
|
* function type which will be cased if result contains value
|
||||||
|
* @param apply_value
|
||||||
|
* function which will be called if result contains value
|
||||||
|
* @return newly created result with another holded value type
|
||||||
|
*/
|
||||||
|
template <typename U, typename UnaryFunctionValue>
|
||||||
|
Result<U, ErrorEnum> onSuccess(UnaryFunctionValue apply_value) const
|
||||||
|
{
|
||||||
|
Result<U, ErrorEnum> result;
|
||||||
|
match([&result, &apply_value](T value) { result = apply_value(value); },
|
||||||
|
[&result](Error<ErrorEnum> error) { result = error; });
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @brief recover to T in case of error
|
||||||
|
* @tparam UnaryFunctionError
|
||||||
|
* function type which will be cased if result contains error
|
||||||
|
* @param apply_error
|
||||||
|
* function which will be cased if result contains error
|
||||||
|
* @return value in case of Result::Content::Value, function call result
|
||||||
|
* otherwise
|
||||||
|
*/
|
||||||
|
template <typename UnaryFunctionError>
|
||||||
|
T recover(UnaryFunctionError apply_error) const
|
||||||
|
{
|
||||||
|
T val;
|
||||||
|
match([&val](T value) { val = value; },
|
||||||
|
[&val, &apply_error](Error<ErrorEnum> error) {
|
||||||
|
val = apply_error(error);
|
||||||
|
});
|
||||||
|
|
||||||
|
return val;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// additional functions
|
// additional functions
|
||||||
@ -217,6 +256,6 @@ void match(const Result<T, ErrorEnum> &result, UnaryFunctionValue apply_value,
|
|||||||
{
|
{
|
||||||
return result.match(apply_value, apply_error);
|
return result.match(apply_value, apply_error);
|
||||||
};
|
};
|
||||||
};
|
}; // namespace Result
|
||||||
|
|
||||||
#endif /* _RESULT_HPP_ */
|
#endif /* _RESULT_HPP_ */
|
||||||
|
Loading…
Reference in New Issue
Block a user