| 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 <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/memory/linked_ptr.h" | |
| 12 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/values.h" | 12 #include "base/values.h" |
| 14 | 13 |
| 15 namespace json_schema_compiler { | 14 namespace json_schema_compiler { |
| 16 | 15 |
| 17 namespace util { | 16 namespace util { |
| 18 | 17 |
| 19 // Populates the item |out| from the value |from|. These are used by template | 18 // Populates the item |out| from the value |from|. These are used by template |
| 20 // specializations of |Get(Optional)ArrayFromList|. | 19 // specializations of |Get(Optional)ArrayFromList|. |
| 21 bool PopulateItem(const base::Value& from, linked_ptr<base::Value>* out); | 20 bool PopulateItem(const base::Value& from, scoped_ptr<base::Value>* out); |
| 22 | 21 |
| 23 bool PopulateItem(const base::Value& from, int* out); | 22 bool PopulateItem(const base::Value& from, int* out); |
| 24 bool PopulateItem(const base::Value& from, int* out, base::string16* error); | 23 bool PopulateItem(const base::Value& from, int* out, base::string16* error); |
| 25 bool PopulateItem(const base::Value& from, bool* out); | 24 bool PopulateItem(const base::Value& from, bool* out); |
| 26 bool PopulateItem(const base::Value& from, bool* out, base::string16* error); | 25 bool PopulateItem(const base::Value& from, bool* out, base::string16* error); |
| 27 bool PopulateItem(const base::Value& from, double* out); | 26 bool PopulateItem(const base::Value& from, double* out); |
| 28 bool PopulateItem(const base::Value& from, double* out, base::string16* error); | 27 bool PopulateItem(const base::Value& from, double* out, base::string16* error); |
| 29 bool PopulateItem(const base::Value& from, std::string* out); | 28 bool PopulateItem(const base::Value& from, std::string* out); |
| 30 bool PopulateItem(const base::Value& from, | 29 bool PopulateItem(const base::Value& from, |
| 31 std::string* out, | 30 std::string* out, |
| 32 base::string16* error); | 31 base::string16* error); |
| 33 bool PopulateItem(const base::Value& from, std::vector<char>* out); | 32 bool PopulateItem(const base::Value& from, std::vector<char>* out); |
| 34 bool PopulateItem(const base::Value& from, | 33 bool PopulateItem(const base::Value& from, |
| 35 std::vector<char>* out, | 34 std::vector<char>* out, |
| 36 base::string16* error); | 35 base::string16* error); |
| 37 bool PopulateItem(const base::Value& from, | 36 bool PopulateItem(const base::Value& from, |
| 38 linked_ptr<base::Value>* out, | 37 scoped_ptr<base::Value>* out, |
| 39 base::string16* error); | 38 base::string16* error); |
| 40 bool PopulateItem(const base::Value& from, linked_ptr<base::Value>* out); | 39 bool PopulateItem(const base::Value& from, scoped_ptr<base::Value>* out); |
| 41 bool PopulateItem(const base::Value& from, | |
| 42 linked_ptr<base::DictionaryValue>* out); | |
| 43 bool PopulateItem(const base::Value& from, | |
| 44 linked_ptr<base::DictionaryValue>* out, | |
| 45 base::string16* error); | |
| 46 | 40 |
| 47 // This template is used for types generated by tools/json_schema_compiler. | 41 // This template is used for types generated by tools/json_schema_compiler. |
| 48 template <class T> | 42 template <class T> |
| 49 bool PopulateItem(const base::Value& from, linked_ptr<T>* out) { | 43 bool PopulateItem(const base::Value& from, scoped_ptr<T>* out) { |
| 50 const base::DictionaryValue* dict = nullptr; | 44 const base::DictionaryValue* dict = nullptr; |
| 51 if (!from.GetAsDictionary(&dict)) | 45 if (!from.GetAsDictionary(&dict)) |
| 52 return false; | 46 return false; |
| 53 scoped_ptr<T> obj(new T()); | 47 scoped_ptr<T> obj(new T()); |
| 54 if (!T::Populate(*dict, obj.get())) | 48 if (!T::Populate(*dict, obj.get())) |
| 55 return false; | 49 return false; |
| 56 *out = linked_ptr<T>(obj.release()); | 50 *out = scoped_ptr<T>(obj.release()); |
| 57 return true; | 51 return true; |
| 58 } | 52 } |
| 59 | 53 |
| 60 // This template is used for types generated by tools/json_schema_compiler. | 54 // This template is used for types generated by tools/json_schema_compiler. |
| 61 template <class T> | 55 template <class T> |
| 62 bool PopulateItem(const base::Value& from, T* out) { | 56 bool PopulateItem(const base::Value& from, T* out) { |
| 63 const base::DictionaryValue* dict = nullptr; | 57 const base::DictionaryValue* dict = nullptr; |
| 64 if (!from.GetAsDictionary(&dict)) | 58 if (!from.GetAsDictionary(&dict)) |
| 65 return false; | 59 return false; |
| 66 T obj; | 60 T obj; |
| 67 if (!T::Populate(*dict, &obj)) | 61 if (!T::Populate(*dict, &obj)) |
| 68 return false; | 62 return false; |
| 69 *out = std::move(obj); | 63 *out = std::move(obj); |
| 70 return true; | 64 return true; |
| 71 } | 65 } |
| 72 | 66 |
| 73 // This template is used for types generated by tools/json_schema_compiler with | 67 // This template is used for types generated by tools/json_schema_compiler with |
| 74 // error generation enabled. | 68 // error generation enabled. |
| 75 template <class T> | 69 template <class T> |
| 76 bool PopulateItem(const base::Value& from, | 70 bool PopulateItem(const base::Value& from, |
| 77 linked_ptr<T>* out, | 71 scoped_ptr<T>* out, |
| 78 base::string16* error) { | 72 base::string16* error) { |
| 79 const base::DictionaryValue* dict = nullptr; | 73 const base::DictionaryValue* dict = nullptr; |
| 80 if (!from.GetAsDictionary(&dict)) | 74 if (!from.GetAsDictionary(&dict)) |
| 81 return false; | 75 return false; |
| 82 scoped_ptr<T> obj(new T()); | 76 scoped_ptr<T> obj(new T()); |
| 83 if (!T::Populate(*dict, obj.get(), error)) | 77 if (!T::Populate(*dict, obj.get(), error)) |
| 84 return false; | 78 return false; |
| 85 *out = linked_ptr<T>(obj.release()); | 79 *out = scoped_ptr<T>(obj.release()); |
| 86 return true; | 80 return true; |
| 87 } | 81 } |
| 88 | 82 |
| 89 // This template is used for types generated by tools/json_schema_compiler with | 83 // This template is used for types generated by tools/json_schema_compiler with |
| 90 // error generation enabled. | 84 // error generation enabled. |
| 91 template <class T> | 85 template <class T> |
| 92 bool PopulateItem(const base::Value& from, T* out, base::string16* error) { | 86 bool PopulateItem(const base::Value& from, T* out, base::string16* error) { |
| 93 const base::DictionaryValue* dict = nullptr; | 87 const base::DictionaryValue* dict = nullptr; |
| 94 if (!from.GetAsDictionary(&dict)) | 88 if (!from.GetAsDictionary(&dict)) |
| 95 return false; | 89 return false; |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 return true; | 154 return true; |
| 161 } | 155 } |
| 162 | 156 |
| 163 // Appends a Value newly created from |from| to |out|. These used by template | 157 // Appends a Value newly created from |from| to |out|. These used by template |
| 164 // specializations of |Set(Optional)ArrayToList|. | 158 // specializations of |Set(Optional)ArrayToList|. |
| 165 void AddItemToList(const int from, base::ListValue* out); | 159 void AddItemToList(const int from, base::ListValue* out); |
| 166 void AddItemToList(const bool from, base::ListValue* out); | 160 void AddItemToList(const bool from, base::ListValue* out); |
| 167 void AddItemToList(const double from, base::ListValue* out); | 161 void AddItemToList(const double from, base::ListValue* out); |
| 168 void AddItemToList(const std::string& from, base::ListValue* out); | 162 void AddItemToList(const std::string& from, base::ListValue* out); |
| 169 void AddItemToList(const std::vector<char>& from, base::ListValue* out); | 163 void AddItemToList(const std::vector<char>& from, base::ListValue* out); |
| 170 void AddItemToList(const linked_ptr<base::Value>& from, base::ListValue* out); | 164 void AddItemToList(const scoped_ptr<base::Value>& from, base::ListValue* out); |
| 171 void AddItemToList(const linked_ptr<base::DictionaryValue>& from, | 165 void AddItemToList(const scoped_ptr<base::DictionaryValue>& from, |
| 172 base::ListValue* out); | 166 base::ListValue* out); |
| 173 | 167 |
| 174 // This template is used for types generated by tools/json_schema_compiler. | 168 // This template is used for types generated by tools/json_schema_compiler. |
| 175 template <class T> | 169 template <class T> |
| 176 void AddItemToList(const linked_ptr<T>& from, base::ListValue* out) { | 170 void AddItemToList(const scoped_ptr<T>& from, base::ListValue* out) { |
| 177 out->Append(from->ToValue().release()); | 171 out->Append(from->ToValue()); |
| 178 } | 172 } |
| 179 | 173 |
| 180 // This template is used for types generated by tools/json_schema_compiler. | 174 // This template is used for types generated by tools/json_schema_compiler. |
| 181 template <class T> | 175 template <class T> |
| 182 void AddItemToList(const T& from, base::ListValue* out) { | 176 void AddItemToList(const T& from, base::ListValue* out) { |
| 183 out->Append(from.ToValue()); | 177 out->Append(from.ToValue()); |
| 184 } | 178 } |
| 185 | 179 |
| 186 // Set |out| to the the contents of |from|. Requires PopulateItem to be | 180 // Set |out| to the the contents of |from|. Requires PopulateItem to be |
| 187 // implemented for |T|. | 181 // implemented for |T|. |
| (...skipping 27 matching lines...) Expand all Loading... |
| 215 return CreateValueFromArray(*from); | 209 return CreateValueFromArray(*from); |
| 216 return scoped_ptr<base::Value>(); | 210 return scoped_ptr<base::Value>(); |
| 217 } | 211 } |
| 218 | 212 |
| 219 std::string ValueTypeToString(base::Value::Type type); | 213 std::string ValueTypeToString(base::Value::Type type); |
| 220 | 214 |
| 221 } // namespace util | 215 } // namespace util |
| 222 } // namespace json_schema_compiler | 216 } // namespace json_schema_compiler |
| 223 | 217 |
| 224 #endif // TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__ | 218 #endif // TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__ |
| OLD | NEW |