diff options
Diffstat (limited to 'tools/aapt2/ResourceTable.h')
-rw-r--r-- | tools/aapt2/ResourceTable.h | 463 |
1 files changed, 225 insertions, 238 deletions
diff --git a/tools/aapt2/ResourceTable.h b/tools/aapt2/ResourceTable.h index 6c246d0d81d6..ebaad41eac6e 100644 --- a/tools/aapt2/ResourceTable.h +++ b/tools/aapt2/ResourceTable.h @@ -37,42 +37,43 @@ namespace aapt { enum class SymbolState { - kUndefined, - kPrivate, - kPublic, + kUndefined, + kPrivate, + kPublic, }; /** * The Public status of a resource. */ struct Symbol { - SymbolState state = SymbolState::kUndefined; - Source source; - std::string comment; + SymbolState state = SymbolState::kUndefined; + Source source; + std::string comment; }; class ResourceConfigValue { -public: - /** - * The configuration for which this value is defined. - */ - const ConfigDescription config; - - /** - * The product for which this value is defined. - */ - const std::string product; - - /** - * The actual Value. - */ - std::unique_ptr<Value> value; - - ResourceConfigValue(const ConfigDescription& config, const StringPiece& product) : - config(config), product(product.toString()) { } - -private: - DISALLOW_COPY_AND_ASSIGN(ResourceConfigValue); + public: + /** + * The configuration for which this value is defined. + */ + const ConfigDescription config; + + /** + * The product for which this value is defined. + */ + const std::string product; + + /** + * The actual Value. + */ + std::unique_ptr<Value> value; + + ResourceConfigValue(const ConfigDescription& config, + const StringPiece& product) + : config(config), product(product.toString()) {} + + private: + DISALLOW_COPY_AND_ASSIGN(ResourceConfigValue); }; /** @@ -80,42 +81,44 @@ private: * varying values for each defined configuration. */ class ResourceEntry { -public: - /** - * The name of the resource. Immutable, as - * this determines the order of this resource - * when doing lookups. - */ - const std::string name; - - /** - * The entry ID for this resource. - */ - Maybe<uint16_t> id; - - /** - * Whether this resource is public (and must maintain the same entry ID across builds). - */ - Symbol symbolStatus; - - /** - * The resource's values for each configuration. - */ - std::vector<std::unique_ptr<ResourceConfigValue>> values; - - explicit ResourceEntry(const StringPiece& name) : name(name.toString()) { } - - ResourceConfigValue* findValue(const ConfigDescription& config); - ResourceConfigValue* findValue(const ConfigDescription& config, const StringPiece& product); - ResourceConfigValue* findOrCreateValue(const ConfigDescription& config, - const StringPiece& product); - std::vector<ResourceConfigValue*> findAllValues(const ConfigDescription& config); - std::vector<ResourceConfigValue*> findValuesIf( - const std::function<bool(ResourceConfigValue*)>& f); - - -private: - DISALLOW_COPY_AND_ASSIGN(ResourceEntry); + public: + /** + * The name of the resource. Immutable, as + * this determines the order of this resource + * when doing lookups. + */ + const std::string name; + + /** + * The entry ID for this resource. + */ + Maybe<uint16_t> id; + + /** + * Whether this resource is public (and must maintain the same entry ID across + * builds). + */ + Symbol symbolStatus; + + /** + * The resource's values for each configuration. + */ + std::vector<std::unique_ptr<ResourceConfigValue>> values; + + explicit ResourceEntry(const StringPiece& name) : name(name.toString()) {} + + ResourceConfigValue* findValue(const ConfigDescription& config); + ResourceConfigValue* findValue(const ConfigDescription& config, + const StringPiece& product); + ResourceConfigValue* findOrCreateValue(const ConfigDescription& config, + const StringPiece& product); + std::vector<ResourceConfigValue*> findAllValues( + const ConfigDescription& config); + std::vector<ResourceConfigValue*> findValuesIf( + const std::function<bool(ResourceConfigValue*)>& f); + + private: + DISALLOW_COPY_AND_ASSIGN(ResourceEntry); }; /** @@ -123,58 +126,53 @@ private: * for this type. */ class ResourceTableType { -public: - /** - * The logical type of resource (string, drawable, layout, etc.). - */ - const ResourceType type; - - /** - * The type ID for this resource. - */ - Maybe<uint8_t> id; - - /** - * Whether this type is public (and must maintain the same - * type ID across builds). - */ - Symbol symbolStatus; - - /** - * List of resources for this type. - */ - std::vector<std::unique_ptr<ResourceEntry>> entries; - - explicit ResourceTableType(const ResourceType type) : type(type) { } - - ResourceEntry* findEntry(const StringPiece& name); - ResourceEntry* findOrCreateEntry(const StringPiece& name); - -private: - DISALLOW_COPY_AND_ASSIGN(ResourceTableType); + public: + /** + * The logical type of resource (string, drawable, layout, etc.). + */ + const ResourceType type; + + /** + * The type ID for this resource. + */ + Maybe<uint8_t> id; + + /** + * Whether this type is public (and must maintain the same + * type ID across builds). + */ + Symbol symbolStatus; + + /** + * List of resources for this type. + */ + std::vector<std::unique_ptr<ResourceEntry>> entries; + + explicit ResourceTableType(const ResourceType type) : type(type) {} + + ResourceEntry* findEntry(const StringPiece& name); + ResourceEntry* findOrCreateEntry(const StringPiece& name); + + private: + DISALLOW_COPY_AND_ASSIGN(ResourceTableType); }; -enum class PackageType { - System, - Vendor, - App, - Dynamic -}; +enum class PackageType { System, Vendor, App, Dynamic }; class ResourceTablePackage { -public: - PackageType type = PackageType::App; - Maybe<uint8_t> id; - std::string name; + public: + PackageType type = PackageType::App; + Maybe<uint8_t> id; + std::string name; - std::vector<std::unique_ptr<ResourceTableType>> types; + std::vector<std::unique_ptr<ResourceTableType>> types; - ResourceTablePackage() = default; - ResourceTableType* findType(ResourceType type); - ResourceTableType* findOrCreateType(const ResourceType type); + ResourceTablePackage() = default; + ResourceTableType* findType(ResourceType type); + ResourceTableType* findOrCreateType(const ResourceType type); -private: - DISALLOW_COPY_AND_ASSIGN(ResourceTablePackage); + private: + DISALLOW_COPY_AND_ASSIGN(ResourceTablePackage); }; /** @@ -182,140 +180,129 @@ private: * flattened into a binary resource table (resources.arsc). */ class ResourceTable { -public: - ResourceTable() = default; - - enum class CollisionResult { - kKeepOriginal, - kConflict, - kTakeNew - }; - - using CollisionResolverFunc = std::function<CollisionResult(Value*, Value*)>; - - /** - * When a collision of resources occurs, this method decides which value to keep. - */ - static CollisionResult resolveValueCollision(Value* existing, Value* incoming); - - bool addResource(const ResourceNameRef& name, - const ConfigDescription& config, - const StringPiece& product, - std::unique_ptr<Value> value, - IDiagnostics* diag); - - bool addResource(const ResourceNameRef& name, - const ResourceId& resId, - const ConfigDescription& config, - const StringPiece& product, - std::unique_ptr<Value> value, - IDiagnostics* diag); - - bool addFileReference(const ResourceNameRef& name, - const ConfigDescription& config, - const Source& source, - const StringPiece& path, - IDiagnostics* diag); - - bool addFileReferenceAllowMangled(const ResourceNameRef& name, - const ConfigDescription& config, - const Source& source, - const StringPiece& path, - io::IFile* file, - IDiagnostics* diag); - - /** - * Same as addResource, but doesn't verify the validity of the name. This is used - * when loading resources from an existing binary resource table that may have mangled - * names. - */ - bool addResourceAllowMangled(const ResourceNameRef& name, - const ConfigDescription& config, - const StringPiece& product, - std::unique_ptr<Value> value, - IDiagnostics* diag); - - bool addResourceAllowMangled(const ResourceNameRef& name, - const ResourceId& id, - const ConfigDescription& config, - const StringPiece& product, - std::unique_ptr<Value> value, - IDiagnostics* diag); - - bool setSymbolState(const ResourceNameRef& name, - const ResourceId& resId, - const Symbol& symbol, - IDiagnostics* diag); - - bool setSymbolStateAllowMangled(const ResourceNameRef& name, - const ResourceId& resId, - const Symbol& symbol, + public: + ResourceTable() = default; + + enum class CollisionResult { kKeepOriginal, kConflict, kTakeNew }; + + using CollisionResolverFunc = std::function<CollisionResult(Value*, Value*)>; + + /** + * When a collision of resources occurs, this method decides which value to + * keep. + */ + static CollisionResult resolveValueCollision(Value* existing, + Value* incoming); + + bool addResource(const ResourceNameRef& name, const ConfigDescription& config, + const StringPiece& product, std::unique_ptr<Value> value, + IDiagnostics* diag); + + bool addResource(const ResourceNameRef& name, const ResourceId& resId, + const ConfigDescription& config, const StringPiece& product, + std::unique_ptr<Value> value, IDiagnostics* diag); + + bool addFileReference(const ResourceNameRef& name, + const ConfigDescription& config, const Source& source, + const StringPiece& path, IDiagnostics* diag); + + bool addFileReferenceAllowMangled(const ResourceNameRef& name, + const ConfigDescription& config, + const Source& source, + const StringPiece& path, io::IFile* file, IDiagnostics* diag); - struct SearchResult { - ResourceTablePackage* package; - ResourceTableType* type; - ResourceEntry* entry; - }; - - Maybe<SearchResult> findResource(const ResourceNameRef& name); - - /** - * The string pool used by this resource table. Values that reference strings must use - * this pool to create their strings. - * - * NOTE: `stringPool` must come before `packages` so that it is destroyed after. - * When `string pool` references are destroyed (as they will be when `packages` - * is destroyed), they decrement a refCount, which would cause invalid - * memory access if the pool was already destroyed. - */ - StringPool stringPool; - - /** - * The list of packages in this table, sorted alphabetically by package name. - */ - std::vector<std::unique_ptr<ResourceTablePackage>> packages; - - /** - * Returns the package struct with the given name, or nullptr if such a package does not - * exist. The empty string is a valid package and typically is used to represent the - * 'current' package before it is known to the ResourceTable. - */ - ResourceTablePackage* findPackage(const StringPiece& name); - - ResourceTablePackage* findPackageById(uint8_t id); - - ResourceTablePackage* createPackage(const StringPiece& name, Maybe<uint8_t> id = {}); - -private: - ResourceTablePackage* findOrCreatePackage(const StringPiece& name); - - bool addResourceImpl(const ResourceNameRef& name, - const ResourceId& resId, - const ConfigDescription& config, - const StringPiece& product, - std::unique_ptr<Value> value, - const char* validChars, - const CollisionResolverFunc& conflictResolver, - IDiagnostics* diag); - - bool addFileReferenceImpl(const ResourceNameRef& name, - const ConfigDescription& config, - const Source& source, - const StringPiece& path, - io::IFile* file, - const char* validChars, - IDiagnostics* diag); - - bool setSymbolStateImpl(const ResourceNameRef& name, - const ResourceId& resId, - const Symbol& symbol, - const char* validChars, + /** + * Same as addResource, but doesn't verify the validity of the name. This is + * used + * when loading resources from an existing binary resource table that may have + * mangled + * names. + */ + bool addResourceAllowMangled(const ResourceNameRef& name, + const ConfigDescription& config, + const StringPiece& product, + std::unique_ptr<Value> value, + IDiagnostics* diag); + + bool addResourceAllowMangled(const ResourceNameRef& name, + const ResourceId& id, + const ConfigDescription& config, + const StringPiece& product, + std::unique_ptr<Value> value, + IDiagnostics* diag); + + bool setSymbolState(const ResourceNameRef& name, const ResourceId& resId, + const Symbol& symbol, IDiagnostics* diag); + + bool setSymbolStateAllowMangled(const ResourceNameRef& name, + const ResourceId& resId, const Symbol& symbol, + IDiagnostics* diag); + + struct SearchResult { + ResourceTablePackage* package; + ResourceTableType* type; + ResourceEntry* entry; + }; + + Maybe<SearchResult> findResource(const ResourceNameRef& name); + + /** + * The string pool used by this resource table. Values that reference strings + * must use + * this pool to create their strings. + * + * NOTE: `stringPool` must come before `packages` so that it is destroyed + * after. + * When `string pool` references are destroyed (as they will be when + * `packages` + * is destroyed), they decrement a refCount, which would cause invalid + * memory access if the pool was already destroyed. + */ + StringPool stringPool; + + /** + * The list of packages in this table, sorted alphabetically by package name. + */ + std::vector<std::unique_ptr<ResourceTablePackage>> packages; + + /** + * Returns the package struct with the given name, or nullptr if such a + * package does not + * exist. The empty string is a valid package and typically is used to + * represent the + * 'current' package before it is known to the ResourceTable. + */ + ResourceTablePackage* findPackage(const StringPiece& name); + + ResourceTablePackage* findPackageById(uint8_t id); + + ResourceTablePackage* createPackage(const StringPiece& name, + Maybe<uint8_t> id = {}); + + private: + ResourceTablePackage* findOrCreatePackage(const StringPiece& name); + + bool addResourceImpl(const ResourceNameRef& name, const ResourceId& resId, + const ConfigDescription& config, + const StringPiece& product, std::unique_ptr<Value> value, + const char* validChars, + const CollisionResolverFunc& conflictResolver, + IDiagnostics* diag); + + bool addFileReferenceImpl(const ResourceNameRef& name, + const ConfigDescription& config, + const Source& source, const StringPiece& path, + io::IFile* file, const char* validChars, IDiagnostics* diag); - DISALLOW_COPY_AND_ASSIGN(ResourceTable); + bool setSymbolStateImpl(const ResourceNameRef& name, const ResourceId& resId, + const Symbol& symbol, const char* validChars, + IDiagnostics* diag); + + DISALLOW_COPY_AND_ASSIGN(ResourceTable); }; -} // namespace aapt +} // namespace aapt -#endif // AAPT_RESOURCE_TABLE_H +#endif // AAPT_RESOURCE_TABLE_H |