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
20#include "Resource.h"
21#include "StringPool.h"
22
23#include <array>
24#include <androidfw/ResourceTypes.h>
25#include <ostream>
26#include <vector>
27
28namespace aapt {
29
30struct ValueVisitor;
31struct ConstValueVisitor;
32struct ValueVisitorArgs;
33
34/**
35 * A resource value. This is an all-encompassing representation
36 * of Item and Map and their subclasses. The way to do
37 * type specific operations is to check the Value's type() and
38 * cast it to the appropriate subclass. This isn't super clean,
39 * but it is the simplest strategy.
40 */
41struct Value {
42    /**
43     * Whether or not this is an Item.
44     */
45    virtual bool isItem() const;
46
47    /**
48     * Whether this value is weak and can be overriden without
49     * warning or error. Default for base class is false.
50     */
51    virtual bool isWeak() const;
52
53    /**
54     * Calls the appropriate overload of ValueVisitor.
55     */
56    virtual void accept(ValueVisitor& visitor, ValueVisitorArgs&& args) = 0;
57
58    /**
59     * Const version of accept().
60     */
61    virtual void accept(ConstValueVisitor& visitor, ValueVisitorArgs&& args) const = 0;
62
63    /**
64     * Clone the value.
65     */
66    virtual Value* clone(StringPool* newPool) const = 0;
67
68    /**
69     * Human readable printout of this value.
70     */
71    virtual void print(std::ostream& out) const = 0;
72};
73
74/**
75 * Inherit from this to get visitor accepting implementations for free.
76 */
77template <typename Derived>
78struct BaseValue : public Value {
79    virtual void accept(ValueVisitor& visitor, ValueVisitorArgs&& args) override;
80    virtual void accept(ConstValueVisitor& visitor, ValueVisitorArgs&& args) const override;
81};
82
83/**
84 * A resource item with a single value. This maps to android::ResTable_entry.
85 */
86struct Item : public Value {
87    /**
88     * An Item is, of course, an Item.
89     */
90    virtual bool isItem() const override;
91
92    /**
93     * Clone the Item.
94     */
95    virtual Item* clone(StringPool* newPool) const override = 0;
96
97    /**
98     * Fills in an android::Res_value structure with this Item's binary representation.
99     * Returns false if an error ocurred.
100     */
101    virtual bool flatten(android::Res_value& outValue) const = 0;
102};
103
104/**
105 * Inherit from this to get visitor accepting implementations for free.
106 */
107template <typename Derived>
108struct BaseItem : public Item {
109    virtual void accept(ValueVisitor& visitor, ValueVisitorArgs&& args) override;
110    virtual void accept(ConstValueVisitor& visitor, ValueVisitorArgs&& args) const override;
111};
112
113/**
114 * A reference to another resource. This maps to android::Res_value::TYPE_REFERENCE.
115 *
116 * A reference can be symbolic (with the name set to a valid resource name) or be
117 * numeric (the id is set to a valid resource ID).
118 */
119struct Reference : public BaseItem<Reference> {
120    enum class Type {
121        kResource,
122        kAttribute,
123    };
124
125    ResourceName name;
126    ResourceId id;
127    Reference::Type referenceType;
128    bool privateReference = false;
129
130    Reference();
131    Reference(const ResourceNameRef& n, Type type = Type::kResource);
132    Reference(const ResourceId& i, Type type = Type::kResource);
133
134    bool flatten(android::Res_value& outValue) const override;
135    Reference* clone(StringPool* newPool) const override;
136    void print(std::ostream& out) const override;
137};
138
139/**
140 * An ID resource. Has no real value, just a place holder.
141 */
142struct Id : public BaseItem<Id> {
143    bool isWeak() const override;
144    bool flatten(android::Res_value& out) const override;
145    Id* clone(StringPool* newPool) const override;
146    void print(std::ostream& out) const override;
147};
148
149/**
150 * A raw, unprocessed string. This may contain quotations,
151 * escape sequences, and whitespace. This shall *NOT*
152 * end up in the final resource table.
153 */
154struct RawString : public BaseItem<RawString> {
155    StringPool::Ref value;
156
157    RawString(const StringPool::Ref& ref);
158
159    bool flatten(android::Res_value& outValue) const override;
160    RawString* clone(StringPool* newPool) const override;
161    void print(std::ostream& out) const override;
162};
163
164struct String : public BaseItem<String> {
165    StringPool::Ref value;
166
167    String(const StringPool::Ref& ref);
168
169    bool flatten(android::Res_value& outValue) const override;
170    String* clone(StringPool* newPool) const override;
171    void print(std::ostream& out) const override;
172};
173
174struct StyledString : public BaseItem<StyledString> {
175    StringPool::StyleRef value;
176
177    StyledString(const StringPool::StyleRef& ref);
178
179    bool flatten(android::Res_value& outValue) const override;
180    StyledString* clone(StringPool* newPool) const override;
181    void print(std::ostream& out) const override;
182};
183
184struct FileReference : public BaseItem<FileReference> {
185    StringPool::Ref path;
186
187    FileReference() = default;
188    FileReference(const StringPool::Ref& path);
189
190    bool flatten(android::Res_value& outValue) const override;
191    FileReference* clone(StringPool* newPool) const override;
192    void print(std::ostream& out) const override;
193};
194
195/**
196 * Represents any other android::Res_value.
197 */
198struct BinaryPrimitive : public BaseItem<BinaryPrimitive> {
199    android::Res_value value;
200
201    BinaryPrimitive() = default;
202    BinaryPrimitive(const android::Res_value& val);
203
204    bool flatten(android::Res_value& outValue) const override;
205    BinaryPrimitive* clone(StringPool* newPool) const override;
206    void print(std::ostream& out) const override;
207};
208
209struct Attribute : public BaseValue<Attribute> {
210    struct Symbol {
211        Reference symbol;
212        uint32_t value;
213    };
214
215    bool weak;
216    uint32_t typeMask;
217    uint32_t minInt;
218    uint32_t maxInt;
219    std::vector<Symbol> symbols;
220
221    Attribute(bool w, uint32_t t = 0u);
222
223    bool isWeak() const override;
224    virtual Attribute* clone(StringPool* newPool) const override;
225    void printMask(std::ostream& out) const;
226    virtual void print(std::ostream& out) const override;
227};
228
229struct Style : public BaseValue<Style> {
230    struct Entry {
231        Reference key;
232        std::unique_ptr<Item> value;
233    };
234
235    Reference parent;
236
237    /**
238     * If set to true, the parent was auto inferred from the
239     * style's name.
240     */
241    bool parentInferred = false;
242
243    std::vector<Entry> entries;
244
245    Style* clone(StringPool* newPool) const override;
246    void print(std::ostream& out) const override;
247};
248
249struct Array : public BaseValue<Array> {
250    std::vector<std::unique_ptr<Item>> items;
251
252    Array* clone(StringPool* newPool) const override;
253    void print(std::ostream& out) const override;
254};
255
256struct Plural : public BaseValue<Plural> {
257    enum {
258        Zero = 0,
259        One,
260        Two,
261        Few,
262        Many,
263        Other,
264        Count
265    };
266
267    std::array<std::unique_ptr<Item>, Count> values;
268
269    Plural* clone(StringPool* newPool) const override;
270    void print(std::ostream& out) const override;
271};
272
273struct Styleable : public BaseValue<Styleable> {
274    std::vector<Reference> entries;
275
276    Styleable* clone(StringPool* newPool) const override;
277    void print(std::ostream& out) const override;
278};
279
280/**
281 * Stream operator for printing Value objects.
282 */
283inline ::std::ostream& operator<<(::std::ostream& out, const Value& value) {
284    value.print(out);
285    return out;
286}
287
288inline ::std::ostream& operator<<(::std::ostream& out, const Attribute::Symbol& s) {
289    return out << s.symbol.name.entry << "=" << s.value;
290}
291
292/**
293 * The argument object that gets passed through the value
294 * back to the ValueVisitor. Subclasses of ValueVisitor should
295 * subclass ValueVisitorArgs to contain the data they need
296 * to operate.
297 */
298struct ValueVisitorArgs {};
299
300/**
301 * Visits a value and runs the appropriate method based on its type.
302 */
303struct ValueVisitor {
304    virtual void visit(Reference& reference, ValueVisitorArgs& args) {
305        visitItem(reference, args);
306    }
307
308    virtual void visit(RawString& string, ValueVisitorArgs& args) {
309        visitItem(string, args);
310    }
311
312    virtual void visit(String& string, ValueVisitorArgs& args) {
313        visitItem(string, args);
314    }
315
316    virtual void visit(StyledString& string, ValueVisitorArgs& args) {
317        visitItem(string, args);
318    }
319
320    virtual void visit(FileReference& file, ValueVisitorArgs& args) {
321        visitItem(file, args);
322    }
323
324    virtual void visit(Id& id, ValueVisitorArgs& args) {
325        visitItem(id, args);
326    }
327
328    virtual void visit(BinaryPrimitive& primitive, ValueVisitorArgs& args) {
329        visitItem(primitive, args);
330    }
331
332    virtual void visit(Attribute& attr, ValueVisitorArgs& args) {}
333    virtual void visit(Style& style, ValueVisitorArgs& args) {}
334    virtual void visit(Array& array, ValueVisitorArgs& args) {}
335    virtual void visit(Plural& array, ValueVisitorArgs& args) {}
336    virtual void visit(Styleable& styleable, ValueVisitorArgs& args) {}
337
338    virtual void visitItem(Item& item, ValueVisitorArgs& args) {}
339};
340
341/**
342 * Const version of ValueVisitor.
343 */
344struct ConstValueVisitor {
345    virtual void visit(const Reference& reference, ValueVisitorArgs& args) {
346        visitItem(reference, args);
347    }
348
349    virtual void visit(const RawString& string, ValueVisitorArgs& args) {
350        visitItem(string, args);
351    }
352
353    virtual void visit(const String& string, ValueVisitorArgs& args) {
354        visitItem(string, args);
355    }
356
357    virtual void visit(const StyledString& string, ValueVisitorArgs& args) {
358        visitItem(string, args);
359    }
360
361    virtual void visit(const FileReference& file, ValueVisitorArgs& args) {
362        visitItem(file, args);
363    }
364
365    virtual void visit(const Id& id, ValueVisitorArgs& args) {
366        visitItem(id, args);
367    }
368
369    virtual void visit(const BinaryPrimitive& primitive, ValueVisitorArgs& args) {
370        visitItem(primitive, args);
371    }
372
373    virtual void visit(const Attribute& attr, ValueVisitorArgs& args) {}
374    virtual void visit(const Style& style, ValueVisitorArgs& args) {}
375    virtual void visit(const Array& array, ValueVisitorArgs& args) {}
376    virtual void visit(const Plural& array, ValueVisitorArgs& args) {}
377    virtual void visit(const Styleable& styleable, ValueVisitorArgs& args) {}
378
379    virtual void visitItem(const Item& item, ValueVisitorArgs& args) {}
380};
381
382/**
383 * Convenience Visitor that forwards a specific type to a function.
384 * Args are not used as the function can bind variables. Do not use
385 * directly, use the wrapper visitFunc() method.
386 */
387template <typename T, typename TFunc>
388struct ValueVisitorFunc : ValueVisitor {
389    TFunc func;
390
391    ValueVisitorFunc(TFunc f) : func(f) {
392    }
393
394    void visit(T& value, ValueVisitorArgs&) override {
395        func(value);
396    }
397};
398
399/**
400 * Const version of ValueVisitorFunc.
401 */
402template <typename T, typename TFunc>
403struct ConstValueVisitorFunc : ConstValueVisitor {
404    TFunc func;
405
406    ConstValueVisitorFunc(TFunc f) : func(f) {
407    }
408
409    void visit(const T& value, ValueVisitorArgs&) override {
410        func(value);
411    }
412};
413
414template <typename T, typename TFunc>
415void visitFunc(Value& value, TFunc f) {
416    ValueVisitorFunc<T, TFunc> visitor(f);
417    value.accept(visitor, ValueVisitorArgs{});
418}
419
420template <typename T, typename TFunc>
421void visitFunc(const Value& value, TFunc f) {
422    ConstValueVisitorFunc<T, TFunc> visitor(f);
423    value.accept(visitor, ValueVisitorArgs{});
424}
425
426template <typename Derived>
427void BaseValue<Derived>::accept(ValueVisitor& visitor, ValueVisitorArgs&& args) {
428    visitor.visit(static_cast<Derived&>(*this), args);
429}
430
431template <typename Derived>
432void BaseValue<Derived>::accept(ConstValueVisitor& visitor, ValueVisitorArgs&& args) const {
433    visitor.visit(static_cast<const Derived&>(*this), args);
434}
435
436template <typename Derived>
437void BaseItem<Derived>::accept(ValueVisitor& visitor, ValueVisitorArgs&& args) {
438    visitor.visit(static_cast<Derived&>(*this), args);
439}
440
441template <typename Derived>
442void BaseItem<Derived>::accept(ConstValueVisitor& visitor, ValueVisitorArgs&& args) const {
443    visitor.visit(static_cast<const Derived&>(*this), args);
444}
445
446} // namespace aapt
447
448#endif // AAPT_RESOURCE_VALUES_H
449