blob: e51477c668dd556614bafa950b8075880141b193 [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
Jeremy Meyer3d8d4a12024-08-23 17:29:03 -070074struct FeatureFlagAttribute {
75 std::string name;
76 bool negated = false;
77
78 std::string ToString() {
79 return (negated ? "!" : "") + name;
80 }
81
82 bool operator==(const FeatureFlagAttribute& o) const = default;
83};
84
Adam Lesinski93190b72017-11-03 15:20:17 -070085android::StringPiece to_string(ResourceType type);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080086
87/**
Adam Lesinski93190b72017-11-03 15:20:17 -070088 * Returns a pointer to a valid ResourceType, or nullptr if the string was invalid.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080089 */
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070090const ResourceType* ParseResourceType(android::StringPiece str);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080091
92/**
Iurii Makhnodfbac392022-02-15 20:02:52 +000093 * Pair of type name as in ResourceTable and actual resource type.
94 * Corresponds to the 'type' in package:type/entry.
95 *
96 * This is to support resource types with custom names inside resource tables.
97 */
98struct ResourceNamedType {
99 std::string name;
100 ResourceType type = ResourceType::kRaw;
101
102 ResourceNamedType() = default;
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700103 ResourceNamedType(android::StringPiece n, ResourceType t);
Iurii Makhnodfbac392022-02-15 20:02:52 +0000104
105 int compare(const ResourceNamedType& other) const;
106
107 const std::string& to_string() const;
108};
109
110/**
111 * Same as ResourceNamedType, but uses StringPieces instead.
112 * Use this if you need to avoid copying and know that
113 * the lifetime of this object is shorter than that
114 * of the original string.
115 */
116struct ResourceNamedTypeRef {
117 android::StringPiece name;
118 ResourceType type = ResourceType::kRaw;
119
120 ResourceNamedTypeRef() = default;
121 ResourceNamedTypeRef(const ResourceNamedTypeRef&) = default;
122 ResourceNamedTypeRef(ResourceNamedTypeRef&&) = default;
123 ResourceNamedTypeRef(const ResourceNamedType& rhs); // NOLINT(google-explicit-constructor)
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700124 ResourceNamedTypeRef(android::StringPiece n, ResourceType t);
Iurii Makhnodfbac392022-02-15 20:02:52 +0000125 ResourceNamedTypeRef& operator=(const ResourceNamedTypeRef& rhs) = default;
126 ResourceNamedTypeRef& operator=(ResourceNamedTypeRef&& rhs) = default;
127 ResourceNamedTypeRef& operator=(const ResourceNamedType& rhs);
128
129 ResourceNamedType ToResourceNamedType() const;
130
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700131 std::string_view to_string() const;
Iurii Makhnodfbac392022-02-15 20:02:52 +0000132};
133
134ResourceNamedTypeRef ResourceNamedTypeWithDefaultName(ResourceType t);
135
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700136std::optional<ResourceNamedTypeRef> ParseResourceNamedType(android::StringPiece s);
Iurii Makhnodfbac392022-02-15 20:02:52 +0000137
138/**
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800139 * A resource's name. This can uniquely identify
140 * a resource in the ResourceTable.
141 */
142struct ResourceName {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700143 std::string package;
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000144 ResourceNamedType type;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700145 std::string entry;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800146
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700147 ResourceName() = default;
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700148 ResourceName(android::StringPiece p, const ResourceNamedTypeRef& t, android::StringPiece e);
149 ResourceName(android::StringPiece p, ResourceType t, android::StringPiece e);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700150
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700151 int compare(const ResourceName& other) const;
Adam Lesinski8197cc462016-08-19 12:16:49 -0700152
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700153 bool is_valid() const;
Adam Lesinski93190b72017-11-03 15:20:17 -0700154 std::string to_string() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800155};
156
157/**
158 * Same as ResourceName, but uses StringPieces instead.
159 * Use this if you need to avoid copying and know that
160 * the lifetime of this object is shorter than that
161 * of the original string.
162 */
163struct ResourceNameRef {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800164 android::StringPiece package;
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000165 ResourceNamedTypeRef type;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800166 android::StringPiece entry;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800167
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700168 ResourceNameRef() = default;
169 ResourceNameRef(const ResourceNameRef&) = default;
170 ResourceNameRef(ResourceNameRef&&) = default;
Chih-Hung Hsieh1fc78e12018-12-20 13:37:44 -0800171 ResourceNameRef(const ResourceName& rhs); // NOLINT(google-explicit-constructor)
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700172 ResourceNameRef(android::StringPiece p, const ResourceNamedTypeRef& t, android::StringPiece e);
173 ResourceNameRef(android::StringPiece p, ResourceType t, android::StringPiece e);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700174 ResourceNameRef& operator=(const ResourceNameRef& rhs) = default;
175 ResourceNameRef& operator=(ResourceNameRef&& rhs) = default;
176 ResourceNameRef& operator=(const ResourceName& rhs);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800177
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700178 bool is_valid() const;
Adam Lesinski93190b72017-11-03 15:20:17 -0700179
180 ResourceName ToResourceName() const;
181 std::string to_string() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800182};
183
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -0800184constexpr const uint8_t kAppPackageId = 0x7fu;
185constexpr const uint8_t kFrameworkPackageId = 0x01u;
186
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800187/**
188 * A binary identifier representing a resource. Internally it
189 * is a 32bit integer split as follows:
190 *
191 * 0xPPTTEEEE
192 *
193 * PP: 8 bit package identifier. 0x01 is reserved for system
194 * and 0x7f is reserved for the running app.
195 * TT: 8 bit type identifier. 0x00 is invalid.
196 * EEEE: 16 bit entry identifier.
197 */
198struct ResourceId {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700199 uint32_t id;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800200
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700201 ResourceId();
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700202 ResourceId(const ResourceId& rhs) = default;
Chih-Hung Hsieh1fc78e12018-12-20 13:37:44 -0800203 ResourceId(uint32_t res_id); // NOLINT(google-explicit-constructor)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700204 ResourceId(uint8_t p, uint8_t t, uint16_t e);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800205
Ryan Mitchellcd78feb2019-12-18 15:20:48 -0800206 // Returns true if the ID is a valid ID that is not dynamic (package ID cannot be 0)
207 bool is_valid_static() const;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800208
209 // Returns true if the ID is a valid ID or dynamic ID (package ID can be 0).
Ryan Mitchellcd78feb2019-12-18 15:20:48 -0800210 bool is_valid() const;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800211
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700212 uint8_t package_id() const;
213 uint8_t type_id() const;
214 uint16_t entry_id() const;
Adam Lesinski93190b72017-11-03 15:20:17 -0700215
216 std::string to_string() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800217};
218
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700219struct SourcedResourceName {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700220 ResourceName name;
221 size_t line;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700222};
223
224struct ResourceFile {
Adam Lesinski00451162017-10-03 07:44:08 -0700225 enum class Type {
226 kUnknown,
227 kPng,
228 kBinaryXml,
229 kProtoXml,
230 };
231
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700232 // Name
233 ResourceName name;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700234
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700235 // Configuration
MÃ¥rten Kongstad24c9aa62018-06-20 08:46:41 +0200236 android::ConfigDescription config;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700237
Adam Lesinski00451162017-10-03 07:44:08 -0700238 // Type
239 Type type;
240
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700241 // Source
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000242 android::Source source;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700243
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700244 // Exported symbols
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700245 std::vector<SourcedResourceName> exported_symbols;
Jeremy Meyere08e0372024-09-17 12:45:26 -0700246
247 // Flag status
248 FlagStatus flag_status = FlagStatus::NoFlag;
249
250 // Flag
251 std::optional<FeatureFlagAttribute> flag;
Jeremy Meyer08c54a72025-02-24 11:01:49 -0800252
253 bool uses_readwrite_feature_flags = false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700254};
255
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800256/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700257 * Useful struct used as a key to represent a unique resource in associative
258 * containers.
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800259 */
260struct ResourceKey {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700261 ResourceName name;
MÃ¥rten Kongstad24c9aa62018-06-20 08:46:41 +0200262 android::ConfigDescription config;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800263};
264
265bool operator<(const ResourceKey& a, const ResourceKey& b);
266
267/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700268 * Useful struct used as a key to represent a unique resource in associative
269 * containers.
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800270 * Holds a reference to the name, so that name better live longer than this key!
271 */
272struct ResourceKeyRef {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700273 ResourceNameRef name;
MÃ¥rten Kongstad24c9aa62018-06-20 08:46:41 +0200274 android::ConfigDescription config;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800275
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700276 ResourceKeyRef() = default;
MÃ¥rten Kongstad24c9aa62018-06-20 08:46:41 +0200277 ResourceKeyRef(const ResourceNameRef& n, const android::ConfigDescription& c)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700278 : name(n), config(c) {}
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800279
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700280 /**
281 * Prevent taking a reference to a temporary. This is bad.
282 */
MÃ¥rten Kongstad24c9aa62018-06-20 08:46:41 +0200283 ResourceKeyRef(ResourceName&& n, const android::ConfigDescription& c) = delete;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800284};
285
286bool operator<(const ResourceKeyRef& a, const ResourceKeyRef& b);
287
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800288//
289// ResourceId implementation.
290//
291
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700292inline ResourceId::ResourceId() : id(0) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800293
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700294inline ResourceId::ResourceId(uint32_t res_id) : id(res_id) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800295
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700296inline ResourceId::ResourceId(uint8_t p, uint8_t t, uint16_t e)
297 : id((p << 24) | (t << 16) | e) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800298
Ryan Mitchellcd78feb2019-12-18 15:20:48 -0800299inline bool ResourceId::is_valid_static() const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700300 return (id & 0xff000000u) != 0 && (id & 0x00ff0000u) != 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800301}
302
Ryan Mitchellcd78feb2019-12-18 15:20:48 -0800303inline bool ResourceId::is_valid() const {
Adam Lesinski93190b72017-11-03 15:20:17 -0700304 return (id & 0x00ff0000u) != 0;
305}
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800306
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700307inline uint8_t ResourceId::package_id() const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700308 return static_cast<uint8_t>(id >> 24);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800309}
310
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700311inline uint8_t ResourceId::type_id() const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700312 return static_cast<uint8_t>(id >> 16);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800313}
314
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700315inline uint16_t ResourceId::entry_id() const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700316 return static_cast<uint16_t>(id);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800317}
318
Adam Lesinski74605cd2016-03-03 15:39:50 -0800319inline bool operator<(const ResourceId& lhs, const ResourceId& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700320 return lhs.id < rhs.id;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800321}
322
Adam Lesinski74605cd2016-03-03 15:39:50 -0800323inline bool operator>(const ResourceId& lhs, const ResourceId& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700324 return lhs.id > rhs.id;
Adam Lesinski24aad162015-04-24 19:19:30 -0700325}
326
Adam Lesinski74605cd2016-03-03 15:39:50 -0800327inline bool operator==(const ResourceId& lhs, const ResourceId& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700328 return lhs.id == rhs.id;
Adam Lesinski74605cd2016-03-03 15:39:50 -0800329}
330
331inline bool operator!=(const ResourceId& lhs, const ResourceId& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700332 return lhs.id != rhs.id;
Adam Lesinski74605cd2016-03-03 15:39:50 -0800333}
334
Adam Lesinski93190b72017-11-03 15:20:17 -0700335inline ::std::ostream& operator<<(::std::ostream& out, const ResourceId& res_id) {
336 return out << res_id.to_string();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800337}
338
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800339// For generic code to call 'using std::to_string; to_string(T);'.
340inline std::string to_string(const ResourceId& id) {
341 return id.to_string();
342}
343
Clark DuVall8f51d6b2020-04-22 13:19:28 -0700344// Helper to compare resource IDs, moving dynamic IDs after framework IDs.
345inline bool cmp_ids_dynamic_after_framework(const ResourceId& a, const ResourceId& b) {
346 // If one of a and b is from the framework package (package ID 0x01), and the
347 // other is a dynamic ID (package ID 0x00), then put the dynamic ID after the
348 // framework ID. This ensures that when AssetManager resolves the dynamic IDs,
349 // they will be in sorted order as expected by AssetManager.
350 if ((a.package_id() == kFrameworkPackageId && b.package_id() == 0x00) ||
351 (a.package_id() == 0x00 && b.package_id() == kFrameworkPackageId)) {
352 return b < a;
353 }
354 return a < b;
355}
356
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800357//
358// ResourceType implementation.
359//
360
Adam Lesinski93190b72017-11-03 15:20:17 -0700361inline ::std::ostream& operator<<(::std::ostream& out, const ResourceType& val) {
362 return out << to_string(val);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800363}
364
365//
Iurii Makhnodfbac392022-02-15 20:02:52 +0000366// ResourceNamedType implementation.
367//
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700368inline ResourceNamedType::ResourceNamedType(android::StringPiece n, ResourceType t)
369 : name(n), type(t) {
Iurii Makhnodfbac392022-02-15 20:02:52 +0000370}
371
372inline int ResourceNamedType::compare(const ResourceNamedType& other) const {
373 int cmp = static_cast<int>(type) - static_cast<int>(other.type);
374 if (cmp != 0) return cmp;
375 cmp = name.compare(other.name);
376 return cmp;
377}
378
379inline const std::string& ResourceNamedType::to_string() const {
380 return name;
381}
382
383inline bool operator<(const ResourceNamedType& lhs, const ResourceNamedType& rhs) {
384 return lhs.compare(rhs) < 0;
385}
386
387inline bool operator==(const ResourceNamedType& lhs, const ResourceNamedType& rhs) {
388 return lhs.compare(rhs) == 0;
389}
390
391inline bool operator!=(const ResourceNamedType& lhs, const ResourceNamedType& rhs) {
392 return lhs.compare(rhs) != 0;
393}
394
395inline ::std::ostream& operator<<(::std::ostream& out, const ResourceNamedType& val) {
396 return out << val.to_string();
397}
398
399//
400// ResourceNamedTypeRef implementation.
401//
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700402inline ResourceNamedTypeRef::ResourceNamedTypeRef(android::StringPiece n, ResourceType t)
Iurii Makhnodfbac392022-02-15 20:02:52 +0000403 : name(n), type(t) {
404}
405
406inline ResourceNamedTypeRef::ResourceNamedTypeRef(const ResourceNamedType& rhs)
407 : name(rhs.name), type(rhs.type) {
408}
409
410inline ResourceNamedTypeRef& ResourceNamedTypeRef::operator=(const ResourceNamedType& rhs) {
411 name = rhs.name;
412 type = rhs.type;
413 return *this;
414}
415
416inline ResourceNamedType ResourceNamedTypeRef::ToResourceNamedType() const {
417 return ResourceNamedType(name, type);
418}
419
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700420inline std::string_view ResourceNamedTypeRef::to_string() const {
421 return name;
Iurii Makhnodfbac392022-02-15 20:02:52 +0000422}
423
424inline bool operator<(const ResourceNamedTypeRef& lhs, const ResourceNamedTypeRef& rhs) {
425 return std::tie(lhs.type, lhs.name) < std::tie(rhs.type, rhs.name);
426}
427
428inline bool operator==(const ResourceNamedTypeRef& lhs, const ResourceNamedTypeRef& rhs) {
429 return std::tie(lhs.type, lhs.name) == std::tie(rhs.type, rhs.name);
430}
431
432inline bool operator!=(const ResourceNamedTypeRef& lhs, const ResourceNamedTypeRef& rhs) {
433 return std::tie(lhs.type, lhs.name) != std::tie(rhs.type, rhs.name);
434}
435
436inline ::std::ostream& operator<<(::std::ostream& out, const ResourceNamedTypeRef& val) {
437 return out << val.name;
438}
439
440//
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800441// ResourceName implementation.
442//
443
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700444inline ResourceName::ResourceName(android::StringPiece p, const ResourceNamedTypeRef& t,
445 android::StringPiece e)
446 : package(p), type(t.ToResourceNamedType()), entry(e) {
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000447}
448
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700449inline ResourceName::ResourceName(android::StringPiece p, ResourceType t, android::StringPiece e)
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000450 : ResourceName(p, ResourceNamedTypeWithDefaultName(t), e) {
451}
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700452
Adam Lesinski8197cc462016-08-19 12:16:49 -0700453inline int ResourceName::compare(const ResourceName& other) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700454 int cmp = package.compare(other.package);
455 if (cmp != 0) return cmp;
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000456 cmp = type.compare(other.type);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700457 if (cmp != 0) return cmp;
458 cmp = entry.compare(other.entry);
459 return cmp;
Adam Lesinski8197cc462016-08-19 12:16:49 -0700460}
461
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700462inline bool ResourceName::is_valid() const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700463 return !package.empty() && !entry.empty();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800464}
465
Adam Lesinski74605cd2016-03-03 15:39:50 -0800466inline bool operator<(const ResourceName& lhs, const ResourceName& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700467 return std::tie(lhs.package, lhs.type, lhs.entry) <
468 std::tie(rhs.package, rhs.type, rhs.entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800469}
470
Adam Lesinski74605cd2016-03-03 15:39:50 -0800471inline bool operator==(const ResourceName& lhs, const ResourceName& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700472 return std::tie(lhs.package, lhs.type, lhs.entry) ==
473 std::tie(rhs.package, rhs.type, rhs.entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800474}
475
Adam Lesinski74605cd2016-03-03 15:39:50 -0800476inline bool operator!=(const ResourceName& lhs, const ResourceName& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700477 return std::tie(lhs.package, lhs.type, lhs.entry) !=
478 std::tie(rhs.package, rhs.type, rhs.entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800479}
480
Adam Lesinski93190b72017-11-03 15:20:17 -0700481inline ::std::ostream& operator<<(::std::ostream& out, const ResourceName& name) {
482 return out << name.to_string();
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700483}
Adam Lesinski769de982015-04-10 19:43:55 -0700484
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800485//
486// ResourceNameRef implementation.
487//
488
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700489inline ResourceNameRef::ResourceNameRef(const ResourceName& rhs)
490 : package(rhs.package), type(rhs.type), entry(rhs.entry) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800491
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700492inline ResourceNameRef::ResourceNameRef(android::StringPiece p, const ResourceNamedTypeRef& t,
493 android::StringPiece e)
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000494 : package(p), type(t), entry(e) {
495}
496
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700497inline ResourceNameRef::ResourceNameRef(android::StringPiece p, ResourceType t,
498 android::StringPiece e)
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000499 : ResourceNameRef(p, ResourceNamedTypeWithDefaultName(t), e) {
500}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800501
502inline ResourceNameRef& ResourceNameRef::operator=(const ResourceName& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700503 package = rhs.package;
504 type = rhs.type;
505 entry = rhs.entry;
506 return *this;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800507}
508
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700509inline ResourceName ResourceNameRef::ToResourceName() const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700510 return ResourceName(package, type, entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800511}
512
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700513inline bool ResourceNameRef::is_valid() const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700514 return !package.empty() && !entry.empty();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800515}
516
Adam Lesinski74605cd2016-03-03 15:39:50 -0800517inline bool operator<(const ResourceNameRef& lhs, const ResourceNameRef& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700518 return std::tie(lhs.package, lhs.type, lhs.entry) <
519 std::tie(rhs.package, rhs.type, rhs.entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800520}
521
Adam Lesinski74605cd2016-03-03 15:39:50 -0800522inline bool operator==(const ResourceNameRef& lhs, const ResourceNameRef& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700523 return std::tie(lhs.package, lhs.type, lhs.entry) ==
524 std::tie(rhs.package, rhs.type, rhs.entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800525}
526
Adam Lesinski74605cd2016-03-03 15:39:50 -0800527inline bool operator!=(const ResourceNameRef& lhs, const ResourceNameRef& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700528 return std::tie(lhs.package, lhs.type, lhs.entry) !=
529 std::tie(rhs.package, rhs.type, rhs.entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800530}
531
Adam Lesinski93190b72017-11-03 15:20:17 -0700532inline ::std::ostream& operator<<(::std::ostream& out, const ResourceNameRef& name) {
533 return out << name.to_string();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800534}
535
Adam Lesinski74605cd2016-03-03 15:39:50 -0800536inline bool operator<(const ResourceName& lhs, const ResourceNameRef& b) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700537 return ResourceNameRef(lhs) < b;
Adam Lesinski74605cd2016-03-03 15:39:50 -0800538}
539
540inline bool operator!=(const ResourceName& lhs, const ResourceNameRef& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700541 return ResourceNameRef(lhs) != rhs;
Adam Lesinski74605cd2016-03-03 15:39:50 -0800542}
543
Adam Lesinski93190b72017-11-03 15:20:17 -0700544inline bool operator==(const SourcedResourceName& lhs, const SourcedResourceName& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700545 return lhs.name == rhs.name && lhs.line == rhs.line;
Adam Lesinski74605cd2016-03-03 15:39:50 -0800546}
547
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700548} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800549
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700550namespace std {
551
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700552template <>
553struct hash<aapt::ResourceName> {
554 size_t operator()(const aapt::ResourceName& name) const {
555 android::hash_t h = 0;
Adam Lesinskic744ae82017-05-17 19:28:38 -0700556 h = android::JenkinsHashMix(h, static_cast<uint32_t>(hash<string>()(name.package)));
Iurii Makhnocff10ce2022-02-15 19:33:50 +0000557 h = android::JenkinsHashMix(h, static_cast<uint32_t>(hash<string>()(name.type.name)));
Adam Lesinskic744ae82017-05-17 19:28:38 -0700558 h = android::JenkinsHashMix(h, static_cast<uint32_t>(hash<string>()(name.entry)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700559 return static_cast<size_t>(h);
560 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700561};
562
Adam Lesinskic744ae82017-05-17 19:28:38 -0700563template <>
564struct hash<aapt::ResourceId> {
565 size_t operator()(const aapt::ResourceId& id) const {
566 return id.id;
567 }
568};
569
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700570} // namespace std
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700571
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700572#endif // AAPT_RESOURCE_H