OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef Array_h | |
6 #define Array_h | |
7 | |
8 #include "platform/inspector_protocol/ErrorSupport.h" | |
9 #include "platform/inspector_protocol/Platform.h" | |
10 #include "platform/inspector_protocol/String16.h" | |
11 #include "platform/inspector_protocol/ValueConversions.h" | |
12 #include "platform/inspector_protocol/Values.h" | |
13 | |
14 #include <vector> | |
15 | |
16 namespace blink { | |
17 namespace protocol { | |
18 | |
19 template<typename T> | |
20 class Array { | |
21 public: | |
22 static std::unique_ptr<Array<T>> create() | |
23 { | |
24 return wrapUnique(new Array<T>()); | |
25 } | |
26 | |
27 static std::unique_ptr<Array<T>> parse(protocol::Value* value, ErrorSupport*
errors) | |
28 { | |
29 protocol::ListValue* array = ListValue::cast(value); | |
30 if (!array) { | |
31 errors->addError("array expected"); | |
32 return nullptr; | |
33 } | |
34 std::unique_ptr<Array<T>> result(new Array<T>()); | |
35 errors->push(); | |
36 for (size_t i = 0; i < array->size(); ++i) { | |
37 errors->setName(String16::fromInteger(i)); | |
38 std::unique_ptr<T> item = ValueConversions<T>::parse(array->at(i), e
rrors); | |
39 result->m_vector.push_back(std::move(item)); | |
40 } | |
41 errors->pop(); | |
42 if (errors->hasErrors()) | |
43 return nullptr; | |
44 return result; | |
45 } | |
46 | |
47 void addItem(std::unique_ptr<T> value) | |
48 { | |
49 m_vector.push_back(std::move(value)); | |
50 } | |
51 | |
52 size_t length() | |
53 { | |
54 return m_vector.size(); | |
55 } | |
56 | |
57 T* get(size_t index) | |
58 { | |
59 return m_vector[index].get(); | |
60 } | |
61 | |
62 std::unique_ptr<protocol::ListValue> serialize() | |
63 { | |
64 std::unique_ptr<protocol::ListValue> result = ListValue::create(); | |
65 for (auto& item : m_vector) | |
66 result->pushValue(ValueConversions<T>::serialize(item)); | |
67 return result; | |
68 } | |
69 | |
70 private: | |
71 std::vector<std::unique_ptr<T>> m_vector; | |
72 }; | |
73 | |
74 template<typename T> | |
75 class ArrayBase { | |
76 public: | |
77 static std::unique_ptr<Array<T>> create() | |
78 { | |
79 return wrapUnique(new Array<T>()); | |
80 } | |
81 | |
82 static std::unique_ptr<Array<T>> parse(protocol::Value* value, ErrorSupport*
errors) | |
83 { | |
84 protocol::ListValue* array = ListValue::cast(value); | |
85 if (!array) { | |
86 errors->addError("array expected"); | |
87 return nullptr; | |
88 } | |
89 errors->push(); | |
90 std::unique_ptr<Array<T>> result(new Array<T>()); | |
91 for (size_t i = 0; i < array->size(); ++i) { | |
92 errors->setName(String16::fromInteger(i)); | |
93 T item = ValueConversions<T>::parse(array->at(i), errors); | |
94 result->m_vector.push_back(item); | |
95 } | |
96 errors->pop(); | |
97 if (errors->hasErrors()) | |
98 return nullptr; | |
99 return result; | |
100 } | |
101 | |
102 void addItem(const T& value) | |
103 { | |
104 m_vector.push_back(value); | |
105 } | |
106 | |
107 size_t length() | |
108 { | |
109 return m_vector.size(); | |
110 } | |
111 | |
112 T get(size_t index) | |
113 { | |
114 return m_vector[index]; | |
115 } | |
116 | |
117 std::unique_ptr<protocol::ListValue> serialize() | |
118 { | |
119 std::unique_ptr<protocol::ListValue> result = ListValue::create(); | |
120 for (auto& item : m_vector) | |
121 result->pushValue(ValueConversions<T>::serialize(item)); | |
122 return result; | |
123 } | |
124 | |
125 private: | |
126 std::vector<T> m_vector; | |
127 }; | |
128 | |
129 template<> class Array<InspectorProtocolConvenienceStringType> : public ArrayBas
e<InspectorProtocolConvenienceStringType> {}; | |
130 template<> class Array<String16> : public ArrayBase<String16> {}; | |
131 template<> class Array<int> : public ArrayBase<int> {}; | |
132 template<> class Array<double> : public ArrayBase<double> {}; | |
133 template<> class Array<bool> : public ArrayBase<bool> {}; | |
134 | |
135 } // namespace platform | |
136 } // namespace blink | |
137 | |
138 #endif // !defined(Array_h) | |
OLD | NEW |