From 4382e44c42d2a1edf92a9ef11dbf34843025e64f Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Wed, 14 Jul 2021 12:53:01 -0700 Subject: Replace Maybe with std::optional With c++17, std::optional provides the functionality that Maybe provided. Bug: 183215655 Test: aapt2_tests Change-Id: I62bb9c2fa4985dc5217a6ed153df92b85ad9a034 --- tools/aapt2/ResourceUtils.cpp | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) (limited to 'tools/aapt2/ResourceUtils.cpp') diff --git a/tools/aapt2/ResourceUtils.cpp b/tools/aapt2/ResourceUtils.cpp index e0e80ac02dea..ead06bf989d5 100644 --- a/tools/aapt2/ResourceUtils.cpp +++ b/tools/aapt2/ResourceUtils.cpp @@ -40,8 +40,7 @@ using ::android::base::StringPrintf; namespace aapt { namespace ResourceUtils { -Maybe ToResourceName( - const android::ResTable::resource_name& name_in) { +std::optional ToResourceName(const android::ResTable::resource_name& name_in) { // TODO: Remove this when ResTable and AssetManager(1) are removed from AAPT2 ResourceName name_out; if (!name_in.package) { @@ -78,7 +77,7 @@ Maybe ToResourceName( return name_out; } -Maybe ToResourceName(const android::AssetManager2::ResourceName& name_in) { +std::optional ToResourceName(const android::AssetManager2::ResourceName& name_in) { ResourceName name_out; if (!name_in.package) { return {}; @@ -251,8 +250,7 @@ bool IsAttributeReference(const StringPiece& str) { * <[*]package>:[style/] * [[*]package:style/] */ -Maybe ParseStyleParentReference(const StringPiece& str, - std::string* out_error) { +std::optional ParseStyleParentReference(const StringPiece& str, std::string* out_error) { if (str.empty()) { return {}; } @@ -301,7 +299,7 @@ Maybe ParseStyleParentReference(const StringPiece& str, return result; } -Maybe ParseXmlAttributeName(const StringPiece& str) { +std::optional ParseXmlAttributeName(const StringPiece& str) { StringPiece trimmed_str = util::TrimWhitespace(str); const char* start = trimmed_str.data(); const char* const end = start + trimmed_str.size(); @@ -326,7 +324,7 @@ Maybe ParseXmlAttributeName(const StringPiece& str) { } ref.name = ResourceName(package, ResourceType::kAttr, name.empty() ? trimmed_str : name); - return Maybe(std::move(ref)); + return std::optional(std::move(ref)); } std::unique_ptr TryParseReference(const StringPiece& str, @@ -488,18 +486,18 @@ std::unique_ptr TryParseColor(const StringPiece& str) { : util::make_unique(value); } -Maybe ParseBool(const StringPiece& str) { +std::optional ParseBool(const StringPiece& str) { StringPiece trimmed_str(util::TrimWhitespace(str)); if (trimmed_str == "true" || trimmed_str == "TRUE" || trimmed_str == "True") { - return Maybe(true); + return std::optional(true); } else if (trimmed_str == "false" || trimmed_str == "FALSE" || trimmed_str == "False") { - return Maybe(false); + return std::optional(false); } return {}; } -Maybe ParseInt(const StringPiece& str) { +std::optional ParseInt(const StringPiece& str) { std::u16string str16 = util::Utf8ToUtf16(str); android::Res_value value; if (android::ResTable::stringToInt(str16.data(), str16.size(), &value)) { @@ -508,7 +506,7 @@ Maybe ParseInt(const StringPiece& str) { return {}; } -Maybe ParseResourceId(const StringPiece& str) { +std::optional ParseResourceId(const StringPiece& str) { StringPiece trimmed_str(util::TrimWhitespace(str)); std::u16string str16 = util::Utf8ToUtf16(trimmed_str); @@ -524,7 +522,7 @@ Maybe ParseResourceId(const StringPiece& str) { return {}; } -Maybe ParseSdkVersion(const StringPiece& str) { +std::optional ParseSdkVersion(const StringPiece& str) { StringPiece trimmed_str(util::TrimWhitespace(str)); std::u16string str16 = util::Utf8ToUtf16(trimmed_str); @@ -534,7 +532,7 @@ Maybe ParseSdkVersion(const StringPiece& str) { } // Try parsing the code name. - Maybe entry = GetDevelopmentSdkCodeNameVersion(trimmed_str); + std::optional entry = GetDevelopmentSdkCodeNameVersion(trimmed_str); if (entry) { return entry.value(); } @@ -551,7 +549,7 @@ Maybe ParseSdkVersion(const StringPiece& str) { } std::unique_ptr TryParseBool(const StringPiece& str) { - if (Maybe maybe_result = ParseBool(str)) { + if (std::optional maybe_result = ParseBool(str)) { const uint32_t data = maybe_result.value() ? 0xffffffffu : 0u; return util::make_unique(android::Res_value::TYPE_INT_BOOLEAN, data); } -- cgit v1.2.3-59-g8ed1b