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