Replace Maybe with std::optional
With c++17, std::optional provides the functionality that Maybe
provided.
Bug: 183215655
Test: aapt2_tests
Change-Id: I62bb9c2fa4985dc5217a6ed153df92b85ad9a034
diff --git a/tools/aapt2/configuration/ConfigurationParser.cpp b/tools/aapt2/configuration/ConfigurationParser.cpp
index dd06b38..e7a4585 100644
--- a/tools/aapt2/configuration/ConfigurationParser.cpp
+++ b/tools/aapt2/configuration/ConfigurationParser.cpp
@@ -34,7 +34,6 @@
#include "io/FileSystem.h"
#include "io/StringStream.h"
#include "util/Files.h"
-#include "util/Maybe.h"
#include "util/Util.h"
#include "xml/XmlActionExecutor.h"
#include "xml/XmlDom.h"
@@ -113,7 +112,7 @@
}
/** Returns the value of the version-code-order attribute for a given element. */
-Maybe<int32_t> GetVersionCodeOrder(const Element* element, IDiagnostics* diag) {
+std::optional<int32_t> GetVersionCodeOrder(const Element* element, IDiagnostics* diag) {
const xml::Attribute* version = element->FindAttribute("", "version-code-order");
if (version == nullptr) {
std::string label = GetLabel(element, diag);
@@ -135,7 +134,7 @@
/** Copies the values referenced in a configuration group to the target list. */
template <typename T>
-bool CopyXmlReferences(const Maybe<std::string>& name, const Group<T>& groups,
+bool CopyXmlReferences(const std::optional<std::string>& name, const Group<T>& groups,
std::vector<T>* target) {
// If there was no item configured, there is nothing to do and no error.
if (!name) {
@@ -159,7 +158,7 @@
* success, or false if the either the placeholder is not found in the name, or the value is not
* present and the placeholder was.
*/
-bool ReplacePlaceholder(const StringPiece& placeholder, const Maybe<StringPiece>& value,
+bool ReplacePlaceholder(const StringPiece& placeholder, const std::optional<StringPiece>& value,
std::string* name, IDiagnostics* diag) {
size_t offset = name->find(placeholder.data());
bool found = (offset != std::string::npos);
@@ -207,17 +206,17 @@
}
/** Converts a ConfiguredArtifact into an OutputArtifact. */
-Maybe<OutputArtifact> ToOutputArtifact(const ConfiguredArtifact& artifact,
- const std::string& apk_name,
- const PostProcessingConfiguration& config,
- IDiagnostics* diag) {
+std::optional<OutputArtifact> ToOutputArtifact(const ConfiguredArtifact& artifact,
+ const std::string& apk_name,
+ const PostProcessingConfiguration& config,
+ IDiagnostics* diag) {
if (!artifact.name && !config.artifact_format) {
diag->Error(
DiagMessage() << "Artifact does not have a name and no global name template defined");
return {};
}
- Maybe<std::string> artifact_name =
+ std::optional<std::string> artifact_name =
(artifact.name) ? artifact.Name(apk_name, diag)
: artifact.ToArtifactName(config.artifact_format.value(), apk_name, diag);
@@ -287,9 +286,9 @@
namespace configuration {
/** Returns the binary reprasentation of the XML configuration. */
-Maybe<PostProcessingConfiguration> ExtractConfiguration(const std::string& contents,
- const std::string& config_path,
- IDiagnostics* diag) {
+std::optional<PostProcessingConfiguration> ExtractConfiguration(const std::string& contents,
+ const std::string& config_path,
+ IDiagnostics* diag) {
StringInputStream in(contents);
std::unique_ptr<xml::XmlResource> doc = xml::Inflate(&in, diag, Source(config_path));
if (!doc) {
@@ -351,7 +350,8 @@
/**
* Returns the common artifact base name from a template string.
*/
-Maybe<std::string> ToBaseName(std::string result, const StringPiece& apk_name, IDiagnostics* diag) {
+std::optional<std::string> ToBaseName(std::string result, const StringPiece& apk_name,
+ IDiagnostics* diag) {
const StringPiece ext = file::GetExtension(apk_name);
size_t end_index = apk_name.to_string().rfind(ext.to_string());
const std::string base_name =
@@ -359,8 +359,8 @@
// Base name is optional.
if (result.find("${basename}") != std::string::npos) {
- Maybe<StringPiece> maybe_base_name =
- base_name.empty() ? Maybe<StringPiece>{} : Maybe<StringPiece>{base_name};
+ auto maybe_base_name = base_name.empty() ? std::nullopt
+ : std::optional<StringPiece>{base_name};
if (!ReplacePlaceholder("${basename}", maybe_base_name, &result, diag)) {
return {};
}
@@ -383,10 +383,10 @@
return result;
}
-Maybe<std::string> ConfiguredArtifact::ToArtifactName(const StringPiece& format,
- const StringPiece& apk_name,
- IDiagnostics* diag) const {
- Maybe<std::string> base = ToBaseName(format.to_string(), apk_name, diag);
+std::optional<std::string> ConfiguredArtifact::ToArtifactName(const StringPiece& format,
+ const StringPiece& apk_name,
+ IDiagnostics* diag) const {
+ std::optional<std::string> base = ToBaseName(format.to_string(), apk_name, diag);
if (!base) {
return {};
}
@@ -419,7 +419,8 @@
return result;
}
-Maybe<std::string> ConfiguredArtifact::Name(const StringPiece& apk_name, IDiagnostics* diag) const {
+std::optional<std::string> ConfiguredArtifact::Name(const StringPiece& apk_name,
+ IDiagnostics* diag) const {
if (!name) {
return {};
}
@@ -430,7 +431,7 @@
} // namespace configuration
/** Returns a ConfigurationParser for the file located at the provided path. */
-Maybe<ConfigurationParser> ConfigurationParser::ForPath(const std::string& path) {
+std::optional<ConfigurationParser> ConfigurationParser::ForPath(const std::string& path) {
std::string contents;
if (!ReadFileToString(path, &contents, true)) {
return {};
@@ -442,9 +443,9 @@
: contents_(std::move(contents)), config_path_(config_path), diag_(&noop_) {
}
-Maybe<std::vector<OutputArtifact>> ConfigurationParser::Parse(
+std::optional<std::vector<OutputArtifact>> ConfigurationParser::Parse(
const android::StringPiece& apk_path) {
- Maybe<PostProcessingConfiguration> maybe_config =
+ std::optional<PostProcessingConfiguration> maybe_config =
ExtractConfiguration(contents_, config_path_, diag_);
if (!maybe_config) {
return {};
@@ -460,7 +461,8 @@
int version = 1;
for (const ConfiguredArtifact& artifact : config.artifacts) {
- Maybe<OutputArtifact> output_artifact = ToOutputArtifact(artifact, apk_name, config, diag_);
+ std::optional<OutputArtifact> output_artifact =
+ ToOutputArtifact(artifact, apk_name, config, diag_);
if (!output_artifact) {
// Defer return an error condition so that all errors are reported.
valid = false;
@@ -538,7 +540,7 @@
bool valid = true;
OrderedEntry<Abi>& entry = config->abi_groups[label];
- Maybe<int32_t> order = GetVersionCodeOrder(root_element, diag);
+ std::optional<int32_t> order = GetVersionCodeOrder(root_element, diag);
if (!order) {
valid = false;
} else {
@@ -589,7 +591,7 @@
bool valid = true;
OrderedEntry<ConfigDescription>& entry = config->screen_density_groups[label];
- Maybe<int32_t> order = GetVersionCodeOrder(root_element, diag);
+ std::optional<int32_t> order = GetVersionCodeOrder(root_element, diag);
if (!order) {
valid = false;
} else {
@@ -656,7 +658,7 @@
bool valid = true;
OrderedEntry<ConfigDescription>& entry = config->locale_groups[label];
- Maybe<int32_t> order = GetVersionCodeOrder(root_element, diag);
+ std::optional<int32_t> order = GetVersionCodeOrder(root_element, diag);
if (!order) {
valid = false;
} else {
@@ -724,19 +726,19 @@
entry.label = attr.value;
valid_attr = true;
} else if (attr.name == "minSdkVersion") {
- Maybe<int> version = ResourceUtils::ParseSdkVersion(attr.value);
+ std::optional<int> version = ResourceUtils::ParseSdkVersion(attr.value);
if (version) {
valid_attr = true;
entry.min_sdk_version = version.value();
}
} else if (attr.name == "targetSdkVersion") {
- Maybe<int> version = ResourceUtils::ParseSdkVersion(attr.value);
+ std::optional<int> version = ResourceUtils::ParseSdkVersion(attr.value);
if (version) {
valid_attr = true;
entry.target_sdk_version = version;
}
} else if (attr.name == "maxSdkVersion") {
- Maybe<int> version = ResourceUtils::ParseSdkVersion(attr.value);
+ std::optional<int> version = ResourceUtils::ParseSdkVersion(attr.value);
if (version) {
valid_attr = true;
entry.max_sdk_version = version;
@@ -778,7 +780,7 @@
bool valid = true;
OrderedEntry<GlTexture>& entry = config->gl_texture_groups[label];
- Maybe<int32_t> order = GetVersionCodeOrder(root_element, diag);
+ std::optional<int32_t> order = GetVersionCodeOrder(root_element, diag);
if (!order) {
valid = false;
} else {
@@ -828,7 +830,7 @@
bool valid = true;
OrderedEntry<DeviceFeature>& entry = config->device_feature_groups[label];
- Maybe<int32_t> order = GetVersionCodeOrder(root_element, diag);
+ std::optional<int32_t> order = GetVersionCodeOrder(root_element, diag);
if (!order) {
valid = false;
} else {
diff --git a/tools/aapt2/configuration/ConfigurationParser.h b/tools/aapt2/configuration/ConfigurationParser.h
index b9e3be9..195b4ba 100644
--- a/tools/aapt2/configuration/ConfigurationParser.h
+++ b/tools/aapt2/configuration/ConfigurationParser.h
@@ -17,6 +17,7 @@
#ifndef AAPT2_CONFIGURATION_H
#define AAPT2_CONFIGURATION_H
+#include <optional>
#include <set>
#include <string>
#include <unordered_map>
@@ -25,7 +26,6 @@
#include "androidfw/ConfigDescription.h"
#include "Diagnostics.h"
-#include "util/Maybe.h"
namespace aapt {
@@ -55,9 +55,9 @@
*/
struct Locale {
/** The ISO<?> standard locale language code. */
- Maybe<std::string> lang;
+ std::optional<std::string> lang;
/** The ISO<?> standard locale region code. */
- Maybe<std::string> region;
+ std::optional<std::string> region;
inline friend bool operator==(const Locale& lhs, const Locale& rhs) {
return lhs.lang == rhs.lang && lhs.region == rhs.region;
@@ -74,9 +74,9 @@
struct AndroidSdk {
std::string label;
int min_sdk_version; // min_sdk_version is mandatory if splitting by SDK.
- Maybe<int> target_sdk_version;
- Maybe<int> max_sdk_version;
- Maybe<AndroidManifest> manifest;
+ std::optional<int> target_sdk_version;
+ std::optional<int> max_sdk_version;
+ std::optional<AndroidManifest> manifest;
static AndroidSdk ForMinSdk(int min_sdk) {
AndroidSdk sdk;
@@ -112,7 +112,7 @@
std::vector<Abi> abis;
std::vector<android::ConfigDescription> screen_densities;
std::vector<android::ConfigDescription> locales;
- Maybe<AndroidSdk> android_sdk;
+ std::optional<AndroidSdk> android_sdk;
std::vector<DeviceFeature> features;
std::vector<GlTexture> textures;
@@ -136,7 +136,7 @@
public:
/** Returns a ConfigurationParser for the file located at the provided path. */
- static Maybe<ConfigurationParser> ForPath(const std::string& path);
+ static std::optional<ConfigurationParser> ForPath(const std::string& path);
/** Returns a ConfigurationParser for the configuration in the provided file contents. */
static ConfigurationParser ForContents(const std::string& contents, const std::string& path) {
@@ -154,7 +154,8 @@
* Parses the configuration file and returns the results. If the configuration could not be parsed
* the result is empty and any errors will be displayed with the provided diagnostics context.
*/
- Maybe<std::vector<configuration::OutputArtifact>> Parse(const android::StringPiece& apk_path);
+ std::optional<std::vector<configuration::OutputArtifact>> Parse(
+ const android::StringPiece& apk_path);
protected:
/**
diff --git a/tools/aapt2/configuration/ConfigurationParser.internal.h b/tools/aapt2/configuration/ConfigurationParser.internal.h
index c541688..42ef5159 100644
--- a/tools/aapt2/configuration/ConfigurationParser.internal.h
+++ b/tools/aapt2/configuration/ConfigurationParser.internal.h
@@ -84,8 +84,8 @@
* have not been able to determine the sort order with the previous comparisons.
*/
template <typename T>
- ComparisonChain& Add(const Group<T>& groups, const Maybe<std::string>& lhs,
- const Maybe<std::string>& rhs) {
+ ComparisonChain& Add(const Group<T>& groups, const std::optional<std::string>& lhs,
+ const std::optional<std::string>& rhs) {
return Add(GetGroupOrder(groups, lhs), GetGroupOrder(groups, rhs));
}
@@ -108,7 +108,7 @@
private:
template <typename T>
- inline size_t GetGroupOrder(const Entry<T>& groups, const Maybe<std::string>& label) {
+ inline size_t GetGroupOrder(const Entry<T>& groups, const std::optional<std::string>& label) {
if (!label) {
return std::numeric_limits<size_t>::max();
}
@@ -122,32 +122,33 @@
/** Output artifact configuration options. */
struct ConfiguredArtifact {
/** Name to use for output of processing foo.apk -> foo.<name>.apk. */
- Maybe<std::string> name;
+ std::optional<std::string> name;
/** If present, uses the ABI group with this name. */
- Maybe<std::string> abi_group;
+ std::optional<std::string> abi_group;
/** If present, uses the screen density group with this name. */
- Maybe<std::string> screen_density_group;
+ std::optional<std::string> screen_density_group;
/** If present, uses the locale group with this name. */
- Maybe<std::string> locale_group;
+ std::optional<std::string> locale_group;
/** If present, uses the Android SDK with this name. */
- Maybe<std::string> android_sdk;
+ std::optional<std::string> android_sdk;
/** If present, uses the device feature group with this name. */
- Maybe<std::string> device_feature_group;
+ std::optional<std::string> device_feature_group;
/** If present, uses the OpenGL texture group with this name. */
- Maybe<std::string> gl_texture_group;
+ std::optional<std::string> gl_texture_group;
/** Convert an artifact name template into a name string based on configuration contents. */
- Maybe<std::string> ToArtifactName(const android::StringPiece& format,
- const android::StringPiece& apk_name, IDiagnostics* diag) const;
+ std::optional<std::string> ToArtifactName(const android::StringPiece& format,
+ const android::StringPiece& apk_name,
+ IDiagnostics* diag) const;
/** Convert an artifact name template into a name string based on configuration contents. */
- Maybe<std::string> Name(const android::StringPiece& apk_name, IDiagnostics* diag) const;
+ std::optional<std::string> Name(const android::StringPiece& apk_name, IDiagnostics* diag) const;
};
/** AAPT2 XML configuration file binary representation. */
struct PostProcessingConfiguration {
std::vector<ConfiguredArtifact> artifacts;
- Maybe<std::string> artifact_format;
+ std::optional<std::string> artifact_format;
Group<Abi> abi_groups;
Group<android::ConfigDescription> screen_density_groups;
@@ -212,9 +213,9 @@
};
/** Parses the provided XML document returning the post processing configuration. */
-Maybe<PostProcessingConfiguration> ExtractConfiguration(const std::string& contents,
- const std::string& config_path,
- IDiagnostics* diag);
+std::optional<PostProcessingConfiguration> ExtractConfiguration(const std::string& contents,
+ const std::string& config_path,
+ IDiagnostics* diag);
namespace handler {
diff --git a/tools/aapt2/configuration/ConfigurationParser_test.cpp b/tools/aapt2/configuration/ConfigurationParser_test.cpp
index e5b3107..e5eaccc 100644
--- a/tools/aapt2/configuration/ConfigurationParser_test.cpp
+++ b/tools/aapt2/configuration/ConfigurationParser_test.cpp
@@ -31,11 +31,6 @@
namespace aapt {
namespace configuration {
-void PrintTo(const AndroidSdk& sdk, std::ostream* os) {
- *os << "SDK: min=" << sdk.min_sdk_version
- << ", target=" << sdk.target_sdk_version.value_or_default(-1)
- << ", max=" << sdk.max_sdk_version.value_or_default(-1);
-}
bool operator==(const ConfiguredArtifact& lhs, const ConfiguredArtifact& rhs) {
return lhs.name == rhs.name && lhs.abi_group == rhs.abi_group &&
@@ -45,20 +40,6 @@
lhs.gl_texture_group == rhs.gl_texture_group;
}
-std::ostream& operator<<(std::ostream& out, const Maybe<std::string>& value) {
- PrintTo(value, &out);
- return out;
-}
-
-void PrintTo(const ConfiguredArtifact& artifact, std::ostream* os) {
- *os << "\n{"
- << "\n name: " << artifact.name << "\n sdk: " << artifact.android_sdk
- << "\n abi: " << artifact.abi_group << "\n density: " << artifact.screen_density_group
- << "\n locale: " << artifact.locale_group
- << "\n features: " << artifact.device_feature_group
- << "\n textures: " << artifact.gl_texture_group << "\n}\n";
-}
-
namespace handler {
namespace {
@@ -186,7 +167,7 @@
}
TEST_F(ConfigurationParserTest, ExtractConfiguration) {
- Maybe<PostProcessingConfiguration> maybe_config =
+ std::optional<PostProcessingConfiguration> maybe_config =
ExtractConfiguration(kValidConfig, "fake.xml", &diag_);
PostProcessingConfiguration config = maybe_config.value();
@@ -928,7 +909,8 @@
EXPECT_FALSE(x86.ToArtifactName("something.${abi${density}}.apk", "", &diag));
- const Maybe<std::string>& name = x86.ToArtifactName("something.${abi${abi}}.apk", "", &diag);
+ const std::optional<std::string>& name =
+ x86.ToArtifactName("something.${abi${abi}}.apk", "", &diag);
ASSERT_TRUE(name);
EXPECT_EQ(name.value(), "something.${abix86}.apk");
}