OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "tools/gn/value_extractors.h" | 5 #include "tools/gn/value_extractors.h" |
6 | 6 |
7 #include "tools/gn/build_settings.h" | 7 #include "tools/gn/build_settings.h" |
8 #include "tools/gn/err.h" | 8 #include "tools/gn/err.h" |
9 #include "tools/gn/label.h" | 9 #include "tools/gn/label.h" |
10 #include "tools/gn/source_dir.h" | 10 #include "tools/gn/source_dir.h" |
11 #include "tools/gn/source_file.h" | 11 #include "tools/gn/source_file.h" |
| 12 #include "tools/gn/target.h" |
| 13 #include "tools/gn/value.h" |
12 | 14 |
13 namespace { | 15 namespace { |
14 | 16 |
| 17 // Sets the error and returns false on failure. |
| 18 template<typename T, class Converter> |
| 19 bool ListValueExtractor(const Value& value, |
| 20 std::vector<T>* dest, |
| 21 Err* err, |
| 22 const Converter& converter) { |
| 23 if (!value.VerifyTypeIs(Value::LIST, err)) |
| 24 return false; |
| 25 const std::vector<Value>& input_list = value.list_value(); |
| 26 dest->resize(input_list.size()); |
| 27 for (size_t i = 0; i < input_list.size(); i++) { |
| 28 if (!converter(input_list[i], &(*dest)[i], err)) |
| 29 return false; |
| 30 } |
| 31 return true; |
| 32 } |
| 33 |
| 34 // Like the above version but extracts to a UniqueVector and sets the error if |
| 35 // there are duplicates. |
| 36 template<typename T, class Converter> |
| 37 bool ListValueUniqueExtractor(const Value& value, |
| 38 UniqueVector<T>* dest, |
| 39 Err* err, |
| 40 const Converter& converter) { |
| 41 if (!value.VerifyTypeIs(Value::LIST, err)) |
| 42 return false; |
| 43 const std::vector<Value>& input_list = value.list_value(); |
| 44 |
| 45 for (size_t i = 0; i < input_list.size(); i++) { |
| 46 T new_one; |
| 47 if (!converter(input_list[i], &new_one, err)) |
| 48 return false; |
| 49 if (!dest->push_back(new_one)) { |
| 50 // Already in the list, throw error. |
| 51 *err = Err(input_list[i], "Duplicate item in list"); |
| 52 size_t previous_index = dest->IndexOf(new_one); |
| 53 err->AppendSubErr(Err(input_list[previous_index], |
| 54 "This was the previous definition.")); |
| 55 return false; |
| 56 } |
| 57 } |
| 58 return true; |
| 59 } |
| 60 |
15 // This extractor rejects files with system-absolute file paths. If we need | 61 // This extractor rejects files with system-absolute file paths. If we need |
16 // that in the future, we'll have to add some flag to control this. | 62 // that in the future, we'll have to add some flag to control this. |
17 struct RelativeFileConverter { | 63 struct RelativeFileConverter { |
18 RelativeFileConverter(const BuildSettings* build_settings_in, | 64 RelativeFileConverter(const BuildSettings* build_settings_in, |
19 const SourceDir& current_dir_in) | 65 const SourceDir& current_dir_in) |
20 : build_settings(build_settings_in), | 66 : build_settings(build_settings_in), |
21 current_dir(current_dir_in) { | 67 current_dir(current_dir_in) { |
22 } | 68 } |
23 bool operator()(const Value& v, SourceFile* out, Err* err) const { | 69 bool operator()(const Value& v, SourceFile* out, Err* err) const { |
24 if (!v.VerifyTypeIs(Value::STRING, err)) | 70 if (!v.VerifyTypeIs(Value::STRING, err)) |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 const SourceDir& current_dir, | 149 const SourceDir& current_dir, |
104 std::vector<SourceDir>* dest, | 150 std::vector<SourceDir>* dest, |
105 Err* err) { | 151 Err* err) { |
106 return ListValueExtractor(value, dest, err, | 152 return ListValueExtractor(value, dest, err, |
107 RelativeDirConverter(build_settings, current_dir)); | 153 RelativeDirConverter(build_settings, current_dir)); |
108 } | 154 } |
109 | 155 |
110 bool ExtractListOfLabels(const Value& value, | 156 bool ExtractListOfLabels(const Value& value, |
111 const SourceDir& current_dir, | 157 const SourceDir& current_dir, |
112 const Label& current_toolchain, | 158 const Label& current_toolchain, |
113 LabelConfigVector* dest, | |
114 Err* err) { | |
115 return ListValueExtractor(value, dest, err, | |
116 LabelResolver<Config>(current_dir, | |
117 current_toolchain)); | |
118 } | |
119 | |
120 bool ExtractListOfLabels(const Value& value, | |
121 const SourceDir& current_dir, | |
122 const Label& current_toolchain, | |
123 LabelTargetVector* dest, | 159 LabelTargetVector* dest, |
124 Err* err) { | 160 Err* err) { |
125 return ListValueExtractor(value, dest, err, | 161 return ListValueExtractor(value, dest, err, |
126 LabelResolver<Target>(current_dir, | 162 LabelResolver<Target>(current_dir, |
127 current_toolchain)); | 163 current_toolchain)); |
128 } | 164 } |
129 | 165 |
| 166 bool ExtractListOfUniqueLabels(const Value& value, |
| 167 const SourceDir& current_dir, |
| 168 const Label& current_toolchain, |
| 169 UniqueVector<LabelConfigPair>* dest, |
| 170 Err* err) { |
| 171 return ListValueUniqueExtractor(value, dest, err, |
| 172 LabelResolver<Config>(current_dir, |
| 173 current_toolchain)); |
| 174 } |
| 175 |
| 176 bool ExtractListOfUniqueLabels(const Value& value, |
| 177 const SourceDir& current_dir, |
| 178 const Label& current_toolchain, |
| 179 UniqueVector<LabelTargetPair>* dest, |
| 180 Err* err) { |
| 181 return ListValueUniqueExtractor(value, dest, err, |
| 182 LabelResolver<Target>(current_dir, |
| 183 current_toolchain)); |
| 184 } |
| 185 |
130 bool ExtractRelativeFile(const BuildSettings* build_settings, | 186 bool ExtractRelativeFile(const BuildSettings* build_settings, |
131 const Value& value, | 187 const Value& value, |
132 const SourceDir& current_dir, | 188 const SourceDir& current_dir, |
133 SourceFile* file, | 189 SourceFile* file, |
134 Err* err) { | 190 Err* err) { |
135 RelativeFileConverter converter(build_settings, current_dir); | 191 RelativeFileConverter converter(build_settings, current_dir); |
136 return converter(value, file, err); | 192 return converter(value, file, err); |
137 } | 193 } |
OLD | NEW |