blob: 4d2c64c0a375e5e8521ab7e439ac286b547271c1 [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
196inline ::std::ostream& operator<<(::std::ostream& out,
197 const ResourceType& val) {
198 return out << toString(val);
199}
200
201//
202// ResourceName implementation.
203//
204
205inline bool ResourceName::isValid() const {
206 return !package.empty() && !entry.empty();
207}
208
209inline bool ResourceName::operator<(const ResourceName& rhs) const {
210 return std::tie(package, type, entry)
211 < std::tie(rhs.package, rhs.type, rhs.entry);
212}
213
214inline bool ResourceName::operator==(const ResourceName& rhs) const {
215 return std::tie(package, type, entry)
216 == std::tie(rhs.package, rhs.type, rhs.entry);
217}
218
219inline bool ResourceName::operator!=(const ResourceName& rhs) const {
220 return std::tie(package, type, entry)
221 != std::tie(rhs.package, rhs.type, rhs.entry);
222}
223
224//
225// ResourceNameRef implementation.
226//
227
228inline ResourceNameRef::ResourceNameRef(const ResourceName& rhs) :
229 package(rhs.package), type(rhs.type), entry(rhs.entry) {
230}
231
232inline ResourceNameRef::ResourceNameRef(const StringPiece16& p, ResourceType t,
233 const StringPiece16& e) :
234 package(p), type(t), entry(e) {
235}
236
237inline ResourceNameRef& ResourceNameRef::operator=(const ResourceName& rhs) {
238 package = rhs.package;
239 type = rhs.type;
240 entry = rhs.entry;
241 return *this;
242}
243
244inline ResourceName ResourceNameRef::toResourceName() const {
245 return { package.toString(), type, entry.toString() };
246}
247
248inline bool ResourceNameRef::isValid() const {
249 return !package.empty() && !entry.empty();
250}
251
252inline bool ResourceNameRef::operator<(const ResourceNameRef& rhs) const {
253 return std::tie(package, type, entry)
254 < std::tie(rhs.package, rhs.type, rhs.entry);
255}
256
257inline bool ResourceNameRef::operator==(const ResourceNameRef& rhs) const {
258 return std::tie(package, type, entry)
259 == std::tie(rhs.package, rhs.type, rhs.entry);
260}
261
262inline bool ResourceNameRef::operator!=(const ResourceNameRef& rhs) const {
263 return std::tie(package, type, entry)
264 != std::tie(rhs.package, rhs.type, rhs.entry);
265}
266
267inline ::std::ostream& operator<<(::std::ostream& out,
268 const ResourceNameRef& name) {
269 if (!name.package.empty()) {
270 out << name.package << ":";
271 }
272 return out << name.type << "/" << name.entry;
273}
274
275} // namespace aapt
276
277#endif // AAPT_RESOURCE_H