Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1 | /* |
| 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 Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 20 | #include <iomanip> |
Adam Lesinski | ca2fc35 | 2015-04-03 12:08:26 -0700 | [diff] [blame] | 21 | #include <limits> |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 22 | #include <sstream> |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 23 | #include <string> |
| 24 | #include <tuple> |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 25 | #include <vector> |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 26 | |
MÃ¥rten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 27 | #include "androidfw/ConfigDescription.h" |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 28 | #include "androidfw/StringPiece.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 29 | #include "utils/JenkinsHash.h" |
| 30 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 31 | #include "Source.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 32 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 33 | namespace aapt { |
| 34 | |
| 35 | /** |
| 36 | * The various types of resource types available. Corresponds |
| 37 | * to the 'type' in package:type/entry. |
| 38 | */ |
| 39 | enum class ResourceType { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 40 | kAnim, |
| 41 | kAnimator, |
| 42 | kArray, |
| 43 | kAttr, |
| 44 | kAttrPrivate, |
| 45 | kBool, |
| 46 | kColor, |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 47 | |
| 48 | // Not really a type, but it shows up in some CTS tests and |
| 49 | // we need to continue respecting it. |
| 50 | kConfigVarying, |
| 51 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 52 | kDimen, |
| 53 | kDrawable, |
Adam Lesinski | c0c3663 | 2016-10-19 18:37:53 -0700 | [diff] [blame] | 54 | kFont, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 55 | kFraction, |
| 56 | kId, |
| 57 | kInteger, |
| 58 | kInterpolator, |
| 59 | kLayout, |
| 60 | kMenu, |
| 61 | kMipmap, |
Adam Lesinski | 3b84124 | 2017-07-25 17:15:42 -0700 | [diff] [blame] | 62 | kNavigation, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 63 | kPlurals, |
| 64 | kRaw, |
| 65 | kString, |
| 66 | kStyle, |
| 67 | kStyleable, |
| 68 | kTransition, |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 69 | |
| 70 | // Not a parsed type. It is only used when loading resource tables that may have modified type |
| 71 | // names |
| 72 | kUnknown, |
| 73 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 74 | kXml, |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 75 | }; |
| 76 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 77 | android::StringPiece to_string(ResourceType type); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 78 | |
| 79 | /** |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 80 | * Returns a pointer to a valid ResourceType, or nullptr if the string was invalid. |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 81 | */ |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 82 | const ResourceType* ParseResourceType(const android::StringPiece& str); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 83 | |
| 84 | /** |
| 85 | * A resource's name. This can uniquely identify |
| 86 | * a resource in the ResourceTable. |
| 87 | */ |
| 88 | struct ResourceName { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 89 | std::string package; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 90 | ResourceType type = ResourceType::kRaw; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 91 | std::string entry; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 92 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 93 | ResourceName() = default; |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 94 | ResourceName(const android::StringPiece& p, ResourceType t, const android::StringPiece& e); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 95 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 96 | int compare(const ResourceName& other) const; |
Adam Lesinski | 8197cc46 | 2016-08-19 12:16:49 -0700 | [diff] [blame] | 97 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 98 | bool is_valid() const; |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 99 | std::string to_string() const; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | /** |
| 103 | * Same as ResourceName, but uses StringPieces instead. |
| 104 | * Use this if you need to avoid copying and know that |
| 105 | * the lifetime of this object is shorter than that |
| 106 | * of the original string. |
| 107 | */ |
| 108 | struct ResourceNameRef { |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 109 | android::StringPiece package; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 110 | ResourceType type = ResourceType::kRaw; |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 111 | android::StringPiece entry; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 112 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 113 | ResourceNameRef() = default; |
| 114 | ResourceNameRef(const ResourceNameRef&) = default; |
| 115 | ResourceNameRef(ResourceNameRef&&) = default; |
Chih-Hung Hsieh | 1fc78e1 | 2018-12-20 13:37:44 -0800 | [diff] [blame] | 116 | ResourceNameRef(const ResourceName& rhs); // NOLINT(google-explicit-constructor) |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 117 | ResourceNameRef(const android::StringPiece& p, ResourceType t, const android::StringPiece& e); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 118 | ResourceNameRef& operator=(const ResourceNameRef& rhs) = default; |
| 119 | ResourceNameRef& operator=(ResourceNameRef&& rhs) = default; |
| 120 | ResourceNameRef& operator=(const ResourceName& rhs); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 121 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 122 | bool is_valid() const; |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 123 | |
| 124 | ResourceName ToResourceName() const; |
| 125 | std::string to_string() const; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 126 | }; |
| 127 | |
Adam Lesinski | b5dc4bd | 2017-02-22 19:29:29 -0800 | [diff] [blame] | 128 | constexpr const uint8_t kAppPackageId = 0x7fu; |
| 129 | constexpr const uint8_t kFrameworkPackageId = 0x01u; |
| 130 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 131 | /** |
| 132 | * A binary identifier representing a resource. Internally it |
| 133 | * is a 32bit integer split as follows: |
| 134 | * |
| 135 | * 0xPPTTEEEE |
| 136 | * |
| 137 | * PP: 8 bit package identifier. 0x01 is reserved for system |
| 138 | * and 0x7f is reserved for the running app. |
| 139 | * TT: 8 bit type identifier. 0x00 is invalid. |
| 140 | * EEEE: 16 bit entry identifier. |
| 141 | */ |
| 142 | struct ResourceId { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 143 | uint32_t id; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 144 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 145 | ResourceId(); |
| 146 | ResourceId(const ResourceId& rhs); |
Chih-Hung Hsieh | 1fc78e1 | 2018-12-20 13:37:44 -0800 | [diff] [blame] | 147 | ResourceId(uint32_t res_id); // NOLINT(google-explicit-constructor) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 148 | ResourceId(uint8_t p, uint8_t t, uint16_t e); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 149 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 150 | bool is_valid() const; |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 151 | |
| 152 | // Returns true if the ID is a valid ID or dynamic ID (package ID can be 0). |
| 153 | bool is_valid_dynamic() const; |
| 154 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 155 | uint8_t package_id() const; |
| 156 | uint8_t type_id() const; |
| 157 | uint16_t entry_id() const; |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 158 | |
| 159 | std::string to_string() const; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 160 | }; |
| 161 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 162 | struct SourcedResourceName { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 163 | ResourceName name; |
| 164 | size_t line; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 165 | }; |
| 166 | |
| 167 | struct ResourceFile { |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 168 | enum class Type { |
| 169 | kUnknown, |
| 170 | kPng, |
| 171 | kBinaryXml, |
| 172 | kProtoXml, |
| 173 | }; |
| 174 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 175 | // Name |
| 176 | ResourceName name; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 177 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 178 | // Configuration |
MÃ¥rten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 179 | android::ConfigDescription config; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 180 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 181 | // Type |
| 182 | Type type; |
| 183 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 184 | // Source |
| 185 | Source source; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 186 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 187 | // Exported symbols |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 188 | std::vector<SourcedResourceName> exported_symbols; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 189 | }; |
| 190 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 191 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 192 | * Useful struct used as a key to represent a unique resource in associative |
| 193 | * containers. |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 194 | */ |
| 195 | struct ResourceKey { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 196 | ResourceName name; |
MÃ¥rten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 197 | android::ConfigDescription config; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 198 | }; |
| 199 | |
| 200 | bool operator<(const ResourceKey& a, const ResourceKey& b); |
| 201 | |
| 202 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 203 | * Useful struct used as a key to represent a unique resource in associative |
| 204 | * containers. |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 205 | * Holds a reference to the name, so that name better live longer than this key! |
| 206 | */ |
| 207 | struct ResourceKeyRef { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 208 | ResourceNameRef name; |
MÃ¥rten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 209 | android::ConfigDescription config; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 210 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 211 | ResourceKeyRef() = default; |
MÃ¥rten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 212 | ResourceKeyRef(const ResourceNameRef& n, const android::ConfigDescription& c) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 213 | : name(n), config(c) {} |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 214 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 215 | /** |
| 216 | * Prevent taking a reference to a temporary. This is bad. |
| 217 | */ |
MÃ¥rten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 218 | ResourceKeyRef(ResourceName&& n, const android::ConfigDescription& c) = delete; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 219 | }; |
| 220 | |
| 221 | bool operator<(const ResourceKeyRef& a, const ResourceKeyRef& b); |
| 222 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 223 | // |
| 224 | // ResourceId implementation. |
| 225 | // |
| 226 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 227 | inline ResourceId::ResourceId() : id(0) {} |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 228 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 229 | inline ResourceId::ResourceId(const ResourceId& rhs) : id(rhs.id) {} |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 230 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 231 | inline ResourceId::ResourceId(uint32_t res_id) : id(res_id) {} |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 232 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 233 | inline ResourceId::ResourceId(uint8_t p, uint8_t t, uint16_t e) |
| 234 | : id((p << 24) | (t << 16) | e) {} |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 235 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 236 | inline bool ResourceId::is_valid() const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 237 | return (id & 0xff000000u) != 0 && (id & 0x00ff0000u) != 0; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 238 | } |
| 239 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 240 | inline bool ResourceId::is_valid_dynamic() const { |
| 241 | return (id & 0x00ff0000u) != 0; |
| 242 | } |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 243 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 244 | inline uint8_t ResourceId::package_id() const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 245 | return static_cast<uint8_t>(id >> 24); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 246 | } |
| 247 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 248 | inline uint8_t ResourceId::type_id() const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 249 | return static_cast<uint8_t>(id >> 16); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 250 | } |
| 251 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 252 | inline uint16_t ResourceId::entry_id() const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 253 | return static_cast<uint16_t>(id); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 254 | } |
| 255 | |
Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame] | 256 | inline bool operator<(const ResourceId& lhs, const ResourceId& rhs) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 257 | return lhs.id < rhs.id; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 258 | } |
| 259 | |
Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame] | 260 | inline bool operator>(const ResourceId& lhs, const ResourceId& rhs) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 261 | return lhs.id > rhs.id; |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame] | 264 | inline bool operator==(const ResourceId& lhs, const ResourceId& rhs) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 265 | return lhs.id == rhs.id; |
Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | inline bool operator!=(const ResourceId& lhs, const ResourceId& rhs) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 269 | return lhs.id != rhs.id; |
Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame] | 270 | } |
| 271 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 272 | inline ::std::ostream& operator<<(::std::ostream& out, const ResourceId& res_id) { |
| 273 | return out << res_id.to_string(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 274 | } |
| 275 | |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 276 | // For generic code to call 'using std::to_string; to_string(T);'. |
| 277 | inline std::string to_string(const ResourceId& id) { |
| 278 | return id.to_string(); |
| 279 | } |
| 280 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 281 | // |
| 282 | // ResourceType implementation. |
| 283 | // |
| 284 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 285 | inline ::std::ostream& operator<<(::std::ostream& out, const ResourceType& val) { |
| 286 | return out << to_string(val); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | // |
| 290 | // ResourceName implementation. |
| 291 | // |
| 292 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 293 | inline ResourceName::ResourceName(const android::StringPiece& p, ResourceType t, |
| 294 | const android::StringPiece& e) |
| 295 | : package(p.to_string()), type(t), entry(e.to_string()) {} |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 296 | |
Adam Lesinski | 8197cc46 | 2016-08-19 12:16:49 -0700 | [diff] [blame] | 297 | inline int ResourceName::compare(const ResourceName& other) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 298 | int cmp = package.compare(other.package); |
| 299 | if (cmp != 0) return cmp; |
| 300 | cmp = static_cast<int>(type) - static_cast<int>(other.type); |
| 301 | if (cmp != 0) return cmp; |
| 302 | cmp = entry.compare(other.entry); |
| 303 | return cmp; |
Adam Lesinski | 8197cc46 | 2016-08-19 12:16:49 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 306 | inline bool ResourceName::is_valid() const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 307 | return !package.empty() && !entry.empty(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 308 | } |
| 309 | |
Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame] | 310 | inline bool operator<(const ResourceName& lhs, const ResourceName& rhs) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 311 | return std::tie(lhs.package, lhs.type, lhs.entry) < |
| 312 | std::tie(rhs.package, rhs.type, rhs.entry); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 313 | } |
| 314 | |
Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame] | 315 | inline bool operator==(const ResourceName& lhs, const ResourceName& rhs) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 316 | return std::tie(lhs.package, lhs.type, lhs.entry) == |
| 317 | std::tie(rhs.package, rhs.type, rhs.entry); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 318 | } |
| 319 | |
Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame] | 320 | inline bool operator!=(const ResourceName& lhs, const ResourceName& rhs) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 321 | return std::tie(lhs.package, lhs.type, lhs.entry) != |
| 322 | std::tie(rhs.package, rhs.type, rhs.entry); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 323 | } |
| 324 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 325 | inline ::std::ostream& operator<<(::std::ostream& out, const ResourceName& name) { |
| 326 | return out << name.to_string(); |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 327 | } |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 328 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 329 | // |
| 330 | // ResourceNameRef implementation. |
| 331 | // |
| 332 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 333 | inline ResourceNameRef::ResourceNameRef(const ResourceName& rhs) |
| 334 | : package(rhs.package), type(rhs.type), entry(rhs.entry) {} |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 335 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 336 | inline ResourceNameRef::ResourceNameRef(const android::StringPiece& p, ResourceType t, |
| 337 | const android::StringPiece& e) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 338 | : package(p), type(t), entry(e) {} |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 339 | |
| 340 | inline ResourceNameRef& ResourceNameRef::operator=(const ResourceName& rhs) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 341 | package = rhs.package; |
| 342 | type = rhs.type; |
| 343 | entry = rhs.entry; |
| 344 | return *this; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 345 | } |
| 346 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 347 | inline ResourceName ResourceNameRef::ToResourceName() const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 348 | return ResourceName(package, type, entry); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 349 | } |
| 350 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 351 | inline bool ResourceNameRef::is_valid() const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 352 | return !package.empty() && !entry.empty(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 353 | } |
| 354 | |
Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame] | 355 | inline bool operator<(const ResourceNameRef& lhs, const ResourceNameRef& rhs) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 356 | return std::tie(lhs.package, lhs.type, lhs.entry) < |
| 357 | std::tie(rhs.package, rhs.type, rhs.entry); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 358 | } |
| 359 | |
Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame] | 360 | inline bool operator==(const ResourceNameRef& lhs, const ResourceNameRef& rhs) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 361 | return std::tie(lhs.package, lhs.type, lhs.entry) == |
| 362 | std::tie(rhs.package, rhs.type, rhs.entry); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 363 | } |
| 364 | |
Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame] | 365 | inline bool operator!=(const ResourceNameRef& lhs, const ResourceNameRef& rhs) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 366 | return std::tie(lhs.package, lhs.type, lhs.entry) != |
| 367 | std::tie(rhs.package, rhs.type, rhs.entry); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 368 | } |
| 369 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 370 | inline ::std::ostream& operator<<(::std::ostream& out, const ResourceNameRef& name) { |
| 371 | return out << name.to_string(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 372 | } |
| 373 | |
Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame] | 374 | inline bool operator<(const ResourceName& lhs, const ResourceNameRef& b) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 375 | return ResourceNameRef(lhs) < b; |
Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | inline bool operator!=(const ResourceName& lhs, const ResourceNameRef& rhs) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 379 | return ResourceNameRef(lhs) != rhs; |
Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame] | 380 | } |
| 381 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 382 | inline bool operator==(const SourcedResourceName& lhs, const SourcedResourceName& rhs) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 383 | return lhs.name == rhs.name && lhs.line == rhs.line; |
Adam Lesinski | 74605cd | 2016-03-03 15:39:50 -0800 | [diff] [blame] | 384 | } |
| 385 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 386 | } // namespace aapt |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 387 | |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 388 | namespace std { |
| 389 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 390 | template <> |
| 391 | struct hash<aapt::ResourceName> { |
| 392 | size_t operator()(const aapt::ResourceName& name) const { |
| 393 | android::hash_t h = 0; |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 394 | h = android::JenkinsHashMix(h, static_cast<uint32_t>(hash<string>()(name.package))); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 395 | h = android::JenkinsHashMix(h, static_cast<uint32_t>(name.type)); |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 396 | h = android::JenkinsHashMix(h, static_cast<uint32_t>(hash<string>()(name.entry))); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 397 | return static_cast<size_t>(h); |
| 398 | } |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 399 | }; |
| 400 | |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 401 | template <> |
| 402 | struct hash<aapt::ResourceId> { |
| 403 | size_t operator()(const aapt::ResourceId& id) const { |
| 404 | return id.id; |
| 405 | } |
| 406 | }; |
| 407 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 408 | } // namespace std |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 409 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 410 | #endif // AAPT_RESOURCE_H |