blob: 31104fbe91b63f44d42bfc2db241e51ee4737577 [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
20#include "StringPiece.h"
21
22#include <iomanip>
Adam Lesinskica2fc352015-04-03 12:08:26 -070023#include <limits>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080024#include <string>
25#include <tuple>
26
27namespace aapt {
28
29/**
30 * The various types of resource types available. Corresponds
31 * to the 'type' in package:type/entry.
32 */
33enum class ResourceType {
34 kAnim,
35 kAnimator,
36 kArray,
37 kAttr,
38 kAttrPrivate,
39 kBool,
40 kColor,
41 kDimen,
42 kDrawable,
43 kFraction,
44 kId,
45 kInteger,
46 kIntegerArray,
47 kInterpolator,
48 kLayout,
49 kMenu,
50 kMipmap,
51 kPlurals,
52 kRaw,
53 kString,
54 kStyle,
55 kStyleable,
56 kTransition,
57 kXml,
58};
59
60StringPiece16 toString(ResourceType type);
61
62/**
63 * Returns a pointer to a valid ResourceType, or nullptr if
64 * the string was invalid.
65 */
66const ResourceType* parseResourceType(const StringPiece16& str);
67
68/**
69 * A resource's name. This can uniquely identify
70 * a resource in the ResourceTable.
71 */
72struct ResourceName {
73 std::u16string package;
74 ResourceType type;
75 std::u16string entry;
76
77 bool isValid() const;
78 bool operator<(const ResourceName& rhs) const;
79 bool operator==(const ResourceName& rhs) const;
80 bool operator!=(const ResourceName& rhs) const;
81};
82
83/**
84 * Same as ResourceName, but uses StringPieces instead.
85 * Use this if you need to avoid copying and know that
86 * the lifetime of this object is shorter than that
87 * of the original string.
88 */
89struct ResourceNameRef {
90 StringPiece16 package;
91 ResourceType type;
92 StringPiece16 entry;
93
94 ResourceNameRef() = default;
95 ResourceNameRef(const ResourceNameRef&) = default;
96 ResourceNameRef(ResourceNameRef&&) = default;
97 ResourceNameRef(const ResourceName& rhs);
98 ResourceNameRef(const StringPiece16& p, ResourceType t, const StringPiece16& e);
99 ResourceNameRef& operator=(const ResourceName& rhs);
100
101 ResourceName toResourceName() const;
102 bool isValid() const;
103
104 bool operator<(const ResourceNameRef& rhs) const;
105 bool operator==(const ResourceNameRef& rhs) const;
106 bool operator!=(const ResourceNameRef& rhs) const;
107};
108
109/**
110 * A binary identifier representing a resource. Internally it
111 * is a 32bit integer split as follows:
112 *
113 * 0xPPTTEEEE
114 *
115 * PP: 8 bit package identifier. 0x01 is reserved for system
116 * and 0x7f is reserved for the running app.
117 * TT: 8 bit type identifier. 0x00 is invalid.
118 * EEEE: 16 bit entry identifier.
119 */
120struct ResourceId {
121 uint32_t id;
122
123 ResourceId();
124 ResourceId(const ResourceId& rhs);
125 ResourceId(uint32_t resId);
126 ResourceId(size_t p, size_t t, size_t e);
127
128 bool isValid() const;
129 uint8_t packageId() const;
130 uint8_t typeId() const;
131 uint16_t entryId() const;
132 bool operator<(const ResourceId& rhs) const;
Adam Lesinski24aad162015-04-24 19:19:30 -0700133 bool operator==(const ResourceId& rhs) const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800134};
135
136//
137// ResourceId implementation.
138//
139
140inline ResourceId::ResourceId() : id(0) {
141}
142
143inline ResourceId::ResourceId(const ResourceId& rhs) : id(rhs.id) {
144}
145
146inline ResourceId::ResourceId(uint32_t resId) : id(resId) {
147}
148
149inline ResourceId::ResourceId(size_t p, size_t t, size_t e) : id(0) {
150 if (p > std::numeric_limits<uint8_t>::max() ||
151 t > std::numeric_limits<uint8_t>::max() ||
152 e > std::numeric_limits<uint16_t>::max()) {
153 // This will leave the ResourceId in an invalid state.
154 return;
155 }
156
157 id = (static_cast<uint8_t>(p) << 24) |
158 (static_cast<uint8_t>(t) << 16) |
159 static_cast<uint16_t>(e);
160}
161
162inline bool ResourceId::isValid() const {
163 return (id & 0xff000000u) != 0 && (id & 0x00ff0000u) != 0;
164}
165
166inline uint8_t ResourceId::packageId() const {
167 return static_cast<uint8_t>(id >> 24);
168}
169
170inline uint8_t ResourceId::typeId() const {
171 return static_cast<uint8_t>(id >> 16);
172}
173
174inline uint16_t ResourceId::entryId() const {
175 return static_cast<uint16_t>(id);
176}
177
178inline bool ResourceId::operator<(const ResourceId& rhs) const {
179 return id < rhs.id;
180}
181
Adam Lesinski24aad162015-04-24 19:19:30 -0700182inline bool ResourceId::operator==(const ResourceId& rhs) const {
183 return id == rhs.id;
184}
185
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800186inline ::std::ostream& operator<<(::std::ostream& out,
187 const ResourceId& resId) {
188 std::ios_base::fmtflags oldFlags = out.flags();
189 char oldFill = out.fill();
190 out << "0x" << std::internal << std::setfill('0') << std::setw(8)
191 << std::hex << resId.id;
192 out.flags(oldFlags);
193 out.fill(oldFill);
194 return out;
195}
196
197//
198// ResourceType implementation.
199//
200
Adam Lesinski769de982015-04-10 19:43:55 -0700201inline ::std::ostream& operator<<(::std::ostream& out, const ResourceType& val) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800202 return out << toString(val);
203}
204
205//
206// ResourceName implementation.
207//
208
209inline bool ResourceName::isValid() const {
210 return !package.empty() && !entry.empty();
211}
212
213inline bool ResourceName::operator<(const ResourceName& rhs) const {
214 return std::tie(package, type, entry)
215 < std::tie(rhs.package, rhs.type, rhs.entry);
216}
217
218inline bool ResourceName::operator==(const ResourceName& rhs) const {
219 return std::tie(package, type, entry)
220 == std::tie(rhs.package, rhs.type, rhs.entry);
221}
222
223inline bool ResourceName::operator!=(const ResourceName& rhs) const {
224 return std::tie(package, type, entry)
225 != std::tie(rhs.package, rhs.type, rhs.entry);
226}
227
Adam Lesinski769de982015-04-10 19:43:55 -0700228inline ::std::ostream& operator<<(::std::ostream& out, const ResourceName& name) {
229 if (!name.package.empty()) {
230 out << name.package << ":";
231 }
232 return out << name.type << "/" << name.entry;
233}
234
235
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800236//
237// ResourceNameRef implementation.
238//
239
240inline ResourceNameRef::ResourceNameRef(const ResourceName& rhs) :
241 package(rhs.package), type(rhs.type), entry(rhs.entry) {
242}
243
244inline ResourceNameRef::ResourceNameRef(const StringPiece16& p, ResourceType t,
245 const StringPiece16& e) :
246 package(p), type(t), entry(e) {
247}
248
249inline ResourceNameRef& ResourceNameRef::operator=(const ResourceName& rhs) {
250 package = rhs.package;
251 type = rhs.type;
252 entry = rhs.entry;
253 return *this;
254}
255
256inline ResourceName ResourceNameRef::toResourceName() const {
257 return { package.toString(), type, entry.toString() };
258}
259
260inline bool ResourceNameRef::isValid() const {
261 return !package.empty() && !entry.empty();
262}
263
264inline bool ResourceNameRef::operator<(const ResourceNameRef& rhs) const {
265 return std::tie(package, type, entry)
266 < std::tie(rhs.package, rhs.type, rhs.entry);
267}
268
269inline bool ResourceNameRef::operator==(const ResourceNameRef& rhs) const {
270 return std::tie(package, type, entry)
271 == std::tie(rhs.package, rhs.type, rhs.entry);
272}
273
274inline bool ResourceNameRef::operator!=(const ResourceNameRef& rhs) const {
275 return std::tie(package, type, entry)
276 != std::tie(rhs.package, rhs.type, rhs.entry);
277}
278
Adam Lesinski769de982015-04-10 19:43:55 -0700279inline ::std::ostream& operator<<(::std::ostream& out, const ResourceNameRef& name) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800280 if (!name.package.empty()) {
281 out << name.package << ":";
282 }
283 return out << name.type << "/" << name.entry;
284}
285
286} // namespace aapt
287
288#endif // AAPT_RESOURCE_H