blob: 94bacd80bbb28973ce2785587faf8054a4b9884f [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef AAPT_RESOURCE_TABLE_H
18#define AAPT_RESOURCE_TABLE_H
19
20#include "ConfigDescription.h"
21#include "Resource.h"
22#include "ResourceValues.h"
23#include "Source.h"
24#include "StringPool.h"
25
26#include <memory>
27#include <string>
28#include <tuple>
29#include <vector>
30
31namespace aapt {
32
33/**
34 * The Public status of a resource.
35 */
36struct Public {
37 bool isPublic = false;
Adam Lesinski6ff19662015-04-30 17:40:46 -070038 SourceLine source;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080039 std::u16string comment;
40};
41
42/**
43 * The resource value for a specific configuration.
44 */
45struct ResourceConfigValue {
46 ConfigDescription config;
47 SourceLine source;
48 std::u16string comment;
49 std::unique_ptr<Value> value;
50};
51
52/**
53 * Represents a resource entry, which may have
54 * varying values for each defined configuration.
55 */
56struct ResourceEntry {
57 enum {
58 kUnsetEntryId = 0xffffffffu
59 };
60
61 /**
62 * The name of the resource. Immutable, as
63 * this determines the order of this resource
64 * when doing lookups.
65 */
66 const std::u16string name;
67
68 /**
69 * The entry ID for this resource.
70 */
71 size_t entryId;
72
73 /**
74 * Whether this resource is public (and must maintain the same
75 * entry ID across builds).
76 */
77 Public publicStatus;
78
79 /**
80 * The resource's values for each configuration.
81 */
82 std::vector<ResourceConfigValue> values;
83
84 inline ResourceEntry(const StringPiece16& _name);
85 inline ResourceEntry(const ResourceEntry* rhs);
86};
87
88/**
89 * Represents a resource type, which holds entries defined
90 * for this type.
91 */
92struct ResourceTableType {
93 enum {
94 kUnsetTypeId = 0xffffffffu
95 };
96
97 /**
98 * The logical type of resource (string, drawable, layout, etc.).
99 */
100 const ResourceType type;
101
102 /**
103 * The type ID for this resource.
104 */
105 size_t typeId;
106
107 /**
108 * Whether this type is public (and must maintain the same
109 * type ID across builds).
110 */
111 Public publicStatus;
112
113 /**
114 * List of resources for this type.
115 */
116 std::vector<std::unique_ptr<ResourceEntry>> entries;
117
118 ResourceTableType(const ResourceType _type);
119 ResourceTableType(const ResourceTableType* rhs);
120};
121
122/**
123 * The container and index for all resources defined for an app. This gets
124 * flattened into a binary resource table (resources.arsc).
125 */
126class ResourceTable {
127public:
128 using iterator = std::vector<std::unique_ptr<ResourceTableType>>::iterator;
129 using const_iterator = std::vector<std::unique_ptr<ResourceTableType>>::const_iterator;
130
131 enum {
132 kUnsetPackageId = 0xffffffff
133 };
134
135 ResourceTable();
136
137 size_t getPackageId() const;
138 void setPackageId(size_t packageId);
139
140 const std::u16string& getPackage() const;
141 void setPackage(const StringPiece16& package);
142
143 bool addResource(const ResourceNameRef& name, const ConfigDescription& config,
144 const SourceLine& source, std::unique_ptr<Value> value);
145
146 bool addResource(const ResourceNameRef& name, const ResourceId resId,
147 const ConfigDescription& config, const SourceLine& source,
148 std::unique_ptr<Value> value);
149
150 bool markPublic(const ResourceNameRef& name, const ResourceId resId, const SourceLine& source);
151
Adam Lesinski769de982015-04-10 19:43:55 -0700152 /*
153 * Merges the resources from `other` into this table, mangling the names of the resources
154 * if `other` has a different package name.
155 */
156 bool merge(ResourceTable&& other);
157
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800158 /**
159 * Returns the string pool used by this ResourceTable.
160 * Values that reference strings should use this pool to create
161 * their strings.
162 */
163 StringPool& getValueStringPool();
164 const StringPool& getValueStringPool() const;
165
166 std::tuple<const ResourceTableType*, const ResourceEntry*>
167 findResource(const ResourceNameRef& name) const;
168
169 iterator begin();
170 iterator end();
171 const_iterator begin() const;
172 const_iterator end() const;
173
174private:
175 std::unique_ptr<ResourceTableType>& findOrCreateType(ResourceType type);
176 std::unique_ptr<ResourceEntry>& findOrCreateEntry(std::unique_ptr<ResourceTableType>& type,
177 const StringPiece16& name);
178
179 std::u16string mPackage;
180 size_t mPackageId;
181
182 // StringPool must come before mTypes so that it is destroyed after.
183 // When StringPool references are destroyed (as they will be when mTypes
184 // is destroyed), they decrement a refCount, which would cause invalid
185 // memory access if the pool was already destroyed.
186 StringPool mValuePool;
187
188 std::vector<std::unique_ptr<ResourceTableType>> mTypes;
189};
190
191//
192// ResourceEntry implementation.
193//
194
195inline ResourceEntry::ResourceEntry(const StringPiece16& _name) :
196 name(_name.toString()), entryId(kUnsetEntryId) {
197}
198
199inline ResourceEntry::ResourceEntry(const ResourceEntry* rhs) :
200 name(rhs->name), entryId(rhs->entryId), publicStatus(rhs->publicStatus) {
201}
202
203//
204// ResourceTableType implementation.
205//
206
207inline ResourceTableType::ResourceTableType(const ResourceType _type) :
208 type(_type), typeId(kUnsetTypeId) {
209}
210
211inline ResourceTableType::ResourceTableType(const ResourceTableType* rhs) :
212 type(rhs->type), typeId(rhs->typeId), publicStatus(rhs->publicStatus) {
213}
214
215//
216// ResourceTable implementation.
217//
218
219inline StringPool& ResourceTable::getValueStringPool() {
220 return mValuePool;
221}
222
223inline const StringPool& ResourceTable::getValueStringPool() const {
224 return mValuePool;
225}
226
227inline ResourceTable::iterator ResourceTable::begin() {
228 return mTypes.begin();
229}
230
231inline ResourceTable::iterator ResourceTable::end() {
232 return mTypes.end();
233}
234
235inline ResourceTable::const_iterator ResourceTable::begin() const {
236 return mTypes.begin();
237}
238
239inline ResourceTable::const_iterator ResourceTable::end() const {
240 return mTypes.end();
241}
242
243inline const std::u16string& ResourceTable::getPackage() const {
244 return mPackage;
245}
246
247inline size_t ResourceTable::getPackageId() const {
248 return mPackageId;
249}
250
251inline void ResourceTable::setPackage(const StringPiece16& package) {
252 mPackage = package.toString();
253}
254
255inline void ResourceTable::setPackageId(size_t packageId) {
256 mPackageId = packageId;
257}
258
259} // namespace aapt
260
261#endif // AAPT_RESOURCE_TABLE_H