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 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
| 12 #include "base/memory/linked_ptr.h" |
12 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
13 | 14 #include "base/values.h" |
14 namespace base { | |
15 class DictionaryValue; | |
16 } | |
17 | 15 |
18 namespace json_schema_compiler { | 16 namespace json_schema_compiler { |
19 namespace util { | 17 namespace util { |
20 | 18 |
21 // Creates a new vector containing the strings |from|.|name| at |out|. Returns | 19 // Creates a new item at |out| from |from|[|index|]. These are used by template |
| 20 // specializations of |Get(Optional)ArrayFromList|. |
| 21 bool GetItemFromList(const ListValue& from, int index, int* out); |
| 22 bool GetItemFromList(const ListValue& from, int index, bool* out); |
| 23 bool GetItemFromList(const ListValue& from, int index, double* out); |
| 24 bool GetItemFromList(const ListValue& from, int index, std::string* out); |
| 25 bool GetItemFromList(const ListValue& from, int index, |
| 26 linked_ptr<base::DictionaryValue>* out); |
| 27 |
| 28 // This template is used for types generated by tools/json_schema_compiler. |
| 29 template<class T> |
| 30 bool GetItemFromList(const ListValue& from, int index, linked_ptr<T>* out) { |
| 31 DictionaryValue* dict; |
| 32 if (!from.GetDictionary(index, &dict)) |
| 33 return false; |
| 34 T* obj(new T()); |
| 35 if (!T::Populate(*dict, obj)) |
| 36 return false; |
| 37 *out = linked_ptr<T>(obj); |
| 38 return true; |
| 39 } |
| 40 // Creates a new vector containing Llist| at |out|. Returns |
22 // false if there is no list at the specified key or if the list has anything | 41 // false if there is no list at the specified key or if the list has anything |
23 // other than strings. | 42 // other than |T|. |
24 bool GetStrings( | 43 template <class T> |
| 44 bool GetArrayFromList( |
| 45 const base::ListValue& list, std::vector<T>* out) { |
| 46 T value; |
| 47 for (size_t i = 0; i < list.GetSize(); ++i) { |
| 48 if (!GetItemFromList(list, i, &value)) |
| 49 return false; |
| 50 out->push_back(value); |
| 51 } |
| 52 |
| 53 return true; |
| 54 } |
| 55 |
| 56 // Creates a new vector containing |from|.|name| at |out|. Returns |
| 57 // false if there is no list at the specified key or if the list has anything |
| 58 // other than |T|. |
| 59 template <class T> |
| 60 bool GetArrayFromDictionary( |
25 const base::DictionaryValue& from, | 61 const base::DictionaryValue& from, |
26 const std::string& name, | 62 const std::string& name, |
27 std::vector<std::string>* out); | 63 std::vector<T>* out) { |
| 64 base::ListValue* list = NULL; |
| 65 if (!from.GetListWithoutPathExpansion(name, &list)) |
| 66 return false; |
28 | 67 |
29 // Creates a new vector containing the strings |from|.|name| at |out|. Returns | 68 return GetArrayFromList(*list, out); |
| 69 } |
| 70 |
| 71 // Creates a new vector containing |list| at |out|. Returns |
30 // true on success or if there is nothing at the specified key. Returns false | 72 // true on success or if there is nothing at the specified key. Returns false |
31 // if anything other than a list of strings is at the specified key. | 73 // if anything other than a list of |T| is at the specified key. |
32 bool GetOptionalStrings( | 74 template <class T> |
| 75 bool GetOptionalArrayFromList( |
| 76 const base::ListValue& list, |
| 77 scoped_ptr<std::vector<T> >* out) { |
| 78 out->reset(new std::vector<T>()); |
| 79 T value; |
| 80 for (size_t i = 0; i < list.GetSize(); ++i) { |
| 81 if (!GetItemFromList(list, i, &value)) { |
| 82 out->reset(); |
| 83 return false; |
| 84 } |
| 85 (*out)->push_back(value); |
| 86 } |
| 87 |
| 88 return true; |
| 89 } |
| 90 |
| 91 // Creates a new vector containing |from|.|name| at |out|. Returns |
| 92 // true on success or if there is nothing at the specified key. Returns false |
| 93 // if anything other than a list of |T| is at the specified key. |
| 94 template <class T> |
| 95 bool GetOptionalArrayFromDictionary( |
33 const base::DictionaryValue& from, | 96 const base::DictionaryValue& from, |
34 const std::string& name, | 97 const std::string& name, |
35 scoped_ptr<std::vector<std::string> >* out); | 98 scoped_ptr<std::vector<T> >* out) { |
| 99 base::ListValue* list = NULL; |
| 100 { |
| 101 base::Value* maybe_list = NULL; |
| 102 // Since |name| is optional, its absence is acceptable. However, anything |
| 103 // other than a ListValue is not. |
| 104 if (!from.GetWithoutPathExpansion(name, &maybe_list)) |
| 105 return true; |
| 106 if (!maybe_list->IsType(base::Value::TYPE_LIST)) |
| 107 return false; |
| 108 list = static_cast<base::ListValue*>(maybe_list); |
| 109 } |
36 | 110 |
37 // Puts the each string in |from| into a new ListValue at |out|.|name|. | 111 return GetOptionalArrayFromList(*list, out); |
38 void SetStrings( | 112 } |
39 const std::vector<std::string>& from, | 113 |
| 114 // Appends a Value newly created from |from| to |out|. These used by template |
| 115 // specializations of |Set(Optional)ArrayToList|. |
| 116 void AddItemToList(const int from, base::ListValue* out); |
| 117 void AddItemToList(const bool from, base::ListValue* out); |
| 118 void AddItemToList(const double from, base::ListValue* out); |
| 119 void AddItemToList(const std::string& from, base::ListValue* out); |
| 120 void AddItemToList(const linked_ptr<base::DictionaryValue>& from, |
| 121 base::ListValue* out); |
| 122 |
| 123 // This template is used for types generated by tools/json_schema_compiler. |
| 124 template<class T> |
| 125 void AddItemToList(const linked_ptr<T>& from, ListValue* out) { |
| 126 out->Append(from->ToValue()); |
| 127 } |
| 128 |
| 129 // Set |out| to the the contents of |from|. Requires GetItemFromList to be |
| 130 // implemented for |T|. |
| 131 template <class T> |
| 132 void SetArrayToList( |
| 133 const std::vector<T>& from, |
| 134 base::ListValue* out) { |
| 135 for (typename std::vector<T>::const_iterator it = from.begin(); |
| 136 it != from.end(); ++it) { |
| 137 AddItemToList(*it, out); |
| 138 } |
| 139 } |
| 140 |
| 141 // Set |out| to the the contents of |from| if |from| is non-NULL. Requires |
| 142 // GetItemFromList to be implemented for |T|. |
| 143 template <class T> |
| 144 void SetOptionalArrayToList( |
| 145 const scoped_ptr<std::vector<T> >& from, |
| 146 base::ListValue* out) { |
| 147 if (from.get()) |
| 148 SetArrayToList(*from, out); |
| 149 } |
| 150 |
| 151 // Sets |out|.|name| to a newly created ListValue containing |from|. Requires |
| 152 // GetItemFromList to be implemented for |T|. |
| 153 template <class T> |
| 154 void SetArrayToDictionary( |
| 155 const std::vector<T>& from, |
40 const std::string& name, | 156 const std::string& name, |
41 base::DictionaryValue* out); | 157 base::DictionaryValue* out) { |
| 158 base::ListValue* list = new base::ListValue(); |
| 159 out->SetWithoutPathExpansion(name, list); |
| 160 SetArrayToList(from, list); |
| 161 } |
42 | 162 |
43 // If from is non-NULL, puts each string in |from| into a new ListValue at | 163 // If |from| is non-NULL, sets |out|.|name| to a newly created ListValue |
44 // |out|.|name|. | 164 // containing |from|. Requires GetItemFromList to be implemented for |T|. |
45 void SetOptionalStrings( | 165 template <class T> |
46 const scoped_ptr<std::vector<std::string> >& from, | 166 void SetOptionalArrayToDictionary( |
| 167 const scoped_ptr<std::vector<T> >& from, |
47 const std::string& name, | 168 const std::string& name, |
48 base::DictionaryValue* out); | 169 base::DictionaryValue* out) { |
| 170 if (from.get()) |
| 171 SetArrayToDictionary(*from, name, out); |
| 172 } |
| 173 |
49 | 174 |
50 } // namespace api_util | 175 } // namespace api_util |
51 } // namespace extensions | 176 } // namespace extensions |
52 | 177 |
53 #endif // TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__ | 178 #endif // TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__ |
OLD | NEW |