2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef AAPT_RESOURCE_VALUES_H
18 #define AAPT_RESOURCE_VALUES_H
20 #include "Diagnostics.h"
22 #include "StringPool.h"
24 #include "util/Maybe.h"
27 #include <androidfw/ResourceTypes.h>
33 struct RawValueVisitor;
36 * A resource value. This is an all-encompassing representation
37 * of Item and Map and their subclasses. The way to do
38 * type specific operations is to check the Value's type() and
39 * cast it to the appropriate subclass. This isn't super clean,
40 * but it is the simplest strategy.
43 virtual ~Value() = default;
46 * Whether this value is weak and can be overridden without
47 * warning or error. Default is false.
53 void setWeak(bool val) {
57 // Whether the value is marked as translateable.
58 // This does not persist when flattened.
59 // It is only used during compilation phase.
60 void setTranslateable(bool val) {
65 bool isTranslateable() const {
66 return mTranslateable;
70 * Returns the source where this value was defined.
72 const Source& getSource() const {
76 void setSource(const Source& source) {
80 void setSource(Source&& source) {
81 mSource = std::move(source);
85 * Returns the comment that was associated with this resource.
87 StringPiece16 getComment() const {
91 void setComment(const StringPiece16& str) {
92 mComment = str.toString();
95 void setComment(std::u16string&& str) {
96 mComment = std::move(str);
99 virtual bool equals(const Value* value) const = 0;
102 * Calls the appropriate overload of ValueVisitor.
104 virtual void accept(RawValueVisitor* visitor) = 0;
109 virtual Value* clone(StringPool* newPool) const = 0;
112 * Human readable printout of this value.
114 virtual void print(std::ostream* out) const = 0;
118 std::u16string mComment;
120 bool mTranslateable = true;
124 * Inherit from this to get visitor accepting implementations for free.
126 template <typename Derived>
127 struct BaseValue : public Value {
128 void accept(RawValueVisitor* visitor) override;
132 * A resource item with a single value. This maps to android::ResTable_entry.
134 struct Item : public Value {
138 virtual Item* clone(StringPool* newPool) const override = 0;
141 * Fills in an android::Res_value structure with this Item's binary representation.
142 * Returns false if an error occurred.
144 virtual bool flatten(android::Res_value* outValue) const = 0;
148 * Inherit from this to get visitor accepting implementations for free.
150 template <typename Derived>
151 struct BaseItem : public Item {
152 void accept(RawValueVisitor* visitor) override;
156 * A reference to another resource. This maps to android::Res_value::TYPE_REFERENCE.
158 * A reference can be symbolic (with the name set to a valid resource name) or be
159 * numeric (the id is set to a valid resource ID).
161 struct Reference : public BaseItem<Reference> {
167 Maybe<ResourceName> name;
168 Maybe<ResourceId> id;
169 Reference::Type referenceType;
170 bool privateReference = false;
173 explicit Reference(const ResourceNameRef& n, Type type = Type::kResource);
174 explicit Reference(const ResourceId& i, Type type = Type::kResource);
176 bool equals(const Value* value) const override;
177 bool flatten(android::Res_value* outValue) const override;
178 Reference* clone(StringPool* newPool) const override;
179 void print(std::ostream* out) const override;
183 * An ID resource. Has no real value, just a place holder.
185 struct Id : public BaseItem<Id> {
186 Id() { mWeak = true; }
187 bool equals(const Value* value) const override;
188 bool flatten(android::Res_value* out) const override;
189 Id* clone(StringPool* newPool) const override;
190 void print(std::ostream* out) const override;
194 * A raw, unprocessed string. This may contain quotations,
195 * escape sequences, and whitespace. This shall *NOT*
196 * end up in the final resource table.
198 struct RawString : public BaseItem<RawString> {
199 StringPool::Ref value;
201 RawString(const StringPool::Ref& ref);
203 bool equals(const Value* value) const override;
204 bool flatten(android::Res_value* outValue) const override;
205 RawString* clone(StringPool* newPool) const override;
206 void print(std::ostream* out) const override;
209 struct String : public BaseItem<String> {
210 StringPool::Ref value;
212 String(const StringPool::Ref& ref);
214 bool equals(const Value* value) const override;
215 bool flatten(android::Res_value* outValue) const override;
216 String* clone(StringPool* newPool) const override;
217 void print(std::ostream* out) const override;
220 struct StyledString : public BaseItem<StyledString> {
221 StringPool::StyleRef value;
223 StyledString(const StringPool::StyleRef& ref);
225 bool equals(const Value* value) const override;
226 bool flatten(android::Res_value* outValue) const override;
227 StyledString* clone(StringPool* newPool) const override;
228 void print(std::ostream* out) const override;
231 struct FileReference : public BaseItem<FileReference> {
232 StringPool::Ref path;
235 * A handle to the file object from which this file can be read.
237 io::IFile* file = nullptr;
239 FileReference() = default;
240 FileReference(const StringPool::Ref& path);
242 bool equals(const Value* value) const override;
243 bool flatten(android::Res_value* outValue) const override;
244 FileReference* clone(StringPool* newPool) const override;
245 void print(std::ostream* out) const override;
249 * Represents any other android::Res_value.
251 struct BinaryPrimitive : public BaseItem<BinaryPrimitive> {
252 android::Res_value value;
254 BinaryPrimitive() = default;
255 BinaryPrimitive(const android::Res_value& val);
256 BinaryPrimitive(uint8_t dataType, uint32_t data);
258 bool equals(const Value* value) const override;
259 bool flatten(android::Res_value* outValue) const override;
260 BinaryPrimitive* clone(StringPool* newPool) const override;
261 void print(std::ostream* out) const override;
264 struct Attribute : public BaseValue<Attribute> {
273 std::vector<Symbol> symbols;
275 Attribute(bool w, uint32_t t = 0u);
277 bool equals(const Value* value) const override;
278 Attribute* clone(StringPool* newPool) const override;
279 void printMask(std::ostream* out) const;
280 void print(std::ostream* out) const override;
281 bool matches(const Item* item, DiagMessage* outMsg) const;
284 struct Style : public BaseValue<Style> {
287 std::unique_ptr<Item> value;
290 Maybe<Reference> parent;
293 * If set to true, the parent was auto inferred from the
296 bool parentInferred = false;
298 std::vector<Entry> entries;
300 bool equals(const Value* value) const override;
301 Style* clone(StringPool* newPool) const override;
302 void print(std::ostream* out) const override;
305 struct Array : public BaseValue<Array> {
306 std::vector<std::unique_ptr<Item>> items;
308 bool equals(const Value* value) const override;
309 Array* clone(StringPool* newPool) const override;
310 void print(std::ostream* out) const override;
313 struct Plural : public BaseValue<Plural> {
324 std::array<std::unique_ptr<Item>, Count> values;
326 bool equals(const Value* value) const override;
327 Plural* clone(StringPool* newPool) const override;
328 void print(std::ostream* out) const override;
331 struct Styleable : public BaseValue<Styleable> {
332 std::vector<Reference> entries;
334 bool equals(const Value* value) const override;
335 Styleable* clone(StringPool* newPool) const override;
336 void print(std::ostream* out) const override;
340 * Stream operator for printing Value objects.
342 inline ::std::ostream& operator<<(::std::ostream& out, const Value& value) {
347 inline ::std::ostream& operator<<(::std::ostream& out, const Attribute::Symbol& s) {
349 out << s.symbol.name.value().entry;
353 return out << "=" << s.value;
358 #endif // AAPT_RESOURCE_VALUES_H