summaryrefslogtreecommitdiffstats
path: root/tools/aapt2/ResourceValues.h
blob: f25bcf08b33069cd0202d2aa1497ea4febb92517 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef AAPT_RESOURCE_VALUES_H
#define AAPT_RESOURCE_VALUES_H

#include "Resource.h"
#include "StringPool.h"

#include <array>
#include <androidfw/ResourceTypes.h>
#include <ostream>
#include <vector>

namespace aapt {

struct ValueVisitor;
struct ConstValueVisitor;
struct ValueVisitorArgs;

/**
 * A resource value. This is an all-encompassing representation
 * of Item and Map and their subclasses. The way to do
 * type specific operations is to check the Value's type() and
 * cast it to the appropriate subclass. This isn't super clean,
 * but it is the simplest strategy.
 */
struct Value {
    /**
     * Whether or not this is an Item.
     */
    virtual bool isItem() const;

    /**
     * Whether this value is weak and can be overriden without
     * warning or error. Default for base class is false.
     */
    virtual bool isWeak() const;

    /**
     * Calls the appropriate overload of ValueVisitor.
     */
    virtual void accept(ValueVisitor& visitor, ValueVisitorArgs&& args) = 0;

    /**
     * Const version of accept().
     */
    virtual void accept(ConstValueVisitor& visitor, ValueVisitorArgs&& args) const = 0;

    /**
     * Clone the value.
     */
    virtual Value* clone() const = 0;

    /**
     * Human readable printout of this value.
     */
    virtual void print(std::ostream& out) const = 0;
};

/**
 * Inherit from this to get visitor accepting implementations for free.
 */
template <typename Derived>
struct BaseValue : public Value {
    virtual void accept(ValueVisitor& visitor, ValueVisitorArgs&& args) override;
    virtual void accept(ConstValueVisitor& visitor, ValueVisitorArgs&& args) const override;
};

/**
 * A resource item with a single value. This maps to android::ResTable_entry.
 */
struct Item : public Value {
    /**
     * An Item is, of course, an Item.
     */
    virtual bool isItem() const override;

    /**
     * Clone the Item.
     */
    virtual Item* clone() const override = 0;

    /**
     * Fills in an android::Res_value structure with this Item's binary representation.
     * Returns false if an error ocurred.
     */
    virtual bool flatten(android::Res_value& outValue) const = 0;
};

/**
 * Inherit from this to get visitor accepting implementations for free.
 */
template <typename Derived>
struct BaseItem : public Item {
    virtual void accept(ValueVisitor& visitor, ValueVisitorArgs&& args) override;
    virtual void accept(ConstValueVisitor& visitor, ValueVisitorArgs&& args) const override;
};

/**
 * A reference to another resource. This maps to android::Res_value::TYPE_REFERENCE.
 *
 * A reference can be symbolic (with the name set to a valid resource name) or be
 * numeric (the id is set to a valid resource ID).
 */
struct Reference : public BaseItem<Reference> {
    enum class Type {
        kResource,
        kAttribute,
    };

    ResourceName name;
    ResourceId id;
    Reference::Type referenceType;
    bool privateReference = false;

    Reference();
    Reference(const ResourceNameRef& n, Type type = Type::kResource);
    Reference(const ResourceId& i, Type type = Type::kResource);

    bool flatten(android::Res_value& outValue) const override;
    Reference* clone() const override;
    void print(std::ostream& out) const override;
};

/**
 * An ID resource. Has no real value, just a place holder.
 */
struct Id : public BaseItem<Id> {
    bool isWeak() const override;
    bool flatten(android::Res_value& out) const override;
    Id* clone() const override;
    void print(std::ostream& out) const override;
};

/**
 * A raw, unprocessed string. This may contain quotations,
 * escape sequences, and whitespace. This shall *NOT*
 * end up in the final resource table.
 */
struct RawString : public BaseItem<RawString> {
    StringPool::Ref value;

    RawString(const StringPool::Ref& ref);

    bool flatten(android::Res_value& outValue) const override;
    RawString* clone() const override;
    void print(std::ostream& out) const override;
};

struct String : public BaseItem<String> {
    StringPool::Ref value;

