OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #ifndef TOOLS_JSON_SCHEMA_COMPILER_UTIL_H_ | 5 #ifndef TOOLS_JSON_SCHEMA_COMPILER_UTIL_H_ |
6 #define TOOLS_JSON_SCHEMA_COMPILER_UTIL_H_ | 6 #define TOOLS_JSON_SCHEMA_COMPILER_UTIL_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include <string> | 9 #include <string> |
10 #include <utility> | 10 #include <utility> |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 *out = std::move(obj); | 94 *out = std::move(obj); |
95 return true; | 95 return true; |
96 } | 96 } |
97 | 97 |
98 // Populates |out| with |list|. Returns false if there is no list at the | 98 // Populates |out| with |list|. Returns false if there is no list at the |
99 // specified key or if the list has anything other than |T|. | 99 // specified key or if the list has anything other than |T|. |
100 template <class T> | 100 template <class T> |
101 bool PopulateArrayFromList(const base::ListValue& list, std::vector<T>* out) { | 101 bool PopulateArrayFromList(const base::ListValue& list, std::vector<T>* out) { |
102 out->clear(); | 102 out->clear(); |
103 T item; | 103 T item; |
104 for (const base::Value* value : list) { | 104 for (const auto& value : list) { |
105 if (!PopulateItem(*value, &item)) | 105 if (!PopulateItem(*value, &item)) |
106 return false; | 106 return false; |
107 // T might not be movable, but in that case it should be copyable, and this | 107 // T might not be movable, but in that case it should be copyable, and this |
108 // will still work. | 108 // will still work. |
109 out->push_back(std::move(item)); | 109 out->push_back(std::move(item)); |
110 } | 110 } |
111 | 111 |
112 return true; | 112 return true; |
113 } | 113 } |
114 | 114 |
115 // Populates |out| with |list|. Returns false and sets |error| if there is no | 115 // Populates |out| with |list|. Returns false and sets |error| if there is no |
116 // list at the specified key or if the list has anything other than |T|. | 116 // list at the specified key or if the list has anything other than |T|. |
117 template <class T> | 117 template <class T> |
118 bool PopulateArrayFromList(const base::ListValue& list, | 118 bool PopulateArrayFromList(const base::ListValue& list, |
119 std::vector<T>* out, | 119 std::vector<T>* out, |
120 base::string16* error) { | 120 base::string16* error) { |
121 out->clear(); | 121 out->clear(); |
122 T item; | 122 T item; |
123 for (const base::Value* value : list) { | 123 for (const auto& value : list) { |
124 if (!PopulateItem(*value, &item, error)) | 124 if (!PopulateItem(*value, &item, error)) |
125 return false; | 125 return false; |
126 out->push_back(std::move(item)); | 126 out->push_back(std::move(item)); |
127 } | 127 } |
128 | 128 |
129 return true; | 129 return true; |
130 } | 130 } |
131 | 131 |
132 // Creates a new vector containing |list| at |out|. Returns | 132 // Creates a new vector containing |list| at |out|. Returns |
133 // true on success or if there is nothing at the specified key. Returns false | 133 // true on success or if there is nothing at the specified key. Returns false |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 return CreateValueFromArray(*from); | 211 return CreateValueFromArray(*from); |
212 return nullptr; | 212 return nullptr; |
213 } | 213 } |
214 | 214 |
215 std::string ValueTypeToString(base::Value::Type type); | 215 std::string ValueTypeToString(base::Value::Type type); |
216 | 216 |
217 } // namespace util | 217 } // namespace util |
218 } // namespace json_schema_compiler | 218 } // namespace json_schema_compiler |
219 | 219 |
220 #endif // TOOLS_JSON_SCHEMA_COMPILER_UTIL_H_ | 220 #endif // TOOLS_JSON_SCHEMA_COMPILER_UTIL_H_ |
OLD | NEW |