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