blob: a7afbb5e4018aeaa8111799e143252edf84bbd56 [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 Lesinski6f6ceb72014-11-14 14:48:12 -0800168//
169// ResourceId implementation.
170//
171
172inline ResourceId::ResourceId() : id(0) {
173}
174
175inline ResourceId::ResourceId(const ResourceId& rhs) : id(rhs.id) {
176}
177
178inline ResourceId::ResourceId(uint32_t resId) : id(resId) {
179}
180
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700181inline ResourceId::ResourceId(uint8_t p, uint8_t t, uint16_t e) : id((p << 24) | (t << 16) | e) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800182}
183
184inline bool ResourceId::isValid() const {
185 return (id & 0xff000000u) != 0 && (id & 0x00ff0000u) != 0;
186}
187
188inline uint8_t ResourceId::packageId() const {
189 return static_cast<uint8_t>(id >> 24);
190}
191
192inline uint8_t ResourceId::typeId() const {
193 return static_cast<uint8_t>(id >> 16);
194}
195
196inline uint16_t ResourceId::entryId() const {
197 return static_cast<uint16_t>(id);
198}
199
200inline bool ResourceId::operator<(const ResourceId& rhs) const {
201 return id < rhs.id;
202}
203
Adam Lesinski24aad162015-04-24 19:19:30 -0700204inline bool ResourceId::operator==(const ResourceId& rhs) const {
205 return id == rhs.id;
206}
207
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800208inline ::std::ostream& operator<<(::std::ostream& out,
209 const ResourceId& resId) {
210 std::ios_base::fmtflags oldFlags = out.flags();
211 char oldFill = out.fill();
212 out << "0x" << std::internal << std::setfill('0') << std::setw(8)
213 << std::hex << resId.id;
214 out.flags(oldFlags);
215 out.fill(oldFill);
216 return out;
217}
218
219//
220// ResourceType implementation.
221//
222
Adam Lesinski769de982015-04-10 19:43:55 -0700223inline ::std::ostream& operator<<(::std::ostream& out, const ResourceType& val) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800224 return out << toString(val);
225}
226
227//
228// ResourceName implementation.
229//
230
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700231inline ResourceName::ResourceName(const StringPiece16& p, ResourceType t, const StringPiece16& e) :
232 package(p.toString()), type(t), entry(e.toString()) {
233}
234
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800235inline bool ResourceName::isValid() const {
236 return !package.empty() && !entry.empty();
237}
238
239inline bool ResourceName::operator<(const ResourceName& rhs) const {
240 return std::tie(package, type, entry)
241 < std::tie(rhs.package, rhs.type, rhs.entry);
242}
243
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700244inline bool operator<(const ResourceName& lhs, const ResourceNameRef& b) {
245 return ResourceNameRef(lhs) < b;
246}
247
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800248inline bool ResourceName::operator==(const ResourceName& rhs) const {
249 return std::tie(package, type, entry)
250 == std::tie(rhs.package, rhs.type, rhs.entry);
251}
252
253inline bool ResourceName::operator!=(const ResourceName& rhs) const {
254 return std::tie(package, type, entry)
255 != std::tie(rhs.package, rhs.type, rhs.entry);
256}
257
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700258inline bool operator!=(const ResourceName& lhs, const ResourceNameRef& rhs) {
259 return ResourceNameRef(lhs) != rhs;
260}
261
262inline std::u16string ResourceName::toString() const {
263 std::u16string result;
264 if (!package.empty()) {
265 result = package + u":";
266 }
267 return result + aapt::toString(type).toString() + u"/" + entry;
268}
269
Adam Lesinski769de982015-04-10 19:43:55 -0700270inline ::std::ostream& operator<<(::std::ostream& out, const ResourceName& name) {
271 if (!name.package.empty()) {
272 out << name.package << ":";
273 }
274 return out << name.type << "/" << name.entry;
275}
276
277
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800278//
279// ResourceNameRef implementation.
280//
281
282inline ResourceNameRef::ResourceNameRef(const ResourceName& rhs) :
283 package(rhs.package), type(rhs.type), entry(rhs.entry) {
284}
285
286inline ResourceNameRef::ResourceNameRef(const StringPiece16& p, ResourceType t,
287 const StringPiece16& e) :
288 package(p), type(t), entry(e) {
289}
290
291inline ResourceNameRef& ResourceNameRef::operator=(const ResourceName& rhs) {
292 package = rhs.package;
293 type = rhs.type;
294 entry = rhs.entry;
295 return *this;
296}
297
298inline ResourceName ResourceNameRef::toResourceName() const {
299 return { package.toString(), type, entry.toString() };
300}
301
302inline bool ResourceNameRef::isValid() const {
303 return !package.empty() && !entry.empty();
304}
305
306inline bool ResourceNameRef::operator<(const ResourceNameRef& rhs) const {
307 return std::tie(package, type, entry)
308 < std::tie(rhs.package, rhs.type, rhs.entry);
309}
310
311inline bool ResourceNameRef::operator==(const ResourceNameRef& rhs) const {
312 return std::tie(package, type, entry)
313 == std::tie(rhs.package, rhs.type, rhs.entry);
314}
315
316inline bool ResourceNameRef::operator!=(const ResourceNameRef& rhs) const {
317 return std::tie(package, type, entry)
318 != std::tie(rhs.package, rhs.type, rhs.entry);
319}
320
Adam Lesinski769de982015-04-10 19:43:55 -0700321inline ::std::ostream& operator<<(::std::ostream& out, const ResourceNameRef& name) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800322 if (!name.package.empty()) {
323 out << name.package << ":";
324 }
325 return out << name.type << "/" << name.entry;
326}
327
328} // namespace aapt
329
330#endif // AAPT_RESOURCE_H