blob: c71e249d71f26bbfb27cf9ac11f1fbd22b136bc5 [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_H
18#define AAPT_RESOURCE_H
19
Adam Lesinski1ab598f2015-08-14 14:26:04 -070020#include "ConfigDescription.h"
21#include "Source.h"
22
23#include "util/StringPiece.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080024
25#include <iomanip>
Adam Lesinskica2fc352015-04-03 12:08:26 -070026#include <limits>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080027#include <string>
28#include <tuple>
Adam Lesinski1ab598f2015-08-14 14:26:04 -070029#include <vector>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080030
31namespace aapt {
32
33/**
34 * The various types of resource types available. Corresponds
35 * to the 'type' in package:type/entry.
36 */
37enum class ResourceType {
38 kAnim,
39 kAnimator,
40 kArray,
41 kAttr,
42 kAttrPrivate,
43 kBool,
44 kColor,
45 kDimen,
46 kDrawable,
47 kFraction,
48 kId,
49 kInteger,
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080050 kInterpolator,
51 kLayout,
52 kMenu,
53 kMipmap,
54 kPlurals,
55 kRaw,
56 kString,
57 kStyle,
58 kStyleable,
59 kTransition,
60 kXml,
61};
62
63StringPiece16 toString(ResourceType type);
64
65/**
66 * Returns a pointer to a valid ResourceType, or nullptr if
67 * the string was invalid.
68 */
69const ResourceType* parseResourceType(const StringPiece16& str);
70
71/**
72 * A resource's name. This can uniquely identify
73 * a resource in the ResourceTable.
74 */
75struct ResourceName {
76 std::u16string package;
77 ResourceType type;
78 std::u16string entry;
79
Adam Lesinski9ba47d82015-10-13 11:37:10 -070080 ResourceName() = default;
81 ResourceName(const StringPiece16& p, ResourceType t, const StringPiece16& e);
82
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080083 bool isValid() const;
84 bool operator<(const ResourceName& rhs) const;
85 bool operator==(const ResourceName& rhs) const;
86 bool operator!=(const ResourceName& rhs) const;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070087 std::u16string toString() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080088};
89
90/**
91 * Same as ResourceName, but uses StringPieces instead.
92 * Use this if you need to avoid copying and know that
93 * the lifetime of this object is shorter than that
94 * of the original string.
95 */
96struct ResourceNameRef {
97 StringPiece16 package;
98 ResourceType type;
99 StringPiece16 entry;
100
101 ResourceNameRef() = default;
102 ResourceNameRef(const ResourceNameRef&) = default;
103 ResourceNameRef(ResourceNameRef&&) = default;
104 ResourceNameRef(const ResourceName& rhs);
105 ResourceNameRef(const StringPiece16& p, ResourceType t, const StringPiece16& e);
Adam Lesinski838a6872015-05-01 13:14:05 -0700106 ResourceNameRef& operator=(const ResourceNameRef& rhs) = default;
107 ResourceNameRef& operator=(ResourceNameRef&& rhs) = default;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800108 ResourceNameRef& operator=(const ResourceName& rhs);
109
110 ResourceName toResourceName() const;
111 bool isValid() const;
112
113 bool operator<(const ResourceNameRef& rhs) const;
114 bool operator==(const ResourceNameRef& rhs) const;
115 bool operator!=(const ResourceNameRef& rhs) const;
116};
117
118/**
119 * A binary identifier representing a resource. Internally it
120 * is a 32bit integer split as follows:
121 *
122 * 0xPPTTEEEE
123 *
124 * PP: 8 bit package identifier. 0x01 is reserved for system
125 * and 0x7f is reserved for the running app.
126 * TT: 8 bit type identifier. 0x00 is invalid.
127 * EEEE: 16 bit entry identifier.
128 */
129struct ResourceId {
130 uint32_t id;
131
132 ResourceId();
133 ResourceId(const ResourceId& rhs);
134 ResourceId(uint32_t resId);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700135 ResourceId(uint8_t p, uint8_t t, uint16_t e);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800136
137 bool isValid() const;
138 uint8_t packageId() const;
139 uint8_t typeId() const;
140 uint16_t entryId() const;
141 bool operator<(const ResourceId& rhs) const;
Adam Lesinski24aad162015-04-24 19:19:30 -0700142 bool operator==(const ResourceId& rhs) const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800143};
144
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700145struct SourcedResourceName {
146 ResourceName name;
147 size_t line;
148
149 inline bool operator==(const SourcedResourceName& rhs) const {
150 return name == rhs.name && line == rhs.line;
151 }
152};
153
154struct ResourceFile {
155 // Name
156 ResourceName name;
157
158 // Configuration
159 ConfigDescription config;
160
161 // Source
162 Source source;
163
164 // Exported symbols
165 std::vector<SourcedResourceName> exportedSymbols;
166};
167
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800168/**
169 * Useful struct used as a key to represent a unique resource in associative containers.
170 */
171struct ResourceKey {
172 ResourceName name;
173 ConfigDescription config;
174};
175
176bool operator<(const ResourceKey& a, const ResourceKey& b);
177
178/**
179 * Useful struct used as a key to represent a unique resource in associative containers.
180 * Holds a reference to the name, so that name better live longer than this key!
181 */
182struct ResourceKeyRef {
183 ResourceNameRef name;
184 ConfigDescription config;
185
186 ResourceKeyRef() = default;
187 ResourceKeyRef(const ResourceNameRef& n, const ConfigDescription& c) : name(n), config(c) {
188 }
189
190 /**
191 * Prevent taking a reference to a temporary. This is bad.
192 */
193 ResourceKeyRef(ResourceName&& n, const ConfigDescription& c) = delete;
194};
195
196bool operator<(const ResourceKeyRef& a, const ResourceKeyRef& b);
197
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800198//
199// ResourceId implementation.
200//
201
202inline ResourceId::ResourceId() : id(0) {
203}
204
205inline ResourceId::ResourceId(const ResourceId& rhs) : id(rhs.id) {
206}
207
208inline ResourceId::ResourceId(uint32_t resId) : id(resId) {
209}
210
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700211inline ResourceId::ResourceId(uint8_t p, uint8_t t, uint16_t e) : id((p << 24) | (t << 16) | e) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800212}
213
214inline bool ResourceId::isValid() const {
215 return (id & 0xff000000u) != 0 && (id & 0x00ff0000u) != 0;
216}
217
218inline uint8_t ResourceId::packageId() const {
219 return static_cast<uint8_t>(id >> 24);
220}
221
222inline uint8_t ResourceId::typeId() const {
223 return static_cast<uint8_t>(id >> 16);
224}
225
226inline uint16_t ResourceId::entryId() const {
227 return static_cast<uint16_t>(id);
228}
229
230inline bool ResourceId::operator<(const ResourceId& rhs) const {
231 return id < rhs.id;
232}
233
Adam Lesinski24aad162015-04-24 19:19:30 -0700234inline bool ResourceId::operator==(const ResourceId& rhs) const {
235 return id == rhs.id;
236}
237
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800238inline ::std::ostream& operator<<(::std::ostream& out,
239 const ResourceId& resId) {
240 std::ios_base::fmtflags oldFlags = out.flags();
241 char oldFill = out.fill();
242 out << "0x" << std::internal << std::setfill('0') << std::setw(8)
243 << std::hex << resId.id;
244 out.flags(oldFlags);
245 out.fill(oldFill);
246 return out;
247}
248
249//
250// ResourceType implementation.
251//
252
Adam Lesinski769de982015-04-10 19:43:55 -0700253inline ::std::ostream& operator<<(::std::ostream& out, const ResourceType& val) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800254 return out << toString(val);
255}
256
257//
258// ResourceName implementation.
259//
260
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700261inline ResourceName::ResourceName(const StringPiece16& p, ResourceType t, const StringPiece16& e) :
262 package(p.toString()), type(t), entry(e.toString()) {
263}
264
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800265inline bool ResourceName::isValid() const {
266 return !package.empty() && !entry.empty();
267}
268
269inline bool ResourceName::operator<(const ResourceName& rhs) const {
270 return std::tie(package, type, entry)
271 < std::tie(rhs.package, rhs.type, rhs.entry);
272}
273
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700274inline bool operator<(const ResourceName& lhs, const ResourceNameRef& b) {
275 return ResourceNameRef(lhs) < b;
276}
277
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800278inline bool ResourceName::operator==(const ResourceName& rhs) const {
279 return std::tie(package, type, entry)
280 == std::tie(rhs.package, rhs.type, rhs.entry);
281}
282
283inline bool ResourceName::operator!=(const ResourceName& rhs) const {
284 return std::tie(package, type, entry)
285 != std::tie(rhs.package, rhs.type, rhs.entry);
286}
287
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700288inline bool operator!=(const ResourceName& lhs, const ResourceNameRef& rhs) {
289 return ResourceNameRef(lhs) != rhs;
290}
291
292inline std::u16string ResourceName::toString() const {
293 std::u16string result;
294 if (!package.empty()) {
295 result = package + u":";
296 }
297 return result + aapt::toString(type).toString() + u"/" + entry;
298}
299
Adam Lesinski769de982015-04-10 19:43:55 -0700300inline ::std::ostream& operator<<(::std::ostream& out, const ResourceName& name) {
301 if (!name.package.empty()) {
302 out << name.package << ":";
303 }
304 return out << name.type << "/" << name.entry;
305}
306
307
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800308//
309// ResourceNameRef implementation.
310//
311
312inline ResourceNameRef::ResourceNameRef(const ResourceName& rhs) :
313 package(rhs.package), type(rhs.type), entry(rhs.entry) {
314}
315
316inline ResourceNameRef::ResourceNameRef(const StringPiece16& p, ResourceType t,
317 const StringPiece16& e) :
318 package(p), type(t), entry(e) {
319}
320
321inline ResourceNameRef& ResourceNameRef::operator=(const ResourceName& rhs) {
322 package = rhs.package;
323 type = rhs.type;
324 entry = rhs.entry;
325 return *this;
326}
327
328inline ResourceName ResourceNameRef::toResourceName() const {
329 return { package.toString(), type, entry.toString() };
330}
331
332inline bool ResourceNameRef::isValid() const {
333 return !package.empty() && !entry.empty();
334}
335
336inline bool ResourceNameRef::operator<(const ResourceNameRef& rhs) const {
337 return std::tie(package, type, entry)
338 < std::tie(rhs.package, rhs.type, rhs.entry);
339}
340
341inline bool ResourceNameRef::operator==(const ResourceNameRef& rhs) const {
342 return std::tie(package, type, entry)
343 == std::tie(rhs.package, rhs.type, rhs.entry);
344}
345
346inline bool ResourceNameRef::operator!=(const ResourceNameRef& rhs) const {
347 return std::tie(package, type, entry)
348 != std::tie(rhs.package, rhs.type, rhs.entry);
349}
350
Adam Lesinski769de982015-04-10 19:43:55 -0700351inline ::std::ostream& operator<<(::std::ostream& out, const ResourceNameRef& name) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800352 if (!name.package.empty()) {
353 out << name.package << ":";
354 }
355 return out << name.type << "/" << name.entry;
356}
357
358} // namespace aapt
359
360#endif // AAPT_RESOURCE_H