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