blob: b971589fe431456b382a5024ac055c59ed18359e [file] [log] [blame]
Tom Cherryd853f772017-10-27 15:18:02 -07001//
2// Copyright (C) 2017 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 PROPERTY_INFO_SERIALIZER_TRIE_BUILDER_H
18#define PROPERTY_INFO_SERIALIZER_TRIE_BUILDER_H
19
20#include <memory>
21#include <set>
22#include <string>
23#include <vector>
24
25namespace android {
26namespace properties {
27
28struct PropertyEntryBuilder {
Tom Cherry4094e5e2018-01-11 16:26:10 -080029 PropertyEntryBuilder() : context(nullptr), type(nullptr) {}
30 PropertyEntryBuilder(const std::string& name, const std::string* context, const std::string* type)
31 : name(name), context(context), type(type) {}
Tom Cherryd853f772017-10-27 15:18:02 -070032 std::string name;
33 const std::string* context;
Tom Cherry4094e5e2018-01-11 16:26:10 -080034 const std::string* type;
Tom Cherryd853f772017-10-27 15:18:02 -070035};
36
37class TrieBuilderNode {
38 public:
39 TrieBuilderNode(const std::string& name) : property_entry_(name, nullptr, nullptr) {}
40
41 TrieBuilderNode* FindChild(const std::string& name) {
42 for (auto& child : children_) {
43 if (child.name() == name) return &child;
44 }
45 return nullptr;
46 }
47
48 const TrieBuilderNode* FindChild(const std::string& name) const {
49 for (const auto& child : children_) {
50 if (child.name() == name) return &child;
51 }
52 return nullptr;
53 }
54
55 TrieBuilderNode* AddChild(const std::string& name) { return &children_.emplace_back(name); }
56
57 bool AddPrefixContext(const std::string& prefix, const std::string* context,
Tom Cherry4094e5e2018-01-11 16:26:10 -080058 const std::string* type) {
Tom Cherryd853f772017-10-27 15:18:02 -070059 if (std::find_if(prefixes_.begin(), prefixes_.end(),
60 [&prefix](const auto& t) { return t.name == prefix; }) != prefixes_.end()) {
61 return false;
62 }
63
Tom Cherry4094e5e2018-01-11 16:26:10 -080064 prefixes_.emplace_back(prefix, context, type);
Tom Cherryd853f772017-10-27 15:18:02 -070065 return true;
66 }
67
68 bool AddExactMatchContext(const std::string& exact_match, const std::string* context,
Tom Cherry4094e5e2018-01-11 16:26:10 -080069 const std::string* type) {
Tom Cherryd853f772017-10-27 15:18:02 -070070 if (std::find_if(exact_matches_.begin(), exact_matches_.end(), [&exact_match](const auto& t) {
71 return t.name == exact_match;
72 }) != exact_matches_.end()) {
73 return false;
74 }
75
Tom Cherry4094e5e2018-01-11 16:26:10 -080076 exact_matches_.emplace_back(exact_match, context, type);
Tom Cherryd853f772017-10-27 15:18:02 -070077 return true;
78 }
79
80 const std::string& name() const { return property_entry_.name; }
81 const std::string* context() const { return property_entry_.context; }
82 void set_context(const std::string* context) { property_entry_.context = context; }
Tom Cherry4094e5e2018-01-11 16:26:10 -080083 const std::string* type() const { return property_entry_.type; }
84 void set_type(const std::string* type) { property_entry_.type = type; }
Tom Cherryd853f772017-10-27 15:18:02 -070085
86 const PropertyEntryBuilder property_entry() const { return property_entry_; }
87
88 const std::vector<TrieBuilderNode>& children() const { return children_; }
89 const std::vector<PropertyEntryBuilder>& prefixes() const { return prefixes_; }
90 const std::vector<PropertyEntryBuilder>& exact_matches() const { return exact_matches_; }
91
92 private:
93 PropertyEntryBuilder property_entry_;
94 std::vector<TrieBuilderNode> children_;
95 std::vector<PropertyEntryBuilder> prefixes_;
96 std::vector<PropertyEntryBuilder> exact_matches_;
97};
98
99class TrieBuilder {
100 public:
Tom Cherry4094e5e2018-01-11 16:26:10 -0800101 TrieBuilder(const std::string& default_context, const std::string& default_type);
102 bool AddToTrie(const std::string& name, const std::string& context, const std::string& type,
Tom Cherryd853f772017-10-27 15:18:02 -0700103 bool exact, std::string* error);
104
105 const TrieBuilderNode builder_root() const { return builder_root_; }
106 const std::set<std::string>& contexts() const { return contexts_; }
Tom Cherry4094e5e2018-01-11 16:26:10 -0800107 const std::set<std::string>& types() const { return types_; }
Tom Cherryd853f772017-10-27 15:18:02 -0700108
109 private:
Tom Cherry4094e5e2018-01-11 16:26:10 -0800110 bool AddToTrie(const std::string& name, const std::string* context, const std::string* type,
Tom Cherryd853f772017-10-27 15:18:02 -0700111 bool exact, std::string* error);
112 const std::string* StringPointerFromContainer(const std::string& string,
113 std::set<std::string>* container);
114
115 TrieBuilderNode builder_root_;
116 std::set<std::string> contexts_;
Tom Cherry4094e5e2018-01-11 16:26:10 -0800117 std::set<std::string> types_;
Tom Cherryd853f772017-10-27 15:18:02 -0700118};
119
120} // namespace properties
121} // namespace android
122
123#endif