| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#ifndef AAPT_DOMINATOR_TREE_H |
| 18 |
#define AAPT_DOMINATOR_TREE_H |
| 19 |
|
| 20 |
#include <map> |
| 21 |
#include <memory> |
| 22 |
#include <string> |
| 23 |
#include <vector> |
| 24 |
|
| 25 |
#include "ResourceTable.h" |
| 26 |
|
| 27 |
namespace aapt { |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
class DominatorTree { |
| 49 |
public: |
| 50 |
explicit DominatorTree( |
| 51 |
const std::vector<std::unique_ptr<ResourceConfigValue>>& configs); |
| 52 |
|
| 53 |
class Node { |
| 54 |
public: |
| 55 |
explicit Node(ResourceConfigValue* value = nullptr, Node* parent = nullptr) |
| 56 |
: value_(value), parent_(parent) {} |
| 57 |
|
| 58 |
inline ResourceConfigValue* value() const { return value_; } |
| 59 |
|
| 60 |
inline Node* parent() const { return parent_; } |
| 61 |
|
| 62 |
inline bool is_root_node() const { return !value_; } |
| 63 |
|
| 64 |
inline const std::vector<std::unique_ptr<Node>>& children() const { |
| 65 |
return children_; |
| 66 |
} |
| 67 |
|
| 68 |
bool TryAddChild(std::unique_ptr<Node> new_child); |
| 69 |
|
| 70 |
private: |
| 71 |
bool AddChild(std::unique_ptr<Node> new_child); |
| 72 |
bool Dominates(const Node* other) const; |
| 73 |
|
| 74 |
ResourceConfigValue* value_; |
| 75 |
Node* parent_; |
| 76 |
std::vector<std::unique_ptr<Node>> children_; |
| 77 |
|
| 78 |
DISALLOW_COPY_AND_ASSIGN(Node); |
| 79 |
}; |
| 80 |
|
| 81 |
struct Visitor { |
| 82 |
virtual ~Visitor() = default; |
| 83 |
virtual void VisitTree(const std::string& product, Node* root) = 0; |
| 84 |
}; |
| 85 |
|
| 86 |
class BottomUpVisitor : public Visitor { |
| 87 |
public: |
| 88 |
virtual ~BottomUpVisitor() = default; |
| 89 |
|
| 90 |
void VisitTree(const std::string& product, Node* root) override { |
| 91 |
for (auto& child : root->children()) { |
| 92 |
VisitNode(child.get()); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
virtual void VisitConfig(Node* node) = 0; |
| 97 |
|
| 98 |
private: |
| 99 |
void VisitNode(Node* node) { |
| 100 |
for (auto& child : node->children()) { |
| 101 |
VisitNode(child.get()); |
| 102 |
} |
| 103 |
VisitConfig(node); |
| 104 |
} |
| 105 |
}; |
| 106 |
|
| 107 |
void Accept(Visitor* visitor); |
| 108 |
|
| 109 |
inline const std::map<std::string, Node>& product_roots() const { |
| 110 |
return product_roots_; |
| 111 |
} |
| 112 |
|
| 113 |
private: |
| 114 |
DISALLOW_COPY_AND_ASSIGN(DominatorTree); |
| 115 |
|
| 116 |
std::map<std::string, Node> product_roots_; |
| 117 |
}; |
| 118 |
|
| 119 |
} |
| 120 |
|
| 121 |
#endif |