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