blob: f25bcf08b33069cd0202d2aa1497ea4febb92517 [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
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() 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() 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() 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() 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() 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() 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() 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() 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() 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() 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() 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 Reference parent;
247 std::vector<Entry> entries;
248
249 Style* clone() const override;
250 void print(std::ostream& out) const override;
251};
252
253struct Array : public BaseValue<Array> {
254 std::vector<std::unique_ptr<Item>> items;
255
256 Array* clone() const override;
257 void print(std::ostream& out) const override;
258};
259
260struct Plural : public BaseValue<Plural> {
261 enum {
262 Zero = 0,
263 One,
264 Two,
265 Few,
266 Many,
267 Other,
268 Count
269 };
270
271 std::array<std::unique_ptr<Item>, Count> values;
272
273 Plural* clone() const override;
274 void print(std::ostream& out) const override;
275};
276
277struct Styleable : public BaseValue<Styleable> {
278 std::vector<Reference> entries;
279
280 Styleable* clone() const override;
281 void print(std::ostream& out) const override;
282};
283
284/**
285 * Stream operator for printing Value objects.
286 */
287inline ::std::ostream& operator<<(::std::ostream& out, const Value& value) {
288 value.print(out);
289 return out;
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(Sentinel& sentinel, ValueVisitorArgs& args) {
333 visitItem(sentinel, args);
334 }
335
336 virtual void visit(Attribute& attr, ValueVisitorArgs& args) {}
337 virtual void visit(Style& style, ValueVisitorArgs& args) {}
338 virtual void visit(Array& array, ValueVisitorArgs& args) {}
339 virtual void visit(Plural& array, ValueVisitorArgs& args) {}
340 virtual void visit(Styleable& styleable, ValueVisitorArgs& args) {}
341
342 virtual void visitItem(Item& item, ValueVisitorArgs& args) {}
343};
344
345/**
346 * Const version of ValueVisitor.
347 */
348struct ConstValueVisitor {
349 virtual void visit(const Reference& reference, ValueVisitorArgs& args) {
350 visitItem(reference, args);
351 }
352
353 virtual void visit(const RawString& string, ValueVisitorArgs& args) {
354 visitItem(string, args);
355 }
356
357 virtual void visit(const String& string, ValueVisitorArgs& args) {
358 visitItem(string, args);
359 }
360
361 virtual void visit(const StyledString& string, ValueVisitorArgs& args) {
362 visitItem(string, args);
363 }
364
365 virtual void visit(const FileReference& file, ValueVisitorArgs& args) {
366 visitItem(file, args);
367 }
368
369 virtual void visit(const Id& id, ValueVisitorArgs& args) {
370 visitItem(id, args);
371 }
372
373 virtual void visit(const BinaryPrimitive& primitive, ValueVisitorArgs& args) {
374 visitItem(primitive, args);
375 }
376
377 virtual void visit(const Sentinel& sentinel, ValueVisitorArgs& args) {
378 visitItem(sentinel, args);
379 }
380
381 virtual void visit(const Attribute& attr, ValueVisitorArgs& args) {}
382 virtual void visit(const Style& style, ValueVisitorArgs& args) {}
383 virtual void visit(const Array& array, ValueVisitorArgs& args) {}
384 virtual void visit(const Plural& array, ValueVisitorArgs& args) {}
385 virtual void visit(const Styleable& styleable, ValueVisitorArgs& args) {}
386
387 virtual void visitItem(const Item& item, ValueVisitorArgs& args) {}
388};
389
390/**
391 * Convenience Visitor that forwards a specific type to a function.
392 * Args are not used as the function can bind variables. Do not use
393 * directly, use the wrapper visitFunc() method.
394 */
395template <typename T, typename TFunc>
396struct ValueVisitorFunc : ValueVisitor {
397 TFunc func;
398
399 ValueVisitorFunc(TFunc f) : func(f) {
400 }
401
402 void visit(T& value, ValueVisitorArgs&) override {
403 func(value);
404 }
405};
406
407/**
408 * Const version of ValueVisitorFunc.
409 */
410template <typename T, typename TFunc>
411struct ConstValueVisitorFunc : ConstValueVisitor {
412 TFunc func;
413
414 ConstValueVisitorFunc(TFunc f) : func(f) {
415 }
416
417 void visit(const T& value, ValueVisitorArgs&) override {
418 func(value);
419 }
420};
421
422template <typename T, typename TFunc>
423void visitFunc(Value& value, TFunc f) {
424 ValueVisitorFunc<T, TFunc> visitor(f);
425 value.accept(visitor, ValueVisitorArgs{});
426}
427
428template <typename T, typename TFunc>
429void visitFunc(const Value& value, TFunc f) {
430 ConstValueVisitorFunc<T, TFunc> visitor(f);
431 value.accept(visitor, ValueVisitorArgs{});
432}
433
434template <typename Derived>
435void BaseValue<Derived>::accept(ValueVisitor& visitor, ValueVisitorArgs&& args) {
436 visitor.visit(static_cast<Derived&>(*this), args);
437}
438
439template <typename Derived>
440void BaseValue<Derived>::accept(ConstValueVisitor& visitor, ValueVisitorArgs&& args) const {
441 visitor.visit(static_cast<const Derived&>(*this), args);
442}
443
444template <typename Derived>
445void BaseItem<Derived>::accept(ValueVisitor& visitor, ValueVisitorArgs&& args) {
446 visitor.visit(static_cast<Derived&>(*this), args);
447}
448
449template <typename Derived>
450void BaseItem<Derived>::accept(ConstValueVisitor& visitor, ValueVisitorArgs&& args) const {
451 visitor.visit(static_cast<const Derived&>(*this), args);
452}
453
454} // namespace aapt
455
456#endif // AAPT_RESOURCE_VALUES_H