blob: 9126b9592faa95a2f47c448bd12ea63ae2ea1836 [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 Lesinski96917c22016-03-09 13:11:25 -080080 ResourceName() : type(ResourceType::kRaw) {}
Adam Lesinski9ba47d82015-10-13 11:37:10 -070081 ResourceName(const StringPiece16& p, ResourceType t, const StringPiece16& e);
82
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080083 bool isValid() const;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070084 std::u16string toString() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080085};
86
87/**
88 * Same as ResourceName, but uses StringPieces instead.
89 * Use this if you need to avoid copying and know that
90 * the lifetime of this object is shorter than that
91 * of the original string.
92 */
93struct ResourceNameRef {
94 StringPiece16 package;
95 ResourceType type;
96 StringPiece16 entry;
97
98 ResourceNameRef() = default;
99 ResourceNameRef(const ResourceNameRef&) = default;
100 ResourceNameRef(ResourceNameRef&&) = default;
Chih-Hung Hsieh8bd37ba2016-08-10 14:15:30 -0700101 ResourceNameRef(const ResourceName& rhs); // NOLINT(implicit)
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800102 ResourceNameRef(const StringPiece16& p, ResourceType t, const StringPiece16& e);
Adam Lesinski838a6872015-05-01 13:14:05 -0700103 ResourceNameRef& operator=(const ResourceNameRef& rhs) = default;
104 ResourceNameRef& operator=(ResourceNameRef&& rhs) = default;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800105 ResourceNameRef& operator=(const ResourceName& rhs);
106
107 ResourceName toResourceName() const;
108 bool isValid() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800109};
110
111/**
112 * A binary identifier representing a resource. Internally it
113 * is a 32bit integer split as follows:
114 *
115 * 0xPPTTEEEE
116 *
117 * PP: 8 bit package identifier. 0x01 is reserved for system
118 * and 0x7f is reserved for the running app.
119 * TT: 8 bit type identifier. 0x00 is invalid.
120 * EEEE: 16 bit entry identifier.
121 */
122struct ResourceId {
123 uint32_t id;
124
125 ResourceId();
126 ResourceId(const ResourceId& rhs);
Chih-Hung Hsieh8bd37ba2016-08-10 14:15:30 -0700127 ResourceId(uint32_t resId); // NOLINT(implicit)
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700128 ResourceId(uint8_t p, uint8_t t, uint16_t e);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800129
130 bool isValid() const;
131 uint8_t packageId() const;
132 uint8_t typeId() const;
133 uint16_t entryId() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800134};
135
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700136struct SourcedResourceName {
137 ResourceName name;
138 size_t line;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700139};
140
141struct ResourceFile {
142 // Name
143 ResourceName name;
144
145 // Configuration
146 ConfigDescription config;
147
148 // Source
149 Source source;
150
151 // Exported symbols
152 std::vector<SourcedResourceName> exportedSymbols;
153};
154
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800155/**
156 * Useful struct used as a key to represent a unique resource in associative containers.
157 */
158struct ResourceKey {
159 ResourceName name;
160 ConfigDescription config;
161};
162
163bool operator<(const ResourceKey& a, const ResourceKey& b);
164
165/**
166 * Useful struct used as a key to represent a unique resource in associative containers.
167 * Holds a reference to the name, so that name better live longer than this key!
168 */
169struct ResourceKeyRef {
170 ResourceNameRef name;
171 ConfigDescription config;
172
173 ResourceKeyRef() = default;
174 ResourceKeyRef(const ResourceNameRef& n, const ConfigDescription& c) : name(n), config(c) {
175 }
176
177 /**
178 * Prevent taking a reference to a temporary. This is bad.
179 */
180 ResourceKeyRef(ResourceName&& n, const ConfigDescription& c) = delete;
181};
182
183bool operator<(const ResourceKeyRef& a, const ResourceKeyRef& b);
184
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800185//
186// ResourceId implementation.
187//
188
189inline ResourceId::ResourceId() : id(0) {
190}
191
192inline ResourceId::ResourceId(const ResourceId& rhs) : id(rhs.id) {
193}
194
195inline ResourceId::ResourceId(uint32_t resId) : id(resId) {
196}
197
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700198inline ResourceId::ResourceId(uint8_t p, uint8_t t, uint16_t e) : id((p << 24) | (t << 16) | e) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800199}
200
201inline bool ResourceId::isValid() const {
202 return (id & 0xff000000u) != 0 && (id & 0x00ff0000u) != 0;
203}
204
205inline uint8_t ResourceId::packageId() const {
206 return static_cast<uint8_t>(id >> 24);
207}
208
209inline uint8_t ResourceId::typeId() const {
210 return static_cast<uint8_t>(id >> 16);
211}
212
213inline uint16_t ResourceId::entryId() const {
214 return static_cast<uint16_t>(id);
215}
216
Adam Lesinski74605cd2016-03-03 15:39:50 -0800217inline bool operator<(const ResourceId& lhs, const ResourceId& rhs) {
218 return lhs.id < rhs.id;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800219}
220
Adam Lesinski74605cd2016-03-03 15:39:50 -0800221inline bool operator>(const ResourceId& lhs, const ResourceId& rhs) {
222 return lhs.id > rhs.id;
Adam Lesinski24aad162015-04-24 19:19:30 -0700223}
224
Adam Lesinski74605cd2016-03-03 15:39:50 -0800225inline bool operator==(const ResourceId& lhs, const ResourceId& rhs) {
226 return lhs.id == rhs.id;
227}
228
229inline bool operator!=(const ResourceId& lhs, const ResourceId& rhs) {
230 return lhs.id != rhs.id;
231}
232
233inline ::std::ostream& operator<<(::std::ostream& out, const ResourceId& resId) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800234 std::ios_base::fmtflags oldFlags = out.flags();
235 char oldFill = out.fill();
236 out << "0x" << std::internal << std::setfill('0') << std::setw(8)
237 << std::hex << resId.id;
238 out.flags(oldFlags);
239 out.fill(oldFill);
240 return out;
241}
242
243//
244// ResourceType implementation.
245//
246
Adam Lesinski769de982015-04-10 19:43:55 -0700247inline ::std::ostream& operator<<(::std::ostream& out, const ResourceType& val) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800248 return out << toString(val);
249}
250
251//
252// ResourceName implementation.
253//
254
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700255inline ResourceName::ResourceName(const StringPiece16& p, ResourceType t, const StringPiece16& e) :
256 package(p.toString()), type(t), entry(e.toString()) {
257}
258
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800259inline bool ResourceName::isValid() const {
260 return !package.empty() && !entry.empty();
261}
262
Adam Lesinski74605cd2016-03-03 15:39:50 -0800263inline bool operator<(const ResourceName& lhs, const ResourceName& rhs) {
264 return std::tie(lhs.package, lhs.type, lhs.entry)
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800265 < std::tie(rhs.package, rhs.type, rhs.entry);
266}
267
Adam Lesinski74605cd2016-03-03 15:39:50 -0800268inline bool operator==(const ResourceName& lhs, const ResourceName& rhs) {
269 return std::tie(lhs.package, lhs.type, lhs.entry)
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800270 == std::tie(rhs.package, rhs.type, rhs.entry);
271}
272
Adam Lesinski74605cd2016-03-03 15:39:50 -0800273inline bool operator!=(const ResourceName& lhs, const ResourceName& rhs) {
274 return std::tie(lhs.package, lhs.type, lhs.entry)
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800275 != std::tie(rhs.package, rhs.type, rhs.entry);
276}
277
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700278inline std::u16string ResourceName::toString() const {
279 std::u16string result;
280 if (!package.empty()) {
281 result = package + u":";
282 }
283 return result + aapt::toString(type).toString() + u"/" + entry;
284}
285
Adam Lesinski769de982015-04-10 19:43:55 -0700286inline ::std::ostream& operator<<(::std::ostream& out, const ResourceName& name) {
287 if (!name.package.empty()) {
288 out << name.package << ":";
289 }
290 return out << name.type << "/" << name.entry;
291}
292
293
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800294//
295// ResourceNameRef implementation.
296//
297
298inline ResourceNameRef::ResourceNameRef(const ResourceName& rhs) :
299 package(rhs.package), type(rhs.type), entry(rhs.entry) {
300}
301
302inline ResourceNameRef::ResourceNameRef(const StringPiece16& p, ResourceType t,
303 const StringPiece16& e) :
304 package(p), type(t), entry(e) {
305}
306
307inline ResourceNameRef& ResourceNameRef::operator=(const ResourceName& rhs) {
308 package = rhs.package;
309 type = rhs.type;
310 entry = rhs.entry;
311 return *this;
312}
313
314inline ResourceName ResourceNameRef::toResourceName() const {
315 return { package.toString(), type, entry.toString() };
316}
317
318inline bool ResourceNameRef::isValid() const {
319 return !package.empty() && !entry.empty();
320}
321
Adam Lesinski74605cd2016-03-03 15:39:50 -0800322inline bool operator<(const ResourceNameRef& lhs, const ResourceNameRef& rhs) {
323 return std::tie(lhs.package, lhs.type, lhs.entry)
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800324 < std::tie(rhs.package, rhs.type, rhs.entry);
325}
326
Adam Lesinski74605cd2016-03-03 15:39:50 -0800327inline bool operator==(const ResourceNameRef& lhs, const ResourceNameRef& rhs) {
328 return std::tie(lhs.package, lhs.type, lhs.entry)
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800329 == std::tie(rhs.package, rhs.type, rhs.entry);
330}
331
Adam Lesinski74605cd2016-03-03 15:39:50 -0800332inline bool operator!=(const ResourceNameRef& lhs, const ResourceNameRef& rhs) {
333 return std::tie(lhs.package, lhs.type, lhs.entry)
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800334 != std::tie(rhs.package, rhs.type, rhs.entry);
335}
336
Adam Lesinski769de982015-04-10 19:43:55 -0700337inline ::std::ostream& operator<<(::std::ostream& out, const ResourceNameRef& name) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800338 if (!name.package.empty()) {
339 out << name.package << ":";
340 }
341 return out << name.type << "/" << name.entry;
342}
343
Adam Lesinski74605cd2016-03-03 15:39:50 -0800344inline bool operator<(const ResourceName& lhs, const ResourceNameRef& b) {
345 return ResourceNameRef(lhs) < b;
346}
347
348inline bool operator!=(const ResourceName& lhs, const ResourceNameRef& rhs) {
349 return ResourceNameRef(lhs) != rhs;
350}
351
352inline bool operator==(const SourcedResourceName& lhs, const SourcedResourceName& rhs) {
353 return lhs.name == rhs.name && lhs.line == rhs.line;
354}
355
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800356} // namespace aapt
357
358#endif // AAPT_RESOURCE_H