11cb0ef41Sopenharmony_ci// This file is generated by Array_h.template. 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ci// Copyright 2016 The Chromium Authors. All rights reserved. 41cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be 51cb0ef41Sopenharmony_ci// found in the LICENSE file. 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ci#ifndef {{"_".join(config.protocol.namespace)}}_Array_h 81cb0ef41Sopenharmony_ci#define {{"_".join(config.protocol.namespace)}}_Array_h 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_ci//#include "ErrorSupport.h" 111cb0ef41Sopenharmony_ci//#include "Forward.h" 121cb0ef41Sopenharmony_ci//#include "ValueConversions.h" 131cb0ef41Sopenharmony_ci//#include "Values.h" 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_ci{% for namespace in config.protocol.namespace %} 161cb0ef41Sopenharmony_cinamespace {{namespace}} { 171cb0ef41Sopenharmony_ci{% endfor %} 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_citemplate<typename T> 201cb0ef41Sopenharmony_ciclass Array { 211cb0ef41Sopenharmony_cipublic: 221cb0ef41Sopenharmony_ci static std::unique_ptr<Array<T>> create() 231cb0ef41Sopenharmony_ci { 241cb0ef41Sopenharmony_ci return std::unique_ptr<Array<T>>(new Array<T>()); 251cb0ef41Sopenharmony_ci } 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_ci static std::unique_ptr<Array<T>> fromValue(protocol::Value* value, ErrorSupport* errors) 281cb0ef41Sopenharmony_ci { 291cb0ef41Sopenharmony_ci protocol::ListValue* array = ListValue::cast(value); 301cb0ef41Sopenharmony_ci if (!array) { 311cb0ef41Sopenharmony_ci errors->addError("array expected"); 321cb0ef41Sopenharmony_ci return nullptr; 331cb0ef41Sopenharmony_ci } 341cb0ef41Sopenharmony_ci std::unique_ptr<Array<T>> result(new Array<T>()); 351cb0ef41Sopenharmony_ci errors->push(); 361cb0ef41Sopenharmony_ci for (size_t i = 0; i < array->size(); ++i) { 371cb0ef41Sopenharmony_ci errors->setName(StringUtil::fromInteger(i)); 381cb0ef41Sopenharmony_ci std::unique_ptr<T> item = ValueConversions<T>::fromValue(array->at(i), errors); 391cb0ef41Sopenharmony_ci result->m_vector.push_back(std::move(item)); 401cb0ef41Sopenharmony_ci } 411cb0ef41Sopenharmony_ci errors->pop(); 421cb0ef41Sopenharmony_ci if (errors->hasErrors()) 431cb0ef41Sopenharmony_ci return nullptr; 441cb0ef41Sopenharmony_ci return result; 451cb0ef41Sopenharmony_ci } 461cb0ef41Sopenharmony_ci 471cb0ef41Sopenharmony_ci void addItem(std::unique_ptr<T> value) 481cb0ef41Sopenharmony_ci { 491cb0ef41Sopenharmony_ci m_vector.push_back(std::move(value)); 501cb0ef41Sopenharmony_ci } 511cb0ef41Sopenharmony_ci 521cb0ef41Sopenharmony_ci size_t length() 531cb0ef41Sopenharmony_ci { 541cb0ef41Sopenharmony_ci return m_vector.size(); 551cb0ef41Sopenharmony_ci } 561cb0ef41Sopenharmony_ci 571cb0ef41Sopenharmony_ci T* get(size_t index) 581cb0ef41Sopenharmony_ci { 591cb0ef41Sopenharmony_ci return m_vector[index].get(); 601cb0ef41Sopenharmony_ci } 611cb0ef41Sopenharmony_ci 621cb0ef41Sopenharmony_ci std::unique_ptr<protocol::ListValue> toValue() 631cb0ef41Sopenharmony_ci { 641cb0ef41Sopenharmony_ci std::unique_ptr<protocol::ListValue> result = ListValue::create(); 651cb0ef41Sopenharmony_ci for (auto& item : m_vector) 661cb0ef41Sopenharmony_ci result->pushValue(ValueConversions<T>::toValue(item)); 671cb0ef41Sopenharmony_ci return result; 681cb0ef41Sopenharmony_ci } 691cb0ef41Sopenharmony_ci 701cb0ef41Sopenharmony_ciprivate: 711cb0ef41Sopenharmony_ci std::vector<std::unique_ptr<T>> m_vector; 721cb0ef41Sopenharmony_ci}; 731cb0ef41Sopenharmony_ci 741cb0ef41Sopenharmony_citemplate<typename T> 751cb0ef41Sopenharmony_ciclass ArrayBase { 761cb0ef41Sopenharmony_cipublic: 771cb0ef41Sopenharmony_ci static std::unique_ptr<Array<T>> create() 781cb0ef41Sopenharmony_ci { 791cb0ef41Sopenharmony_ci return std::unique_ptr<Array<T>>(new Array<T>()); 801cb0ef41Sopenharmony_ci } 811cb0ef41Sopenharmony_ci 821cb0ef41Sopenharmony_ci static std::unique_ptr<Array<T>> fromValue(protocol::Value* value, ErrorSupport* errors) 831cb0ef41Sopenharmony_ci { 841cb0ef41Sopenharmony_ci protocol::ListValue* array = ListValue::cast(value); 851cb0ef41Sopenharmony_ci if (!array) { 861cb0ef41Sopenharmony_ci errors->addError("array expected"); 871cb0ef41Sopenharmony_ci return nullptr; 881cb0ef41Sopenharmony_ci } 891cb0ef41Sopenharmony_ci errors->push(); 901cb0ef41Sopenharmony_ci std::unique_ptr<Array<T>> result(new Array<T>()); 911cb0ef41Sopenharmony_ci for (size_t i = 0; i < array->size(); ++i) { 921cb0ef41Sopenharmony_ci errors->setName(StringUtil::fromInteger(i)); 931cb0ef41Sopenharmony_ci T item = ValueConversions<T>::fromValue(array->at(i), errors); 941cb0ef41Sopenharmony_ci result->m_vector.push_back(item); 951cb0ef41Sopenharmony_ci } 961cb0ef41Sopenharmony_ci errors->pop(); 971cb0ef41Sopenharmony_ci if (errors->hasErrors()) 981cb0ef41Sopenharmony_ci return nullptr; 991cb0ef41Sopenharmony_ci return result; 1001cb0ef41Sopenharmony_ci } 1011cb0ef41Sopenharmony_ci 1021cb0ef41Sopenharmony_ci void addItem(const T& value) 1031cb0ef41Sopenharmony_ci { 1041cb0ef41Sopenharmony_ci m_vector.push_back(value); 1051cb0ef41Sopenharmony_ci } 1061cb0ef41Sopenharmony_ci 1071cb0ef41Sopenharmony_ci size_t length() 1081cb0ef41Sopenharmony_ci { 1091cb0ef41Sopenharmony_ci return m_vector.size(); 1101cb0ef41Sopenharmony_ci } 1111cb0ef41Sopenharmony_ci 1121cb0ef41Sopenharmony_ci T get(size_t index) 1131cb0ef41Sopenharmony_ci { 1141cb0ef41Sopenharmony_ci return m_vector[index]; 1151cb0ef41Sopenharmony_ci } 1161cb0ef41Sopenharmony_ci 1171cb0ef41Sopenharmony_ci std::unique_ptr<protocol::ListValue> toValue() 1181cb0ef41Sopenharmony_ci { 1191cb0ef41Sopenharmony_ci std::unique_ptr<protocol::ListValue> result = ListValue::create(); 1201cb0ef41Sopenharmony_ci for (auto& item : m_vector) 1211cb0ef41Sopenharmony_ci result->pushValue(ValueConversions<T>::toValue(item)); 1221cb0ef41Sopenharmony_ci return result; 1231cb0ef41Sopenharmony_ci } 1241cb0ef41Sopenharmony_ci 1251cb0ef41Sopenharmony_ciprivate: 1261cb0ef41Sopenharmony_ci std::vector<T> m_vector; 1271cb0ef41Sopenharmony_ci}; 1281cb0ef41Sopenharmony_ci 1291cb0ef41Sopenharmony_citemplate<> class Array<String> : public ArrayBase<String> {}; 1301cb0ef41Sopenharmony_citemplate<> class Array<int> : public ArrayBase<int> {}; 1311cb0ef41Sopenharmony_citemplate<> class Array<double> : public ArrayBase<double> {}; 1321cb0ef41Sopenharmony_citemplate<> class Array<bool> : public ArrayBase<bool> {}; 1331cb0ef41Sopenharmony_ci 1341cb0ef41Sopenharmony_ci{% for namespace in config.protocol.namespace %} 1351cb0ef41Sopenharmony_ci} // namespace {{namespace}} 1361cb0ef41Sopenharmony_ci{% endfor %} 1371cb0ef41Sopenharmony_ci 1381cb0ef41Sopenharmony_ci#endif // !defined({{"_".join(config.protocol.namespace)}}_Array_h) 139