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