Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(151)

Side by Side Diff: tools/json_schema_compiler/util.h

Issue 1841483002: [Extensions] Convert APIs to use movable types [11] (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « extensions/common/api/printer_provider/usb_printer_manifest_data.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
79 const base::DictionaryValue* dict = nullptr; 79 const base::DictionaryValue* dict = nullptr;
80 if (!from.GetAsDictionary(&dict)) 80 if (!from.GetAsDictionary(&dict))
81 return false; 81 return false;
82 scoped_ptr<T> obj(new T()); 82 scoped_ptr<T> obj(new T());
83 if (!T::Populate(*dict, obj.get(), error)) 83 if (!T::Populate(*dict, obj.get(), error))
84 return false; 84 return false;
85 *out = linked_ptr<T>(obj.release()); 85 *out = linked_ptr<T>(obj.release());
86 return true; 86 return true;
87 } 87 }
88 88
89 // This template is used for types generated by tools/json_schema_compiler with
90 // error generation enabled.
91 template <class T>
92 bool PopulateItem(const base::Value& from, T* out, base::string16* error) {
93 const base::DictionaryValue* dict = nullptr;
94 if (!from.GetAsDictionary(&dict))
95 return false;
96 T obj;
97 if (!T::Populate(*dict, &obj, error))
98 return false;
99 *out = std::move(obj);
100 return true;
101 }
102
89 // Populates |out| with |list|. Returns false if there is no list at the 103 // Populates |out| with |list|. Returns false if there is no list at the
90 // specified key or if the list has anything other than |T|. 104 // specified key or if the list has anything other than |T|.
91 template <class T> 105 template <class T>
92 bool PopulateArrayFromList(const base::ListValue& list, std::vector<T>* out) { 106 bool PopulateArrayFromList(const base::ListValue& list, std::vector<T>* out) {
93 out->clear(); 107 out->clear();
94 T item; 108 T item;
95 for (const base::Value* value : list) { 109 for (const base::Value* value : list) {
96 if (!PopulateItem(*value, &item)) 110 if (!PopulateItem(*value, &item))
97 return false; 111 return false;
98 // T might not be movable, but in that case it should be copyable, and this 112 // T might not be movable, but in that case it should be copyable, and this
99 // will still work. 113 // will still work.
100 out->push_back(std::move(item)); 114 out->push_back(std::move(item));
101 } 115 }
102 116
103 return true; 117 return true;
104 } 118 }
105 119
106 // Populates |out| with |list|. Returns false and sets |error| if there is no 120 // Populates |out| with |list|. Returns false and sets |error| if there is no
107 // list at the specified key or if the list has anything other than |T|. 121 // list at the specified key or if the list has anything other than |T|.
108 template <class T> 122 template <class T>
109 bool PopulateArrayFromList(const base::ListValue& list, 123 bool PopulateArrayFromList(const base::ListValue& list,
110 std::vector<T>* out, 124 std::vector<T>* out,
111 base::string16* error) { 125 base::string16* error) {
112 out->clear(); 126 out->clear();
113 T item; 127 T item;
114 for (const base::Value* value : list) { 128 for (const base::Value* value : list) {
115 if (!PopulateItem(*value, &item, error)) 129 if (!PopulateItem(*value, &item, error))
116 return false; 130 return false;
117 out->push_back(item); 131 out->push_back(std::move(item));
118 } 132 }
119 133
120 return true; 134 return true;
121 } 135 }
122 136
123 // Creates a new vector containing |list| at |out|. Returns 137 // Creates a new vector containing |list| at |out|. Returns
124 // true on success or if there is nothing at the specified key. Returns false 138 // true on success or if there is nothing at the specified key. Returns false
125 // if anything other than a list of |T| is at the specified key. 139 // if anything other than a list of |T| is at the specified key.
126 template <class T> 140 template <class T>
127 bool PopulateOptionalArrayFromList(const base::ListValue& list, 141 bool PopulateOptionalArrayFromList(const base::ListValue& list,
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 return CreateValueFromArray(*from); 215 return CreateValueFromArray(*from);
202 return scoped_ptr<base::Value>(); 216 return scoped_ptr<base::Value>();
203 } 217 }
204 218
205 std::string ValueTypeToString(base::Value::Type type); 219 std::string ValueTypeToString(base::Value::Type type);
206 220
207 } // namespace util 221 } // namespace util
208 } // namespace json_schema_compiler 222 } // namespace json_schema_compiler
209 223
210 #endif // TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__ 224 #endif // TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__
OLDNEW
« no previous file with comments | « extensions/common/api/printer_provider/usb_printer_manifest_data.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698