blob: 31fe298670ae2f03745c3a66aacc83f22e14257c [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
81 bool isValid() const;
82 bool operator<(const ResourceName& rhs) const;
83 bool operator==(const ResourceName& rhs) const;
84 bool operator!=(const ResourceName& rhs) const;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070085 std::u16string toString() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080086};
87
88/**
89 * Same as ResourceName, but uses StringPieces instead.
90 * Use this if you need to avoid copying and know that
91 * the lifetime of this object is shorter than that
92 * of the original string.
93 */
94struct ResourceNameRef {
95 StringPiece16 package;
96 ResourceType type;
97 StringPiece16 entry;
98
99 ResourceNameRef() = default;
100 ResourceNameRef(const ResourceNameRef&) = default;
101 ResourceNameRef(ResourceNameRef&&) = default;
102 ResourceNameRef(const ResourceName& rhs);
103 ResourceNameRef(const StringPiece16& p, ResourceType t, const StringPiece16& e);
Adam Lesinski838a6872015-05-01 13:14:05 -0700104 ResourceNameRef& operator=(const ResourceNameRef& rhs) = default;
105 ResourceNameRef& operator=(ResourceNameRef&& rhs) = default;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800106 ResourceNameRef& operator=(const ResourceName& rhs);
107
108 ResourceName toResourceName() const;
109 bool isValid() const;
110
111 bool operator<(const ResourceNameRef& rhs) const;
112 bool operator==(const ResourceNameRef& rhs) const;
113 bool operator!=(const ResourceNameRef& rhs) const;
114};
115
116/**
117 * A binary identifier representing a resource. Internally it
118 * is a 32bit integer split as follows:
119 *
120 * 0xPPTTEEEE
121 *
122 * PP: 8 bit package identifier. 0x01 is reserved for system
123 * and 0x7f is reserved for the running app.
124 * TT: 8 bit type identifier. 0x00 is invalid.
125 * EEEE: 16 bit entry identifier.
126 */
127struct ResourceId {
128 uint32_t id;
129
130 ResourceId();
131 ResourceId(const ResourceId& rhs);
132 ResourceId(uint32_t resId);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700133 ResourceId(uint8_t p, uint8_t t, uint16_t e);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800134
135 bool isValid() const;
136 uint8_t packageId() const;
137 uint8_t typeId() const;
138 uint16_t entryId() const;
139 bool operator<(const ResourceId& rhs) const;
Adam Lesinski24aad162015-04-24 19:19:30 -0700140 bool operator==(const ResourceId& rhs) const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800141};
142
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700143struct SourcedResourceName {
144 ResourceName name;
145 size_t line;
146
147 inline bool operator==(const SourcedResourceName& rhs) const {
148 return name == rhs.name && line == rhs.line;
149 }
150};
151
152struct ResourceFile {
153 // Name
154 ResourceName name;
155
156 // Configuration
157 ConfigDescription config;
158
159 // Source
160 Source source;
161
162 // Exported symbols
163 std::vector<SourcedResourceName> exportedSymbols;
164};
165
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800166//
167// ResourceId implementation.
168//
169
170inline ResourceId::ResourceId() : id(0) {
171}
172
173inline ResourceId::ResourceId(const ResourceId& rhs) : id(rhs.id) {
174}
175
176inline ResourceId::ResourceId(uint32_t resId) : id(resId) {
177}
178
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700179inline ResourceId::ResourceId(uint8_t p, uint8_t t, uint16_t e) : id((p << 24) | (t << 16) | e) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800180}
181
182inline bool ResourceId::isValid() const {
183 return (id & 0xff000000u) != 0 && (id & 0x00ff0000u) != 0;
184}
185
186inline uint8_t ResourceId::packageId() const {
187 return static_cast<uint8_t>(id >> 24);
188}
189
190inline uint8_t ResourceId::typeId() const {
191 return static_cast<uint8_t>(id >> 16);
192}
193
194inline uint16_t ResourceId::entryId() const {
195 return static_cast<uint16_t>(id);
196}
197
198inline bool ResourceId::operator<(const ResourceId& rhs) const {
199 return id < rhs.id;
200}
201
Adam Lesinski24aad162015-04-24 19:19:30 -0700202inline bool ResourceId::operator==(const ResourceId& rhs) const {
203 return id == rhs.id;
204}
205
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800206inline ::std::ostream& operator<<(::std::ostream& out,
207 const ResourceId& resId) {
208 std::ios_base::fmtflags oldFlags = out.flags();
209 char oldFill = out.fill();
210 out << "0x" << std::internal << std::setfill('0') << std::setw(8)
211 << std::hex << resId.id;
212 out.flags(oldFlags);
213 out.fill(oldFill);
214 return out;
215}
216
217//
218// ResourceType implementation.
219//
220
Adam Lesinski769de982015-04-10 19:43:55 -0700221inline ::std::ostream& operator<<(::std::ostream& out, const ResourceType& val) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800222 return out << toString(val);
223}
224
225//
226// ResourceName implementation.
227//
228
229inline bool ResourceName::isValid() const {
230 return !package.empty() && !entry.empty();
231}
232
233inline bool ResourceName::operator<(const ResourceName& rhs) const {
234 return std::tie(package, type, entry)
235 < std::tie(rhs.package, rhs.type, rhs.entry);
236}
237
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700238inline bool operator<(const ResourceName& lhs, const ResourceNameRef& b) {
239 return ResourceNameRef(lhs) < b;
240}
241
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800242inline bool ResourceName::operator==(const ResourceName& rhs) const {
243 return std::tie(package, type, entry)
244 == std::tie(rhs.package, rhs.type, rhs.entry);
245}
246
247inline bool ResourceName::operator!=(const ResourceName& rhs) const {
248 return std::tie(package, type, entry)
249 != std::tie(rhs.package, rhs.type, rhs.entry);
250}
251
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700252inline bool operator!=(const ResourceName& lhs, const ResourceNameRef& rhs) {
253 return ResourceNameRef(lhs) != rhs;
254}
255
256inline std::u16string ResourceName::toString() const {
257 std::u16string result;
258 if (!package.empty()) {
259 result = package + u":";
260 }
261 return result + aapt::toString(type).toString() + u"/" + entry;
262}
263
Adam Lesinski769de982015-04-10 19:43:55 -0700264inline ::std::ostream& operator<<(::std::ostream& out, const ResourceName& name) {
265 if (!name.package.empty()) {
266 out << name.package << ":";
267 }
268 return out << name.type << "/" << name.entry;
269}
270
271
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800272//
273// ResourceNameRef implementation.
274//
275
276inline ResourceNameRef::ResourceNameRef(const ResourceName& rhs) :
277 package(rhs.package), type(rhs.type), entry(rhs.entry) {
278}
279
280inline ResourceNameRef::ResourceNameRef(const StringPiece16& p, ResourceType t,
281 const StringPiece16& e) :
282 package(p), type(t), entry(e) {
283}
284
285inline ResourceNameRef& ResourceNameRef::operator=(const ResourceName& rhs) {
286 package = rhs.package;
287 type = rhs.type;
288 entry = rhs.entry;
289 return *this;
290}
291
292inline ResourceName ResourceNameRef::toResourceName() const {
293 return { package.toString(), type, entry.toString() };
294}
295
296inline bool ResourceNameRef::isValid() const {
297 return !package.empty() && !entry.empty();
298}
299
300inline bool ResourceNameRef::operator<(const ResourceNameRef& rhs) const {
301 return std::tie(package, type, entry)
302 < std::tie(rhs.package, rhs.type, rhs.entry);
303}
304
305inline bool ResourceNameRef::operator==(const ResourceNameRef& rhs) const {
306 return std::tie(package, type, entry)
307 == std::tie(rhs.package, rhs.type, rhs.entry);
308}
309
310inline bool ResourceNameRef::operator!=(const ResourceNameRef& rhs) const {
311 return std::tie(package, type, entry)
312 != std::tie(rhs.package, rhs.type, rhs.entry);
313}
314
Adam Lesinski769de982015-04-10 19:43:55 -0700315inline ::std::ostream& operator<<(::std::ostream& out, const ResourceNameRef& name) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800316 if (!name.package.empty()) {
317 out << name.package << ":";
318 }
319 return out << name.type << "/" << name.entry;
320}
321
322} // namespace aapt
323
324#endif // AAPT_RESOURCE_H