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