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 {{"_".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 wrapUnique(new Array<T>()); | 22 return wrapUnique(new Array<T>()); |
23 } | 23 } |
24 | 24 |
25 static std::unique_ptr<Array<T>> fromVector(std::vector<std::unique_ptr<T>> vector) | |
26 { | |
27 return wrapUnique(new Array<T>(std::move(vector))); | |
28 } | |
29 | |
25 static std::unique_ptr<Array<T>> parse(protocol::Value* value, ErrorSupport* errors) | 30 static std::unique_ptr<Array<T>> parse(protocol::Value* value, ErrorSupport* errors) |
26 { | 31 { |
27 protocol::ListValue* array = ListValue::cast(value); | 32 protocol::ListValue* array = ListValue::cast(value); |
28 if (!array) { | 33 if (!array) { |
29 errors->addError("array expected"); | 34 errors->addError("array expected"); |
30 return nullptr; | 35 return nullptr; |
31 } | 36 } |
32 std::unique_ptr<Array<T>> result(new Array<T>()); | 37 std::unique_ptr<Array<T>> result(new Array<T>()); |
33 errors->push(); | 38 errors->push(); |
34 for (size_t i = 0; i < array->size(); ++i) { | 39 for (size_t i = 0; i < array->size(); ++i) { |
35 errors->setName(StringUtil::fromInteger(i)); | 40 errors->setName(StringUtil::fromInteger(i)); |
36 std::unique_ptr<T> item = ValueConversions<T>::parse(array->at(i), e rrors); | 41 std::unique_ptr<T> item = ValueConversions<T>::parse(array->at(i), e rrors); |
37 result->m_vector.push_back(std::move(item)); | 42 result->m_vector.push_back(std::move(item)); |
38 } | 43 } |
39 errors->pop(); | 44 errors->pop(); |
40 if (errors->hasErrors()) | 45 if (errors->hasErrors()) |
41 return nullptr; | 46 return nullptr; |
42 return result; | 47 return result; |
43 } | 48 } |
44 | 49 |
50 | |
dgozman
2016/10/14 01:32:34
nit: extra blank line
alex clarke (OOO till 29th)
2016/10/14 11:45:01
Acknowledged.
| |
45 void addItem(std::unique_ptr<T> value) | 51 void addItem(std::unique_ptr<T> value) |
46 { | 52 { |
47 m_vector.push_back(std::move(value)); | 53 m_vector.push_back(std::move(value)); |
48 } | 54 } |
49 | 55 |
50 size_t length() | 56 size_t length() |
51 { | 57 { |
52 return m_vector.size(); | 58 return m_vector.size(); |
53 } | 59 } |
54 | 60 |
55 T* get(size_t index) | 61 T* get(size_t index) |
56 { | 62 { |
57 return m_vector[index].get(); | 63 return m_vector[index].get(); |
58 } | 64 } |
59 | 65 |
60 std::unique_ptr<protocol::ListValue> serialize() | 66 std::unique_ptr<protocol::ListValue> serialize() |
61 { | 67 { |
62 std::unique_ptr<protocol::ListValue> result = ListValue::create(); | 68 std::unique_ptr<protocol::ListValue> result = ListValue::create(); |
63 for (auto& item : m_vector) | 69 for (auto& item : m_vector) |
64 result->pushValue(ValueConversions<T>::serialize(item)); | 70 result->pushValue(ValueConversions<T>::serialize(item)); |
65 return result; | 71 return result; |
66 } | 72 } |
67 | 73 |
68 private: | 74 private: |
75 Array() {} | |
76 explicit Array(std::vector<std::unique_ptr<T>> vector) : m_vector(std::move( vector)) { } | |
77 | |
69 std::vector<std::unique_ptr<T>> m_vector; | 78 std::vector<std::unique_ptr<T>> m_vector; |
70 }; | 79 }; |
71 | 80 |
72 template<typename T> | 81 template<typename T> |
73 class ArrayBase { | 82 class ArrayBase { |
74 public: | 83 public: |
75 static std::unique_ptr<Array<T>> create() | 84 static std::unique_ptr<Array<T>> create() |
76 { | 85 { |
77 return wrapUnique(new Array<T>()); | 86 return wrapUnique(new Array<T>()); |
78 } | 87 } |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
127 template<> class Array<String> : public ArrayBase<String> {}; | 136 template<> class Array<String> : public ArrayBase<String> {}; |
128 template<> class Array<int> : public ArrayBase<int> {}; | 137 template<> class Array<int> : public ArrayBase<int> {}; |
129 template<> class Array<double> : public ArrayBase<double> {}; | 138 template<> class Array<double> : public ArrayBase<double> {}; |
130 template<> class Array<bool> : public ArrayBase<bool> {}; | 139 template<> class Array<bool> : public ArrayBase<bool> {}; |
131 | 140 |
132 {% for namespace in config.protocol.namespace %} | 141 {% for namespace in config.protocol.namespace %} |
133 } // namespace {{namespace}} | 142 } // namespace {{namespace}} |
134 {% endfor %} | 143 {% endfor %} |
135 | 144 |
136 #endif // !defined({{"_".join(config.protocol.namespace)}}_Array_h) | 145 #endif // !defined({{"_".join(config.protocol.namespace)}}_Array_h) |
OLD | NEW |