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