/* * Copyright (C) 2015 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef AAPT_PROCESS_SYMBOLTABLE_H #define AAPT_PROCESS_SYMBOLTABLE_H #include "Resource.h" #include "ResourceTable.h" #include "ResourceValues.h" #include "util/Util.h" #include #include #include #include #include #include #include namespace aapt { struct ISymbolTable { virtual ~ISymbolTable() = default; struct Symbol { ResourceId id; std::unique_ptr attribute; bool isPublic; }; /** * Never hold on to the result between calls to findByName or findById. The results * are typically stored in a cache which may evict entries. */ virtual const Symbol* findByName(const ResourceName& name) = 0; virtual const Symbol* findById(ResourceId id) = 0; }; inline android::hash_t hash_type(const ResourceName& name) { std::hash strHash; android::hash_t hash = 0; hash = android::JenkinsHashMix(hash, strHash(name.package)); hash = android::JenkinsHashMix(hash, (uint32_t) name.type); hash = android::JenkinsHashMix(hash, strHash(name.entry)); return hash; } inline android::hash_t hash_type(const ResourceId& id) { return android::hash_type(id.id); } /** * Presents a ResourceTable as an ISymbolTable, caching results. * Instances of this class must outlive the encompassed ResourceTable. * Since symbols are cached, the ResourceTable should not change during the * lifetime of this SymbolTableWrapper. * * If a resource in the ResourceTable does not have a ResourceID assigned to it, * it is ignored. * * Lookups by ID are ignored. */ class SymbolTableWrapper : public ISymbolTable { private: ResourceTable* mTable; // We use shared_ptr because unique_ptr is not supported and // we need automatic deletion. android::LruCache> mCache; public: SymbolTableWrapper(ResourceTable* table) : mTable(table), mCache(200) { } const Symbol* findByName(const ResourceName& name) override; // Unsupported, all queries to ResourceTable should be done by name. const Symbol* findById(ResourceId id) override { return {}; } }; class AssetManagerSymbolTableBuilder { private: struct AssetManagerSymbolTable : public ISymbolTable { std::vector> mAssets; // We use shared_ptr because unique_ptr is not supported and // we need automatic deletion. android::LruCache> mCache; android::LruCache> mIdCache; AssetManagerSymbolTable() : mCache(200), mIdCache(200) { } const Symbol* findByName(const ResourceName& name) override; const Symbol* findById(ResourceId id) override; }; std::unique_ptr mSymbolTable = util::make_unique(); public: AssetManagerSymbolTableBuilder& add(std::unique_ptr assetManager) { mSymbolTable->mAssets.push_back(std::move(assetManager)); return *this; } std::unique_ptr build() { return std::move(mSymbolTable); } }; class JoinedSymbolTableBuilder { private: struct JoinedSymbolTable : public ISymbolTable { std::vector> mSymbolTables; const Symbol* findByName(const ResourceName& name) override; const Symbol* findById(ResourceId id) override; }; std::unique_ptr mSymbolTable = util::make_unique(); public: JoinedSymbolTableBuilder& addSymbolTable(std::unique_ptr table) { mSymbolTable->mSymbolTables.push_back(std::move(table)); return *this; } std::unique_ptr build() { return std::move(mSymbolTable); } }; } // namespace aapt #endif /* AAPT_PROCESS_SYMBOLTABLE_H */