Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(449)

Side by Side Diff: tools/json_schema_compiler/util.h

Issue 851673003: Cleanup: Some simplifications in json_schema_compiler. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@fix_dart_tests
Patch Set: Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 struct ITEMS_ARE_BINARY {};
20 struct ITEMS_ARE_NOT_BINARY {};
not at google - send to devlin 2015/01/13 18:15:13 Mhm ok I'm going to add Jeffrey here to advise on
Jeffrey Yasskin 2015/01/13 18:57:08 Function pointers are generally slow to call, alth
not at google - send to devlin 2015/01/13 19:39:56 I'm fairly certain a lot more is going on than cal
Jeffrey Yasskin 2015/01/13 21:17:21 Nope. :)
21
19 // Creates a new item at |out| from |from|[|index|]. These are used by template 22 // Creates a new item at |out| from |from|[|index|]. These are used by template
20 // specializations of |Get(Optional)ArrayFromList|. 23 // specializations of |Get(Optional)ArrayFromList|.
21 bool GetItemFromList(const base::ListValue& from, int index, int* out);
22 bool GetItemFromList(const base::ListValue& from, int index, bool* out);
23 bool GetItemFromList(const base::ListValue& from, int index, double* out);
24 bool GetItemFromList(const base::ListValue& from, int index, std::string* out);
25 bool GetItemFromList(const base::ListValue& from, 24 bool GetItemFromList(const base::ListValue& from,
26 int index, 25 int index,
26 ITEMS_ARE_NOT_BINARY binary_tag,
27 int* out);
28 bool GetItemFromList(const base::ListValue& from,
29 int index,
30 ITEMS_ARE_NOT_BINARY binary_tag,
31 bool* out);
32 bool GetItemFromList(const base::ListValue& from,
33 int index,
34 ITEMS_ARE_NOT_BINARY binary_tag,
35 double* out);
36 bool GetItemFromList(const base::ListValue& from,
37 int index,
38 ITEMS_ARE_NOT_BINARY binary_tag,
39 std::string* out);
40 bool GetItemFromList(const base::ListValue& from,
41 int index,
42 ITEMS_ARE_BINARY,
43 std::string* out);
44 bool GetItemFromList(const base::ListValue& from,
45 int index,
46 ITEMS_ARE_NOT_BINARY binary_tag,
27 linked_ptr<base::Value>* out); 47 linked_ptr<base::Value>* out);
28 bool GetItemFromList(const base::ListValue& from, 48 bool GetItemFromList(const base::ListValue& from,
29 int index, 49 int index,
50 ITEMS_ARE_NOT_BINARY binary_tag,
30 linked_ptr<base::DictionaryValue>* out); 51 linked_ptr<base::DictionaryValue>* out);
31 52
32 // This template is used for types generated by tools/json_schema_compiler. 53 // This template is used for types generated by tools/json_schema_compiler.
33 template<class T> 54 template <class T>
34 bool GetItemFromList(const base::ListValue& from, 55 bool GetItemFromList(const base::ListValue& from,
35 int index, 56 int index,
57 ITEMS_ARE_NOT_BINARY /* binary_tag */,
36 linked_ptr<T>* out) { 58 linked_ptr<T>* out) {
37 const base::DictionaryValue* dict; 59 const base::DictionaryValue* dict;
38 if (!from.GetDictionary(index, &dict)) 60 if (!from.GetDictionary(index, &dict))
39 return false; 61 return false;
40 scoped_ptr<T> obj(new T()); 62 scoped_ptr<T> obj(new T());
41 if (!T::Populate(*dict, obj.get())) 63 if (!T::Populate(*dict, obj.get()))
42 return false; 64 return false;
43 *out = linked_ptr<T>(obj.release()); 65 *out = linked_ptr<T>(obj.release());
44 return true; 66 return true;
45 } 67 }
46 68
47 // Populates |out| with |list|. Returns false if there is no list at the 69 // 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|. 70 // specified key or if the list has anything other than |T|.
49 template <class T> 71 template <class T, class BINARY_TAG>
50 bool PopulateArrayFromList( 72 bool PopulateArrayFromList(
51 const base::ListValue& list, std::vector<T>* out) { 73 const base::ListValue& list, BINARY_TAG binary_tag, std::vector<T>* out) {
52 out->clear(); 74 out->clear();
53 T value; 75 T value;
54 for (size_t i = 0; i < list.GetSize(); ++i) { 76 for (size_t i = 0; i < list.GetSize(); ++i) {
55 if (!GetItemFromList(list, i, &value)) 77 if (!GetItemFromList(list, i, binary_tag, &value))
56 return false; 78 return false;
57 out->push_back(value); 79 out->push_back(value);
58 } 80 }
59 81
60 return true; 82 return true;
61 } 83 }
62 84
63 // Populates |out| with |from|.|name|. Returns false if there is no list at 85 // 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|. 86 // the specified key or if the list has anything other than |T|.
65 template <class T> 87 template <class T>
66 bool PopulateArrayFromDictionary( 88 bool PopulateArrayFromDictionary(
67 const base::DictionaryValue& from, 89 const base::DictionaryValue& from,
68 const std::string& name, 90 const std::string& name,
69 std::vector<T>* out) { 91 std::vector<T>* out) {
70 const base::ListValue* list = NULL; 92 const base::ListValue* list = NULL;
71 if (!from.GetListWithoutPathExpansion(name, &list)) 93 if (!from.GetListWithoutPathExpansion(name, &list))
72 return false; 94 return false;
73 95
74 return PopulateArrayFromList(*list, out); 96 return PopulateArrayFromList(*list, ITEMS_ARE_NOT_BINARY(), out);
75 } 97 }
76 98
77 // Creates a new vector containing |list| at |out|. Returns 99 // Creates a new vector containing |list| at |out|. Returns
78 // true on success or if there is nothing at the specified key. Returns false 100 // 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. 101 // if anything other than a list of |T| is at the specified key.
80 template <class T> 102 template <class T, class BINARY_TAG>
81 bool PopulateOptionalArrayFromList( 103 bool PopulateOptionalArrayFromList(const base::ListValue& list,
82 const base::ListValue& list, 104 BINARY_TAG binary_tag,
83 scoped_ptr<std::vector<T> >* out) { 105 scoped_ptr<std::vector<T>>* out) {
84 out->reset(new std::vector<T>()); 106 out->reset(new std::vector<T>());
85 T value; 107 T value;
86 for (size_t i = 0; i < list.GetSize(); ++i) { 108 for (size_t i = 0; i < list.GetSize(); ++i) {
87 if (!GetItemFromList(list, i, &value)) { 109 if (!GetItemFromList(list, i, binary_tag, &value)) {
88 out->reset(); 110 out->reset();
89 return false; 111 return false;
90 } 112 }
91 (*out)->push_back(value); 113 (*out)->push_back(value);
92 } 114 }
93 115
94 return true; 116 return true;
95 } 117 }
96 118
97 // Creates a new vector containing |from|.|name| at |out|. Returns 119 // 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 120 // 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. 121 // if anything other than a list of |T| is at the specified key.
100 template <class T> 122 template <class T>
101 bool PopulateOptionalArrayFromDictionary( 123 bool PopulateOptionalArrayFromDictionary(
102 const base::DictionaryValue& from, 124 const base::DictionaryValue& from,
103 const std::string& name, 125 const std::string& name,
104 scoped_ptr<std::vector<T> >* out) { 126 scoped_ptr<std::vector<T> >* out) {
105 const base::ListValue* list = NULL; 127 const base::ListValue* list = NULL;
106 { 128 {
107 const base::Value* maybe_list = NULL; 129 const base::Value* maybe_list = NULL;
108 // Since |name| is optional, its absence is acceptable. However, anything 130 // Since |name| is optional, its absence is acceptable. However, anything
109 // other than a ListValue is not. 131 // other than a ListValue is not.
110 if (!from.GetWithoutPathExpansion(name, &maybe_list)) 132 if (!from.GetWithoutPathExpansion(name, &maybe_list))
111 return true; 133 return true;
112 if (!maybe_list->IsType(base::Value::TYPE_LIST)) 134 if (!maybe_list->IsType(base::Value::TYPE_LIST))
113 return false; 135 return false;
114 list = static_cast<const base::ListValue*>(maybe_list); 136 list = static_cast<const base::ListValue*>(maybe_list);
115 } 137 }
116 138
117 return PopulateOptionalArrayFromList(*list, out); 139 return PopulateOptionalArrayFromList(*list, ITEMS_ARE_NOT_BINARY(), out);
118 } 140 }
119 141
120 // Appends a Value newly created from |from| to |out|. These used by template 142 // Appends a Value newly created from |from| to |out|. These used by template
121 // specializations of |Set(Optional)ArrayToList|. 143 // specializations of |Set(Optional)ArrayToList|.
122 void AddItemToList(const int from, base::ListValue* out); 144 void AddItemToList(const int from, base::ListValue* out);
123 void AddItemToList(const bool from, base::ListValue* out); 145 void AddItemToList(const bool from, base::ListValue* out);
124 void AddItemToList(const double from, base::ListValue* out); 146 void AddItemToList(const double from, base::ListValue* out);
125 void AddItemToList(const std::string& from, base::ListValue* out); 147 void AddItemToList(const std::string& from, base::ListValue* out);
126 void AddItemToList(const linked_ptr<base::Value>& from, 148 void AddItemToList(const linked_ptr<base::Value>& from,
127 base::ListValue* out); 149 base::ListValue* out);
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
172 return CreateValueFromArray(*from); 194 return CreateValueFromArray(*from);
173 return scoped_ptr<base::Value>(); 195 return scoped_ptr<base::Value>();
174 } 196 }
175 197
176 std::string ValueTypeToString(base::Value::Type type); 198 std::string ValueTypeToString(base::Value::Type type);
177 199
178 } // namespace util 200 } // namespace util
179 } // namespace json_schema_compiler 201 } // namespace json_schema_compiler
180 202
181 #endif // TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__ 203 #endif // TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698