blob: 7ef18973d89be17c1084164e1251907d91c01cd2 [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,
50 kIntegerArray,
51 kInterpolator,
52 kLayout,
53 kMenu,
54 kMipmap,
55 kPlurals,
56 kRaw,
57 kString,
58 kStyle,
59 kStyleable,
60 kTransition,
61 kXml,
62};
63
64StringPiece16 toString(ResourceType type);
65
66/**
67 * Returns a pointer to a valid ResourceType, or nullptr if
68 * the string was invalid.
69 */
70const ResourceType* parseResourceType(const StringPiece16& str);
71
72/**
73 * A resource's name. This can uniquely identify
74 * a resource in the ResourceTable.
75 */
76struct ResourceName {
77 std::u16string package;
78 ResourceType type;
79 std::u16string entry;
80
Adam Lesinski9ba47d82015-10-13 11:37:10 -070081 ResourceName() = default;
82 ResourceName(const StringPiece16& p, ResourceType t, const StringPiece16& e);
83
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080084 bool isValid() const;
85 bool operator<(const ResourceName& rhs) const;
86 bool operator==(const ResourceName& rhs) const;
87 bool operator!=(const ResourceName& rhs) const;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070088 std::u16string toString() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080089};
90
91/**
92 * Same as ResourceName, but uses StringPieces instead.
93 * Use this if you need to avoid copying and know that
94 * the lifetime of this object is shorter than that
95 * of the original string.
96 */
97struct ResourceNameRef {
98 StringPiece16 package;
99 ResourceType type;
100 StringPiece16 entry;
101
102 ResourceNameRef() = default;
103 ResourceNameRef(const ResourceNameRef&) = default;
104 ResourceNameRef(ResourceNameRef&&) = default;
105 ResourceNameRef(const ResourceName& rhs);
106 ResourceNameRef(const StringPiece16& p, ResourceType t, const StringPiece16& e);
Adam Lesinski838a6872015-05-01 13:14:05 -0700107 ResourceNameRef& operator=(const ResourceNameRef& rhs) = default;
108 ResourceNameRef& operator=(ResourceNameRef&& rhs) = default;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800109 ResourceNameRef& operator=(const ResourceName& rhs);
110
111 ResourceName toResourceName() const;
112 bool isValid() const;
113
114 bool operator<(const ResourceNameRef& rhs) const;
115 bool operator==(const ResourceNameRef& rhs) const;
116 bool operator!=(const ResourceNameRef& rhs) const;
117};
118
119/**
120 * A binary identifier representing a resource. Internally it
121 * is a 32bit integer split as follows:
122 *
123 * 0xPPTTEEEE
124 *
125 * PP: 8 bit package identifier. 0x01 is reserved for system
126 * and 0x7f is reserved for the running app.
127 * TT: 8 bit type identifier. 0x00 is invalid.
128 * EEEE: 16 bit entry identifier.
129 */
130struct ResourceId {
131 uint32_t id;
132
133 ResourceId();
134 ResourceId(const ResourceId& rhs);
135 ResourceId(uint32_t resId);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700136 ResourceId(uint8_t p, uint8_t t, uint16_t e);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800137
138 bool isValid() const;
139 uint8_t packageId() const;
140 uint8_t typeId() const;
141 uint16_t entryId() const;
142 bool operator<(const ResourceId& rhs) const;
Adam Lesinski24aad162015-04-24 19:19:30 -0700143 bool operator==(const ResourceId& rhs) const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800144};
145
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700146struct SourcedResourceName {
147 ResourceName name;
148 size_t line;
149
150 inline bool operator==(const SourcedResourceName& rhs) const {
151 return name == rhs.name && line == rhs.line;
152 }
153};
154
155struct ResourceFile {
156 // Name
157 ResourceName name;
158
159 // Configuration
160 ConfigDescription config;
161
162 // Source
163 Source source;
164
165 // Exported symbols
166 std::vector<SourcedResourceName> exportedSymbols;
167};
168
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800169//
170// ResourceId implementation.
171//
172
173inline ResourceId::ResourceId() : id(0) {
174}
175
176inline ResourceId::ResourceId(const ResourceId& rhs) : id(rhs.id) {
177}
178
179inline ResourceId::ResourceId(uint32_t resId) : id(resId) {
180}
181
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700182inline ResourceId::ResourceId(uint8_t p, uint8_t t, uint16_t e) : id((p << 24) | (t << 16) | e) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800183}
184
185inline bool ResourceId::isValid() const {
186 return (id & 0xff000000u) != 0 && (id & 0x00ff0000u) != 0;
187}
188
189inline uint8_t ResourceId::packageId() const {
190 return static_cast<uint8_t>(id >> 24);
191}
192
193inline uint8_t ResourceId::typeId() const {
194 return static_cast<uint8_t>(id >> 16);
195}
196
197inline uint16_t ResourceId::entryId() const {
198 return static_cast<uint16_t>(id);
199}
200
201inline bool ResourceId::operator<(const ResourceId& rhs) const {
202 return id < rhs.id;
203}
204
Adam Lesinski24aad162015-04-24 19:19:30 -0700205inline bool ResourceId::operator==(const ResourceId& rhs) const {
206 return id == rhs.id;
207}
208
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800209inline ::std::ostream& operator<<(::std::ostream& out,
210 const ResourceId& resId) {
211 std::ios_base::fmtflags oldFlags = out.flags();
212 char oldFill = out.fill();
213 out << "0x" << std::internal << std::setfill('0') << std::setw(8)
214 << std::hex << resId.id;
215 out.flags(oldFlags);
216 out.fill(oldFill);
217 return out;
218}
219
220//
221// ResourceType implementation.
222//
223
Adam Lesinski769de982015-04-10 19:43:55 -0700224inline ::std::ostream& operator<<(::std::ostream& out, const ResourceType& val) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800225 return out << toString(val);
226}
227
228//
229// ResourceName implementation.
230//
231
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700232inline ResourceName::ResourceName(const StringPiece16& p, ResourceType t, const StringPiece16& e) :
233 package(p.toString()), type(t), entry(e.toString()) {
234}
235
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800236inline bool ResourceName::isValid() const {
237 return !package.empty() && !entry.empty();
238}
239
240inline bool ResourceName::operator<(const ResourceName& rhs) const {
241 return std::tie(package, type, entry)
242 < std::tie(rhs.package, rhs.type, rhs.entry);
243}
244
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700245inline bool operator<(const ResourceName& lhs, const ResourceNameRef& b) {
246 return ResourceNameRef(lhs) < b;
247}
248
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800249inline bool ResourceName::operator==(const ResourceName& rhs) const {
250 return std::tie(package, type, entry)
251 == std::tie(rhs.package, rhs.type, rhs.entry);
252}
253
254inline bool ResourceName::operator!=(const ResourceName& rhs) const {
255 return std::tie(package, type, entry)
256 != std::tie(rhs.package, rhs.type, rhs.entry);
257}
258
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700259inline bool operator!=(const ResourceName& lhs, const ResourceNameRef& rhs) {
260 return ResourceNameRef(lhs) != rhs;
261}
262
263inline std::u16string ResourceName::toString() const {
264 std::u16string result;
265 if (!package.empty()) {
266 result = package + u":";
267 }
268 return result + aapt::toString(type).toString() + u"/" + entry;
269}
270
Adam Lesinski769de982015-04-10 19:43:55 -0700271inline ::std::ostream& operator<<(::std::ostream& out, const ResourceName& name) {
272 if (!name.package.empty()) {
273 out << name.package << ":";
274 }
275 return out << name.type << "/" << name.entry;
276}
277
278
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800279//
280// ResourceNameRef implementation.
281//
282
283inline ResourceNameRef::ResourceNameRef(const ResourceName& rhs) :
284 package(rhs.package), type(rhs.type), entry(rhs.entry) {
285}
286
287inline ResourceNameRef::ResourceNameRef(const StringPiece16& p, ResourceType t,
288 const StringPiece16& e) :
289 package(p), type(t), entry(e) {
290}
291
292inline ResourceNameRef& ResourceNameRef::operator=(const ResourceName& rhs) {
293 package = rhs.package;
294 type = rhs.type;
295 entry = rhs.entry;
296 return *this;
297}
298
299inline ResourceName ResourceNameRef::toResourceName() const {
300 return { package.toString(), type, entry.toString() };
301}
302
303inline bool ResourceNameRef::isValid() const {
304 return !package.empty() && !entry.empty();
305}
306
307inline bool ResourceNameRef::operator<(const ResourceNameRef& rhs) const {
308 return std::tie(package, type, entry)
309 < std::tie(rhs.package, rhs.type, rhs.entry);
310}
311
312inline bool ResourceNameRef::operator==(const ResourceNameRef& rhs) const {
313 return std::tie(package, type, entry)
314 == std::tie(rhs.package, rhs.type, rhs.entry);
315}
316
317inline bool ResourceNameRef::operator!=(const ResourceNameRef& rhs) const {
318 return std::tie(package, type, entry)
319 != std::tie(rhs.package, rhs.type, rhs.entry);
320}
321
Adam Lesinski769de982015-04-10 19:43:55 -0700322inline ::std::ostream& operator<<(::std::ostream& out, const ResourceNameRef& name) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800323 if (!name.package.empty()) {
324 out << name.package << ":";
325 }
326 return out << name.type << "/" << name.entry;
327}
328
329} // namespace aapt
330
331#endif // AAPT_RESOURCE_H