• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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_TRANSFORMER_H
18 #define AAPT_VALUE_TRANSFORMER_H
19 
20 #include <memory>
21 
22 #include "StringPool.h"
23 
24 namespace aapt {
25 
26 class Value;
27 struct Item;
28 struct Reference;
29 struct Id;
30 struct RawString;
31 struct String;
32 struct StyledString;
33 struct FileReference;
34 struct BinaryPrimitive;
35 struct Attribute;
36 struct Style;
37 struct Array;
38 struct Plural;
39 struct Styleable;
40 struct Macro;
41 
42 #define AAPT_TRANSFORM_VALUE(T)                                    \
43   virtual std::unique_ptr<T> TransformDerived(const T* value) = 0; \
44   virtual std::unique_ptr<Value> TransformValue(const T* value);
45 
46 #define AAPT_TRANSFORM_ITEM(T)                                 \
47   virtual std::unique_ptr<Item> TransformItem(const T* value); \
48   AAPT_TRANSFORM_VALUE(T)
49 
50 /**
51  * An interface for consuming a Value type and transforming it into another Value.
52  *
53  * The interface defines 2 methods for each type (T) that inherits from TransformableValue:
54  *   std::unique_ptr<T> TransformDerived(const T*)
55  *   std::unique_ptr<Value> TransformValue(const T*)
56  *
57  * The interface defines 3 method for each type (T) that inherits from TransformableItem:
58  *   std::unique_ptr<T> TransformDerived(const T*)
59  *   std::unique_ptr<Item> TransformItem(const T*)
60  *   std::unique_ptr<Value> TransformValue(const T*)
61  *
62  * TransformDerived is invoked when Transform is invoked on the derived type T.
63  * TransformItem is invoked when Transform is invoked on an Item type.
64  * TransformValue is invoked when Transform is invoked on a Value type.
65  *
66  *  ValueTransformerImpl transformer(&string_pool);
67  *  T* derived = ...;
68  *  std::unique_ptr<T> new_type = derived->Transform(transformer); // Invokes TransformDerived
69  *
70  *  Item* item = derived;
71  *  std::unique_ptr<Item> new_item = item->TransformItem(transformer); // Invokes TransformItem
72  *
73  *  Value* value = item;
74  *  std::unique_ptr<Value> new_value = value->TransformValue(transformer); // Invokes TransformValue
75  *
76  * For types that inherit from AbstractTransformableItem, the default implementation of
77  * TransformValue invokes TransformItem which invokes TransformDerived.
78  *
79  * For types that inherit from AbstractTransformableValue, the default implementation of
80  * TransformValue invokes TransformDerived.
81  */
82 struct ValueTransformer {
83   // `new_pool` is the new StringPool that newly created Values should use for string storing string
84   // values.
85   explicit ValueTransformer(StringPool* new_pool);
86   virtual ~ValueTransformer() = default;
87 
88   AAPT_TRANSFORM_ITEM(Id);
89   AAPT_TRANSFORM_ITEM(Reference);
90   AAPT_TRANSFORM_ITEM(RawString);
91   AAPT_TRANSFORM_ITEM(String);
92   AAPT_TRANSFORM_ITEM(StyledString);
93   AAPT_TRANSFORM_ITEM(FileReference);
94   AAPT_TRANSFORM_ITEM(BinaryPrimitive);
95 
96   AAPT_TRANSFORM_VALUE(Attribute);
97   AAPT_TRANSFORM_VALUE(Style);
98   AAPT_TRANSFORM_VALUE(Array);
99   AAPT_TRANSFORM_VALUE(Plural);
100   AAPT_TRANSFORM_VALUE(Styleable);
101   AAPT_TRANSFORM_VALUE(Macro);
102 
103  protected:
104   StringPool* const pool_;
105 };
106 
107 #undef AAPT_TRANSFORM_VALUE
108 #undef AAPT_TRANSFORM_ITEM
109 
110 template <typename Derived, typename Base>
111 struct TransformableValue : public Base {
112   // Transform this Derived into another Derived using the transformer.
113   std::unique_ptr<Derived> Transform(ValueTransformer& transformer) const;
114 
115  private:
116   Value* TransformValueImpl(ValueTransformer& transformer) const override;
117 };
118 
119 template <typename Derived, typename Base>
120 struct TransformableItem : public TransformableValue<Derived, Base> {
121  private:
122   Item* TransformItemImpl(ValueTransformer& transformer) const override;
123 };
124 
125 }  // namespace aapt
126 
127 // Implementation
128 #include "ValueTransformer_inline.h"
129 
130 #endif  // AAPT_VALUE_TRANSFORMER_H