On 3/22/23 12:03, Andrey Semashev wrote:
On 3/22/23 05:02, Klemens Morgenstern wrote:
I've written my own replacement and am very much in favor of the scope-utilities.
Have you thought about if this can be made to work with system::error_code or even system::result? E.g.
make_scope_fail https://lastique.github.io/scope/libs/scope/doc/html/boost/scope/make_scope_...(ec, handle) // checks ec in order to decide if it failed
I haven't thought about this, but it sounds like an interesting potential extension. In general, it might be useful to be able to specify a failure detection function, which by default would be to check for an exception, but could be changed to check an error_code or some arbitrary result code (e.g. from errno).
This could look like this:
error_code ec; scope_fail guard( [&] { std::cout << "Failure" << std::endl; }, [&ec] { return !!ec; });
Similarly with make_scope_fail. There is a small complication with the active flag that can be specified as the second argument currently, but I think I could make it mandatory in this case, or do some type deduction heuristic. I'll need to think about this.
This is now implemented. https://lastique.github.io/scope/libs/scope/doc/html/scope/scope_guards.html... scope_success and scope_fail now support an optional condition predicate that decides whether the scope guard needs to invoke its action. By default, the scope guards still act depending on whether an exception is thrown, for compatibility with Library Fundamentals TS. Also, added a new scope_check guard that does not have a default condition and requires the user to specify one. Functionally, it is similar to scope_fail and opposite to scope_success. The library provides two scope guard condition predicates out of the box. One is the exception_checker, which is used by scope_success and scope_fail by default. The other is error_code_checker, which can be used to check an error code for an error indication. It supports any type of error code that supports `!ec` expression for testing for "not an error" value. So, the earlier code sample could be written like this: error_code ec; scope_fail guard( [&] { std::cout << "Failure" << std::endl; }, check_error_code(ec)); Here, check_error_code is a factory function for the error_code_checker predicate.