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