Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 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_VALUE_VISITOR_H |
| 18 | #define AAPT_VALUE_VISITOR_H |
| 19 | |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 20 | #include "ResourceTable.h" |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 21 | #include "ResourceValues.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 22 | |
| 23 | namespace aapt { |
| 24 | |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 25 | // Visits a value and invokes the appropriate method based on its type. |
| 26 | // Does not traverse into compound types. Use ValueVisitor for that. |
| 27 | class ValueVisitor { |
| 28 | public: |
| 29 | virtual ~ValueVisitor() = default; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 30 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 31 | virtual void VisitAny(Value* value) {} |
| 32 | virtual void VisitItem(Item* value) { VisitAny(value); } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 33 | virtual void Visit(Reference* value) { VisitItem(value); } |
| 34 | virtual void Visit(RawString* value) { VisitItem(value); } |
| 35 | virtual void Visit(String* value) { VisitItem(value); } |
| 36 | virtual void Visit(StyledString* value) { VisitItem(value); } |
| 37 | virtual void Visit(FileReference* value) { VisitItem(value); } |
| 38 | virtual void Visit(Id* value) { VisitItem(value); } |
| 39 | virtual void Visit(BinaryPrimitive* value) { VisitItem(value); } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 40 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 41 | virtual void Visit(Attribute* value) { VisitAny(value); } |
| 42 | virtual void Visit(Style* value) { VisitAny(value); } |
| 43 | virtual void Visit(Array* value) { VisitAny(value); } |
| 44 | virtual void Visit(Plural* value) { VisitAny(value); } |
| 45 | virtual void Visit(Styleable* value) { VisitAny(value); } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 46 | }; |
| 47 | |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 48 | // Const version of ValueVisitor. |
| 49 | class ConstValueVisitor { |
| 50 | public: |
| 51 | virtual ~ConstValueVisitor() = default; |
| 52 | |
| 53 | virtual void VisitAny(const Value* value) { |
| 54 | } |
| 55 | virtual void VisitItem(const Item* value) { |
| 56 | VisitAny(value); |
| 57 | } |
| 58 | virtual void Visit(const Reference* value) { |
| 59 | VisitItem(value); |
| 60 | } |
| 61 | virtual void Visit(const RawString* value) { |
| 62 | VisitItem(value); |
| 63 | } |
| 64 | virtual void Visit(const String* value) { |
| 65 | VisitItem(value); |
| 66 | } |
| 67 | virtual void Visit(const StyledString* value) { |
| 68 | VisitItem(value); |
| 69 | } |
| 70 | virtual void Visit(const FileReference* value) { |
| 71 | VisitItem(value); |
| 72 | } |
| 73 | virtual void Visit(const Id* value) { |
| 74 | VisitItem(value); |
| 75 | } |
| 76 | virtual void Visit(const BinaryPrimitive* value) { |
| 77 | VisitItem(value); |
| 78 | } |
| 79 | |
| 80 | virtual void Visit(const Attribute* value) { |
| 81 | VisitAny(value); |
| 82 | } |
| 83 | virtual void Visit(const Style* value) { |
| 84 | VisitAny(value); |
| 85 | } |
| 86 | virtual void Visit(const Array* value) { |
| 87 | VisitAny(value); |
| 88 | } |
| 89 | virtual void Visit(const Plural* value) { |
| 90 | VisitAny(value); |
| 91 | } |
| 92 | virtual void Visit(const Styleable* value) { |
| 93 | VisitAny(value); |
| 94 | } |
| 95 | }; |
| 96 | |
Chih-Hung Hsieh | 470f8fc | 2016-08-15 12:32:51 -0700 | [diff] [blame] | 97 | // NOLINT, do not add parentheses around T. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 98 | #define DECL_VISIT_COMPOUND_VALUE(T) \ |
| 99 | virtual void Visit(T* value) override { /* NOLINT */ \ |
| 100 | VisitSubValues(value); \ |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 101 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 102 | |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 103 | // Visits values, and if they are compound values, descends into their components as well. |
| 104 | struct DescendingValueVisitor : public ValueVisitor { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 105 | // The compiler will think we're hiding an overload, when we actually intend |
| 106 | // to call into RawValueVisitor. This will expose the visit methods in the |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 107 | // super class so the compiler knows we are trying to call them. |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 108 | using ValueVisitor::Visit; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 109 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 110 | void VisitSubValues(Attribute* attribute) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 111 | for (Attribute::Symbol& symbol : attribute->symbols) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 112 | Visit(&symbol.symbol); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 116 | void VisitSubValues(Style* style) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 117 | if (style->parent) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 118 | Visit(&style->parent.value()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 121 | for (Style::Entry& entry : style->entries) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 122 | Visit(&entry.key); |
| 123 | entry.value->Accept(this); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 124 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 125 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 126 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 127 | void VisitSubValues(Array* array) { |
Adam Lesinski | 4ffea04 | 2017-08-10 15:37:28 -0700 | [diff] [blame] | 128 | for (std::unique_ptr<Item>& item : array->elements) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 129 | item->Accept(this); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 130 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 131 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 132 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 133 | void VisitSubValues(Plural* plural) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 134 | for (std::unique_ptr<Item>& item : plural->values) { |
| 135 | if (item) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 136 | item->Accept(this); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 137 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 138 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 139 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 140 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 141 | void VisitSubValues(Styleable* styleable) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 142 | for (Reference& reference : styleable->entries) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 143 | Visit(&reference); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 144 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 145 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 146 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 147 | DECL_VISIT_COMPOUND_VALUE(Attribute); |
| 148 | DECL_VISIT_COMPOUND_VALUE(Style); |
| 149 | DECL_VISIT_COMPOUND_VALUE(Array); |
| 150 | DECL_VISIT_COMPOUND_VALUE(Plural); |
| 151 | DECL_VISIT_COMPOUND_VALUE(Styleable); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 152 | }; |
| 153 | |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 154 | // Do not use directly. Helper struct for dyn_cast. |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 155 | template <typename T> |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 156 | struct DynCastVisitor : public ConstValueVisitor { |
| 157 | const T* value = nullptr; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 158 | |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 159 | void Visit(const T* v) override { |
| 160 | value = v; |
| 161 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 162 | }; |
| 163 | |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 164 | // Specialization that checks if the value is an Item. |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 165 | template <> |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 166 | struct DynCastVisitor<Item> : public ConstValueVisitor { |
| 167 | const Item* value = nullptr; |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 168 | |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 169 | void VisitItem(const Item* item) override { |
| 170 | value = item; |
| 171 | } |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 172 | }; |
| 173 | |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 174 | // Returns a valid pointer to T if the value is an instance of T. Returns nullptr if value is |
| 175 | // nullptr of if value is not an instance of T. |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 176 | template <typename T> |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 177 | const T* ValueCast(const Value* value) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 178 | if (!value) { |
| 179 | return nullptr; |
| 180 | } |
| 181 | DynCastVisitor<T> visitor; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 182 | value->Accept(&visitor); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 183 | return visitor.value; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 186 | // Non-const version of ValueCast. |
| 187 | template <typename T> |
| 188 | T* ValueCast(Value* value) { |
| 189 | return const_cast<T*>(ValueCast<T>(static_cast<const Value*>(value))); |
| 190 | } |
| 191 | |
| 192 | inline void VisitAllValuesInPackage(ResourceTablePackage* pkg, ValueVisitor* visitor) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 193 | for (auto& type : pkg->types) { |
| 194 | for (auto& entry : type->entries) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 195 | for (auto& config_value : entry->values) { |
| 196 | config_value->value->Accept(visitor); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 197 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 198 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 199 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 200 | } |
| 201 | |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 202 | inline void VisitAllValuesInTable(ResourceTable* table, ValueVisitor* visitor) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 203 | for (auto& pkg : table->packages) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 204 | VisitAllValuesInPackage(pkg.get(), visitor); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 205 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 206 | } |
| 207 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 208 | } // namespace aapt |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 209 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 210 | #endif // AAPT_VALUE_VISITOR_H |