blob: f8ece6f4e4052f900d23624745411272688a9e1d [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 */
Adam Lesinski769de982015-04-10 19:43:55 -070066 virtual Value* clone(StringPool* newPool) const = 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080067
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 */
Adam Lesinski769de982015-04-10 19:43:55 -070095 virtual Item* clone(StringPool* newPool) const override = 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080096
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;
Adam Lesinski769de982015-04-10 19:43:55 -0700135 Reference* clone(StringPool* newPool) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800136 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;
Adam Lesinski769de982015-04-10 19:43:55 -0700145 Id* clone(StringPool* newPool) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800146 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;
Adam Lesinski769de982015-04-10 19:43:55 -0700160 RawString* clone(StringPool* newPool) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800161 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;
Adam Lesinski769de982015-04-10 19:43:55 -0700170 String* clone(StringPool* newPool) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800171 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;
Adam Lesinski769de982015-04-10 19:43:55 -0700180 StyledString* clone(StringPool* newPool) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800181 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;
Adam Lesinski769de982015-04-10 19:43:55 -0700191 FileReference* clone(StringPool* newPool) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800192 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;
Adam Lesinski769de982015-04-10 19:43:55 -0700205 BinaryPrimitive* clone(StringPool* newPool) const override;
206 void print(std::ostream& out) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800207};
208
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800209struct 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;
Adam Lesinski769de982015-04-10 19:43:55 -0700224 virtual Attribute* clone(StringPool* newPool) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800225 virtual void print(std::ostream& out) const override;
226};
227
228struct Style : public BaseValue<Style> {
229 struct Entry {
230 Reference key;
231 std::unique_ptr<Item> value;
232 };
233
Adam Lesinski769de982015-04-10 19:43:55 -0700234 bool weak;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800235 Reference parent;
236 std::vector<Entry> entries;
237
Adam Lesinski769de982015-04-10 19:43:55 -0700238 Style(bool weak);
239 bool isWeak() const override;
240 Style* clone(StringPool* newPool) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800241 void print(std::ostream& out) const override;
242};
243
244struct Array : public BaseValue<Array> {
245 std::vector<std::unique_ptr<Item>> items;
246
Adam Lesinski769de982015-04-10 19:43:55 -0700247 Array* clone(StringPool* newPool) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800248 void print(std::ostream& out) const override;
249};
250
251struct Plural : public BaseValue<Plural> {
252 enum {
253 Zero = 0,
254 One,
255 Two,
256 Few,
257 Many,
258 Other,
259 Count
260 };
261
262 std::array<std::unique_ptr<Item>, Count> values;
263
Adam Lesinski769de982015-04-10 19:43:55 -0700264 Plural* clone(StringPool* newPool) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800265 void print(std::ostream& out) const override;
266};
267
268struct Styleable : public BaseValue<Styleable> {
269 std::vector<Reference> entries;
270
Adam Lesinski769de982015-04-10 19:43:55 -0700271 Styleable* clone(StringPool* newPool) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800272 void print(std::ostream& out) const override;
273};
274
275/**
276 * Stream operator for printing Value objects.
277 */
278inline ::std::ostream& operator<<(::std::ostream& out, const Value& value) {
279 value.print(out);
280 return out;
281}
282
283/**
284 * The argument object that gets passed through the value
285 * back to the ValueVisitor. Subclasses of ValueVisitor should
286 * subclass ValueVisitorArgs to contain the data they need
287 * to operate.
288 */
289struct ValueVisitorArgs {};
290
291/**
292 * Visits a value and runs the appropriate method based on its type.
293 */
294struct ValueVisitor {
295 virtual void visit(Reference& reference, ValueVisitorArgs& args) {
296 visitItem(reference, args);
297 }
298
299 virtual void visit(RawString& string, ValueVisitorArgs& args) {
300 visitItem(string, args);
301 }
302
303 virtual void visit(String& string, ValueVisitorArgs& args) {
304 visitItem(string, args);
305 }
306
307 virtual void visit(StyledString& string, ValueVisitorArgs& args) {
308 visitItem(string, args);
309 }
310
311 virtual void visit(FileReference& file, ValueVisitorArgs& args) {
312 visitItem(file, args);
313 }
314
315 virtual void visit(Id& id, ValueVisitorArgs& args) {
316 visitItem(id, args);
317 }
318
319 virtual void visit(BinaryPrimitive& primitive, ValueVisitorArgs& args) {
320 visitItem(primitive, args);
321 }
322
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800323 virtual void visit(Attribute& attr, ValueVisitorArgs& args) {}
324 virtual void visit(Style& style, ValueVisitorArgs& args) {}
325 virtual void visit(Array& array, ValueVisitorArgs& args) {}
326 virtual void visit(Plural& array, ValueVisitorArgs& args) {}
327 virtual void visit(Styleable& styleable, ValueVisitorArgs& args) {}
328
329 virtual void visitItem(Item& item, ValueVisitorArgs& args) {}
330};
331
332/**
333 * Const version of ValueVisitor.
334 */
335struct ConstValueVisitor {
336 virtual void visit(const Reference& reference, ValueVisitorArgs& args) {
337 visitItem(reference, args);
338 }
339
340 virtual void visit(const RawString& string, ValueVisitorArgs& args) {
341 visitItem(string, args);
342 }
343
344 virtual void visit(const String& string, ValueVisitorArgs& args) {
345 visitItem(string, args);
346 }
347
348 virtual void visit(const StyledString& string, ValueVisitorArgs& args) {
349 visitItem(string, args);
350 }
351
352 virtual void visit(const FileReference& file, ValueVisitorArgs& args) {
353 visitItem(file, args);
354 }
355
356 virtual void visit(const Id& id, ValueVisitorArgs& args) {
357 visitItem(id, args);
358 }
359
360 virtual void visit(const BinaryPrimitive& primitive, ValueVisitorArgs& args) {
361 visitItem(primitive, args);
362 }
363
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800364 virtual void visit(const Attribute& attr, ValueVisitorArgs& args) {}
365 virtual void visit(const Style& style, ValueVisitorArgs& args) {}
366 virtual void visit(const Array& array, ValueVisitorArgs& args) {}
367 virtual void visit(const Plural& array, ValueVisitorArgs& args) {}
368 virtual void visit(const Styleable& styleable, ValueVisitorArgs& args) {}
369
370 virtual void visitItem(const Item& item, ValueVisitorArgs& args) {}
371};
372
373/**
374 * Convenience Visitor that forwards a specific type to a function.
375 * Args are not used as the function can bind variables. Do not use
376 * directly, use the wrapper visitFunc() method.
377 */
378template <typename T, typename TFunc>
379struct ValueVisitorFunc : ValueVisitor {
380 TFunc func;
381
382 ValueVisitorFunc(TFunc f) : func(f) {
383 }
384
385 void visit(T& value, ValueVisitorArgs&) override {
386 func(value);
387 }
388};
389
390/**
391 * Const version of ValueVisitorFunc.
392 */
393template <typename T, typename TFunc>
394struct ConstValueVisitorFunc : ConstValueVisitor {
395 TFunc func;
396
397 ConstValueVisitorFunc(TFunc f) : func(f) {
398 }
399
400 void visit(const T& value, ValueVisitorArgs&) override {
401 func(value);
402 }
403};
404
405template <typename T, typename TFunc>
406void visitFunc(Value& value, TFunc f) {
407 ValueVisitorFunc<T, TFunc> visitor(f);
408 value.accept(visitor, ValueVisitorArgs{});
409}
410
411template <typename T, typename TFunc>
412void visitFunc(const Value& value, TFunc f) {
413 ConstValueVisitorFunc<T, TFunc> visitor(f);
414 value.accept(visitor, ValueVisitorArgs{});
415}
416
417template <typename Derived>
418void BaseValue<Derived>::accept(ValueVisitor& visitor, ValueVisitorArgs&& args) {
419 visitor.visit(static_cast<Derived&>(*this), args);
420}
421
422template <typename Derived>
423void BaseValue<Derived>::accept(ConstValueVisitor& visitor, ValueVisitorArgs&& args) const {
424 visitor.visit(static_cast<const Derived&>(*this), args);
425}
426
427template <typename Derived>
428void BaseItem<Derived>::accept(ValueVisitor& visitor, ValueVisitorArgs&& args) {
429 visitor.visit(static_cast<Derived&>(*this), args);
430}
431
432template <typename Derived>
433void BaseItem<Derived>::accept(ConstValueVisitor& visitor, ValueVisitorArgs&& args) const {
434 visitor.visit(static_cast<const Derived&>(*this), args);
435}
436
437} // namespace aapt
438
439#endif // AAPT_RESOURCE_VALUES_H