idmap2: introduce improved Result class
Add a new version of the Result class that functions like the old
Result, but in case of an error, also encodes a string detailing the
error. This will allow us to write the following type of code:
Result<Foo> CreateFoo() {
if (...) {
return Error("errno=%d", errno());
}
return Foo(...);
}
auto foo = CreateFoo();
if (!foo) {
std::cerr << "error: " << foo.GetErrorMessage() << std::endl;
abort();
}
std::cout << "foo=" << *foo << std::endl;
This commit only adds the new Result class. A later change will replace
uses of the old version.
Test: make idmap2_tests
Change-Id: I674d8a06866402adedf85f8514400f25840d5eda
diff --git a/cmds/idmap2/Android.bp b/cmds/idmap2/Android.bp
index 056add5..2fef407 100644
--- a/cmds/idmap2/Android.bp
+++ b/cmds/idmap2/Android.bp
@@ -43,6 +43,7 @@
"libidmap2/PrettyPrintVisitor.cpp",
"libidmap2/RawPrintVisitor.cpp",
"libidmap2/ResourceUtils.cpp",
+ "libidmap2/Result.cpp",
"libidmap2/Xml.cpp",
"libidmap2/ZipFile.cpp",
],
@@ -93,6 +94,7 @@
"tests/PrettyPrintVisitorTests.cpp",
"tests/RawPrintVisitorTests.cpp",
"tests/ResourceUtilsTests.cpp",
+ "tests/ResultTests.cpp",
"tests/XmlTests.cpp",
"tests/ZipFileTests.cpp",
],