ResourceValues.h revision 769de98f2dd41bfe39a1c9f76aefd1ad58942733
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
209/**
210 * Sentinel value that should be ignored in the final output.
211 * Mainly used as a placeholder for public entries with no
212 * values defined yet.
213 */
214struct Sentinel : public BaseItem<Sentinel> {
215    bool isWeak() const override;
216    bool flatten(android::Res_value& outValue) const override;
217    Sentinel* clone(StringPool* newPool) const override;
218    void print(std::ostream& out) const override;
219};
220
221struct Attribute : public BaseValue<Attribute> {
222    struct Symbol {
223        Reference symbol;
224        uint32_t value;
225    };
226
227    bool weak;
228    uint32_t typeMask;
229    uint32_t minInt;
230    uint32_t maxInt;
231    std::vector<Symbol> symbols;
232
233    Attribute(bool w, uint32_t t = 0u);
234
235    bool isWeak() const override;
236    virtual Attribute* clone(StringPool* newPool) const override;
237    virtual void print(std::ostream& out) const override;
238};
239
240struct Style : public BaseValue<Style> {
241    struct Entry {
242        Reference key;
243        std::unique_ptr<Item> value;
244    };
245
246    bool weak;
247    Reference parent;
248    std::vector<Entry> entries;
249
250    Style(bool weak);
251    bool isWeak() const override;
252    Style* clone(StringPool* newPool) const override;
253    void print(std::ostream& out) const override;
254};
255
256struct Array : public BaseValue<Array> {
257    std::vector<std::unique_ptr<Item>> items;
258
259    Array* clone(StringPool* newPool) const override;
260    void print(std::ostream& out) const override;
261};
262
263struct Plural : public BaseValue<Plural> {
264    enum {
265        Zero = 0,
266        One,
267        Two,
268        Few,
269        Many,
270        Other,
271        Count
272    };
273
274    std::array<std::unique_ptr<Item>, Count> values;
275
276    Plural* clone(StringPool* newPool) const override;
277    void print(std::ostream& out) const override;
278};
279
280struct Styleable : public BaseValue<Styleable> {
281    std::vector<Reference> entries;
282
283    Styleable* clone(StringPool* newPool) const override;
284    void print(std::ostream& out) const override;
285};
286
287/**
288 * Stream operator for printing Value objects.
289 */
290inline ::std::ostream& operator<<(::std::ostream& out, const Value& value) {
291    value.print(out);
292    return out;
293}
294
295/**
296 * The argument object that gets passed through the value
297 * back to the ValueVisitor. Subclasses of ValueVisitor should
298 * subclass ValueVisitorArgs to contain the data they need
299 * to operate.
300 */
301struct ValueVisitorArgs {};
302
303/**
304 * Visits a value and runs the appropriate method based on its type.
305 */
306struct ValueVisitor {
307    virtual void visit(Reference& reference, ValueVisitorArgs& args) {
308        visitItem(reference, args);
309    }
310
311    virtual void visit(RawString& string, ValueVisitorArgs& args) {
312        visitItem(string, args);
313    }
314
315    virtual void visit(String& string, ValueVisitorArgs& args) {
316        visitItem(string, args);
317    }
318
319    virtual void visit(StyledString& string, ValueVisitorArgs& args) {
320        visitItem(string, args);
321    }
322
323    virtual void visit(FileReference& file, ValueVisitorArgs& args) {
324        visitItem(file, args);
325    }
326
327    virtual void visit(Id& id, ValueVisitorArgs& args) {
328        visitItem(id, args);
329    }
330
331    virtual void visit(BinaryPrimitive& primitive, ValueVisitorArgs& args) {
332        visitItem(primitive, args);
333    }
334
335    virtual void visit(Sentinel& sentinel, ValueVisitorArgs& args) {
336        visitItem(sentinel, args);
337    }
338
339    virtual void visit(Attribute& attr, ValueVisitorArgs& args) {}
340    virtual void visit(Style& style, ValueVisitorArgs& args) {}
341    virtual void visit(Array& array, ValueVisitorArgs& args) {}
342    virtual void visit(Plural& array, ValueVisitorArgs& args) {}
343    virtual void visit(Styleable& styleable, ValueVisitorArgs& args) {}
344
345    virtual void visitItem(Item& item, ValueVisitorArgs& args) {}
346};
347
348/**
349 * Const version of ValueVisitor.
350 */
351struct ConstValueVisitor {
352    virtual void visit(const Reference& reference, ValueVisitorArgs& args) {
353        visitItem(reference, args);
354    }
355
356    virtual void visit(const RawString& string, ValueVisitorArgs& args) {
357        visitItem(string, args);
358    }
359
360    virtual void visit(const String& string, ValueVisitorArgs& args) {
361        visitItem(string, args);
362    }
363
364    virtual void visit(const StyledString& string, ValueVisitorArgs& args) {
365        visitItem(string, args);
366    }
367
368    virtual void visit(const FileReference& file, ValueVisitorArgs& args) {
369        visitItem(file, args);
370    }
371
372    virtual void visit(const Id& id, ValueVisitorArgs& args) {
373        visitItem(id, args);
374    }
375
376    virtual void visit(const BinaryPrimitive& primitive, ValueVisitorArgs& args) {
377        visitItem(primitive, args);
378    }
379
380    virtual void visit(const Sentinel& sentinel, ValueVisitorArgs& args) {
381        visitItem(sentinel, args);
382    }
383
384    virtual void visit(const Attribute& attr, ValueVisitorArgs& args) {}
385    virtual void visit(const Style& style, ValueVisitorArgs& args) {}
386    virtual void visit(const Array& array, ValueVisitorArgs& args) {}
387    virtual void visit(const Plural& array, ValueVisitorArgs& args) {}
388    virtual void visit(const Styleable& styleable, ValueVisitorArgs& args) {}
389
390    virtual void visitItem(const Item& item, ValueVisitorArgs& args) {}
391};
392
393/**
394 * Convenience Visitor that forwards a specific type to a function.
395 * Args are not used as the function can bind variables. Do not use
396 * directly, use the wrapper visitFunc() method.
397 */
398template <typename T, typename TFunc>
399struct ValueVisitorFunc : ValueVisitor {
400    TFunc func;
401
402    ValueVisitorFunc(TFunc f) : func(f) {
403    }
404
405    void visit(T& value, ValueVisitorArgs&) override {
406        func(value);
407    }
408};
409
410/**
411 * Const version of ValueVisitorFunc.
412 */
413template <typename T, typename TFunc>
414struct ConstValueVisitorFunc : ConstValueVisitor {
415    TFunc func;
416
417    ConstValueVisitorFunc(TFunc f) : func(f) {
418    }
419
420    void visit(const T& value, ValueVisitorArgs&) override {
421        func(value);
422    }
423};
424
425template <typename T, typename TFunc>
426void visitFunc(Value& value, TFunc f) {
427    ValueVisitorFunc<T, TFunc> visitor(f);
428    value.accept(visitor, ValueVisitorArgs{});
429}
430
431template <typename T, typename TFunc>
432void visitFunc(const Value& value, TFunc f) {
433    ConstValueVisitorFunc<T, TFunc> visitor(f);
434    value.accept(visitor, ValueVisitorArgs{});
435}
436
437template <typename Derived>
438void BaseValue<Derived>::accept(ValueVisitor& visitor, ValueVisitorArgs&& args) {
439    visitor.visit(static_cast<Derived&>(*this), args);
440}
441
442template <typename Derived>
443void BaseValue<Derived>::accept(ConstValueVisitor& visitor, ValueVisitorArgs&& args) const {
444    visitor.visit(static_cast<const Derived&>(*this), args);
445}
446
447template <typename Derived>
448void BaseItem<Derived>::accept(ValueVisitor& visitor, ValueVisitorArgs&& args) {
449    visitor.visit(static_cast<Derived&>(*this), args);
450}
451
452template <typename Derived>
453void BaseItem<Derived>::accept(ConstValueVisitor& visitor, ValueVisitorArgs&& args) const {
454    visitor.visit(static_cast<const Derived&>(*this), args);
455}
456
457} // namespace aapt
458
459#endif // AAPT_RESOURCE_VALUES_H
460