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