    String(const StringPool::Ref& ref);

    bool flatten(android::Res_value& outValue) const override;
    String* clone() const override;
    void print(std::ostream& out) const override;
};

struct StyledString : public BaseItem<StyledString> {
    StringPool::StyleRef value;

    StyledString(const StringPool::StyleRef& ref);

    bool flatten(android::Res_value& outValue) const override;
    StyledString* clone() const override;
    void print(std::ostream& out) const override;
};

struct FileReference : public BaseItem<FileReference> {
    StringPool::Ref path;

    FileReference() = default;
    FileReference(const StringPool::Ref& path);

    bool flatten(android::Res_value& outValue) const override;
    FileReference* clone() const override;
    void print(std::ostream& out) const override;
};

/**
 * Represents any other android::Res_value.
 */
struct BinaryPrimitive : public BaseItem<BinaryPrimitive> {
    android::Res_value value;

    BinaryPrimitive() = default;
    BinaryPrimitive(const android::Res_value& val);

    bool flatten(android::Res_value& outValue) const override;
    BinaryPrimitive* clone() const override;
    void print(::std::ostream& out) const override;
};

/**
 * Sentinel value that should be ignored in the final output.
 * Mainly used as a placeholder for public entries with no
 * values defined yet.
 */
struct Sentinel : public BaseItem<Sentinel> {
    bool isWeak() const override;
    bool flatten(android::Res_value& outValue) const override;
    Sentinel* clone() const override;
    void print(::std::ostream& out) const override;
};

struct Attribute : public BaseValue<Attribute> {
    struct Symbol {
        Reference symbol;
        uint32_t value;
    };

    bool weak;
    uint32_t typeMask;
    uint32_t minInt;
    uint32_t maxInt;
    std::vector<Symbol> symbols;

    Attribute(bool w, uint32_t t = 0u);

    bool isWeak() const override;
    virtual Attribute* clone() const override;
    virtual void print(std::ostream& out) const override;
};

struct Style : public BaseValue<Style> {
    struct Entry {
        Reference key;
        std::unique_ptr<Item> value;
    };

    Reference parent;
    std::vector<Entry> entries;

    Style* clone() const override;
    void print(std::ostream& out) const override;
};

struct Array : public BaseValue<Array> {
    std::vector<std::unique_ptr<Item>> items;

    Array* clone() const override;
    void print(std::ostream& out) const override;
};

struct Plural : public BaseValue<Plural> {
    enum {
        Zero = 0,
        One,
        Two,
        Few,
        Many,
        Other,
        Count
    };

    std::array<std::unique_ptr<Item>, Count> values;

    Plural* clone() const override;
    void print(std::ostream& out) const override;
};

struct Styleable : public BaseValue<Styleable> {
    std::vector<Reference> entries;

    Styleable* clone() const override;
    void print(std::ostream& out) const override;
};

/**
 * Stream operator for printing Value objects.
 */
inline ::std::ostream& operator<<(::std::ostream& out, const Value& value) {
    value.print(out);
    return out;
}

/**
 * The argument object that gets passed through the value
 * back to the ValueVisitor. Subclasses of ValueVisitor should
 * subclass ValueVisitorArgs to contain the data they need
 * to operate.
 */
struct ValueVisitorArgs {};

/**
 * Visits a value and runs the appropriate method based on its type.
 */
struct ValueVisitor {
    virtual void visit(Reference& reference, ValueVisitorArgs& args) {
        visitItem(reference, args);
    }

    virtual void visit(RawString& string, ValueVisitorArgs& args) {
        visitItem(string, args);
    }

    virtual void visit(String& string, ValueVisitorArgs& args) {
        visitItem(string, args);
    }

    virtual void visit(StyledString& string, ValueVisitorArgs& args) {
        visitItem(string, args);
    }

    virtual void visit(FileReference& file, ValueVisitorArgs& args) {
        visitItem(file, args);
    }

    virtual void visit(Id& id, ValueVisitorArgs& args) {
        visitItem(id, args);
    }

    virtual void visit(BinaryPrimitive& primitive, ValueVisitorArgs& args) {
        visitItem(primitive, args);
    }

