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