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