blob: 7ba3277d20939d1de924a44a99f7c4c3bbb47e57 [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 Lesinski6f6ceb72014-11-14 14:48:12 -080020#include <iomanip>
Adam Lesinskica2fc352015-04-03 12:08:26 -070021#include <limits>
Iurii Makhnodfbac392022-02-15 20:02:52 +000022#include <optional>
Adam Lesinskid0f116b2016-07-08 15:00:32 -070023#include <sstream>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080024#include <string>
25#include <tuple>
Adam Lesinski1ab598f2015-08-14 14:26:04 -070026#include <vector>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080027
MÃ¥rten Kongstad24c9aa62018-06-20 08:46:41 +020028#include "androidfw/ConfigDescription.h"
Jeremy Meyer56f36e82022-05-20 20:35:42 +000029#include "androidfw/Source.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080030#include "androidfw/StringPiece.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070031#include "utils/JenkinsHash.h"
32
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080033namespace aapt {
34
35/**
Iurii Makhnodfbac392022-02-15 20:02:52 +000036 * The various types of resource types available.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080037 */
38enum class ResourceType {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070039 kAnim,
40 kAnimator,
41 kArray,
42 kAttr,
43 kAttrPrivate,
44 kBool,
45 kColor,
Adam Lesinski86d67df2017-01-31 13:47:27 -080046
47 // Not really a type, but it shows up in some CTS tests and
48 // we need to continue respecting it.
49 kConfigVarying,
50
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 kDimen,
52 kDrawable,
Adam Lesinskic0c36632016-10-19 18:37:53 -070053 kFont,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 kFraction,
55 kId,
56 kInteger,
57 kInterpolator,
58 kLayout,
Ryan Mitchell326e35ff2021-04-12 07:50:42 -070059 kMacro,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070060 kMenu,
61 kMipmap,
Adam Lesinski3b841242017-07-25 17:15:42 -070062 kNavigation,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070063 kPlurals,
64 kRaw,
65 kString,
66 kStyle,
67 kStyleable,
68 kTransition,
69 kXml,
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080070};
71
Adam Lesinski93190b72017-11-03 15:20:17 -070072android::StringPiece to_string(ResourceType type);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080073
74/**
Adam Lesinski93190b72017-11-03 15:20:17 -070075 * Returns a pointer to a valid ResourceType, or nullptr if the string was invalid.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080076 */
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070077const ResourceType* ParseResourceType(android::StringPiece str);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080078
79/**
Iurii Makhnodfbac392022-02-15 20:02:52 +000080 * Pair of type name as in ResourceTable and actual resource type.
81 * Corresponds to the 'type' in package:type/entry.
82 *
83 * This is to support resource types with custom names inside resource tables.
84 */
85struct ResourceNamedType {
86 std::string name;
87 ResourceType type = ResourceType::kRaw;
88
89 ResourceNamedType() = default;
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070090 ResourceNamedType(android::StringPiece n, ResourceType t);
Iurii Makhnodfbac392022-02-15 20:02:52 +000091
92 int compare(const ResourceNamedType& other) const;
93
94 const std::string& to_string() const;
95};
96
97/**
98 * Same as ResourceNamedType, but uses StringPieces instead.
99 * Use this if you need to avoid copying and know that
100 * the lifetime of this object is shorter than that
101 * of the original string.
102 */
103struct ResourceNamedTypeRef {
104 android::StringPiece name;
105 ResourceType type = ResourceType::kRaw;
106
107 ResourceNamedTypeRef() = default;
108 ResourceNamedTypeRef(const ResourceNamedTypeRef&) = default;
109 ResourceNamedTypeRef(ResourceNamedTypeRef&&) = default;
110 ResourceNamedTypeRef(const ResourceNamedType& rhs); // NOLINT(google-explicit-constructor)
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700111 ResourceNamedTypeRef(android::StringPiece n, ResourceType t);
Iurii Makhnodfbac392022-02-15 20:02:52 +0000112 ResourceNamedTypeRef& operator=(const ResourceNamedTypeRef& rhs) = default;
113 ResourceNamedTypeRef& operator=(ResourceNamedTypeRef&& rhs) = default;
114 ResourceNamedTypeRef& operator=(const ResourceNamedType& rhs);
115
116 ResourceNamedType ToResourceNamedType() const;
117
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700118 std::string_view to_string() const;
Iurii Makhnodfbac392022-02-15 20:02:52 +0000119};
120
121ResourceNamedTypeRef ResourceNamedTypeWithDefaultName(ResourceType t);
122
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700123std::optional<ResourceNamedTypeRef> ParseResourceNamedType(android::StringPiece s);
Iurii Makhnodfbac392022-02-15 20:02:52 +0000124
125/**
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800126 * A resource's name. This can uniquely identify
127 * a resource in the ResourceTable.
128 */
129struct ResourceName {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700130 std::string package;
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000131 ResourceNamedType type;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700132 std::string entry;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800133
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700134 ResourceName() = default;
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700135 ResourceName(android::StringPiece p, const ResourceNamedTypeRef& t, android::StringPiece e);
136 ResourceName(android::StringPiece p, ResourceType t, android::StringPiece e);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700137
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700138 int compare(const ResourceName& other) const;
Adam Lesinski8197cc462016-08-19 12:16:49 -0700139
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700140 bool is_valid() const;
Adam Lesinski93190b72017-11-03 15:20:17 -0700141 std::string to_string() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800142};
143
144/**
145 * Same as ResourceName, but uses StringPieces instead.
146 * Use this if you need to avoid copying and know that
147 * the lifetime of this object is shorter than that
148 * of the original string.
149 */
150struct ResourceNameRef {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800151 android::StringPiece package;
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000152 ResourceNamedTypeRef type;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800153 android::StringPiece entry;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800154
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700155 ResourceNameRef() = default;
156 ResourceNameRef(const ResourceNameRef&) = default;
157 ResourceNameRef(ResourceNameRef&&) = default;
Chih-Hung Hsieh1fc78e12018-12-20 13:37:44 -0800158 ResourceNameRef(const ResourceName& rhs); // NOLINT(google-explicit-constructor)
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700159 ResourceNameRef(android::StringPiece p, const ResourceNamedTypeRef& t, android::StringPiece e);
160 ResourceNameRef(android::StringPiece p, ResourceType t, android::StringPiece e);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700161 ResourceNameRef& operator=(const ResourceNameRef& rhs) = default;
162 ResourceNameRef& operator=(ResourceNameRef&& rhs) = default;
163 ResourceNameRef& operator=(const ResourceName& rhs);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800164
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700165 bool is_valid() const;
Adam Lesinski93190b72017-11-03 15:20:17 -0700166
167 ResourceName ToResourceName() const;
168 std::string to_string() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800169};
170
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -0800171constexpr const uint8_t kAppPackageId = 0x7fu;
172constexpr const uint8_t kFrameworkPackageId = 0x01u;
173
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800174/**
175 * A binary identifier representing a resource. Internally it
176 * is a 32bit integer split as follows:
177 *
178 * 0xPPTTEEEE
179 *
180 * PP: 8 bit package identifier. 0x01 is reserved for system
181 * and 0x7f is reserved for the running app.
182 * TT: 8 bit type identifier. 0x00 is invalid.
183 * EEEE: 16 bit entry identifier.
184 */
185struct ResourceId {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700186 uint32_t id;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800187
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700188 ResourceId();
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700189 ResourceId(const ResourceId& rhs) = default;
Chih-Hung Hsieh1fc78e12018-12-20 13:37:44 -0800190 ResourceId(uint32_t res_id); // NOLINT(google-explicit-constructor)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700191 ResourceId(uint8_t p, uint8_t t, uint16_t e);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800192
Ryan Mitchellcd78feb2019-12-18 15:20:48 -0800193 // Returns true if the ID is a valid ID that is not dynamic (package ID cannot be 0)
194 bool is_valid_static() const;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800195
196 // Returns true if the ID is a valid ID or dynamic ID (package ID can be 0).
Ryan Mitchellcd78feb2019-12-18 15:20:48 -0800197 bool is_valid() const;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800198
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700199 uint8_t package_id() const;
200 uint8_t type_id() const;
201 uint16_t entry_id() const;
Adam Lesinski93190b72017-11-03 15:20:17 -0700202
203 std::string to_string() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800204};
205
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700206struct SourcedResourceName {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700207 ResourceName name;
208 size_t line;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700209};
210
211struct ResourceFile {
Adam Lesinski00451162017-10-03 07:44:08 -0700212 enum class Type {
213 kUnknown,
214 kPng,
215 kBinaryXml,
216 kProtoXml,
217 };
218
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700219 // Name
220 ResourceName name;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700221
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700222 // Configuration
MÃ¥rten Kongstad24c9aa62018-06-20 08:46:41 +0200223 android::ConfigDescription config;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700224
Adam Lesinski00451162017-10-03 07:44:08 -0700225 // Type
226 Type type;
227
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700228 // Source
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000229 android::Source source;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700230
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700231 // Exported symbols
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700232 std::vector<SourcedResourceName> exported_symbols;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700233};
234
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800235/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700236 * Useful struct used as a key to represent a unique resource in associative
237 * containers.
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800238 */
239struct ResourceKey {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700240 ResourceName name;
MÃ¥rten Kongstad24c9aa62018-06-20 08:46:41 +0200241 android::ConfigDescription config;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800242};
243
244bool operator<(const ResourceKey& a, const ResourceKey& b);
245
246/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700247 * Useful struct used as a key to represent a unique resource in associative
248 * containers.
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800249 * Holds a reference to the name, so that name better live longer than this key!
250 */
251struct ResourceKeyRef {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700252 ResourceNameRef name;
MÃ¥rten Kongstad24c9aa62018-06-20 08:46:41 +0200253 android::ConfigDescription config;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800254
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700255 ResourceKeyRef() = default;
MÃ¥rten Kongstad24c9aa62018-06-20 08:46:41 +0200256 ResourceKeyRef(const ResourceNameRef& n, const android::ConfigDescription& c)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700257 : name(n), config(c) {}
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800258
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700259 /**
260 * Prevent taking a reference to a temporary. This is bad.
261 */
MÃ¥rten Kongstad24c9aa62018-06-20 08:46:41 +0200262 ResourceKeyRef(ResourceName&& n, const android::ConfigDescription& c) = delete;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800263};
264
265bool operator<(const ResourceKeyRef& a, const ResourceKeyRef& b);
266
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800267//
268// ResourceId implementation.
269//
270
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700271inline ResourceId::ResourceId() : id(0) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800272
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700273inline ResourceId::ResourceId(uint32_t res_id) : id(res_id) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800274
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700275inline ResourceId::ResourceId(uint8_t p, uint8_t t, uint16_t e)
276 : id((p << 24) | (t << 16) | e) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800277
Ryan Mitchellcd78feb2019-12-18 15:20:48 -0800278inline bool ResourceId::is_valid_static() const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700279 return (id & 0xff000000u) != 0 && (id & 0x00ff0000u) != 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800280}
281
Ryan Mitchellcd78feb2019-12-18 15:20:48 -0800282inline bool ResourceId::is_valid() const {
Adam Lesinski93190b72017-11-03 15:20:17 -0700283 return (id & 0x00ff0000u) != 0;
284}
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800285
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700286inline uint8_t ResourceId::package_id() const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700287 return static_cast<uint8_t>(id >> 24);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800288}
289
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700290inline uint8_t ResourceId::type_id() const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700291 return static_cast<uint8_t>(id >> 16);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800292}
293
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700294inline uint16_t ResourceId::entry_id() const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700295 return static_cast<uint16_t>(id);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800296}
297
Adam Lesinski74605cd2016-03-03 15:39:50 -0800298inline bool operator<(const ResourceId& lhs, const ResourceId& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700299 return lhs.id < rhs.id;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800300}
301
Adam Lesinski74605cd2016-03-03 15:39:50 -0800302inline bool operator>(const ResourceId& lhs, const ResourceId& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700303 return lhs.id > rhs.id;
Adam Lesinski24aad162015-04-24 19:19:30 -0700304}
305
Adam Lesinski74605cd2016-03-03 15:39:50 -0800306inline bool operator==(const ResourceId& lhs, const ResourceId& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700307 return lhs.id == rhs.id;
Adam Lesinski74605cd2016-03-03 15:39:50 -0800308}
309
310inline bool operator!=(const ResourceId& lhs, const ResourceId& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700311 return lhs.id != rhs.id;
Adam Lesinski74605cd2016-03-03 15:39:50 -0800312}
313
Adam Lesinski93190b72017-11-03 15:20:17 -0700314inline ::std::ostream& operator<<(::std::ostream& out, const ResourceId& res_id) {
315 return out << res_id.to_string();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800316}
317
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800318// For generic code to call 'using std::to_string; to_string(T);'.
319inline std::string to_string(const ResourceId& id) {
320 return id.to_string();
321}
322
Clark DuVall8f51d6b2020-04-22 13:19:28 -0700323// Helper to compare resource IDs, moving dynamic IDs after framework IDs.
324inline bool cmp_ids_dynamic_after_framework(const ResourceId& a, const ResourceId& b) {
325 // If one of a and b is from the framework package (package ID 0x01), and the
326 // other is a dynamic ID (package ID 0x00), then put the dynamic ID after the
327 // framework ID. This ensures that when AssetManager resolves the dynamic IDs,
328 // they will be in sorted order as expected by AssetManager.
329 if ((a.package_id() == kFrameworkPackageId && b.package_id() == 0x00) ||
330 (a.package_id() == 0x00 && b.package_id() == kFrameworkPackageId)) {
331 return b < a;
332 }
333 return a < b;
334}
335
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800336//
337// ResourceType implementation.
338//
339
Adam Lesinski93190b72017-11-03 15:20:17 -0700340inline ::std::ostream& operator<<(::std::ostream& out, const ResourceType& val) {
341 return out << to_string(val);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800342}
343
344//
Iurii Makhnodfbac392022-02-15 20:02:52 +0000345// ResourceNamedType implementation.
346//
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700347inline ResourceNamedType::ResourceNamedType(android::StringPiece n, ResourceType t)
348 : name(n), type(t) {
Iurii Makhnodfbac392022-02-15 20:02:52 +0000349}
350
351inline int ResourceNamedType::compare(const ResourceNamedType& other) const {
352 int cmp = static_cast<int>(type) - static_cast<int>(other.type);
353 if (cmp != 0) return cmp;
354 cmp = name.compare(other.name);
355 return cmp;
356}
357
358inline const std::string& ResourceNamedType::to_string() const {
359 return name;
360}
361
362inline bool operator<(const ResourceNamedType& lhs, const ResourceNamedType& rhs) {
363 return lhs.compare(rhs) < 0;
364}
365
366inline bool operator==(const ResourceNamedType& lhs, const ResourceNamedType& rhs) {
367 return lhs.compare(rhs) == 0;
368}
369
370inline bool operator!=(const ResourceNamedType& lhs, const ResourceNamedType& rhs) {
371 return lhs.compare(rhs) != 0;
372}
373
374inline ::std::ostream& operator<<(::std::ostream& out, const ResourceNamedType& val) {
375 return out << val.to_string();
376}
377
378//
379// ResourceNamedTypeRef implementation.
380//
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700381inline ResourceNamedTypeRef::ResourceNamedTypeRef(android::StringPiece n, ResourceType t)
Iurii Makhnodfbac392022-02-15 20:02:52 +0000382 : name(n), type(t) {
383}
384
385inline ResourceNamedTypeRef::ResourceNamedTypeRef(const ResourceNamedType& rhs)
386 : name(rhs.name), type(rhs.type) {
387}
388
389inline ResourceNamedTypeRef& ResourceNamedTypeRef::operator=(const ResourceNamedType& rhs) {
390 name = rhs.name;
391 type = rhs.type;
392 return *this;
393}
394
395inline ResourceNamedType ResourceNamedTypeRef::ToResourceNamedType() const {
396 return ResourceNamedType(name, type);
397}
398
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700399inline std::string_view ResourceNamedTypeRef::to_string() const {
400 return name;
Iurii Makhnodfbac392022-02-15 20:02:52 +0000401}
402
403inline bool operator<(const ResourceNamedTypeRef& lhs, const ResourceNamedTypeRef& rhs) {
404 return std::tie(lhs.type, lhs.name) < std::tie(rhs.type, rhs.name);
405}
406
407inline bool operator==(const ResourceNamedTypeRef& lhs, const ResourceNamedTypeRef& rhs) {
408 return std::tie(lhs.type, lhs.name) == std::tie(rhs.type, rhs.name);
409}
410
411inline bool operator!=(const ResourceNamedTypeRef& lhs, const ResourceNamedTypeRef& rhs) {
412 return std::tie(lhs.type, lhs.name) != std::tie(rhs.type, rhs.name);
413}
414
415inline ::std::ostream& operator<<(::std::ostream& out, const ResourceNamedTypeRef& val) {
416 return out << val.name;
417}
418
419//
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800420// ResourceName implementation.
421//
422
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700423inline ResourceName::ResourceName(android::StringPiece p, const ResourceNamedTypeRef& t,
424 android::StringPiece e)
425 : package(p), type(t.ToResourceNamedType()), entry(e) {
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000426}
427
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700428inline ResourceName::ResourceName(android::StringPiece p, ResourceType t, android::StringPiece e)
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000429 : ResourceName(p, ResourceNamedTypeWithDefaultName(t), e) {
430}
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700431
Adam Lesinski8197cc462016-08-19 12:16:49 -0700432inline int ResourceName::compare(const ResourceName& other) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700433 int cmp = package.compare(other.package);
434 if (cmp != 0) return cmp;
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000435 cmp = type.compare(other.type);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700436 if (cmp != 0) return cmp;
437 cmp = entry.compare(other.entry);
438 return cmp;
Adam Lesinski8197cc462016-08-19 12:16:49 -0700439}
440
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700441inline bool ResourceName::is_valid() const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700442 return !package.empty() && !entry.empty();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800443}
444
Adam Lesinski74605cd2016-03-03 15:39:50 -0800445inline bool operator<(const ResourceName& lhs, const ResourceName& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700446 return std::tie(lhs.package, lhs.type, lhs.entry) <
447 std::tie(rhs.package, rhs.type, rhs.entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800448}
449
Adam Lesinski74605cd2016-03-03 15:39:50 -0800450inline bool operator==(const ResourceName& lhs, const ResourceName& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700451 return std::tie(lhs.package, lhs.type, lhs.entry) ==
452 std::tie(rhs.package, rhs.type, rhs.entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800453}
454
Adam Lesinski74605cd2016-03-03 15:39:50 -0800455inline bool operator!=(const ResourceName& lhs, const ResourceName& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700456 return std::tie(lhs.package, lhs.type, lhs.entry) !=
457 std::tie(rhs.package, rhs.type, rhs.entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800458}
459
Adam Lesinski93190b72017-11-03 15:20:17 -0700460inline ::std::ostream& operator<<(::std::ostream& out, const ResourceName& name) {
461 return out << name.to_string();
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700462}
Adam Lesinski769de982015-04-10 19:43:55 -0700463
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800464//
465// ResourceNameRef implementation.
466//
467
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700468inline ResourceNameRef::ResourceNameRef(const ResourceName& rhs)
469 : package(rhs.package), type(rhs.type), entry(rhs.entry) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800470
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700471inline ResourceNameRef::ResourceNameRef(android::StringPiece p, const ResourceNamedTypeRef& t,
472 android::StringPiece e)
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000473 : package(p), type(t), entry(e) {
474}
475
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700476inline ResourceNameRef::ResourceNameRef(android::StringPiece p, ResourceType t,
477 android::StringPiece e)
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000478 : ResourceNameRef(p, ResourceNamedTypeWithDefaultName(t), e) {
479}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800480
481inline ResourceNameRef& ResourceNameRef::operator=(const ResourceName& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700482 package = rhs.package;
483 type = rhs.type;
484 entry = rhs.entry;
485 return *this;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800486}
487
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700488inline ResourceName ResourceNameRef::ToResourceName() const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700489 return ResourceName(package, type, entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800490}
491
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700492inline bool ResourceNameRef::is_valid() const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700493 return !package.empty() && !entry.empty();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800494}
495
Adam Lesinski74605cd2016-03-03 15:39:50 -0800496inline bool operator<(const ResourceNameRef& lhs, const ResourceNameRef& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700497 return std::tie(lhs.package, lhs.type, lhs.entry) <
498 std::tie(rhs.package, rhs.type, rhs.entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800499}
500
Adam Lesinski74605cd2016-03-03 15:39:50 -0800501inline bool operator==(const ResourceNameRef& lhs, const ResourceNameRef& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700502 return std::tie(lhs.package, lhs.type, lhs.entry) ==
503 std::tie(rhs.package, rhs.type, rhs.entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800504}
505
Adam Lesinski74605cd2016-03-03 15:39:50 -0800506inline bool operator!=(const ResourceNameRef& lhs, const ResourceNameRef& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700507 return std::tie(lhs.package, lhs.type, lhs.entry) !=
508 std::tie(rhs.package, rhs.type, rhs.entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800509}
510
Adam Lesinski93190b72017-11-03 15:20:17 -0700511inline ::std::ostream& operator<<(::std::ostream& out, const ResourceNameRef& name) {
512 return out << name.to_string();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800513}
514
Adam Lesinski74605cd2016-03-03 15:39:50 -0800515inline bool operator<(const ResourceName& lhs, const ResourceNameRef& b) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700516 return ResourceNameRef(lhs) < b;
Adam Lesinski74605cd2016-03-03 15:39:50 -0800517}
518
519inline bool operator!=(const ResourceName& lhs, const ResourceNameRef& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700520 return ResourceNameRef(lhs) != rhs;
Adam Lesinski74605cd2016-03-03 15:39:50 -0800521}
522
Adam Lesinski93190b72017-11-03 15:20:17 -0700523inline bool operator==(const SourcedResourceName& lhs, const SourcedResourceName& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700524 return lhs.name == rhs.name && lhs.line == rhs.line;
Adam Lesinski74605cd2016-03-03 15:39:50 -0800525}
526
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700527} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800528
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700529namespace std {
530
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700531template <>
532struct hash<aapt::ResourceName> {
533 size_t operator()(const aapt::ResourceName& name) const {
534 android::hash_t h = 0;
Adam Lesinskic744ae82017-05-17 19:28:38 -0700535 h = android::JenkinsHashMix(h, static_cast<uint32_t>(hash<string>()(name.package)));
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000536 h = android::JenkinsHashMix(h, static_cast<uint32_t>(hash<string>()(name.type.name)));
Adam Lesinskic744ae82017-05-17 19:28:38 -0700537 h = android::JenkinsHashMix(h, static_cast<uint32_t>(hash<string>()(name.entry)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700538 return static_cast<size_t>(h);
539 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700540};
541
Adam Lesinskic744ae82017-05-17 19:28:38 -0700542template <>
543struct hash<aapt::ResourceId> {
544 size_t operator()(const aapt::ResourceId& id) const {
545 return id.id;
546 }
547};
548
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700549} // namespace std
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700550
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700551#endif // AAPT_RESOURCE_H