blob: a274f047586cfb35fc0184779875ca859862d6fb [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
Jeremy Meyer211bec22024-06-04 14:22:03 -070072enum class FlagStatus { NoFlag = 0, Disabled = 1, Enabled = 2 };
73
Adam Lesinski93190b72017-11-03 15:20:17 -070074android::StringPiece to_string(ResourceType type);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080075
76/**
Adam Lesinski93190b72017-11-03 15:20:17 -070077 * Returns a pointer to a valid ResourceType, or nullptr if the string was invalid.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080078 */
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070079const ResourceType* ParseResourceType(android::StringPiece str);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080080
81/**
Iurii Makhnodfbac392022-02-15 20:02:52 +000082 * Pair of type name as in ResourceTable and actual resource type.
83 * Corresponds to the 'type' in package:type/entry.
84 *
85 * This is to support resource types with custom names inside resource tables.
86 */
87struct ResourceNamedType {
88 std::string name;
89 ResourceType type = ResourceType::kRaw;
90
91 ResourceNamedType() = default;
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070092 ResourceNamedType(android::StringPiece n, ResourceType t);
Iurii Makhnodfbac392022-02-15 20:02:52 +000093
94 int compare(const ResourceNamedType& other) const;
95
96 const std::string& to_string() const;
97};
98
99/**
100 * Same as ResourceNamedType, but uses StringPieces instead.
101 * Use this if you need to avoid copying and know that
102 * the lifetime of this object is shorter than that
103 * of the original string.
104 */
105struct ResourceNamedTypeRef {
106 android::StringPiece name;
107 ResourceType type = ResourceType::kRaw;
108
109 ResourceNamedTypeRef() = default;
110 ResourceNamedTypeRef(const ResourceNamedTypeRef&) = default;
111 ResourceNamedTypeRef(ResourceNamedTypeRef&&) = default;
112 ResourceNamedTypeRef(const ResourceNamedType& rhs); // NOLINT(google-explicit-constructor)
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700113 ResourceNamedTypeRef(android::StringPiece n, ResourceType t);
Iurii Makhnodfbac392022-02-15 20:02:52 +0000114 ResourceNamedTypeRef& operator=(const ResourceNamedTypeRef& rhs) = default;
115 ResourceNamedTypeRef& operator=(ResourceNamedTypeRef&& rhs) = default;
116 ResourceNamedTypeRef& operator=(const ResourceNamedType& rhs);
117
118 ResourceNamedType ToResourceNamedType() const;
119
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700120 std::string_view to_string() const;
Iurii Makhnodfbac392022-02-15 20:02:52 +0000121};
122
123ResourceNamedTypeRef ResourceNamedTypeWithDefaultName(ResourceType t);
124
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700125std::optional<ResourceNamedTypeRef> ParseResourceNamedType(android::StringPiece s);
Iurii Makhnodfbac392022-02-15 20:02:52 +0000126
127/**
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800128 * A resource's name. This can uniquely identify
129 * a resource in the ResourceTable.
130 */
131struct ResourceName {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700132 std::string package;
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000133 ResourceNamedType type;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700134 std::string entry;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800135
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700136 ResourceName() = default;
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700137 ResourceName(android::StringPiece p, const ResourceNamedTypeRef& t, android::StringPiece e);
138 ResourceName(android::StringPiece p, ResourceType t, android::StringPiece e);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700139
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700140 int compare(const ResourceName& other) const;
Adam Lesinski8197cc462016-08-19 12:16:49 -0700141
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700142 bool is_valid() const;
Adam Lesinski93190b72017-11-03 15:20:17 -0700143 std::string to_string() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800144};
145
146/**
147 * Same as ResourceName, but uses StringPieces instead.
148 * Use this if you need to avoid copying and know that
149 * the lifetime of this object is shorter than that
150 * of the original string.
151 */
152struct ResourceNameRef {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800153 android::StringPiece package;
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000154 ResourceNamedTypeRef type;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800155 android::StringPiece entry;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800156
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700157 ResourceNameRef() = default;
158 ResourceNameRef(const ResourceNameRef&) = default;
159 ResourceNameRef(ResourceNameRef&&) = default;
Chih-Hung Hsieh1fc78e12018-12-20 13:37:44 -0800160 ResourceNameRef(const ResourceName& rhs); // NOLINT(google-explicit-constructor)
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700161 ResourceNameRef(android::StringPiece p, const ResourceNamedTypeRef& t, android::StringPiece e);
162 ResourceNameRef(android::StringPiece p, ResourceType t, android::StringPiece e);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700163 ResourceNameRef& operator=(const ResourceNameRef& rhs) = default;
164 ResourceNameRef& operator=(ResourceNameRef&& rhs) = default;
165 ResourceNameRef& operator=(const ResourceName& rhs);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800166
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700167 bool is_valid() const;
Adam Lesinski93190b72017-11-03 15:20:17 -0700168
169 ResourceName ToResourceName() const;
170 std::string to_string() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800171};
172
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -0800173constexpr const uint8_t kAppPackageId = 0x7fu;
174constexpr const uint8_t kFrameworkPackageId = 0x01u;
175
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800176/**
177 * A binary identifier representing a resource. Internally it
178 * is a 32bit integer split as follows:
179 *
180 * 0xPPTTEEEE
181 *
182 * PP: 8 bit package identifier. 0x01 is reserved for system
183 * and 0x7f is reserved for the running app.
184 * TT: 8 bit type identifier. 0x00 is invalid.
185 * EEEE: 16 bit entry identifier.
186 */
187struct ResourceId {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700188 uint32_t id;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800189
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700190 ResourceId();
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700191 ResourceId(const ResourceId& rhs) = default;
Chih-Hung Hsieh1fc78e12018-12-20 13:37:44 -0800192 ResourceId(uint32_t res_id); // NOLINT(google-explicit-constructor)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700193 ResourceId(uint8_t p, uint8_t t, uint16_t e);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800194
Ryan Mitchellcd78feb2019-12-18 15:20:48 -0800195 // Returns true if the ID is a valid ID that is not dynamic (package ID cannot be 0)
196 bool is_valid_static() const;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800197
198 // Returns true if the ID is a valid ID or dynamic ID (package ID can be 0).
Ryan Mitchellcd78feb2019-12-18 15:20:48 -0800199 bool is_valid() const;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800200
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700201 uint8_t package_id() const;
202 uint8_t type_id() const;
203 uint16_t entry_id() const;
Adam Lesinski93190b72017-11-03 15:20:17 -0700204
205 std::string to_string() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800206};
207
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700208struct SourcedResourceName {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700209 ResourceName name;
210 size_t line;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700211};
212
213struct ResourceFile {
Adam Lesinski00451162017-10-03 07:44:08 -0700214 enum class Type {
215 kUnknown,
216 kPng,
217 kBinaryXml,
218 kProtoXml,
219 };
220
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700221 // Name
222 ResourceName name;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700223
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700224 // Configuration
MÃ¥rten Kongstad24c9aa62018-06-20 08:46:41 +0200225 android::ConfigDescription config;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700226
Adam Lesinski00451162017-10-03 07:44:08 -0700227 // Type
228 Type type;
229
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700230 // Source
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000231 android::Source source;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700232
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700233 // Exported symbols
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700234 std::vector<SourcedResourceName> exported_symbols;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700235};
236
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800237/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700238 * Useful struct used as a key to represent a unique resource in associative
239 * containers.
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800240 */
241struct ResourceKey {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700242 ResourceName name;
MÃ¥rten Kongstad24c9aa62018-06-20 08:46:41 +0200243 android::ConfigDescription config;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800244};
245
246bool operator<(const ResourceKey& a, const ResourceKey& b);
247
248/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700249 * Useful struct used as a key to represent a unique resource in associative
250 * containers.
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800251 * Holds a reference to the name, so that name better live longer than this key!
252 */
253struct ResourceKeyRef {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700254 ResourceNameRef name;
MÃ¥rten Kongstad24c9aa62018-06-20 08:46:41 +0200255 android::ConfigDescription config;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800256
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700257 ResourceKeyRef() = default;
MÃ¥rten Kongstad24c9aa62018-06-20 08:46:41 +0200258 ResourceKeyRef(const ResourceNameRef& n, const android::ConfigDescription& c)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700259 : name(n), config(c) {}
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800260
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700261 /**
262 * Prevent taking a reference to a temporary. This is bad.
263 */
MÃ¥rten Kongstad24c9aa62018-06-20 08:46:41 +0200264 ResourceKeyRef(ResourceName&& n, const android::ConfigDescription& c) = delete;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800265};
266
267bool operator<(const ResourceKeyRef& a, const ResourceKeyRef& b);
268
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800269//
270// ResourceId implementation.
271//
272
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700273inline ResourceId::ResourceId() : id(0) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800274
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700275inline ResourceId::ResourceId(uint32_t res_id) : id(res_id) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800276
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700277inline ResourceId::ResourceId(uint8_t p, uint8_t t, uint16_t e)
278 : id((p << 24) | (t << 16) | e) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800279
Ryan Mitchellcd78feb2019-12-18 15:20:48 -0800280inline bool ResourceId::is_valid_static() const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700281 return (id & 0xff000000u) != 0 && (id & 0x00ff0000u) != 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800282}
283
Ryan Mitchellcd78feb2019-12-18 15:20:48 -0800284inline bool ResourceId::is_valid() const {
Adam Lesinski93190b72017-11-03 15:20:17 -0700285 return (id & 0x00ff0000u) != 0;
286}
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800287
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700288inline uint8_t ResourceId::package_id() const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700289 return static_cast<uint8_t>(id >> 24);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800290}
291
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700292inline uint8_t ResourceId::type_id() const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700293 return static_cast<uint8_t>(id >> 16);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800294}
295
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700296inline uint16_t ResourceId::entry_id() const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700297 return static_cast<uint16_t>(id);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800298}
299
Adam Lesinski74605cd2016-03-03 15:39:50 -0800300inline bool operator<(const ResourceId& lhs, const ResourceId& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700301 return lhs.id < rhs.id;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800302}
303
Adam Lesinski74605cd2016-03-03 15:39:50 -0800304inline bool operator>(const ResourceId& lhs, const ResourceId& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700305 return lhs.id > rhs.id;
Adam Lesinski24aad162015-04-24 19:19:30 -0700306}
307
Adam Lesinski74605cd2016-03-03 15:39:50 -0800308inline bool operator==(const ResourceId& lhs, const ResourceId& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700309 return lhs.id == rhs.id;
Adam Lesinski74605cd2016-03-03 15:39:50 -0800310}
311
312inline bool operator!=(const ResourceId& lhs, const ResourceId& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700313 return lhs.id != rhs.id;
Adam Lesinski74605cd2016-03-03 15:39:50 -0800314}
315
Adam Lesinski93190b72017-11-03 15:20:17 -0700316inline ::std::ostream& operator<<(::std::ostream& out, const ResourceId& res_id) {
317 return out << res_id.to_string();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800318}
319
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800320// For generic code to call 'using std::to_string; to_string(T);'.
321inline std::string to_string(const ResourceId& id) {
322 return id.to_string();
323}
324
Clark DuVall8f51d6b2020-04-22 13:19:28 -0700325// Helper to compare resource IDs, moving dynamic IDs after framework IDs.
326inline bool cmp_ids_dynamic_after_framework(const ResourceId& a, const ResourceId& b) {
327 // If one of a and b is from the framework package (package ID 0x01), and the
328 // other is a dynamic ID (package ID 0x00), then put the dynamic ID after the
329 // framework ID. This ensures that when AssetManager resolves the dynamic IDs,
330 // they will be in sorted order as expected by AssetManager.
331 if ((a.package_id() == kFrameworkPackageId && b.package_id() == 0x00) ||
332 (a.package_id() == 0x00 && b.package_id() == kFrameworkPackageId)) {
333 return b < a;
334 }
335 return a < b;
336}
337
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800338//
339// ResourceType implementation.
340//
341
Adam Lesinski93190b72017-11-03 15:20:17 -0700342inline ::std::ostream& operator<<(::std::ostream& out, const ResourceType& val) {
343 return out << to_string(val);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800344}
345
346//
Iurii Makhnodfbac392022-02-15 20:02:52 +0000347// ResourceNamedType implementation.
348//
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700349inline ResourceNamedType::ResourceNamedType(android::StringPiece n, ResourceType t)
350 : name(n), type(t) {
Iurii Makhnodfbac392022-02-15 20:02:52 +0000351}
352
353inline int ResourceNamedType::compare(const ResourceNamedType& other) const {
354 int cmp = static_cast<int>(type) - static_cast<int>(other.type);
355 if (cmp != 0) return cmp;
356 cmp = name.compare(other.name);
357 return cmp;
358}
359
360inline const std::string& ResourceNamedType::to_string() const {
361 return name;
362}
363
364inline bool operator<(const ResourceNamedType& lhs, const ResourceNamedType& rhs) {
365 return lhs.compare(rhs) < 0;
366}
367
368inline bool operator==(const ResourceNamedType& lhs, const ResourceNamedType& rhs) {
369 return lhs.compare(rhs) == 0;
370}
371
372inline bool operator!=(const ResourceNamedType& lhs, const ResourceNamedType& rhs) {
373 return lhs.compare(rhs) != 0;
374}
375
376inline ::std::ostream& operator<<(::std::ostream& out, const ResourceNamedType& val) {
377 return out << val.to_string();
378}
379
380//
381// ResourceNamedTypeRef implementation.
382//
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700383inline ResourceNamedTypeRef::ResourceNamedTypeRef(android::StringPiece n, ResourceType t)
Iurii Makhnodfbac392022-02-15 20:02:52 +0000384 : name(n), type(t) {
385}
386
387inline ResourceNamedTypeRef::ResourceNamedTypeRef(const ResourceNamedType& rhs)
388 : name(rhs.name), type(rhs.type) {
389}
390
391inline ResourceNamedTypeRef& ResourceNamedTypeRef::operator=(const ResourceNamedType& rhs) {
392 name = rhs.name;
393 type = rhs.type;
394 return *this;
395}
396
397inline ResourceNamedType ResourceNamedTypeRef::ToResourceNamedType() const {
398 return ResourceNamedType(name, type);
399}
400
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700401inline std::string_view ResourceNamedTypeRef::to_string() const {
402 return name;
Iurii Makhnodfbac392022-02-15 20:02:52 +0000403}
404
405inline bool operator<(const ResourceNamedTypeRef& lhs, const ResourceNamedTypeRef& rhs) {
406 return std::tie(lhs.type, lhs.name) < std::tie(rhs.type, rhs.name);
407}
408
409inline bool operator==(const ResourceNamedTypeRef& lhs, const ResourceNamedTypeRef& rhs) {
410 return std::tie(lhs.type, lhs.name) == std::tie(rhs.type, rhs.name);
411}
412
413inline bool operator!=(const ResourceNamedTypeRef& lhs, const ResourceNamedTypeRef& rhs) {
414 return std::tie(lhs.type, lhs.name) != std::tie(rhs.type, rhs.name);
415}
416
417inline ::std::ostream& operator<<(::std::ostream& out, const ResourceNamedTypeRef& val) {
418 return out << val.name;
419}
420
421//
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800422// ResourceName implementation.
423//
424
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700425inline ResourceName::ResourceName(android::StringPiece p, const ResourceNamedTypeRef& t,
426 android::StringPiece e)
427 : package(p), type(t.ToResourceNamedType()), entry(e) {
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000428}
429
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700430inline ResourceName::ResourceName(android::StringPiece p, ResourceType t, android::StringPiece e)
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000431 : ResourceName(p, ResourceNamedTypeWithDefaultName(t), e) {
432}
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700433
Adam Lesinski8197cc462016-08-19 12:16:49 -0700434inline int ResourceName::compare(const ResourceName& other) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700435 int cmp = package.compare(other.package);
436 if (cmp != 0) return cmp;
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000437 cmp = type.compare(other.type);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700438 if (cmp != 0) return cmp;
439 cmp = entry.compare(other.entry);
440 return cmp;
Adam Lesinski8197cc462016-08-19 12:16:49 -0700441}
442
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700443inline bool ResourceName::is_valid() const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700444 return !package.empty() && !entry.empty();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800445}
446
Adam Lesinski74605cd2016-03-03 15:39:50 -0800447inline bool operator<(const ResourceName& lhs, const ResourceName& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700448 return std::tie(lhs.package, lhs.type, lhs.entry) <
449 std::tie(rhs.package, rhs.type, rhs.entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800450}
451
Adam Lesinski74605cd2016-03-03 15:39:50 -0800452inline bool operator==(const ResourceName& lhs, const ResourceName& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700453 return std::tie(lhs.package, lhs.type, lhs.entry) ==
454 std::tie(rhs.package, rhs.type, rhs.entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800455}
456
Adam Lesinski74605cd2016-03-03 15:39:50 -0800457inline bool operator!=(const ResourceName& lhs, const ResourceName& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700458 return std::tie(lhs.package, lhs.type, lhs.entry) !=
459 std::tie(rhs.package, rhs.type, rhs.entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800460}
461
Adam Lesinski93190b72017-11-03 15:20:17 -0700462inline ::std::ostream& operator<<(::std::ostream& out, const ResourceName& name) {
463 return out << name.to_string();
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700464}
Adam Lesinski769de982015-04-10 19:43:55 -0700465
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800466//
467// ResourceNameRef implementation.
468//
469
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700470inline ResourceNameRef::ResourceNameRef(const ResourceName& rhs)
471 : package(rhs.package), type(rhs.type), entry(rhs.entry) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800472
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700473inline ResourceNameRef::ResourceNameRef(android::StringPiece p, const ResourceNamedTypeRef& t,
474 android::StringPiece e)
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000475 : package(p), type(t), entry(e) {
476}
477
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700478inline ResourceNameRef::ResourceNameRef(android::StringPiece p, ResourceType t,
479 android::StringPiece e)
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000480 : ResourceNameRef(p, ResourceNamedTypeWithDefaultName(t), e) {
481}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800482
483inline ResourceNameRef& ResourceNameRef::operator=(const ResourceName& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700484 package = rhs.package;
485 type = rhs.type;
486 entry = rhs.entry;
487 return *this;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800488}
489
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700490inline ResourceName ResourceNameRef::ToResourceName() const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700491 return ResourceName(package, type, entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800492}
493
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700494inline bool ResourceNameRef::is_valid() const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700495 return !package.empty() && !entry.empty();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800496}
497
Adam Lesinski74605cd2016-03-03 15:39:50 -0800498inline bool operator<(const ResourceNameRef& lhs, const ResourceNameRef& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700499 return std::tie(lhs.package, lhs.type, lhs.entry) <
500 std::tie(rhs.package, rhs.type, rhs.entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800501}
502
Adam Lesinski74605cd2016-03-03 15:39:50 -0800503inline bool operator==(const ResourceNameRef& lhs, const ResourceNameRef& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700504 return std::tie(lhs.package, lhs.type, lhs.entry) ==
505 std::tie(rhs.package, rhs.type, rhs.entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800506}
507
Adam Lesinski74605cd2016-03-03 15:39:50 -0800508inline bool operator!=(const ResourceNameRef& lhs, const ResourceNameRef& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700509 return std::tie(lhs.package, lhs.type, lhs.entry) !=
510 std::tie(rhs.package, rhs.type, rhs.entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800511}
512
Adam Lesinski93190b72017-11-03 15:20:17 -0700513inline ::std::ostream& operator<<(::std::ostream& out, const ResourceNameRef& name) {
514 return out << name.to_string();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800515}
516
Adam Lesinski74605cd2016-03-03 15:39:50 -0800517inline bool operator<(const ResourceName& lhs, const ResourceNameRef& b) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700518 return ResourceNameRef(lhs) < b;
Adam Lesinski74605cd2016-03-03 15:39:50 -0800519}
520
521inline bool operator!=(const ResourceName& lhs, const ResourceNameRef& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700522 return ResourceNameRef(lhs) != rhs;
Adam Lesinski74605cd2016-03-03 15:39:50 -0800523}
524
Adam Lesinski93190b72017-11-03 15:20:17 -0700525inline bool operator==(const SourcedResourceName& lhs, const SourcedResourceName& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700526 return lhs.name == rhs.name && lhs.line == rhs.line;
Adam Lesinski74605cd2016-03-03 15:39:50 -0800527}
528
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700529} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800530
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700531namespace std {
532
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700533template <>
534struct hash<aapt::ResourceName> {
535 size_t operator()(const aapt::ResourceName& name) const {
536 android::hash_t h = 0;
Adam Lesinskic744ae82017-05-17 19:28:38 -0700537 h = android::JenkinsHashMix(h, static_cast<uint32_t>(hash<string>()(name.package)));
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000538 h = android::JenkinsHashMix(h, static_cast<uint32_t>(hash<string>()(name.type.name)));
Adam Lesinskic744ae82017-05-17 19:28:38 -0700539 h = android::JenkinsHashMix(h, static_cast<uint32_t>(hash<string>()(name.entry)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700540 return static_cast<size_t>(h);
541 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700542};
543
Adam Lesinskic744ae82017-05-17 19:28:38 -0700544template <>
545struct hash<aapt::ResourceId> {
546 size_t operator()(const aapt::ResourceId& id) const {
547 return id.id;
548 }
549};
550
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700551} // namespace std
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700552
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700553#endif // AAPT_RESOURCE_H