OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 Array_h | 5 #ifndef {{"_".join(config.protocol.namespace)}}_Array_h |
6 #define Array_h | 6 #define {{"_".join(config.protocol.namespace)}}_Array_h |
7 | 7 |
8 //#include "ErrorSupport.h" | 8 //#include "ErrorSupport.h" |
9 //#include "Platform.h" | 9 //#include "Platform.h" |
10 //#include "String16.h" | 10 //#include "String.h" |
11 //#include "ValueConversions.h" | 11 //#include "ValueConversions.h" |
12 //#include "Values.h" | 12 //#include "Values.h" |
13 | 13 |
14 #include <vector> | 14 {% for namespace in config.protocol.namespace %} |
15 | 15 namespace {{namespace}} { |
16 namespace blink { | 16 {% endfor %} |
17 namespace protocol { | |
18 | 17 |
19 template<typename T> | 18 template<typename T> |
20 class Array { | 19 class Array { |
21 public: | 20 public: |
22 static std::unique_ptr<Array<T>> create() | 21 static std::unique_ptr<Array<T>> create() |
23 { | 22 { |
24 return wrapUnique(new Array<T>()); | 23 return wrapUnique(new Array<T>()); |
25 } | 24 } |
26 | 25 |
27 static std::unique_ptr<Array<T>> parse(protocol::Value* value, ErrorSupport*
errors) | 26 static std::unique_ptr<Array<T>> parse(protocol::Value* value, ErrorSupport*
errors) |
28 { | 27 { |
29 protocol::ListValue* array = ListValue::cast(value); | 28 protocol::ListValue* array = ListValue::cast(value); |
30 if (!array) { | 29 if (!array) { |
31 errors->addError("array expected"); | 30 errors->addError("array expected"); |
32 return nullptr; | 31 return nullptr; |
33 } | 32 } |
34 std::unique_ptr<Array<T>> result(new Array<T>()); | 33 std::unique_ptr<Array<T>> result(new Array<T>()); |
35 errors->push(); | 34 errors->push(); |
36 for (size_t i = 0; i < array->size(); ++i) { | 35 for (size_t i = 0; i < array->size(); ++i) { |
37 errors->setName(String16::fromInteger(i)); | 36 errors->setName(StringUtil::fromInteger(i)); |
38 std::unique_ptr<T> item = ValueConversions<T>::parse(array->at(i), e
rrors); | 37 std::unique_ptr<T> item = ValueConversions<T>::parse(array->at(i), e
rrors); |
39 result->m_vector.push_back(std::move(item)); | 38 result->m_vector.push_back(std::move(item)); |
40 } | 39 } |
41 errors->pop(); | 40 errors->pop(); |
42 if (errors->hasErrors()) | 41 if (errors->hasErrors()) |
43 return nullptr; | 42 return nullptr; |
44 return result; | 43 return result; |
45 } | 44 } |
46 | 45 |
47 void addItem(std::unique_ptr<T> value) | 46 void addItem(std::unique_ptr<T> value) |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 static std::unique_ptr<Array<T>> parse(protocol::Value* value, ErrorSupport*
errors) | 81 static std::unique_ptr<Array<T>> parse(protocol::Value* value, ErrorSupport*
errors) |
83 { | 82 { |
84 protocol::ListValue* array = ListValue::cast(value); | 83 protocol::ListValue* array = ListValue::cast(value); |
85 if (!array) { | 84 if (!array) { |
86 errors->addError("array expected"); | 85 errors->addError("array expected"); |
87 return nullptr; | 86 return nullptr; |
88 } | 87 } |
89 errors->push(); | 88 errors->push(); |
90 std::unique_ptr<Array<T>> result(new Array<T>()); | 89 std::unique_ptr<Array<T>> result(new Array<T>()); |
91 for (size_t i = 0; i < array->size(); ++i) { | 90 for (size_t i = 0; i < array->size(); ++i) { |
92 errors->setName(String16::fromInteger(i)); | 91 errors->setName(StringUtil::fromInteger(i)); |
93 T item = ValueConversions<T>::parse(array->at(i), errors); | 92 T item = ValueConversions<T>::parse(array->at(i), errors); |
94 result->m_vector.push_back(item); | 93 result->m_vector.push_back(item); |
95 } | 94 } |
96 errors->pop(); | 95 errors->pop(); |
97 if (errors->hasErrors()) | 96 if (errors->hasErrors()) |
98 return nullptr; | 97 return nullptr; |
99 return result; | 98 return result; |
100 } | 99 } |
101 | 100 |
102 void addItem(const T& value) | 101 void addItem(const T& value) |
(...skipping 16 matching lines...) Expand all Loading... |
119 std::unique_ptr<protocol::ListValue> result = ListValue::create(); | 118 std::unique_ptr<protocol::ListValue> result = ListValue::create(); |
120 for (auto& item : m_vector) | 119 for (auto& item : m_vector) |
121 result->pushValue(ValueConversions<T>::serialize(item)); | 120 result->pushValue(ValueConversions<T>::serialize(item)); |
122 return result; | 121 return result; |
123 } | 122 } |
124 | 123 |
125 private: | 124 private: |
126 std::vector<T> m_vector; | 125 std::vector<T> m_vector; |
127 }; | 126 }; |
128 | 127 |
129 template<> class Array<InspectorProtocolConvenienceStringType> : public ArrayBas
e<InspectorProtocolConvenienceStringType> {}; | 128 template<> class Array<String> : public ArrayBase<String> {}; |
130 template<> class Array<String16> : public ArrayBase<String16> {}; | |
131 template<> class Array<int> : public ArrayBase<int> {}; | 129 template<> class Array<int> : public ArrayBase<int> {}; |
132 template<> class Array<double> : public ArrayBase<double> {}; | 130 template<> class Array<double> : public ArrayBase<double> {}; |
133 template<> class Array<bool> : public ArrayBase<bool> {}; | 131 template<> class Array<bool> : public ArrayBase<bool> {}; |
134 | 132 |
135 } // namespace platform | 133 {% for namespace in config.protocol.namespace %} |
136 } // namespace blink | 134 } // namespace {{namespace}} |
| 135 {% endfor %} |
137 | 136 |
138 #endif // !defined(Array_h) | 137 #endif // !defined({{"_".join(config.protocol.namespace)}}_Array_h) |
OLD | NEW |