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" | 11 #include "base/memory/linked_ptr.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/values.h" | 13 #include "base/values.h" |
14 | 14 |
15 namespace json_schema_compiler { | 15 namespace json_schema_compiler { |
16 | 16 |
17 namespace util { | 17 namespace util { |
18 | 18 |
19 // Creates a new item at |out| from |from|[|index|]. These are used by template | 19 // Populates the item |out| from the value |from|. These are used by template |
20 // specializations of |Get(Optional)ArrayFromList|. | 20 // specializations of |Get(Optional)ArrayFromList|. |
21 bool GetItemFromList(const base::ListValue& from, int index, int* out); | 21 bool PopulateItem(const base::Value& from, int* out); |
22 bool GetItemFromList(const base::ListValue& from, int index, bool* out); | 22 bool PopulateItem(const base::Value& from, bool* out); |
23 bool GetItemFromList(const base::ListValue& from, int index, double* out); | 23 bool PopulateItem(const base::Value& from, double* out); |
24 bool GetItemFromList(const base::ListValue& from, int index, std::string* out); | 24 bool PopulateItem(const base::Value& from, std::string* out); |
25 bool GetItemFromList(const base::ListValue& from, | 25 bool PopulateItem(const base::Value& from, linked_ptr<base::Value>* out); |
26 int index, | 26 bool PopulateItem(const base::Value& from, |
27 linked_ptr<base::Value>* out); | 27 linked_ptr<base::DictionaryValue>* out); |
28 bool GetItemFromList(const base::ListValue& from, | |
29 int index, | |
30 linked_ptr<base::DictionaryValue>* out); | |
31 | 28 |
32 // This template is used for types generated by tools/json_schema_compiler. | 29 // This template is used for types generated by tools/json_schema_compiler. |
33 template<class T> | 30 template <class T> |
34 bool GetItemFromList(const base::ListValue& from, | 31 bool PopulateItem(const base::Value& from, linked_ptr<T>* out) { |
35 int index, | 32 const base::DictionaryValue* dict = nullptr; |
36 linked_ptr<T>* out) { | 33 if (!from.GetAsDictionary(&dict)) |
37 const base::DictionaryValue* dict; | |
38 if (!from.GetDictionary(index, &dict)) | |
39 return false; | 34 return false; |
40 scoped_ptr<T> obj(new T()); | 35 scoped_ptr<T> obj(new T()); |
41 if (!T::Populate(*dict, obj.get())) | 36 if (!T::Populate(*dict, obj.get())) |
42 return false; | 37 return false; |
43 *out = linked_ptr<T>(obj.release()); | 38 *out = linked_ptr<T>(obj.release()); |
44 return true; | 39 return true; |
45 } | 40 } |
46 | 41 |
47 // Populates |out| with |list|. Returns false if there is no list at the | 42 // Populates |out| with |list|. Returns false if there is no list at the |
48 // specified key or if the list has anything other than |T|. | 43 // specified key or if the list has anything other than |T|. |
49 template <class T> | 44 template <class T> |
50 bool PopulateArrayFromList( | 45 bool PopulateArrayFromList(const base::ListValue& list, |
51 const base::ListValue& list, std::vector<T>* out) { | 46 std::vector<T>* out) { |
52 out->clear(); | 47 out->clear(); |
53 T value; | 48 T item; |
54 for (size_t i = 0; i < list.GetSize(); ++i) { | 49 for (size_t i = 0; i < list.GetSize(); ++i) { |
not at google - send to devlin
2015/01/15 20:47:30
I notice that ListValue has an iterator-like inter
pneubeck (no reviews)
2015/01/16 08:20:13
Yes, forgot about that.
Did the same to the other
| |
55 if (!GetItemFromList(list, i, &value)) | 50 const base::Value* value = nullptr; |
51 if (!list.Get(i, &value) || !PopulateItem(*value, &item)) | |
56 return false; | 52 return false; |
57 out->push_back(value); | 53 out->push_back(item); |
58 } | 54 } |
59 | 55 |
60 return true; | 56 return true; |
61 } | 57 } |
62 | 58 |
63 // Creates a new vector containing |list| at |out|. Returns | 59 // Creates a new vector containing |list| at |out|. Returns |
64 // true on success or if there is nothing at the specified key. Returns false | 60 // true on success or if there is nothing at the specified key. Returns false |
65 // if anything other than a list of |T| is at the specified key. | 61 // if anything other than a list of |T| is at the specified key. |
66 template <class T> | 62 template <class T> |
67 bool PopulateOptionalArrayFromList( | 63 bool PopulateOptionalArrayFromList(const base::ListValue& list, |
68 const base::ListValue& list, | 64 scoped_ptr<std::vector<T>>* out) { |
69 scoped_ptr<std::vector<T> >* out) { | |
70 out->reset(new std::vector<T>()); | 65 out->reset(new std::vector<T>()); |
71 T value; | 66 if (!PopulateArrayFromList(list, out->get())) { |
72 for (size_t i = 0; i < list.GetSize(); ++i) { | 67 out->reset(); |
73 if (!GetItemFromList(list, i, &value)) { | 68 return false; |
74 out->reset(); | |
75 return false; | |
76 } | |
77 (*out)->push_back(value); | |
78 } | 69 } |
79 | |
80 return true; | 70 return true; |
81 } | 71 } |
82 | 72 |
83 // Appends a Value newly created from |from| to |out|. These used by template | 73 // Appends a Value newly created from |from| to |out|. These used by template |
84 // specializations of |Set(Optional)ArrayToList|. | 74 // specializations of |Set(Optional)ArrayToList|. |
85 void AddItemToList(const int from, base::ListValue* out); | 75 void AddItemToList(const int from, base::ListValue* out); |
86 void AddItemToList(const bool from, base::ListValue* out); | 76 void AddItemToList(const bool from, base::ListValue* out); |
87 void AddItemToList(const double from, base::ListValue* out); | 77 void AddItemToList(const double from, base::ListValue* out); |
88 void AddItemToList(const std::string& from, base::ListValue* out); | 78 void AddItemToList(const std::string& from, base::ListValue* out); |
89 void AddItemToList(const linked_ptr<base::Value>& from, | 79 void AddItemToList(const linked_ptr<base::Value>& from, |
90 base::ListValue* out); | 80 base::ListValue* out); |
91 void AddItemToList(const linked_ptr<base::DictionaryValue>& from, | 81 void AddItemToList(const linked_ptr<base::DictionaryValue>& from, |
92 base::ListValue* out); | 82 base::ListValue* out); |
93 | 83 |
94 // This template is used for types generated by tools/json_schema_compiler. | 84 // This template is used for types generated by tools/json_schema_compiler. |
95 template<class T> | 85 template<class T> |
96 void AddItemToList(const linked_ptr<T>& from, base::ListValue* out) { | 86 void AddItemToList(const linked_ptr<T>& from, base::ListValue* out) { |
97 out->Append(from->ToValue().release()); | 87 out->Append(from->ToValue().release()); |
98 } | 88 } |
99 | 89 |
100 // Set |out| to the the contents of |from|. Requires GetItemFromList to be | 90 // Set |out| to the the contents of |from|. Requires PopulateItem to be |
101 // implemented for |T|. | 91 // implemented for |T|. |
102 template <class T> | 92 template <class T> |
103 void PopulateListFromArray( | 93 void PopulateListFromArray( |
104 const std::vector<T>& from, | 94 const std::vector<T>& from, |
105 base::ListValue* out) { | 95 base::ListValue* out) { |
106 out->Clear(); | 96 out->Clear(); |
107 for (typename std::vector<T>::const_iterator it = from.begin(); | 97 for (typename std::vector<T>::const_iterator it = from.begin(); |
108 it != from.end(); ++it) { | 98 it != from.end(); ++it) { |
109 AddItemToList(*it, out); | 99 AddItemToList(*it, out); |
110 } | 100 } |
111 } | 101 } |
112 | 102 |
113 // Set |out| to the the contents of |from| if |from| is non-NULL. Requires | 103 // Set |out| to the the contents of |from| if |from| is not null. Requires |
114 // GetItemFromList to be implemented for |T|. | 104 // PopulateItem to be implemented for |T|. |
115 template <class T> | 105 template <class T> |
116 void PopulateListFromOptionalArray( | 106 void PopulateListFromOptionalArray( |
117 const scoped_ptr<std::vector<T> >& from, | 107 const scoped_ptr<std::vector<T> >& from, |
118 base::ListValue* out) { | 108 base::ListValue* out) { |
119 if (from.get()) | 109 if (from.get()) |
120 PopulateListFromArray(*from, out); | 110 PopulateListFromArray(*from, out); |
121 | 111 |
122 } | 112 } |
123 | 113 |
124 template <class T> | 114 template <class T> |
(...skipping 10 matching lines...) Expand all Loading... | |
135 return CreateValueFromArray(*from); | 125 return CreateValueFromArray(*from); |
136 return scoped_ptr<base::Value>(); | 126 return scoped_ptr<base::Value>(); |
137 } | 127 } |
138 | 128 |
139 std::string ValueTypeToString(base::Value::Type type); | 129 std::string ValueTypeToString(base::Value::Type type); |
140 | 130 |
141 } // namespace util | 131 } // namespace util |
142 } // namespace json_schema_compiler | 132 } // namespace json_schema_compiler |
143 | 133 |
144 #endif // TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__ | 134 #endif // TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__ |
OLD | NEW |