commit | 7706f49b24f10d6943ca0c6ea516d959e066b802 | [log] [tgz] |
---|---|---|
author | Jiyong Park <jiyong@google.com> | Tue Dec 14 22:33:38 2021 +0900 |
committer | Jiyong Park <jiyong@google.com> | Fri Dec 31 11:19:45 2021 +0900 |
tree | fe052346f5248d4311b66c815353cf7186332dd5 | |
parent | 30dbe0193c149a30d043c378f4c810e114cde128 [diff] |
Add OR_RETURN and OR_FATAL macros OR_RETURN is a macro that, evaluates a given statement, and then either yields a success value or returns from the surrounding function with an error value. It is generic so that it accepts any type of statement and can be used in functions of any return type. OR_FATAL is similar to OR_RETURN, but aborts when the statement yields an error value. For example: \#include <android-base/result.h> Result<T, E> inner(); Result<U, E> outer() { // if inner() is a success, the success value is set to t. // if inner() is a failure, the error of type ResultError<E> is // returned to the caller. T t = OR_RETURN(inner()); ... } E anotherOuter() { // The same macro works in a diffrent function returning E // if inner() is a failure, the error of type E is returned to the // caller. T t = OR_RETURN(inner()); ... } Internally, it relies on a template heler class OkOrFail<T>. For a statement whose type is T, the specialization of the class for T has to be provided. The specialization is responsible for providing functions that the OR_RETURN macro uses. e.g. the member function bool is_ok(const T&) provides a way to test if T is a success of not. In this CL, the specialization for T = Result<V, E> is provided. Bug: 209929099 Test: atest libbase_test Change-Id: I66c95e2ebcd063577c81f0facbfe1cec60818a21
This library is a collection of convenience functions to make common tasks easier and less error-prone.
In this context, "error-prone" covers both "hard to do correctly" and "hard to do with good performance", but as a general purpose library, libbase's primary focus is on making it easier to do things easily and correctly when a compromise has to be made between "simplest API" on the one hand and "fastest implementation" on the other. Though obviously the ideal is to have both.
The intention is to cover the 80% use cases, not be all things to all users.
If you have a routine that's really useful in your project, congratulations. But that doesn't mean it should be here rather than just in your project.
The question for libbase is "should everyone be doing this?"/"does this make everyone's code cleaner/safer?". Historically we've considered the bar for inclusion to be "are there at least three unrelated projects that would be cleaned up by doing so".
If your routine is actually something from a future C++ standard (that isn't yet in libc++), or it's widely used in another library, that helps show that there's precedent. Being able to say "so-and-so has used this API for n years" is a good way to reduce concerns about API choices.
Unlike most Android code, code in libbase has to build for Mac and Windows too.
Code here is also expected to have good test coverage.
By its nature, it's difficult to change libbase API. It's often best to start using your routine just in your project, and let it "graduate" after you're certain that the API is solid.