blob: f928acd7c8d779586500776ddd1de1a39afec5bd [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;
133};
134
135//
136// ResourceId implementation.
137//
138
139inline ResourceId::ResourceId() : id(0) {
140}
141
142inline ResourceId::ResourceId(const ResourceId& rhs) : id(rhs.id) {
143}
144
145inline ResourceId::ResourceId(uint32_t resId) : id(resId) {
146}
147
148inline ResourceId::ResourceId(size_t p, size_t t, size_t e) : id(0) {
149 if (p > std::numeric_limits<uint8_t>::max() ||
150 t > std::numeric_limits<uint8_t>::max() ||
151 e > std::numeric_limits<uint16_t>::max()) {
152 // This will leave the ResourceId in an invalid state.
153 return;
154 }
155
156 id = (static_cast<uint8_t>(p) << 24) |
157 (static_cast<uint8_t>(t) << 16) |
158 static_cast<uint16_t>(e);
159}
160
161inline bool ResourceId::isValid() const {
162 return (id & 0xff000000u) != 0 && (id & 0x00ff0000u) != 0;
163}
164
165inline uint8_t ResourceId::packageId() const {
166 return static_cast<uint8_t>(id >> 24);
167}
168
169inline uint8_t ResourceId::typeId() const {
170 return static_cast<uint8_t>(id >> 16);
171}
172
173inline uint16_t ResourceId::entryId() const {
174 return static_cast<uint16_t>(id);
175}
176
177inline bool ResourceId::operator<(const ResourceId& rhs) const {
178 return id < rhs.id;
179}
180
181inline ::std::ostream& operator<<(::std::ostream& out,
182 const ResourceId& resId) {
183 std::ios_base::fmtflags oldFlags = out.flags();
184 char oldFill = out.fill();
185 out << "0x" << std::internal << std::setfill('0') << std::setw(8)
186 << std::hex << resId.id;
187 out.flags(oldFlags);
188 out.fill(oldFill);
189 return out;
190}
191
192//
193// ResourceType implementation.
194//
195
Adam Lesinski769de982015-04-10 19:43:55 -0700196inline ::std::ostream& operator<<(::std::ostream& out, const ResourceType& val) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800197 return out << toString(val);
198}
199
200//
201// ResourceName implementation.
202//
203
204inline bool ResourceName::isValid() const {
205 return !package.empty() && !entry.empty();
206}
207
208inline bool ResourceName::operator<(const ResourceName& rhs) const {
209 return std::tie(package, type, entry)
210 < std::tie(rhs.package, rhs.type, rhs.entry);
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
Adam Lesinski769de982015-04-10 19:43:55 -0700223inline ::std::ostream& operator<<(::std::ostream& out, const ResourceName& name) {
224 if (!name.package.empty()) {
225 out << name.package << ":";
226 }
227 return out << name.type << "/" << name.entry;
228}
229
230
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800231//
232// ResourceNameRef implementation.
233//
234
235inline ResourceNameRef::ResourceNameRef(const ResourceName& rhs) :
236 package(rhs.package), type(rhs.type), entry(rhs.entry) {
237}
238
239inline ResourceNameRef::ResourceNameRef(const StringPiece16& p, ResourceType t,
240 const StringPiece16& e) :
241 package(p), type(t), entry(e) {
242}
243
244inline ResourceNameRef& ResourceNameRef::operator=(const ResourceName& rhs) {
245 package = rhs.package;
246 type = rhs.type;
247 entry = rhs.entry;
248 return *this;
249}
250
251inline ResourceName ResourceNameRef::toResourceName() const {
252 return { package.toString(), type, entry.toString() };
253}
254
255inline bool ResourceNameRef::isValid() const {
256 return !package.empty() && !entry.empty();
257}
258
259inline bool ResourceNameRef::operator<(const ResourceNameRef& rhs) const {
260 return std::tie(package, type, entry)
261 < std::tie(rhs.package, rhs.type, rhs.entry);
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
Adam Lesinski769de982015-04-10 19:43:55 -0700274inline ::std::ostream& operator<<(::std::ostream& out, const ResourceNameRef& name) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800275 if (!name.package.empty()) {
276 out << name.package << ":";
277 }
278 return out << name.type << "/" << name.entry;
279}
280
281} // namespace aapt
282
283#endif // AAPT_RESOURCE_H