blob: d394b24d9947a7c4a8d5c897c8d621045bbc8056 [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
Iurii Makhnodfbac392022-02-15 20:02:52 +000028#include "Source.h"
MÃ¥rten Kongstad24c9aa62018-06-20 08:46:41 +020029#include "androidfw/ConfigDescription.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 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080077const ResourceType* ParseResourceType(const 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;
90 ResourceNamedType(const android::StringPiece& n, ResourceType t);
91
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)
111 ResourceNamedTypeRef(const android::StringPiece& n, ResourceType t);
112 ResourceNamedTypeRef& operator=(const ResourceNamedTypeRef& rhs) = default;
113 ResourceNamedTypeRef& operator=(ResourceNamedTypeRef&& rhs) = default;
114 ResourceNamedTypeRef& operator=(const ResourceNamedType& rhs);
115
116 ResourceNamedType ToResourceNamedType() const;
117
118 std::string to_string() const;
119};
120
121ResourceNamedTypeRef ResourceNamedTypeWithDefaultName(ResourceType t);
122
123std::optional<ResourceNamedTypeRef> ParseResourceNamedType(const android::StringPiece& s);
124
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;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700131 ResourceType type = ResourceType::kRaw;
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;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800135 ResourceName(const android::StringPiece& p, ResourceType t, const android::StringPiece& e);
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700136
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700137 int compare(const ResourceName& other) const;
Adam Lesinski8197cc462016-08-19 12:16:49 -0700138
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700139 bool is_valid() const;
Adam Lesinski93190b72017-11-03 15:20:17 -0700140 std::string to_string() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800141};
142
143/**
144 * Same as ResourceName, but uses StringPieces instead.
145 * Use this if you need to avoid copying and know that
146 * the lifetime of this object is shorter than that
147 * of the original string.
148 */
149struct ResourceNameRef {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800150 android::StringPiece package;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700151 ResourceType type = ResourceType::kRaw;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800152 android::StringPiece entry;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800153
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700154 ResourceNameRef() = default;
155 ResourceNameRef(const ResourceNameRef&) = default;
156 ResourceNameRef(ResourceNameRef&&) = default;
Chih-Hung Hsieh1fc78e12018-12-20 13:37:44 -0800157 ResourceNameRef(const ResourceName& rhs); // NOLINT(google-explicit-constructor)
Adam Lesinskid5083f62017-01-16 15:07:21 -0800158 ResourceNameRef(const android::StringPiece& p, ResourceType t, const android::StringPiece& e);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700159 ResourceNameRef& operator=(const ResourceNameRef& rhs) = default;
160 ResourceNameRef& operator=(ResourceNameRef&& rhs) = default;
161 ResourceNameRef& operator=(const ResourceName& rhs);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800162
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700163 bool is_valid() const;
Adam Lesinski93190b72017-11-03 15:20:17 -0700164
165 ResourceName ToResourceName() const;
166 std::string to_string() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800167};
168
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -0800169constexpr const uint8_t kAppPackageId = 0x7fu;
170constexpr const uint8_t kFrameworkPackageId = 0x01u;
171
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800172/**
173 * A binary identifier representing a resource. Internally it
174 * is a 32bit integer split as follows:
175 *
176 * 0xPPTTEEEE
177 *
178 * PP: 8 bit package identifier. 0x01 is reserved for system
179 * and 0x7f is reserved for the running app.
180 * TT: 8 bit type identifier. 0x00 is invalid.
181 * EEEE: 16 bit entry identifier.
182 */
183struct ResourceId {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700184 uint32_t id;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800185
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700186 ResourceId();
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700187 ResourceId(const ResourceId& rhs) = default;
Chih-Hung Hsieh1fc78e12018-12-20 13:37:44 -0800188 ResourceId(uint32_t res_id); // NOLINT(google-explicit-constructor)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700189 ResourceId(uint8_t p, uint8_t t, uint16_t e);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800190
Ryan Mitchellcd78feb2019-12-18 15:20:48 -0800191 // Returns true if the ID is a valid ID that is not dynamic (package ID cannot be 0)
192 bool is_valid_static() const;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800193
194 // Returns true if the ID is a valid ID or dynamic ID (package ID can be 0).
Ryan Mitchellcd78feb2019-12-18 15:20:48 -0800195 bool is_valid() const;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800196
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700197 uint8_t package_id() const;
198 uint8_t type_id() const;
199 uint16_t entry_id() const;
Adam Lesinski93190b72017-11-03 15:20:17 -0700200
201 std::string to_string() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800202};
203
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700204struct SourcedResourceName {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700205 ResourceName name;
206 size_t line;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700207};
208
209struct ResourceFile {
Adam Lesinski00451162017-10-03 07:44:08 -0700210 enum class Type {
211 kUnknown,
212 kPng,
213 kBinaryXml,
214 kProtoXml,
215 };
216
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700217 // Name
218 ResourceName name;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700219
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700220 // Configuration
MÃ¥rten Kongstad24c9aa62018-06-20 08:46:41 +0200221 android::ConfigDescription config;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700222
Adam Lesinski00451162017-10-03 07:44:08 -0700223 // Type
224 Type type;
225
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700226 // Source
227 Source source;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700228
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700229 // Exported symbols
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700230 std::vector<SourcedResourceName> exported_symbols;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700231};
232
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800233/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700234 * Useful struct used as a key to represent a unique resource in associative
235 * containers.
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800236 */
237struct ResourceKey {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700238 ResourceName name;
MÃ¥rten Kongstad24c9aa62018-06-20 08:46:41 +0200239 android::ConfigDescription config;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800240};
241
242bool operator<(const ResourceKey& a, const ResourceKey& b);
243
244/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700245 * Useful struct used as a key to represent a unique resource in associative
246 * containers.
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800247 * Holds a reference to the name, so that name better live longer than this key!
248 */
249struct ResourceKeyRef {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700250 ResourceNameRef name;
MÃ¥rten Kongstad24c9aa62018-06-20 08:46:41 +0200251 android::ConfigDescription config;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800252
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700253 ResourceKeyRef() = default;
MÃ¥rten Kongstad24c9aa62018-06-20 08:46:41 +0200254 ResourceKeyRef(const ResourceNameRef& n, const android::ConfigDescription& c)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700255 : name(n), config(c) {}
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800256
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700257 /**
258 * Prevent taking a reference to a temporary. This is bad.
259 */
MÃ¥rten Kongstad24c9aa62018-06-20 08:46:41 +0200260 ResourceKeyRef(ResourceName&& n, const android::ConfigDescription& c) = delete;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800261};
262
263bool operator<(const ResourceKeyRef& a, const ResourceKeyRef& b);
264
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800265//
266// ResourceId implementation.
267//
268
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700269inline ResourceId::ResourceId() : id(0) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800270
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700271inline ResourceId::ResourceId(uint32_t res_id) : id(res_id) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800272
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700273inline ResourceId::ResourceId(uint8_t p, uint8_t t, uint16_t e)
274 : id((p << 24) | (t << 16) | e) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800275
Ryan Mitchellcd78feb2019-12-18 15:20:48 -0800276inline bool ResourceId::is_valid_static() const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700277 return (id & 0xff000000u) != 0 && (id & 0x00ff0000u) != 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800278}
279
Ryan Mitchellcd78feb2019-12-18 15:20:48 -0800280inline bool ResourceId::is_valid() const {
Adam Lesinski93190b72017-11-03 15:20:17 -0700281 return (id & 0x00ff0000u) != 0;
282}
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800283
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700284inline uint8_t ResourceId::package_id() const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700285 return static_cast<uint8_t>(id >> 24);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800286}
287
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700288inline uint8_t ResourceId::type_id() const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700289 return static_cast<uint8_t>(id >> 16);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800290}
291
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700292inline uint16_t ResourceId::entry_id() const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700293 return static_cast<uint16_t>(id);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800294}
295
Adam Lesinski74605cd2016-03-03 15:39:50 -0800296inline bool operator<(const ResourceId& lhs, const ResourceId& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700297 return lhs.id < rhs.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 Lesinski24aad162015-04-24 19:19:30 -0700302}
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 Lesinski74605cd2016-03-03 15:39:50 -0800306}
307
308inline 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
Adam Lesinski93190b72017-11-03 15:20:17 -0700312inline ::std::ostream& operator<<(::std::ostream& out, const ResourceId& res_id) {
313 return out << res_id.to_string();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800314}
315
Adam Lesinskia693c4a2017-11-09 11:29:39 -0800316// For generic code to call 'using std::to_string; to_string(T);'.
317inline std::string to_string(const ResourceId& id) {
318 return id.to_string();
319}
320
Clark DuVall8f51d6b2020-04-22 13:19:28 -0700321// Helper to compare resource IDs, moving dynamic IDs after framework IDs.
322inline bool cmp_ids_dynamic_after_framework(const ResourceId& a, const ResourceId& b) {
323 // If one of a and b is from the framework package (package ID 0x01), and the
324 // other is a dynamic ID (package ID 0x00), then put the dynamic ID after the
325 // framework ID. This ensures that when AssetManager resolves the dynamic IDs,
326 // they will be in sorted order as expected by AssetManager.
327 if ((a.package_id() == kFrameworkPackageId && b.package_id() == 0x00) ||
328 (a.package_id() == 0x00 && b.package_id() == kFrameworkPackageId)) {
329 return b < a;
330 }
331 return a < b;
332}
333
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800334//
335// ResourceType implementation.
336//
337
Adam Lesinski93190b72017-11-03 15:20:17 -0700338inline ::std::ostream& operator<<(::std::ostream& out, const ResourceType& val) {
339 return out << to_string(val);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800340}
341
342//
Iurii Makhnodfbac392022-02-15 20:02:52 +0000343// ResourceNamedType implementation.
344//
345inline ResourceNamedType::ResourceNamedType(const android::StringPiece& n, ResourceType t)
346 : name(n.to_string()), type(t) {
347}
348
349inline int ResourceNamedType::compare(const ResourceNamedType& other) const {
350 int cmp = static_cast<int>(type) - static_cast<int>(other.type);
351 if (cmp != 0) return cmp;
352 cmp = name.compare(other.name);
353 return cmp;
354}
355
356inline const std::string& ResourceNamedType::to_string() const {
357 return name;
358}
359
360inline bool operator<(const ResourceNamedType& lhs, const ResourceNamedType& rhs) {
361 return lhs.compare(rhs) < 0;
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 ::std::ostream& operator<<(::std::ostream& out, const ResourceNamedType& val) {
373 return out << val.to_string();
374}
375
376//
377// ResourceNamedTypeRef implementation.
378//
379inline ResourceNamedTypeRef::ResourceNamedTypeRef(const android::StringPiece& n, ResourceType t)
380 : name(n), type(t) {
381}
382
383inline ResourceNamedTypeRef::ResourceNamedTypeRef(const ResourceNamedType& rhs)
384 : name(rhs.name), type(rhs.type) {
385}
386
387inline ResourceNamedTypeRef& ResourceNamedTypeRef::operator=(const ResourceNamedType& rhs) {
388 name = rhs.name;
389 type = rhs.type;
390 return *this;
391}
392
393inline ResourceNamedType ResourceNamedTypeRef::ToResourceNamedType() const {
394 return ResourceNamedType(name, type);
395}
396
397inline std::string ResourceNamedTypeRef::to_string() const {
398 return name.to_string();
399}
400
401inline bool operator<(const ResourceNamedTypeRef& lhs, const ResourceNamedTypeRef& rhs) {
402 return std::tie(lhs.type, lhs.name) < std::tie(rhs.type, rhs.name);
403}
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 ::std::ostream& operator<<(::std::ostream& out, const ResourceNamedTypeRef& val) {
414 return out << val.name;
415}
416
417//
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800418// ResourceName implementation.
419//
420
Adam Lesinskid5083f62017-01-16 15:07:21 -0800421inline ResourceName::ResourceName(const android::StringPiece& p, ResourceType t,
422 const android::StringPiece& e)
423 : package(p.to_string()), type(t), entry(e.to_string()) {}
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700424
Adam Lesinski8197cc462016-08-19 12:16:49 -0700425inline int ResourceName::compare(const ResourceName& other) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700426 int cmp = package.compare(other.package);
427 if (cmp != 0) return cmp;
428 cmp = static_cast<int>(type) - static_cast<int>(other.type);
429 if (cmp != 0) return cmp;
430 cmp = entry.compare(other.entry);
431 return cmp;
Adam Lesinski8197cc462016-08-19 12:16:49 -0700432}
433
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700434inline bool ResourceName::is_valid() const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700435 return !package.empty() && !entry.empty();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800436}
437
Adam Lesinski74605cd2016-03-03 15:39:50 -0800438inline bool operator<(const ResourceName& lhs, const ResourceName& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700439 return std::tie(lhs.package, lhs.type, lhs.entry) <
440 std::tie(rhs.package, rhs.type, rhs.entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800441}
442
Adam Lesinski74605cd2016-03-03 15:39:50 -0800443inline bool operator==(const ResourceName& lhs, const ResourceName& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700444 return std::tie(lhs.package, lhs.type, lhs.entry) ==
445 std::tie(rhs.package, rhs.type, rhs.entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800446}
447
Adam Lesinski74605cd2016-03-03 15:39:50 -0800448inline bool operator!=(const ResourceName& lhs, const ResourceName& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700449 return std::tie(lhs.package, lhs.type, lhs.entry) !=
450 std::tie(rhs.package, rhs.type, rhs.entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800451}
452
Adam Lesinski93190b72017-11-03 15:20:17 -0700453inline ::std::ostream& operator<<(::std::ostream& out, const ResourceName& name) {
454 return out << name.to_string();
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700455}
Adam Lesinski769de982015-04-10 19:43:55 -0700456
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800457//
458// ResourceNameRef implementation.
459//
460
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700461inline ResourceNameRef::ResourceNameRef(const ResourceName& rhs)
462 : package(rhs.package), type(rhs.type), entry(rhs.entry) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800463
Adam Lesinskid5083f62017-01-16 15:07:21 -0800464inline ResourceNameRef::ResourceNameRef(const android::StringPiece& p, ResourceType t,
465 const android::StringPiece& e)
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700466 : package(p), type(t), entry(e) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800467
468inline ResourceNameRef& ResourceNameRef::operator=(const ResourceName& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700469 package = rhs.package;
470 type = rhs.type;
471 entry = rhs.entry;
472 return *this;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800473}
474
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700475inline ResourceName ResourceNameRef::ToResourceName() const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700476 return ResourceName(package, type, entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800477}
478
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700479inline bool ResourceNameRef::is_valid() const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700480 return !package.empty() && !entry.empty();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800481}
482
Adam Lesinski74605cd2016-03-03 15:39:50 -0800483inline bool operator<(const ResourceNameRef& lhs, const ResourceNameRef& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700484 return std::tie(lhs.package, lhs.type, lhs.entry) <
485 std::tie(rhs.package, rhs.type, rhs.entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800486}
487
Adam Lesinski74605cd2016-03-03 15:39:50 -0800488inline bool operator==(const ResourceNameRef& lhs, const ResourceNameRef& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700489 return std::tie(lhs.package, lhs.type, lhs.entry) ==
490 std::tie(rhs.package, rhs.type, rhs.entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800491}
492
Adam Lesinski74605cd2016-03-03 15:39:50 -0800493inline bool operator!=(const ResourceNameRef& lhs, const ResourceNameRef& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700494 return std::tie(lhs.package, lhs.type, lhs.entry) !=
495 std::tie(rhs.package, rhs.type, rhs.entry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800496}
497
Adam Lesinski93190b72017-11-03 15:20:17 -0700498inline ::std::ostream& operator<<(::std::ostream& out, const ResourceNameRef& name) {
499 return out << name.to_string();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800500}
501
Adam Lesinski74605cd2016-03-03 15:39:50 -0800502inline bool operator<(const ResourceName& lhs, const ResourceNameRef& b) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700503 return ResourceNameRef(lhs) < b;
Adam Lesinski74605cd2016-03-03 15:39:50 -0800504}
505
506inline bool operator!=(const ResourceName& lhs, const ResourceNameRef& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700507 return ResourceNameRef(lhs) != rhs;
Adam Lesinski74605cd2016-03-03 15:39:50 -0800508}
509
Adam Lesinski93190b72017-11-03 15:20:17 -0700510inline bool operator==(const SourcedResourceName& lhs, const SourcedResourceName& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700511 return lhs.name == rhs.name && lhs.line == rhs.line;
Adam Lesinski74605cd2016-03-03 15:39:50 -0800512}
513
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700514} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800515
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700516namespace std {
517
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700518template <>
519struct hash<aapt::ResourceName> {
520 size_t operator()(const aapt::ResourceName& name) const {
521 android::hash_t h = 0;
Adam Lesinskic744ae82017-05-17 19:28:38 -0700522 h = android::JenkinsHashMix(h, static_cast<uint32_t>(hash<string>()(name.package)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700523 h = android::JenkinsHashMix(h, static_cast<uint32_t>(name.type));
Adam Lesinskic744ae82017-05-17 19:28:38 -0700524 h = android::JenkinsHashMix(h, static_cast<uint32_t>(hash<string>()(name.entry)));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700525 return static_cast<size_t>(h);
526 }
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700527};
528
Adam Lesinskic744ae82017-05-17 19:28:38 -0700529template <>
530struct hash<aapt::ResourceId> {
531 size_t operator()(const aapt::ResourceId& id) const {
532 return id.id;
533 }
534};
535
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700536} // namespace std
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700537
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700538#endif // AAPT_RESOURCE_H