    virtual void visit(Sentinel& sentinel, ValueVisitorArgs& args) {
        visitItem(sentinel, args);
    }

    virtual void visit(Attribute& attr, ValueVisitorArgs& args) {}
    virtual void visit(Style& style, ValueVisitorArgs& args) {}
    virtual void visit(Array& array, ValueVisitorArgs& args) {}
    virtual void visit(Plural& array, ValueVisitorArgs& args) {}
    virtual void visit(Styleable& styleable, ValueVisitorArgs& args) {}

    virtual void visitItem(Item& item, ValueVisitorArgs& args) {}
};

/**
 * Const version of ValueVisitor.
 */
struct ConstValueVisitor {
    virtual void visit(const Reference& reference, ValueVisitorArgs& args) {
        visitItem(reference, args);
    }

    virtual void visit(const RawString& string, ValueVisitorArgs& args) {
        visitItem(string, args);
    }

    virtual void visit(const String& string, ValueVisitorArgs& args) {
        visitItem(string, args);
    }

    virtual void visit(const StyledString& string, ValueVisitorArgs& args) {
        visitItem(string, args);
    }

    virtual void visit(const FileReference& file, ValueVisitorArgs& args) {
        visitItem(file, args);
    }

    virtual void visit(const Id& id, ValueVisitorArgs& args) {
        visitItem(id, args);
    }

    virtual void visit(const BinaryPrimitive& primitive, ValueVisitorArgs& args) {
        visitItem(primitive, args);
    }

    virtual void visit(const Sentinel& sentinel, ValueVisitorArgs& args) {
        visitItem(sentinel, args);
    }

    virtual void visit(const Attribute& attr, ValueVisitorArgs& args) {}
    virtual void visit(const Style& style, ValueVisitorArgs& args) {}
    virtual void visit(const Array& array, ValueVisitorArgs& args) {}
    virtual void visit(const Plural& array, ValueVisitorArgs& args) {}
    virtual void visit(const Styleable& styleable, ValueVisitorArgs& args) {}

    virtual void visitItem(const Item& item, ValueVisitorArgs& args) {}
};

/**
 * Convenience Visitor that forwards a specific type to a function.
 * Args are not used as the function can bind variables. Do not use
 * directly, use the wrapper visitFunc() method.
 */
template <typename T, typename TFunc>
struct ValueVisitorFunc : ValueVisitor {
    TFunc func;

    ValueVisitorFunc(TFunc f) : func(f) {
    }

    void visit(T& value, ValueVisitorArgs&) override {
        func(value);
    }
};

/**
 * Const version of ValueVisitorFunc.
 */
template <typename T, typename TFunc>
struct ConstValueVisitorFunc : ConstValueVisitor {
    TFunc func;

    ConstValueVisitorFunc(TFunc f) : func(f) {
    }

    void visit(const T& value, ValueVisitorArgs&) override {
        func(value);
    }
};

template <typename T, typename TFunc>
void visitFunc(Value& value, TFunc f) {
    ValueVisitorFunc<T, TFunc> visitor(f);
    value.accept(visitor, ValueVisitorArgs{});
}

template <typename T, typename TFunc>
void visitFunc(const Value& value, TFunc f) {
    ConstValueVisitorFunc<T, TFunc> visitor(f);
    value.accept(visitor, ValueVisitorArgs{});
}

template <typename Derived>
void BaseValue<Derived>::accept(ValueVisitor& visitor, ValueVisitorArgs&& args) {
    visitor.visit(static_cast<Derived&>(*this), args);
}

template <typename Derived>
void BaseValue<Derived>::accept(ConstValueVisitor& visitor, ValueVisitorArgs&& args) const {
    visitor.visit(static_cast<const Derived&>(*this), args);
}

template <typename Derived>
void BaseItem<Derived>::accept(ValueVisitor& visitor, ValueVisitorArgs&& args) {
    visitor.visit(static_cast<Derived&>(*this), args);
}

template <typename Derived>
void BaseItem<Derived>::accept(ConstValueVisitor& visitor, ValueVisitorArgs&& args) const {
    visitor.visit(static_cast<const Derived&>(*this), args);
}

} // namespace aapt

#endif // AAPT_RESOURCE_VALUES_H