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

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

Issue 1869503004: Convert //tools to use std::unique_ptr (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase, change iwyu fixes for converted directories to include <memory> 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 | « tools/json_schema_compiler/test/test_util.cc ('k') | tools/json_schema_compiler/util.cc » ('j') | 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 <memory>
8 #include <string> 9 #include <string>
10 #include <utility>
9 #include <vector> 11 #include <vector>
10 12
11 #include "base/memory/scoped_ptr.h"
12 #include "base/values.h" 13 #include "base/values.h"
13 14
14 namespace json_schema_compiler { 15 namespace json_schema_compiler {
15 16
16 namespace util { 17 namespace util {
17 18
18 // Populates the item |out| from the value |from|. These are used by template 19 // Populates the item |out| from the value |from|. These are used by template
19 // specializations of |Get(Optional)ArrayFromList|. 20 // specializations of |Get(Optional)ArrayFromList|.
20 bool PopulateItem(const base::Value& from, scoped_ptr<base::Value>* out); 21 bool PopulateItem(const base::Value& from, std::unique_ptr<base::Value>* out);
21 22
22 bool PopulateItem(const base::Value& from, int* out); 23 bool PopulateItem(const base::Value& from, int* out);
23 bool PopulateItem(const base::Value& from, int* out, base::string16* error); 24 bool PopulateItem(const base::Value& from, int* out, base::string16* error);
24 bool PopulateItem(const base::Value& from, bool* out); 25 bool PopulateItem(const base::Value& from, bool* out);
25 bool PopulateItem(const base::Value& from, bool* out, base::string16* error); 26 bool PopulateItem(const base::Value& from, bool* out, base::string16* error);
26 bool PopulateItem(const base::Value& from, double* out); 27 bool PopulateItem(const base::Value& from, double* out);
27 bool PopulateItem(const base::Value& from, double* out, base::string16* error); 28 bool PopulateItem(const base::Value& from, double* out, base::string16* error);
28 bool PopulateItem(const base::Value& from, std::string* out); 29 bool PopulateItem(const base::Value& from, std::string* out);
29 bool PopulateItem(const base::Value& from, 30 bool PopulateItem(const base::Value& from,
30 std::string* out, 31 std::string* out,
31 base::string16* error); 32 base::string16* error);
32 bool PopulateItem(const base::Value& from, std::vector<char>* out); 33 bool PopulateItem(const base::Value& from, std::vector<char>* out);
33 bool PopulateItem(const base::Value& from, 34 bool PopulateItem(const base::Value& from,
34 std::vector<char>* out, 35 std::vector<char>* out,
35 base::string16* error); 36 base::string16* error);
36 bool PopulateItem(const base::Value& from, 37 bool PopulateItem(const base::Value& from,
37 scoped_ptr<base::Value>* out, 38 std::unique_ptr<base::Value>* out,
38 base::string16* error); 39 base::string16* error);
39 bool PopulateItem(const base::Value& from, scoped_ptr<base::Value>* out); 40 bool PopulateItem(const base::Value& from, std::unique_ptr<base::Value>* out);
40 41
41 // This template is used for types generated by tools/json_schema_compiler. 42 // This template is used for types generated by tools/json_schema_compiler.
42 template <class T> 43 template <class T>
43 bool PopulateItem(const base::Value& from, scoped_ptr<T>* out) { 44 bool PopulateItem(const base::Value& from, std::unique_ptr<T>* out) {
44 const base::DictionaryValue* dict = nullptr; 45 const base::DictionaryValue* dict = nullptr;
45 if (!from.GetAsDictionary(&dict)) 46 if (!from.GetAsDictionary(&dict))
46 return false; 47 return false;
47 scoped_ptr<T> obj(new T()); 48 std::unique_ptr<T> obj(new T());
48 if (!T::Populate(*dict, obj.get())) 49 if (!T::Populate(*dict, obj.get()))
49 return false; 50 return false;
50 *out = scoped_ptr<T>(obj.release()); 51 *out = std::move(obj);
51 return true; 52 return true;
52 } 53 }
53 54
54 // This template is used for types generated by tools/json_schema_compiler. 55 // This template is used for types generated by tools/json_schema_compiler.
55 template <class T> 56 template <class T>
56 bool PopulateItem(const base::Value& from, T* out) { 57 bool PopulateItem(const base::Value& from, T* out) {
57 const base::DictionaryValue* dict = nullptr; 58 const base::DictionaryValue* dict = nullptr;
58 if (!from.GetAsDictionary(&dict)) 59 if (!from.GetAsDictionary(&dict))
59 return false; 60 return false;
60 T obj; 61 T obj;
61 if (!T::Populate(*dict, &obj)) 62 if (!T::Populate(*dict, &obj))
62 return false; 63 return false;
63 *out = std::move(obj); 64 *out = std::move(obj);
64 return true; 65 return true;
65 } 66 }
66 67
67 // This template is used for types generated by tools/json_schema_compiler with 68 // This template is used for types generated by tools/json_schema_compiler with
68 // error generation enabled. 69 // error generation enabled.
69 template <class T> 70 template <class T>
70 bool PopulateItem(const base::Value& from, 71 bool PopulateItem(const base::Value& from,
71 scoped_ptr<T>* out, 72 std::unique_ptr<T>* out,
72 base::string16* error) { 73 base::string16* error) {
73 const base::DictionaryValue* dict = nullptr; 74 const base::DictionaryValue* dict = nullptr;
74 if (!from.GetAsDictionary(&dict)) 75 if (!from.GetAsDictionary(&dict))
75 return false; 76 return false;
76 scoped_ptr<T> obj(new T()); 77 std::unique_ptr<T> obj(new T());
77 if (!T::Populate(*dict, obj.get(), error)) 78 if (!T::Populate(*dict, obj.get(), error))
78 return false; 79 return false;
79 *out = scoped_ptr<T>(obj.release()); 80 *out = std::move(obj);
80 return true; 81 return true;
81 } 82 }
82 83
83 // This template is used for types generated by tools/json_schema_compiler with 84 // This template is used for types generated by tools/json_schema_compiler with
84 // error generation enabled. 85 // error generation enabled.
85 template <class T> 86 template <class T>
86 bool PopulateItem(const base::Value& from, T* out, base::string16* error) { 87 bool PopulateItem(const base::Value& from, T* out, base::string16* error) {
87 const base::DictionaryValue* dict = nullptr; 88 const base::DictionaryValue* dict = nullptr;
88 if (!from.GetAsDictionary(&dict)) 89 if (!from.GetAsDictionary(&dict))
89 return false; 90 return false;
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
126 } 127 }
127 128
128 return true; 129 return true;
129 } 130 }
130 131
131 // Creates a new vector containing |list| at |out|. Returns 132 // Creates a new vector containing |list| at |out|. Returns
132 // 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
133 // if anything other than a list of |T| is at the specified key. 134 // if anything other than a list of |T| is at the specified key.
134 template <class T> 135 template <class T>
135 bool PopulateOptionalArrayFromList(const base::ListValue& list, 136 bool PopulateOptionalArrayFromList(const base::ListValue& list,
136 scoped_ptr<std::vector<T>>* out) { 137 std::unique_ptr<std::vector<T>>* out) {
137 out->reset(new std::vector<T>()); 138 out->reset(new std::vector<T>());
138 if (!PopulateArrayFromList(list, out->get())) { 139 if (!PopulateArrayFromList(list, out->get())) {
139 out->reset(); 140 out->reset();
140 return false; 141 return false;
141 } 142 }
142 return true; 143 return true;
143 } 144 }
144 145
145 template <class T> 146 template <class T>
146 bool PopulateOptionalArrayFromList(const base::ListValue& list, 147 bool PopulateOptionalArrayFromList(const base::ListValue& list,
147 scoped_ptr<std::vector<T>>* out, 148 std::unique_ptr<std::vector<T>>* out,
148 base::string16* error) { 149 base::string16* error) {
149 out->reset(new std::vector<T>()); 150 out->reset(new std::vector<T>());
150 if (!PopulateArrayFromList(list, out->get(), error)) { 151 if (!PopulateArrayFromList(list, out->get(), error)) {
151 out->reset(); 152 out->reset();
152 return false; 153 return false;
153 } 154 }
154 return true; 155 return true;
155 } 156 }
156 157
157 // Appends a Value newly created from |from| to |out|. These used by template 158 // Appends a Value newly created from |from| to |out|. These used by template
158 // specializations of |Set(Optional)ArrayToList|. 159 // specializations of |Set(Optional)ArrayToList|.
159 void AddItemToList(const int from, base::ListValue* out); 160 void AddItemToList(const int from, base::ListValue* out);
160 void AddItemToList(const bool from, base::ListValue* out); 161 void AddItemToList(const bool from, base::ListValue* out);
161 void AddItemToList(const double from, base::ListValue* out); 162 void AddItemToList(const double from, base::ListValue* out);
162 void AddItemToList(const std::string& from, base::ListValue* out); 163 void AddItemToList(const std::string& from, base::ListValue* out);
163 void AddItemToList(const std::vector<char>& from, base::ListValue* out); 164 void AddItemToList(const std::vector<char>& from, base::ListValue* out);
164 void AddItemToList(const scoped_ptr<base::Value>& from, base::ListValue* out); 165 void AddItemToList(const std::unique_ptr<base::Value>& from,
165 void AddItemToList(const scoped_ptr<base::DictionaryValue>& from, 166 base::ListValue* out);
167 void AddItemToList(const std::unique_ptr<base::DictionaryValue>& from,
166 base::ListValue* out); 168 base::ListValue* out);
167 169
168 // This template is used for types generated by tools/json_schema_compiler. 170 // This template is used for types generated by tools/json_schema_compiler.
169 template <class T> 171 template <class T>
170 void AddItemToList(const scoped_ptr<T>& from, base::ListValue* out) { 172 void AddItemToList(const std::unique_ptr<T>& from, base::ListValue* out) {
171 out->Append(from->ToValue()); 173 out->Append(from->ToValue());
172 } 174 }
173 175
174 // This template is used for types generated by tools/json_schema_compiler. 176 // This template is used for types generated by tools/json_schema_compiler.
175 template <class T> 177 template <class T>
176 void AddItemToList(const T& from, base::ListValue* out) { 178 void AddItemToList(const T& from, base::ListValue* out) {
177 out->Append(from.ToValue()); 179 out->Append(from.ToValue());
178 } 180 }
179 181
180 // Set |out| to the the contents of |from|. Requires PopulateItem to be 182 // Set |out| to the the contents of |from|. Requires PopulateItem to be
181 // implemented for |T|. 183 // implemented for |T|.
182 template <class T> 184 template <class T>
183 void PopulateListFromArray(const std::vector<T>& from, base::ListValue* out) { 185 void PopulateListFromArray(const std::vector<T>& from, base::ListValue* out) {
184 out->Clear(); 186 out->Clear();
185 for (const T& item : from) 187 for (const T& item : from)
186 AddItemToList(item, out); 188 AddItemToList(item, out);
187 } 189 }
188 190
189 // Set |out| to the the contents of |from| if |from| is not null. Requires 191 // Set |out| to the the contents of |from| if |from| is not null. Requires
190 // PopulateItem to be implemented for |T|. 192 // PopulateItem to be implemented for |T|.
191 template <class T> 193 template <class T>
192 void PopulateListFromOptionalArray(const scoped_ptr<std::vector<T>>& from, 194 void PopulateListFromOptionalArray(const std::unique_ptr<std::vector<T>>& from,
193 base::ListValue* out) { 195 base::ListValue* out) {
194 if (from.get()) 196 if (from)
195 PopulateListFromArray(*from, out); 197 PopulateListFromArray(*from, out);
196 } 198 }
197 199
198 template <class T> 200 template <class T>
199 scoped_ptr<base::Value> CreateValueFromArray(const std::vector<T>& from) { 201 std::unique_ptr<base::Value> CreateValueFromArray(const std::vector<T>& from) {
200 base::ListValue* list = new base::ListValue(); 202 std::unique_ptr<base::ListValue> list(new base::ListValue());
201 PopulateListFromArray(from, list); 203 PopulateListFromArray(from, list.get());
202 return scoped_ptr<base::Value>(list); 204 return std::move(list);
203 } 205 }
204 206
205 template <class T> 207 template <class T>
206 scoped_ptr<base::Value> CreateValueFromOptionalArray( 208 std::unique_ptr<base::Value> CreateValueFromOptionalArray(
207 const scoped_ptr<std::vector<T>>& from) { 209 const std::unique_ptr<std::vector<T>>& from) {
208 if (from.get()) 210 if (from)
209 return CreateValueFromArray(*from); 211 return CreateValueFromArray(*from);
210 return scoped_ptr<base::Value>(); 212 return nullptr;
211 } 213 }
212 214
213 std::string ValueTypeToString(base::Value::Type type); 215 std::string ValueTypeToString(base::Value::Type type);
214 216
215 } // namespace util 217 } // namespace util
216 } // namespace json_schema_compiler 218 } // namespace json_schema_compiler
217 219
218 #endif // TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__ 220 #endif // TOOLS_JSON_SCHEMA_COMPILER_UTIL_H_
OLDNEW
« no previous file with comments | « tools/json_schema_compiler/test/test_util.cc ('k') | tools/json_schema_compiler/util.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698