| 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 |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 return true; | 60 return true; |
| 61 } | 61 } |
| 62 | 62 |
| 63 // Populates |out| with |from|.|name|. Returns false if there is no list at | 63 // Populates |out| with |from|.|name|. Returns false if there is no list at |
| 64 // the specified key or if the list has anything other than |T|. | 64 // the specified key or if the list has anything other than |T|. |
| 65 template <class T> | 65 template <class T> |
| 66 bool PopulateArrayFromDictionary( | 66 bool PopulateArrayFromDictionary( |
| 67 const base::DictionaryValue& from, | 67 const base::DictionaryValue& from, |
| 68 const std::string& name, | 68 const std::string& name, |
| 69 std::vector<T>* out) { | 69 std::vector<T>* out) { |
| 70 base::ListValue* list = NULL; | 70 const base::ListValue* list = NULL; |
| 71 if (!from.GetListWithoutPathExpansion(name, &list)) | 71 if (!from.GetListWithoutPathExpansion(name, &list)) |
| 72 return false; | 72 return false; |
| 73 | 73 |
| 74 return PopulateArrayFromList(*list, out); | 74 return PopulateArrayFromList(*list, out); |
| 75 } | 75 } |
| 76 | 76 |
| 77 // Creates a new vector containing |list| at |out|. Returns | 77 // Creates a new vector containing |list| at |out|. Returns |
| 78 // true on success or if there is nothing at the specified key. Returns false | 78 // true on success or if there is nothing at the specified key. Returns false |
| 79 // if anything other than a list of |T| is at the specified key. | 79 // if anything other than a list of |T| is at the specified key. |
| 80 template <class T> | 80 template <class T> |
| (...skipping 14 matching lines...) Expand all Loading... |
| 95 } | 95 } |
| 96 | 96 |
| 97 // Creates a new vector containing |from|.|name| at |out|. Returns | 97 // Creates a new vector containing |from|.|name| at |out|. Returns |
| 98 // true on success or if there is nothing at the specified key. Returns false | 98 // true on success or if there is nothing at the specified key. Returns false |
| 99 // if anything other than a list of |T| is at the specified key. | 99 // if anything other than a list of |T| is at the specified key. |
| 100 template <class T> | 100 template <class T> |
| 101 bool PopulateOptionalArrayFromDictionary( | 101 bool PopulateOptionalArrayFromDictionary( |
| 102 const base::DictionaryValue& from, | 102 const base::DictionaryValue& from, |
| 103 const std::string& name, | 103 const std::string& name, |
| 104 scoped_ptr<std::vector<T> >* out) { | 104 scoped_ptr<std::vector<T> >* out) { |
| 105 base::ListValue* list = NULL; | 105 const base::ListValue* list = NULL; |
| 106 { | 106 { |
| 107 base::Value* maybe_list = NULL; | 107 const base::Value* maybe_list = NULL; |
| 108 // Since |name| is optional, its absence is acceptable. However, anything | 108 // Since |name| is optional, its absence is acceptable. However, anything |
| 109 // other than a ListValue is not. | 109 // other than a ListValue is not. |
| 110 if (!from.GetWithoutPathExpansion(name, &maybe_list)) | 110 if (!from.GetWithoutPathExpansion(name, &maybe_list)) |
| 111 return true; | 111 return true; |
| 112 if (!maybe_list->IsType(base::Value::TYPE_LIST)) | 112 if (!maybe_list->IsType(base::Value::TYPE_LIST)) |
| 113 return false; | 113 return false; |
| 114 list = static_cast<base::ListValue*>(maybe_list); | 114 list = static_cast<const base::ListValue*>(maybe_list); |
| 115 } | 115 } |
| 116 | 116 |
| 117 return PopulateOptionalArrayFromList(*list, out); | 117 return PopulateOptionalArrayFromList(*list, out); |
| 118 } | 118 } |
| 119 | 119 |
| 120 // Appends a Value newly created from |from| to |out|. These used by template | 120 // Appends a Value newly created from |from| to |out|. These used by template |
| 121 // specializations of |Set(Optional)ArrayToList|. | 121 // specializations of |Set(Optional)ArrayToList|. |
| 122 void AddItemToList(const int from, base::ListValue* out); | 122 void AddItemToList(const int from, base::ListValue* out); |
| 123 void AddItemToList(const bool from, base::ListValue* out); | 123 void AddItemToList(const bool from, base::ListValue* out); |
| 124 void AddItemToList(const double from, base::ListValue* out); | 124 void AddItemToList(const double from, base::ListValue* out); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 const scoped_ptr<std::vector<T> >& from) { | 171 const scoped_ptr<std::vector<T> >& from) { |
| 172 if (from.get()) | 172 if (from.get()) |
| 173 return CreateValueFromArray(*from); | 173 return CreateValueFromArray(*from); |
| 174 return scoped_ptr<Value>(); | 174 return scoped_ptr<Value>(); |
| 175 } | 175 } |
| 176 | 176 |
| 177 } // namespace util | 177 } // namespace util |
| 178 } // namespace json_schema_compiler | 178 } // namespace json_schema_compiler |
| 179 | 179 |
| 180 #endif // TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__ | 180 #endif // TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__ |
| OLD | NEW |