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