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