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/cmd/Command.cpp b/tools/aapt2/cmd/Command.cpp
index 919b4c9..b1452fa 100644
--- a/tools/aapt2/cmd/Command.cpp
+++ b/tools/aapt2/cmd/Command.cpp
@@ -72,7 +72,7 @@
}
void Command::AddOptionalFlag(const StringPiece& name, const StringPiece& description,
- Maybe<std::string>* value, uint32_t flags) {
+ std::optional<std::string>* value, uint32_t flags) {
auto func = [value, flags](const StringPiece& arg) -> bool {
*value = (flags & Command::kPath) ? GetSafePath(arg) : arg.to_string();
return true;
diff --git a/tools/aapt2/cmd/Command.h b/tools/aapt2/cmd/Command.h
index d21571d..8678cda 100644
--- a/tools/aapt2/cmd/Command.h
+++ b/tools/aapt2/cmd/Command.h
@@ -18,6 +18,7 @@
#define AAPT_COMMAND_H
#include <functional>
+#include <optional>
#include <ostream>
#include <string>
#include <unordered_set>
@@ -25,19 +26,20 @@
#include "androidfw/StringPiece.h"
-#include "util/Maybe.h"
-
namespace aapt {
class Command {
public:
- explicit Command(const android::StringPiece& name) : name_(name.to_string()),
- short_name_(""),
- full_subcommand_name_(name.to_string()) {}
+ explicit Command(const android::StringPiece& name)
+ : name_(name.to_string()), full_subcommand_name_(name.to_string()){};
explicit Command(const android::StringPiece& name, const android::StringPiece& short_name)
- : name_(name.to_string()), short_name_(short_name.to_string()),
- full_subcommand_name_(name.to_string()) {}
+ : name_(name.to_string()),
+ short_name_(short_name.to_string()),
+ full_subcommand_name_(name.to_string()){};
+
+ Command(Command&&) = default;
+ Command& operator=(Command&&) = default;
virtual ~Command() = default;
@@ -58,7 +60,7 @@
uint32_t flags = 0);
void AddOptionalFlag(const android::StringPiece& name, const android::StringPiece& description,
- Maybe<std::string>* value, uint32_t flags = 0);
+ std::optional<std::string>* value, uint32_t flags = 0);
void AddOptionalFlagList(const android::StringPiece& name,
const android::StringPiece& description, std::vector<std::string>* value,
@@ -87,8 +89,6 @@
virtual int Action(const std::vector<std::string>& args) = 0;
private:
- DISALLOW_COPY_AND_ASSIGN(Command);
-
struct Flag {
explicit Flag(const android::StringPiece& name, const android::StringPiece& description,
const bool is_required, const size_t num_args,
@@ -104,8 +104,8 @@
bool found = false;
};
- const std::string name_;
- const std::string short_name_;
+ std::string name_;
+ std::string short_name_;
std::string description_ = "";
std::string full_subcommand_name_;
diff --git a/tools/aapt2/cmd/Command_test.cpp b/tools/aapt2/cmd/Command_test.cpp
index 65608fd..7aa1aa01 100644
--- a/tools/aapt2/cmd/Command_test.cpp
+++ b/tools/aapt2/cmd/Command_test.cpp
@@ -38,7 +38,7 @@
TestCommand command;
std::string required_flag;
command.AddRequiredFlag("--rflag", "", &required_flag, Command::kPath);
- Maybe<std::string> optional_flag;
+ std::optional<std::string> optional_flag;
command.AddOptionalFlag("--oflag", "", &optional_flag, Command::kPath);
std::vector<std::string> required_flag_list;
command.AddRequiredFlagList("--rlflag", "", &required_flag_list, Command::kPath);
diff --git a/tools/aapt2/cmd/Compile.cpp b/tools/aapt2/cmd/Compile.cpp
index cd5015e..fe56018 100644
--- a/tools/aapt2/cmd/Compile.cpp
+++ b/tools/aapt2/cmd/Compile.cpp
@@ -47,7 +47,6 @@
#include "io/ZipArchive.h"
#include "trace/TraceBuffer.h"
#include "util/Files.h"
-#include "util/Maybe.h"
#include "util/Util.h"
#include "xml/XmlDom.h"
#include "xml/XmlPullParser.h"
@@ -75,10 +74,10 @@
};
// Resource file paths are expected to look like: [--/res/]type[-config]/name
-static Maybe<ResourcePathData> ExtractResourcePathData(const std::string& path,
- const char dir_sep,
- std::string* out_error,
- const CompileOptions& options) {
+static std::optional<ResourcePathData> ExtractResourcePathData(const std::string& path,
+ const char dir_sep,
+ std::string* out_error,
+ const CompileOptions& options) {
std::vector<std::string> parts = util::Split(path, dir_sep);
if (parts.size() < 2) {
if (out_error) *out_error = "bad resource path";
@@ -337,7 +336,7 @@
if (file_type == file::FileType::kDirectory) {
context->GetDiagnostics()->Error(DiagMessage(input_path)
<< "resource file cannot be a directory");
- } else if (file_type == file::FileType::kNonexistant) {
+ } else if (file_type == file::FileType::kNonExistant) {
context->GetDiagnostics()->Error(DiagMessage(input_path) << "file not found");
} else {
context->GetDiagnostics()->Error(DiagMessage(input_path)
diff --git a/tools/aapt2/cmd/Compile.h b/tools/aapt2/cmd/Compile.h
index 1bc1f66..bd2e3d7 100644
--- a/tools/aapt2/cmd/Compile.h
+++ b/tools/aapt2/cmd/Compile.h
@@ -17,7 +17,10 @@
#ifndef AAPT2_COMPILE_H
#define AAPT2_COMPILE_H
-#include "androidfw/StringPiece.h"
+#include <optional>
+
+#include <androidfw/StringPiece.h>
+
#include "format/Archive.h"
#include "process/IResourceTableConsumer.h"
#include "Command.h"
@@ -28,11 +31,11 @@
struct CompileOptions {
std::string output_path;
- Maybe<std::string> source_path;
- Maybe<std::string> res_dir;
- Maybe<std::string> res_zip;
- Maybe<std::string> generate_text_symbols_path;
- Maybe<Visibility::Level> visibility;
+ std::optional<std::string> source_path;
+ std::optional<std::string> res_dir;
+ std::optional<std::string> res_zip;
+ std::optional<std::string> generate_text_symbols_path;
+ std::optional<Visibility::Level> visibility;
bool pseudolocalize = false;
bool no_png_crunch = false;
bool legacy_mode = false;
@@ -80,8 +83,8 @@
private:
IDiagnostics* diagnostic_;
CompileOptions options_;
- Maybe<std::string> visibility_;
- Maybe<std::string> trace_folder_;
+ std::optional<std::string> visibility_;
+ std::optional<std::string> trace_folder_;
};
int Compile(IAaptContext* context, io::IFileCollection* inputs, IArchiveWriter* output_writer,
diff --git a/tools/aapt2/cmd/Convert.cpp b/tools/aapt2/cmd/Convert.cpp
index 22bcd85..3b097e0 100644
--- a/tools/aapt2/cmd/Convert.cpp
+++ b/tools/aapt2/cmd/Convert.cpp
@@ -367,8 +367,7 @@
return 1;
}
- Maybe<AppInfo> app_info = ExtractAppInfoFromBinaryManifest(*apk->GetManifest(),
- context.GetDiagnostics());
+ auto app_info = ExtractAppInfoFromBinaryManifest(*apk->GetManifest(), context.GetDiagnostics());
if (!app_info) {
return 1;
}
diff --git a/tools/aapt2/cmd/Convert.h b/tools/aapt2/cmd/Convert.h
index 7e2029d..2cdb0c8 100644
--- a/tools/aapt2/cmd/Convert.h
+++ b/tools/aapt2/cmd/Convert.h
@@ -17,6 +17,8 @@
#ifndef AAPT2_CONVERT_H
#define AAPT2_CONVERT_H
+#include <optional>
+
#include "Command.h"
#include "LoadedApk.h"
#include "format/binary/TableFlattener.h"
@@ -52,7 +54,7 @@
TableFlattenerOptions table_flattener_options_;
XmlFlattenerOptions xml_flattener_options_;
std::string output_path_;
- Maybe<std::string> output_format_;
+ std::optional<std::string> output_format_;
bool verbose_ = false;
};
diff --git a/tools/aapt2/cmd/Diff.cpp b/tools/aapt2/cmd/Diff.cpp
index 3950f33..d9e8c92 100644
--- a/tools/aapt2/cmd/Diff.cpp
+++ b/tools/aapt2/cmd/Diff.cpp
@@ -87,8 +87,8 @@
}
template <typename Id>
-static bool IsIdDiff(const Visibility::Level& level_a, const Maybe<Id>& id_a,
- const Visibility::Level& level_b, const Maybe<Id>& id_b) {
+static bool IsIdDiff(const Visibility::Level& level_a, const std::optional<Id>& id_a,
+ const Visibility::Level& level_b, const std::optional<Id>& id_b) {
if (level_a == Visibility::Level::kPublic || level_b == Visibility::Level::kPublic) {
return id_a != id_b;
}
diff --git a/tools/aapt2/cmd/Dump.cpp b/tools/aapt2/cmd/Dump.cpp
index 3982d12..0a1e021 100644
--- a/tools/aapt2/cmd/Dump.cpp
+++ b/tools/aapt2/cmd/Dump.cpp
@@ -257,12 +257,11 @@
}
int DumpPackageNameCommand::Dump(LoadedApk* apk) {
- Maybe<std::string> package_name = GetPackageName(apk);
- if (!package_name) {
+ auto package_name = GetPackageName(apk);
+ if (!package_name.has_value()) {
return 1;
}
-
- GetPrinter()->Println(package_name.value());
+ GetPrinter()->Println(*package_name);
return 0;
}
@@ -283,12 +282,12 @@
}
int DumpStyleParentCommand::Dump(LoadedApk* apk) {
- Maybe<std::string> package_name = GetPackageName(apk);
- if (!package_name) {
+ auto package_name = GetPackageName(apk);
+ if (!package_name.has_value()) {
return 1;
}
- const auto target_style = ResourceName(package_name.value(), ResourceType::kStyle, style_);
+ const auto target_style = ResourceName(*package_name, ResourceType::kStyle, style_);
const auto table = apk->GetResourceTable();
if (!table) {
@@ -296,7 +295,7 @@
return 1;
}
- Maybe<ResourceTable::SearchResult> target = table->FindResource(target_style);
+ std::optional<ResourceTable::SearchResult> target = table->FindResource(target_style);
if (!target) {
GetDiagnostics()->Error(
DiagMessage() << "Target style \"" << target_style.entry << "\" does not exist");
diff --git a/tools/aapt2/cmd/Dump.h b/tools/aapt2/cmd/Dump.h
index cd51f7a..52616fa 100644
--- a/tools/aapt2/cmd/Dump.h
+++ b/tools/aapt2/cmd/Dump.h
@@ -43,17 +43,17 @@
return diag_;
}
- Maybe<std::string> GetPackageName(LoadedApk* apk) {
+ std::optional<std::string> GetPackageName(LoadedApk* apk) {
xml::Element* manifest_el = apk->GetManifest()->root.get();
if (!manifest_el) {
GetDiagnostics()->Error(DiagMessage() << "No AndroidManifest.");
- return Maybe<std::string>();
+ return {};
}
xml::Attribute* attr = manifest_el->FindAttribute({}, "package");
if (!attr) {
GetDiagnostics()->Error(DiagMessage() << "No package name.");
- return Maybe<std::string>();
+ return {};
}
return attr->value;
}
diff --git a/tools/aapt2/cmd/Link.cpp b/tools/aapt2/cmd/Link.cpp
index e4d0f3b..e614a75 100644
--- a/tools/aapt2/cmd/Link.cpp
+++ b/tools/aapt2/cmd/Link.cpp
@@ -299,7 +299,7 @@
bool do_not_fail_on_missing_resources = false;
OutputFormat output_format = OutputFormat::kApk;
std::unordered_set<std::string> extensions_to_not_compress;
- Maybe<std::regex> regex_to_not_compress;
+ std::optional<std::regex> regex_to_not_compress;
};
// A sampling of public framework resource IDs.
@@ -741,7 +741,7 @@
const size_t res_id_str_len = line.size() - res_id_start_idx;
StringPiece res_id_str = util::TrimWhitespace(line.substr(res_id_start_idx, res_id_str_len));
- Maybe<ResourceId> maybe_id = ResourceUtils::ParseResourceId(res_id_str);
+ std::optional<ResourceId> maybe_id = ResourceUtils::ParseResourceId(res_id_str);
if (!maybe_id) {
diag->Error(DiagMessage(Source(path, line_no)) << "invalid resource ID '" << res_id_str
<< "'");
@@ -793,7 +793,7 @@
if (!options_.manifest_fixer_options.compile_sdk_version) {
xml::Attribute* attr = manifest_xml->root->FindAttribute(xml::kSchemaAndroid, "versionCode");
if (attr != nullptr) {
- Maybe<std::string>& compile_sdk_version = options_.manifest_fixer_options.compile_sdk_version;
+ auto& compile_sdk_version = options_.manifest_fixer_options.compile_sdk_version;
if (BinaryPrimitive* prim = ValueCast<BinaryPrimitive>(attr->compiled_value.get())) {
switch (prim->value.dataType) {
case Res_value::TYPE_INT_DEC:
@@ -816,7 +816,7 @@
if (!options_.manifest_fixer_options.compile_sdk_version_codename) {
xml::Attribute* attr = manifest_xml->root->FindAttribute(xml::kSchemaAndroid, "versionName");
if (attr != nullptr) {
- Maybe<std::string>& compile_sdk_version_codename =
+ std::optional<std::string>& compile_sdk_version_codename =
options_.manifest_fixer_options.compile_sdk_version_codename;
if (String* str = ValueCast<String>(attr->compiled_value.get())) {
compile_sdk_version_codename = *str->value;
@@ -912,7 +912,7 @@
return true;
}
- Maybe<AppInfo> ExtractAppInfoFromManifest(xml::XmlResource* xml_res, IDiagnostics* diag) {
+ std::optional<AppInfo> ExtractAppInfoFromManifest(xml::XmlResource* xml_res, IDiagnostics* diag) {
TRACE_CALL();
// Make sure the first element is <manifest> with package attribute.
xml::Element* manifest_el = xml::FindRootElement(xml_res->root.get());
@@ -937,7 +937,7 @@
if (xml::Attribute* version_code_attr =
manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode")) {
- Maybe<uint32_t> maybe_code = ResourceUtils::ParseInt(version_code_attr->value);
+ std::optional<uint32_t> maybe_code = ResourceUtils::ParseInt(version_code_attr->value);
if (!maybe_code) {
diag->Error(DiagMessage(xml_res->file.source.WithLine(manifest_el->line_number))
<< "invalid android:versionCode '" << version_code_attr->value << "'");
@@ -948,7 +948,7 @@
if (xml::Attribute* version_code_major_attr =
manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor")) {
- Maybe<uint32_t> maybe_code = ResourceUtils::ParseInt(version_code_major_attr->value);
+ std::optional<uint32_t> maybe_code = ResourceUtils::ParseInt(version_code_major_attr->value);
if (!maybe_code) {
diag->Error(DiagMessage(xml_res->file.source.WithLine(manifest_el->line_number))
<< "invalid android:versionCodeMajor '"
@@ -960,7 +960,7 @@
if (xml::Attribute* revision_code_attr =
manifest_el->FindAttribute(xml::kSchemaAndroid, "revisionCode")) {
- Maybe<uint32_t> maybe_code = ResourceUtils::ParseInt(revision_code_attr->value);
+ std::optional<uint32_t> maybe_code = ResourceUtils::ParseInt(revision_code_attr->value);
if (!maybe_code) {
diag->Error(DiagMessage(xml_res->file.source.WithLine(manifest_el->line_number))
<< "invalid android:revisionCode '" << revision_code_attr->value << "'");
@@ -1094,7 +1094,7 @@
bool WriteJavaFile(ResourceTable* table, const StringPiece& package_name_to_generate,
const StringPiece& out_package, const JavaClassGeneratorOptions& java_options,
- const Maybe<std::string>& out_text_symbols_path = {}) {
+ const std::optional<std::string>& out_text_symbols_path = {}) {
if (!options_.generate_java_class_path && !out_text_symbols_path) {
return true;
}
@@ -1251,7 +1251,7 @@
}
const std::string package_utf8 =
- options_.custom_java_package.value_or_default(context_->GetCompilationPackage());
+ options_.custom_java_package.value_or(context_->GetCompilationPackage());
std::string out_path = options_.generate_java_class_path.value();
file::AppendPath(&out_path, file::PackageToPath(package_utf8));
@@ -1283,7 +1283,7 @@
return true;
}
- bool WriteProguardFile(const Maybe<std::string>& out, const proguard::KeepSet& keep_set) {
+ bool WriteProguardFile(const std::optional<std::string>& out, const proguard::KeepSet& keep_set) {
TRACE_CALL();
if (!out) {
return true;
@@ -1374,7 +1374,7 @@
res_name.package = context_->GetCompilationPackage();
}
- Maybe<ResourceName> mangled_name = context_->GetNameMangler()->MangleName(res_name);
+ std::optional<ResourceName> mangled_name = context_->GetNameMangler()->MangleName(res_name);
if (mangled_name) {
res_name = mangled_name.value();
}
@@ -1550,7 +1550,7 @@
bool CopyAssetsDirsToApk(IArchiveWriter* writer) {
std::map<std::string, std::unique_ptr<io::RegularFile>> merged_assets;
for (const std::string& assets_dir : options_.assets_dirs) {
- Maybe<std::vector<std::string>> files =
+ std::optional<std::vector<std::string>> files =
file::FindFiles(assets_dir, context_->GetDiagnostics(), nullptr);
if (!files) {
return false;
@@ -1783,7 +1783,7 @@
package_to_rewrite = table->packages.back().get();
std::string new_package_name =
StringPrintf("%s.%s", package_to_rewrite->name.c_str(),
- app_info_.split_name.value_or_default("feature").c_str());
+ app_info_.split_name.value_or("feature").c_str());
if (context_->IsVerbose()) {
context_->GetDiagnostics()->Note(
@@ -1823,7 +1823,7 @@
}
// First extract the Package name without modifying it (via --rename-manifest-package).
- if (Maybe<AppInfo> maybe_app_info =
+ if (std::optional<AppInfo> maybe_app_info =
ExtractAppInfoFromManifest(manifest_xml.get(), context_->GetDiagnostics())) {
const AppInfo& app_info = maybe_app_info.value();
context_->SetCompilationPackage(app_info.package);
@@ -1850,14 +1850,14 @@
return 1;
}
- Maybe<AppInfo> maybe_app_info =
+ std::optional<AppInfo> maybe_app_info =
ExtractAppInfoFromManifest(manifest_xml.get(), context_->GetDiagnostics());
if (!maybe_app_info) {
return 1;
}
app_info_ = maybe_app_info.value();
- context_->SetMinSdkVersion(app_info_.min_sdk_version.value_or_default(0));
+ context_->SetMinSdkVersion(app_info_.min_sdk_version.value_or(0));
context_->SetNameManglerPolicy(NameManglerPolicy{context_->GetCompilationPackage()});
context_->SetSplitNameDependencies(app_info_.split_name_dependencies);
@@ -2231,7 +2231,7 @@
std::map<size_t, std::string> shared_libs_;
// The package name of the base application, if it is included.
- Maybe<std::string> included_feature_base_;
+ std::optional<std::string> included_feature_base_;
};
int LinkCommand::Action(const std::vector<std::string>& args) {
@@ -2315,7 +2315,8 @@
return 1;
}
- const Maybe<uint32_t> maybe_package_id_int = ResourceUtils::ParseInt(package_id_.value());
+ const std::optional<uint32_t> maybe_package_id_int =
+ ResourceUtils::ParseInt(package_id_.value());
if (!maybe_package_id_int) {
context.GetDiagnostics()->Error(DiagMessage() << "package ID '" << package_id_.value()
<< "' is not a valid integer");
@@ -2360,7 +2361,7 @@
}
if (preferred_density_) {
- Maybe<uint16_t> density =
+ std::optional<uint16_t> density =
ParseTargetDensityParameter(preferred_density_.value(), context.GetDiagnostics());
if (!density) {
return 1;
diff --git a/tools/aapt2/cmd/Link.h b/tools/aapt2/cmd/Link.h
index 768b4b2..d8c76e2 100644
--- a/tools/aapt2/cmd/Link.h
+++ b/tools/aapt2/cmd/Link.h
@@ -45,21 +45,21 @@
bool auto_add_overlay = false;
bool override_styles_instead_of_overlaying = false;
OutputFormat output_format = OutputFormat::kApk;
- Maybe<std::string> rename_resources_package;
+ std::optional<std::string> rename_resources_package;
// Java/Proguard options.
- Maybe<std::string> generate_java_class_path;
- Maybe<std::string> custom_java_package;
+ std::optional<std::string> generate_java_class_path;
+ std::optional<std::string> custom_java_package;
std::set<std::string> extra_java_packages;
- Maybe<std::string> generate_text_symbols_path;
- Maybe<std::string> generate_proguard_rules_path;
- Maybe<std::string> generate_main_dex_proguard_rules_path;
+ std::optional<std::string> generate_text_symbols_path;
+ std::optional<std::string> generate_proguard_rules_path;
+ std::optional<std::string> generate_main_dex_proguard_rules_path;
bool generate_conditional_proguard_rules = false;
bool generate_minimal_proguard_rules = false;
bool generate_non_final_ids = false;
bool no_proguard_location_reference = false;
std::vector<std::string> javadoc_annotations;
- Maybe<std::string> private_symbols;
+ std::optional<std::string> private_symbols;
// Optimizations/features.
bool no_auto_version = false;
@@ -70,7 +70,7 @@
bool no_xml_namespaces = false;
bool do_not_compress_anything = false;
std::unordered_set<std::string> extensions_to_not_compress;
- Maybe<std::regex> regex_to_not_compress;
+ std::optional<std::regex> regex_to_not_compress;
// Static lib options.
bool no_static_lib_packages = false;
@@ -97,7 +97,7 @@
// Stable ID options.
std::unordered_map<ResourceName, ResourceId> stable_id_map;
- Maybe<std::string> resource_id_map_path;
+ std::optional<std::string> resource_id_map_path;
// When 'true', allow reserved package IDs to be used for applications. Pre-O, the platform
// treats negative resource IDs [those with a package ID of 0x80 or higher] as invalid.
@@ -321,20 +321,20 @@
std::vector<std::string> overlay_arg_list_;
std::vector<std::string> extra_java_packages_;
- Maybe<std::string> package_id_;
+ std::optional<std::string> package_id_;
std::vector<std::string> configs_;
- Maybe<std::string> preferred_density_;
- Maybe<std::string> product_list_;
- Maybe<std::string> no_compress_regex;
+ std::optional<std::string> preferred_density_;
+ std::optional<std::string> product_list_;
+ std::optional<std::string> no_compress_regex;
bool legacy_x_flag_ = false;
bool require_localization_ = false;
bool verbose_ = false;
bool shared_lib_ = false;
bool static_lib_ = false;
bool proto_format_ = false;
- Maybe<std::string> stable_id_file_path_;
+ std::optional<std::string> stable_id_file_path_;
std::vector<std::string> split_args_;
- Maybe<std::string> trace_folder_;
+ std::optional<std::string> trace_folder_;
};
}// namespace aapt
diff --git a/tools/aapt2/cmd/Optimize.cpp b/tools/aapt2/cmd/Optimize.cpp
index 5b18a37..caa3e60 100644
--- a/tools/aapt2/cmd/Optimize.cpp
+++ b/tools/aapt2/cmd/Optimize.cpp
@@ -354,7 +354,7 @@
return false;
}
- Maybe<AppInfo> app_info = ExtractAppInfoFromBinaryManifest(*manifest, context->GetDiagnostics());
+ auto app_info = ExtractAppInfoFromBinaryManifest(*manifest, context->GetDiagnostics());
if (!app_info) {
context->GetDiagnostics()->Error(DiagMessage()
<< "failed to extract data from AndroidManifest.xml");
@@ -362,7 +362,7 @@
}
out_options->app_info = std::move(app_info.value());
- context->SetMinSdkVersion(out_options->app_info.min_sdk_version.value_or_default(0));
+ context->SetMinSdkVersion(out_options->app_info.min_sdk_version.value_or(0));
return true;
}
@@ -380,7 +380,7 @@
if (config_path_) {
std::string& path = config_path_.value();
- Maybe<ConfigurationParser> for_path = ConfigurationParser::ForPath(path);
+ std::optional<ConfigurationParser> for_path = ConfigurationParser::ForPath(path);
if (for_path) {
options_.apk_artifacts = for_path.value().WithDiagnostics(diag).Parse(apk_path);
if (!options_.apk_artifacts) {
@@ -427,7 +427,7 @@
if (target_densities_) {
// Parse the target screen densities.
for (const StringPiece& config_str : util::Tokenize(target_densities_.value(), ',')) {
- Maybe<uint16_t> target_density = ParseTargetDensityParameter(config_str, diag);
+ std::optional<uint16_t> target_density = ParseTargetDensityParameter(config_str, diag);
if (!target_density) {
return 1;
}
diff --git a/tools/aapt2/cmd/Optimize.h b/tools/aapt2/cmd/Optimize.h
index 3afc46b..ff63e8d 100644
--- a/tools/aapt2/cmd/Optimize.h
+++ b/tools/aapt2/cmd/Optimize.h
@@ -29,9 +29,9 @@
friend class OptimizeCommand;
// Path to the output APK.
- Maybe<std::string> output_path;
+ std::optional<std::string> output_path;
// Path to the output APK directory for splits.
- Maybe<std::string> output_dir;
+ std::optional<std::string> output_dir;
// Details of the app extracted from the AndroidManifest.xml
AppInfo app_info;
@@ -50,7 +50,7 @@
TableFlattenerOptions table_flattener_options;
- Maybe<std::vector<aapt::configuration::OutputArtifact>> apk_artifacts;
+ std::optional<std::vector<aapt::configuration::OutputArtifact>> apk_artifacts;
// Set of artifacts to keep when generating multi-APK splits. If the list is empty, all artifacts
// are kept and will be written as output.
@@ -60,7 +60,7 @@
bool shorten_resource_paths = false;
// Path to the output map of original resource paths to shortened paths.
- Maybe<std::string> shortened_paths_map_path;
+ std::optional<std::string> shortened_paths_map_path;
};
class OptimizeCommand : public Command {
@@ -122,9 +122,9 @@
bool WriteObfuscatedPathsMap(const std::map<std::string, std::string> &path_map,
const std::string &file_path);
- Maybe<std::string> config_path_;
- Maybe<std::string> resources_config_path_;
- Maybe<std::string> target_densities_;
+ std::optional<std::string> config_path_;
+ std::optional<std::string> resources_config_path_;
+ std::optional<std::string> target_densities_;
std::vector<std::string> configs_;
std::vector<std::string> split_args_;
std::unordered_set<std::string> kept_artifacts_;
diff --git a/tools/aapt2/cmd/Util.cpp b/tools/aapt2/cmd/Util.cpp
index 7214f1a..3244fb8 100644
--- a/tools/aapt2/cmd/Util.cpp
+++ b/tools/aapt2/cmd/Util.cpp
@@ -21,11 +21,10 @@
#include "android-base/logging.h"
#include "androidfw/ConfigDescription.h"
#include "androidfw/Locale.h"
-
#include "ResourceUtils.h"
#include "ValueVisitor.h"
#include "split/TableSplitter.h"
-#include "util/Maybe.h"
+
#include "util/Util.h"
using ::android::ConfigDescription;
@@ -35,7 +34,7 @@
namespace aapt {
-Maybe<uint16_t> ParseTargetDensityParameter(const StringPiece& arg, IDiagnostics* diag) {
+std::optional<uint16_t> ParseTargetDensityParameter(const StringPiece& arg, IDiagnostics* diag) {
ConfigDescription preferred_density_config;
if (!ConfigDescription::Parse(arg, &preferred_density_config)) {
diag->Error(DiagMessage() << "invalid density '" << arg << "' for --preferred-density option");
@@ -245,8 +244,8 @@
return doc;
}
-static Maybe<std::string> ExtractCompiledString(const xml::Attribute& attr,
- std::string* out_error) {
+static std::optional<std::string> ExtractCompiledString(const xml::Attribute& attr,
+ std::string* out_error) {
if (attr.compiled_value != nullptr) {
const String* compiled_str = ValueCast<String>(attr.compiled_value.get());
if (compiled_str != nullptr) {
@@ -269,7 +268,8 @@
return {};
}
-static Maybe<uint32_t> ExtractCompiledInt(const xml::Attribute& attr, std::string* out_error) {
+static std::optional<uint32_t> ExtractCompiledInt(const xml::Attribute& attr,
+ std::string* out_error) {
if (attr.compiled_value != nullptr) {
const BinaryPrimitive* compiled_prim = ValueCast<BinaryPrimitive>(attr.compiled_value.get());
if (compiled_prim != nullptr) {
@@ -283,7 +283,7 @@
}
// Fallback to the plain text value if there is one.
- Maybe<uint32_t> integer = ResourceUtils::ParseInt(attr.value);
+ std::optional<uint32_t> integer = ResourceUtils::ParseInt(attr.value);
if (integer) {
return integer;
}
@@ -293,7 +293,7 @@
return {};
}
-static Maybe<int> ExtractSdkVersion(const xml::Attribute& attr, std::string* out_error) {
+static std::optional<int> ExtractSdkVersion(const xml::Attribute& attr, std::string* out_error) {
if (attr.compiled_value != nullptr) {
const BinaryPrimitive* compiled_prim = ValueCast<BinaryPrimitive>(attr.compiled_value.get());
if (compiled_prim != nullptr) {
@@ -307,7 +307,7 @@
const String* compiled_str = ValueCast<String>(attr.compiled_value.get());
if (compiled_str != nullptr) {
- Maybe<int> sdk_version = ResourceUtils::ParseSdkVersion(*compiled_str->value);
+ std::optional<int> sdk_version = ResourceUtils::ParseSdkVersion(*compiled_str->value);
if (sdk_version) {
return sdk_version;
}
@@ -320,7 +320,7 @@
}
// Fallback to the plain text value if there is one.
- Maybe<int> sdk_version = ResourceUtils::ParseSdkVersion(attr.value);
+ std::optional<int> sdk_version = ResourceUtils::ParseSdkVersion(attr.value);
if (sdk_version) {
return sdk_version;
}
@@ -330,8 +330,8 @@
return {};
}
-Maybe<AppInfo> ExtractAppInfoFromBinaryManifest(const xml::XmlResource& xml_res,
- IDiagnostics* diag) {
+std::optional<AppInfo> ExtractAppInfoFromBinaryManifest(const xml::XmlResource& xml_res,
+ IDiagnostics* diag) {
// Make sure the first element is <manifest> with package attribute.
const xml::Element* manifest_el = xml_res.root.get();
if (manifest_el == nullptr) {
@@ -352,7 +352,7 @@
}
std::string error_msg;
- Maybe<std::string> maybe_package = ExtractCompiledString(*package_attr, &error_msg);
+ std::optional<std::string> maybe_package = ExtractCompiledString(*package_attr, &error_msg);
if (!maybe_package) {
diag->Error(DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
<< "invalid package name: " << error_msg);
@@ -362,7 +362,7 @@
if (const xml::Attribute* version_code_attr =
manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode")) {
- Maybe<uint32_t> maybe_code = ExtractCompiledInt(*version_code_attr, &error_msg);
+ std::optional<uint32_t> maybe_code = ExtractCompiledInt(*version_code_attr, &error_msg);
if (!maybe_code) {
diag->Error(DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
<< "invalid android:versionCode: " << error_msg);
@@ -373,7 +373,7 @@
if (const xml::Attribute* version_code_major_attr =
manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor")) {
- Maybe<uint32_t> maybe_code = ExtractCompiledInt(*version_code_major_attr, &error_msg);
+ std::optional<uint32_t> maybe_code = ExtractCompiledInt(*version_code_major_attr, &error_msg);
if (!maybe_code) {
diag->Error(DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
<< "invalid android:versionCodeMajor: " << error_msg);
@@ -384,7 +384,7 @@
if (const xml::Attribute* revision_code_attr =
manifest_el->FindAttribute(xml::kSchemaAndroid, "revisionCode")) {
- Maybe<uint32_t> maybe_code = ExtractCompiledInt(*revision_code_attr, &error_msg);
+ std::optional<uint32_t> maybe_code = ExtractCompiledInt(*revision_code_attr, &error_msg);
if (!maybe_code) {
diag->Error(DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
<< "invalid android:revisionCode: " << error_msg);
@@ -394,7 +394,8 @@
}
if (const xml::Attribute* split_name_attr = manifest_el->FindAttribute({}, "split")) {
- Maybe<std::string> maybe_split_name = ExtractCompiledString(*split_name_attr, &error_msg);
+ std::optional<std::string> maybe_split_name =
+ ExtractCompiledString(*split_name_attr, &error_msg);
if (!maybe_split_name) {
diag->Error(DiagMessage(xml_res.file.source.WithLine(manifest_el->line_number))
<< "invalid split name: " << error_msg);
@@ -406,7 +407,7 @@
if (const xml::Element* uses_sdk_el = manifest_el->FindChild({}, "uses-sdk")) {
if (const xml::Attribute* min_sdk =
uses_sdk_el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion")) {
- Maybe<int> maybe_sdk = ExtractSdkVersion(*min_sdk, &error_msg);
+ std::optional<int> maybe_sdk = ExtractSdkVersion(*min_sdk, &error_msg);
if (!maybe_sdk) {
diag->Error(DiagMessage(xml_res.file.source.WithLine(uses_sdk_el->line_number))
<< "invalid android:minSdkVersion: " << error_msg);
diff --git a/tools/aapt2/cmd/Util.h b/tools/aapt2/cmd/Util.h
index 2a7c62e..1b98eb4 100644
--- a/tools/aapt2/cmd/Util.h
+++ b/tools/aapt2/cmd/Util.h
@@ -26,14 +26,14 @@
#include "SdkConstants.h"
#include "filter/ConfigFilter.h"
#include "split/TableSplitter.h"
-#include "util/Maybe.h"
#include "xml/XmlDom.h"
namespace aapt {
// Parses a configuration density (ex. hdpi, xxhdpi, 234dpi, anydpi, etc).
// Returns Nothing and logs a human friendly error message if the string was not legal.
-Maybe<uint16_t> ParseTargetDensityParameter(const android::StringPiece& arg, IDiagnostics* diag);
+std::optional<uint16_t> ParseTargetDensityParameter(const android::StringPiece& arg,
+ IDiagnostics* diag);
// Parses a string of the form 'path/to/output.apk:<config>[,<config>...]' and fills in
// `out_path` with the path and `out_split` with the set of ConfigDescriptions.
@@ -59,8 +59,8 @@
const SplitConstraints& constraints);
// Extracts relevant info from the AndroidManifest.xml.
-Maybe<AppInfo> ExtractAppInfoFromBinaryManifest(const xml::XmlResource& xml_res,
- IDiagnostics* diag);
+std::optional<AppInfo> ExtractAppInfoFromBinaryManifest(const xml::XmlResource& xml_res,
+ IDiagnostics* diag);
// Returns a copy of 'name' which conforms to the regex '[a-zA-Z]+[a-zA-Z0-9_]*' by
// replacing nonconforming characters with underscores.