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_VALUES_H |
| 18 | #define AAPT_RESOURCE_VALUES_H |
| 19 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 20 | #include <array> |
| 21 | #include <ostream> |
| 22 | #include <vector> |
| 23 | |
| 24 | #include "androidfw/ResourceTypes.h" |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 25 | #include "androidfw/StringPiece.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 26 | |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 27 | #include "Diagnostics.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 28 | #include "Resource.h" |
| 29 | #include "StringPool.h" |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 30 | #include "io/File.h" |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 31 | #include "util/Maybe.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 32 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 33 | namespace aapt { |
| 34 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 35 | struct RawValueVisitor; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 36 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 37 | // A resource value. This is an all-encompassing representation |
| 38 | // of Item and Map and their subclasses. The way to do |
| 39 | // type specific operations is to check the Value's type() and |
| 40 | // cast it to the appropriate subclass. This isn't super clean, |
| 41 | // but it is the simplest strategy. |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 42 | struct Value { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 43 | virtual ~Value() = default; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 44 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 45 | // Whether this value is weak and can be overridden without warning or error. Default is false. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 46 | bool IsWeak() const { return weak_; } |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 47 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 48 | void SetWeak(bool val) { weak_ = val; } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 49 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 50 | // Whether the value is marked as translatable. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 51 | // This does not persist when flattened. |
| 52 | // It is only used during compilation phase. |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 53 | void SetTranslatable(bool val) { translatable_ = val; } |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 54 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 55 | // Default true. |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 56 | bool IsTranslatable() const { return translatable_; } |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 57 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 58 | // Returns the source where this value was defined. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 59 | const Source& GetSource() const { return source_; } |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 60 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 61 | void SetSource(const Source& source) { source_ = source; } |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 62 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 63 | void SetSource(Source&& source) { source_ = std::move(source); } |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 64 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 65 | // Returns the comment that was associated with this resource. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 66 | const std::string& GetComment() const { return comment_; } |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 67 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 68 | void SetComment(const android::StringPiece& str) { comment_ = str.to_string(); } |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 69 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 70 | void SetComment(std::string&& str) { comment_ = std::move(str); } |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 71 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 72 | virtual bool Equals(const Value* value) const = 0; |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 73 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 74 | // Calls the appropriate overload of ValueVisitor. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 75 | virtual void Accept(RawValueVisitor* visitor) = 0; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 76 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 77 | // Clone the value. `new_pool` is the new StringPool that |
| 78 | // any resources with strings should use when copying their string. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 79 | virtual Value* Clone(StringPool* new_pool) const = 0; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 80 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 81 | // Human readable printout of this value. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 82 | virtual void Print(std::ostream* out) const = 0; |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 83 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 84 | protected: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 85 | Source source_; |
| 86 | std::string comment_; |
| 87 | bool weak_ = false; |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 88 | bool translatable_ = true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 89 | }; |
| 90 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 91 | // Inherit from this to get visitor accepting implementations for free. |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 92 | template <typename Derived> |
| 93 | struct BaseValue : public Value { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 94 | void Accept(RawValueVisitor* visitor) override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 95 | }; |
| 96 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 97 | // A resource item with a single value. This maps to android::ResTable_entry. |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 98 | struct Item : public Value { |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 99 | // Clone the Item. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 100 | virtual Item* Clone(StringPool* new_pool) const override = 0; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 101 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 102 | // Fills in an android::Res_value structure with this Item's binary representation. |
| 103 | // Returns false if an error occurred. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 104 | virtual bool Flatten(android::Res_value* out_value) const = 0; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 105 | }; |
| 106 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 107 | // Inherit from this to get visitor accepting implementations for free. |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 108 | template <typename Derived> |
| 109 | struct BaseItem : public Item { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 110 | void Accept(RawValueVisitor* visitor) override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 111 | }; |
| 112 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 113 | // A reference to another resource. This maps to android::Res_value::TYPE_REFERENCE. |
| 114 | // A reference can be symbolic (with the name set to a valid resource name) or be |
| 115 | // numeric (the id is set to a valid resource ID). |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 116 | struct Reference : public BaseItem<Reference> { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 117 | enum class Type { |
| 118 | kResource, |
| 119 | kAttribute, |
| 120 | }; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 121 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 122 | Maybe<ResourceName> name; |
| 123 | Maybe<ResourceId> id; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 124 | Reference::Type reference_type; |
| 125 | bool private_reference = false; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 126 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 127 | Reference(); |
| 128 | explicit Reference(const ResourceNameRef& n, Type type = Type::kResource); |
| 129 | explicit Reference(const ResourceId& i, Type type = Type::kResource); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 130 | Reference(const ResourceNameRef& n, const ResourceId& i); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 131 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 132 | bool Equals(const Value* value) const override; |
| 133 | bool Flatten(android::Res_value* out_value) const override; |
| 134 | Reference* Clone(StringPool* new_pool) const override; |
| 135 | void Print(std::ostream* out) const override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 136 | }; |
| 137 | |
Adam Lesinski | 8197cc46 | 2016-08-19 12:16:49 -0700 | [diff] [blame] | 138 | bool operator<(const Reference&, const Reference&); |
| 139 | bool operator==(const Reference&, const Reference&); |
| 140 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 141 | // An ID resource. Has no real value, just a place holder. |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 142 | struct Id : public BaseItem<Id> { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 143 | Id() { weak_ = true; } |
| 144 | bool Equals(const Value* value) const override; |
| 145 | bool Flatten(android::Res_value* out) const override; |
| 146 | Id* Clone(StringPool* new_pool) const override; |
| 147 | void Print(std::ostream* out) const override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 148 | }; |
| 149 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 150 | // A raw, unprocessed string. This may contain quotations, escape sequences, and whitespace. |
| 151 | // This shall *NOT* end up in the final resource table. |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 152 | struct RawString : public BaseItem<RawString> { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 153 | StringPool::Ref value; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 154 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 155 | explicit RawString(const StringPool::Ref& ref); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 156 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 157 | bool Equals(const Value* value) const override; |
| 158 | bool Flatten(android::Res_value* out_value) const override; |
| 159 | RawString* Clone(StringPool* new_pool) const override; |
| 160 | void Print(std::ostream* out) const override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 161 | }; |
| 162 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 163 | // Identifies a range of characters in a string that are untranslatable. |
| 164 | // These should not be pseudolocalized. The start and end indices are measured in bytes. |
| 165 | struct UntranslatableSection { |
| 166 | // Start offset inclusive. |
| 167 | size_t start; |
| 168 | |
| 169 | // End offset exclusive. |
| 170 | size_t end; |
| 171 | }; |
| 172 | |
| 173 | inline bool operator==(const UntranslatableSection& a, const UntranslatableSection& b) { |
| 174 | return a.start == b.start && a.end == b.end; |
| 175 | } |
| 176 | |
| 177 | inline bool operator!=(const UntranslatableSection& a, const UntranslatableSection& b) { |
| 178 | return a.start != b.start || a.end != b.end; |
| 179 | } |
| 180 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 181 | struct String : public BaseItem<String> { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 182 | StringPool::Ref value; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 183 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 184 | // Sections of the string to NOT translate. Mainly used |
| 185 | // for pseudolocalization. This data is NOT persisted |
| 186 | // in any format. |
| 187 | std::vector<UntranslatableSection> untranslatable_sections; |
| 188 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 189 | explicit String(const StringPool::Ref& ref); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 190 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 191 | bool Equals(const Value* value) const override; |
| 192 | bool Flatten(android::Res_value* out_value) const override; |
| 193 | String* Clone(StringPool* new_pool) const override; |
| 194 | void Print(std::ostream* out) const override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 195 | }; |
| 196 | |
| 197 | struct StyledString : public BaseItem<StyledString> { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 198 | StringPool::StyleRef value; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 199 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 200 | // Sections of the string to NOT translate. Mainly used |
| 201 | // for pseudolocalization. This data is NOT persisted |
| 202 | // in any format. |
| 203 | std::vector<UntranslatableSection> untranslatable_sections; |
| 204 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 205 | explicit StyledString(const StringPool::StyleRef& ref); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 206 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 207 | bool Equals(const Value* value) const override; |
| 208 | bool Flatten(android::Res_value* out_value) const override; |
| 209 | StyledString* Clone(StringPool* new_pool) const override; |
| 210 | void Print(std::ostream* out) const override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 211 | }; |
| 212 | |
| 213 | struct FileReference : public BaseItem<FileReference> { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 214 | StringPool::Ref path; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 215 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 216 | // A handle to the file object from which this file can be read. |
| 217 | // This field is NOT persisted in any format. It is transient. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 218 | io::IFile* file = nullptr; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 219 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 220 | FileReference() = default; |
| 221 | explicit FileReference(const StringPool::Ref& path); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 222 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 223 | bool Equals(const Value* value) const override; |
| 224 | bool Flatten(android::Res_value* out_value) const override; |
| 225 | FileReference* Clone(StringPool* new_pool) const override; |
| 226 | void Print(std::ostream* out) const override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 227 | }; |
| 228 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 229 | // Represents any other android::Res_value. |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 230 | struct BinaryPrimitive : public BaseItem<BinaryPrimitive> { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 231 | android::Res_value value; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 232 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 233 | BinaryPrimitive() = default; |
| 234 | explicit BinaryPrimitive(const android::Res_value& val); |
| 235 | BinaryPrimitive(uint8_t dataType, uint32_t data); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 236 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 237 | bool Equals(const Value* value) const override; |
| 238 | bool Flatten(android::Res_value* out_value) const override; |
| 239 | BinaryPrimitive* Clone(StringPool* new_pool) const override; |
| 240 | void Print(std::ostream* out) const override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 241 | }; |
| 242 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 243 | struct Attribute : public BaseValue<Attribute> { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 244 | struct Symbol { |
| 245 | Reference symbol; |
| 246 | uint32_t value; |
| 247 | }; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 248 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 249 | uint32_t type_mask; |
| 250 | int32_t min_int; |
| 251 | int32_t max_int; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 252 | std::vector<Symbol> symbols; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 253 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 254 | explicit Attribute(bool w, uint32_t t = 0u); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 255 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 256 | bool Equals(const Value* value) const override; |
| 257 | Attribute* Clone(StringPool* new_pool) const override; |
| 258 | void PrintMask(std::ostream* out) const; |
| 259 | void Print(std::ostream* out) const override; |
| 260 | bool Matches(const Item* item, DiagMessage* out_msg) const; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 261 | }; |
| 262 | |
| 263 | struct Style : public BaseValue<Style> { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 264 | struct Entry { |
| 265 | Reference key; |
| 266 | std::unique_ptr<Item> value; |
| 267 | }; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 268 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 269 | Maybe<Reference> parent; |
Adam Lesinski | bdaa092 | 2015-05-08 20:16:23 -0700 | [diff] [blame] | 270 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 271 | // If set to true, the parent was auto inferred from the style's name. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 272 | bool parent_inferred = false; |
Adam Lesinski | bdaa092 | 2015-05-08 20:16:23 -0700 | [diff] [blame] | 273 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 274 | std::vector<Entry> entries; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 275 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 276 | bool Equals(const Value* value) const override; |
| 277 | Style* Clone(StringPool* new_pool) const override; |
| 278 | void Print(std::ostream* out) const override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 279 | }; |
| 280 | |
| 281 | struct Array : public BaseValue<Array> { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 282 | std::vector<std::unique_ptr<Item>> items; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 283 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 284 | bool Equals(const Value* value) const override; |
| 285 | Array* Clone(StringPool* new_pool) const override; |
| 286 | void Print(std::ostream* out) const override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 287 | }; |
| 288 | |
| 289 | struct Plural : public BaseValue<Plural> { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 290 | enum { Zero = 0, One, Two, Few, Many, Other, Count }; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 291 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 292 | std::array<std::unique_ptr<Item>, Count> values; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 293 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 294 | bool Equals(const Value* value) const override; |
| 295 | Plural* Clone(StringPool* new_pool) const override; |
| 296 | void Print(std::ostream* out) const override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 297 | }; |
| 298 | |
| 299 | struct Styleable : public BaseValue<Styleable> { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 300 | std::vector<Reference> entries; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 301 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 302 | bool Equals(const Value* value) const override; |
| 303 | Styleable* Clone(StringPool* newPool) const override; |
| 304 | void Print(std::ostream* out) const override; |
| 305 | void MergeWith(Styleable* styleable); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 306 | }; |
| 307 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 308 | // Stream operator for printing Value objects. |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 309 | inline ::std::ostream& operator<<(::std::ostream& out, const Value& value) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 310 | value.Print(&out); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 311 | return out; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 312 | } |
| 313 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 314 | inline ::std::ostream& operator<<(::std::ostream& out, |
| 315 | const Attribute::Symbol& s) { |
| 316 | if (s.symbol.name) { |
| 317 | out << s.symbol.name.value().entry; |
| 318 | } else { |
| 319 | out << "???"; |
| 320 | } |
| 321 | return out << "=" << s.value; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 322 | } |
| 323 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 324 | } // namespace aapt |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 325 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 326 | #endif // AAPT_RESOURCE_VALUES_H |