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