Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(388)

Side by Side Diff: lib/Array_h.template

Issue 2526603002: [inspector_protocol] Introduce protocol::Serializable (Closed)
Patch Set: minor fix Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « CodeGenerator.py ('k') | lib/DispatcherBase_cpp.template » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 {{"_".join(config.protocol.namespace)}}_Array_h 5 #ifndef {{"_".join(config.protocol.namespace)}}_Array_h
6 #define {{"_".join(config.protocol.namespace)}}_Array_h 6 #define {{"_".join(config.protocol.namespace)}}_Array_h
7 7
8 //#include "ErrorSupport.h" 8 //#include "ErrorSupport.h"
9 //#include "Forward.h" 9 //#include "Forward.h"
10 //#include "ValueConversions.h" 10 //#include "ValueConversions.h"
11 //#include "Values.h" 11 //#include "Values.h"
12 12
13 {% for namespace in config.protocol.namespace %} 13 {% for namespace in config.protocol.namespace %}
14 namespace {{namespace}} { 14 namespace {{namespace}} {
15 {% endfor %} 15 {% endfor %}
16 16
17 template<typename T> 17 template<typename T>
18 class Array { 18 class Array {
19 public: 19 public:
20 static std::unique_ptr<Array<T>> create() 20 static std::unique_ptr<Array<T>> create()
21 { 21 {
22 return std::unique_ptr<Array<T>>(new Array<T>()); 22 return std::unique_ptr<Array<T>>(new Array<T>());
23 } 23 }
24 24
25 static std::unique_ptr<Array<T>> parse(protocol::Value* value, ErrorSupport* errors) 25 static std::unique_ptr<Array<T>> fromValue(protocol::Value* value, ErrorSupp ort* errors)
26 { 26 {
27 protocol::ListValue* array = ListValue::cast(value); 27 protocol::ListValue* array = ListValue::cast(value);
28 if (!array) { 28 if (!array) {
29 errors->addError("array expected"); 29 errors->addError("array expected");
30 return nullptr; 30 return nullptr;
31 } 31 }
32 std::unique_ptr<Array<T>> result(new Array<T>()); 32 std::unique_ptr<Array<T>> result(new Array<T>());
33 errors->push(); 33 errors->push();
34 for (size_t i = 0; i < array->size(); ++i) { 34 for (size_t i = 0; i < array->size(); ++i) {
35 errors->setName(StringUtil::fromInteger(i)); 35 errors->setName(StringUtil::fromInteger(i));
36 std::unique_ptr<T> item = ValueConversions<T>::parse(array->at(i), e rrors); 36 std::unique_ptr<T> item = ValueConversions<T>::fromValue(array->at(i ), errors);
37 result->m_vector.push_back(std::move(item)); 37 result->m_vector.push_back(std::move(item));
38 } 38 }
39 errors->pop(); 39 errors->pop();
40 if (errors->hasErrors()) 40 if (errors->hasErrors())
41 return nullptr; 41 return nullptr;
42 return result; 42 return result;
43 } 43 }
44 44
45 void addItem(std::unique_ptr<T> value) 45 void addItem(std::unique_ptr<T> value)
46 { 46 {
47 m_vector.push_back(std::move(value)); 47 m_vector.push_back(std::move(value));
48 } 48 }
49 49
50 size_t length() 50 size_t length()
51 { 51 {
52 return m_vector.size(); 52 return m_vector.size();
53 } 53 }
54 54
55 T* get(size_t index) 55 T* get(size_t index)
56 { 56 {
57 return m_vector[index].get(); 57 return m_vector[index].get();
58 } 58 }
59 59
60 std::unique_ptr<protocol::ListValue> serialize() 60 std::unique_ptr<protocol::ListValue> toValue()
61 { 61 {
62 std::unique_ptr<protocol::ListValue> result = ListValue::create(); 62 std::unique_ptr<protocol::ListValue> result = ListValue::create();
63 for (auto& item : m_vector) 63 for (auto& item : m_vector)
64 result->pushValue(ValueConversions<T>::serialize(item)); 64 result->pushValue(ValueConversions<T>::toValue(item));
65 return result; 65 return result;
66 } 66 }
67 67
68 private: 68 private:
69 std::vector<std::unique_ptr<T>> m_vector; 69 std::vector<std::unique_ptr<T>> m_vector;
70 }; 70 };
71 71
72 template<typename T> 72 template<typename T>
73 class ArrayBase { 73 class ArrayBase {
74 public: 74 public:
75 static std::unique_ptr<Array<T>> create() 75 static std::unique_ptr<Array<T>> create()
76 { 76 {
77 return std::unique_ptr<Array<T>>(new Array<T>()); 77 return std::unique_ptr<Array<T>>(new Array<T>());
78 } 78 }
79 79
80 static std::unique_ptr<Array<T>> parse(protocol::Value* value, ErrorSupport* errors) 80 static std::unique_ptr<Array<T>> fromValue(protocol::Value* value, ErrorSupp ort* errors)
81 { 81 {
82 protocol::ListValue* array = ListValue::cast(value); 82 protocol::ListValue* array = ListValue::cast(value);
83 if (!array) { 83 if (!array) {
84 errors->addError("array expected"); 84 errors->addError("array expected");
85 return nullptr; 85 return nullptr;
86 } 86 }
87 errors->push(); 87 errors->push();
88 std::unique_ptr<Array<T>> result(new Array<T>()); 88 std::unique_ptr<Array<T>> result(new Array<T>());
89 for (size_t i = 0; i < array->size(); ++i) { 89 for (size_t i = 0; i < array->size(); ++i) {
90 errors->setName(StringUtil::fromInteger(i)); 90 errors->setName(StringUtil::fromInteger(i));
91 T item = ValueConversions<T>::parse(array->at(i), errors); 91 T item = ValueConversions<T>::fromValue(array->at(i), errors);
92 result->m_vector.push_back(item); 92 result->m_vector.push_back(item);
93 } 93 }
94 errors->pop(); 94 errors->pop();
95 if (errors->hasErrors()) 95 if (errors->hasErrors())
96 return nullptr; 96 return nullptr;
97 return result; 97 return result;
98 } 98 }
99 99
100 void addItem(const T& value) 100 void addItem(const T& value)
101 { 101 {
102 m_vector.push_back(value); 102 m_vector.push_back(value);
103 } 103 }
104 104
105 size_t length() 105 size_t length()
106 { 106 {
107 return m_vector.size(); 107 return m_vector.size();
108 } 108 }
109 109
110 T get(size_t index) 110 T get(size_t index)
111 { 111 {
112 return m_vector[index]; 112 return m_vector[index];
113 } 113 }
114 114
115 std::unique_ptr<protocol::ListValue> serialize() 115 std::unique_ptr<protocol::ListValue> toValue()
116 { 116 {
117 std::unique_ptr<protocol::ListValue> result = ListValue::create(); 117 std::unique_ptr<protocol::ListValue> result = ListValue::create();
118 for (auto& item : m_vector) 118 for (auto& item : m_vector)
119 result->pushValue(ValueConversions<T>::serialize(item)); 119 result->pushValue(ValueConversions<T>::toValue(item));
120 return result; 120 return result;
121 } 121 }
122 122
123 private: 123 private:
124 std::vector<T> m_vector; 124 std::vector<T> m_vector;
125 }; 125 };
126 126
127 template<> class Array<String> : public ArrayBase<String> {}; 127 template<> class Array<String> : public ArrayBase<String> {};
128 template<> class Array<int> : public ArrayBase<int> {}; 128 template<> class Array<int> : public ArrayBase<int> {};
129 template<> class Array<double> : public ArrayBase<double> {}; 129 template<> class Array<double> : public ArrayBase<double> {};
130 template<> class Array<bool> : public ArrayBase<bool> {}; 130 template<> class Array<bool> : public ArrayBase<bool> {};
131 131
132 {% for namespace in config.protocol.namespace %} 132 {% for namespace in config.protocol.namespace %}
133 } // namespace {{namespace}} 133 } // namespace {{namespace}}
134 {% endfor %} 134 {% endfor %}
135 135
136 #endif // !defined({{"_".join(config.protocol.namespace)}}_Array_h) 136 #endif // !defined({{"_".join(config.protocol.namespace)}}_Array_h)
OLDNEW
« no previous file with comments | « CodeGenerator.py ('k') | lib/DispatcherBase_cpp.template » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698