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

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

Issue 1811413002: [Extensions] Update generated code to support move operations (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Antony's Created 4 years, 9 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
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 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
50 const base::DictionaryValue* dict = nullptr; 50 const base::DictionaryValue* dict = nullptr;
51 if (!from.GetAsDictionary(&dict)) 51 if (!from.GetAsDictionary(&dict))
52 return false; 52 return false;
53 scoped_ptr<T> obj(new T()); 53 scoped_ptr<T> obj(new T());
54 if (!T::Populate(*dict, obj.get())) 54 if (!T::Populate(*dict, obj.get()))
55 return false; 55 return false;
56 *out = linked_ptr<T>(obj.release()); 56 *out = linked_ptr<T>(obj.release());
57 return true; 57 return true;
58 } 58 }
59 59
60 // This template is used for types generated by tools/json_schema_compiler.
61 template <class T>
62 bool PopulateItem(const base::Value& from, T* out) {
63 const base::DictionaryValue* dict = nullptr;
64 if (!from.GetAsDictionary(&dict))
65 return false;
66 T obj;
67 if (!T::Populate(*dict, &obj))
68 return false;
69 *out = std::move(obj);
70 return true;
71 }
72
60 // This template is used for types generated by tools/json_schema_compiler with 73 // This template is used for types generated by tools/json_schema_compiler with
61 // error generation enabled. 74 // error generation enabled.
62 template <class T> 75 template <class T>
63 bool PopulateItem(const base::Value& from, 76 bool PopulateItem(const base::Value& from,
64 linked_ptr<T>* out, 77 linked_ptr<T>* out,
65 base::string16* error) { 78 base::string16* error) {
66 const base::DictionaryValue* dict = nullptr; 79 const base::DictionaryValue* dict = nullptr;
67 if (!from.GetAsDictionary(&dict)) 80 if (!from.GetAsDictionary(&dict))
68 return false; 81 return false;
69 scoped_ptr<T> obj(new T()); 82 scoped_ptr<T> obj(new T());
70 if (!T::Populate(*dict, obj.get(), error)) 83 if (!T::Populate(*dict, obj.get(), error))
71 return false; 84 return false;
72 *out = linked_ptr<T>(obj.release()); 85 *out = linked_ptr<T>(obj.release());
73 return true; 86 return true;
74 } 87 }
75 88
76 // Populates |out| with |list|. Returns false if there is no list at the 89 // Populates |out| with |list|. Returns false if there is no list at the
77 // specified key or if the list has anything other than |T|. 90 // specified key or if the list has anything other than |T|.
78 template <class T> 91 template <class T>
79 bool PopulateArrayFromList(const base::ListValue& list, std::vector<T>* out) { 92 bool PopulateArrayFromList(const base::ListValue& list, std::vector<T>* out) {
80 out->clear(); 93 out->clear();
81 T item; 94 T item;
82 for (const base::Value* value : list) { 95 for (const base::Value* value : list) {
83 if (!PopulateItem(*value, &item)) 96 if (!PopulateItem(*value, &item))
84 return false; 97 return false;
85 out->push_back(item); 98 // T might not be movable, but in that case it should be copyable, and this
99 // will still work.
100 out->push_back(std::move(item));
86 } 101 }
87 102
88 return true; 103 return true;
89 } 104 }
90 105
91 // Populates |out| with |list|. Returns false and sets |error| if there is no 106 // Populates |out| with |list|. Returns false and sets |error| if there is no
92 // list at the specified key or if the list has anything other than |T|. 107 // list at the specified key or if the list has anything other than |T|.
93 template <class T> 108 template <class T>
94 bool PopulateArrayFromList(const base::ListValue& list, 109 bool PopulateArrayFromList(const base::ListValue& list,
95 std::vector<T>* out, 110 std::vector<T>* out,
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 void AddItemToList(const linked_ptr<base::Value>& from, base::ListValue* out); 156 void AddItemToList(const linked_ptr<base::Value>& from, base::ListValue* out);
142 void AddItemToList(const linked_ptr<base::DictionaryValue>& from, 157 void AddItemToList(const linked_ptr<base::DictionaryValue>& from,
143 base::ListValue* out); 158 base::ListValue* out);
144 159
145 // This template is used for types generated by tools/json_schema_compiler. 160 // This template is used for types generated by tools/json_schema_compiler.
146 template <class T> 161 template <class T>
147 void AddItemToList(const linked_ptr<T>& from, base::ListValue* out) { 162 void AddItemToList(const linked_ptr<T>& from, base::ListValue* out) {
148 out->Append(from->ToValue().release()); 163 out->Append(from->ToValue().release());
149 } 164 }
150 165
166 // This template is used for types generated by tools/json_schema_compiler.
167 template <class T>
168 void AddItemToList(const T& from, base::ListValue* out) {
169 out->Append(from.ToValue().release());
dcheng 2016/03/22 17:42:34 No .release().
Devlin 2016/03/22 17:58:40 Done.
170 }
171
151 // Set |out| to the the contents of |from|. Requires PopulateItem to be 172 // Set |out| to the the contents of |from|. Requires PopulateItem to be
152 // implemented for |T|. 173 // implemented for |T|.
153 template <class T> 174 template <class T>
154 void PopulateListFromArray(const std::vector<T>& from, base::ListValue* out) { 175 void PopulateListFromArray(const std::vector<T>& from, base::ListValue* out) {
155 out->Clear(); 176 out->Clear();
156 for (const auto& item : from) 177 for (const auto& item : from)
157 AddItemToList(item, out); 178 AddItemToList(item, out);
158 } 179 }
159 180
160 // Set |out| to the the contents of |from| if |from| is not null. Requires 181 // Set |out| to the the contents of |from| if |from| is not null. Requires
(...skipping 19 matching lines...) Expand all
180 return CreateValueFromArray(*from); 201 return CreateValueFromArray(*from);
181 return scoped_ptr<base::Value>(); 202 return scoped_ptr<base::Value>();
182 } 203 }
183 204
184 std::string ValueTypeToString(base::Value::Type type); 205 std::string ValueTypeToString(base::Value::Type type);
185 206
186 } // namespace util 207 } // namespace util
187 } // namespace json_schema_compiler 208 } // namespace json_schema_compiler
188 209
189 #endif // TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__ 210 #endif // TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698