// This file is generated by Array_h.template. // Copyright 2016 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef {{"_".join(config.protocol.namespace)}}_Array_h #define {{"_".join(config.protocol.namespace)}}_Array_h //#include "ErrorSupport.h" //#include "Forward.h" //#include "ValueConversions.h" //#include "Values.h" {% for namespace in config.protocol.namespace %} namespace {{namespace}} { {% endfor %} template class Array { public: static std::unique_ptr> create() { return std::unique_ptr>(new Array()); } static std::unique_ptr> fromValue(protocol::Value* value, ErrorSupport* errors) { protocol::ListValue* array = ListValue::cast(value); if (!array) { errors->addError("array expected"); return nullptr; } std::unique_ptr> result(new Array()); errors->push(); for (size_t i = 0; i < array->size(); ++i) { errors->setName(StringUtil::fromInteger(i)); std::unique_ptr item = ValueConversions::fromValue(array->at(i), errors); result->m_vector.push_back(std::move(item)); } errors->pop(); if (errors->hasErrors()) return nullptr; return result; } void addItem(std::unique_ptr value) { m_vector.push_back(std::move(value)); } size_t length() { return m_vector.size(); } T* get(size_t index) { return m_vector[index].get(); } std::unique_ptr toValue() { std::unique_ptr result = ListValue::create(); for (auto& item : m_vector) result->pushValue(ValueConversions::toValue(item)); return result; } private: std::vector> m_vector; }; template class ArrayBase { public: static std::unique_ptr> create() { return std::unique_ptr>(new Array()); } static std::unique_ptr> fromValue(protocol::Value* value, ErrorSupport* errors) { protocol::ListValue* array = ListValue::cast(value); if (!array) { errors->addError("array expected"); return nullptr; } errors->push(); std::unique_ptr> result(new Array()); for (size_t i = 0; i < array->size(); ++i) { errors->setName(StringUtil::fromInteger(i)); T item = ValueConversions::fromValue(array->at(i), errors); result->m_vector.push_back(item); } errors->pop(); if (errors->hasErrors()) return nullptr; return result; } void addItem(const T& value) { m_vector.push_back(value); } size_t length() { return m_vector.size(); } T get(size_t index) { return m_vector[index]; } std::unique_ptr toValue() { std::unique_ptr result = ListValue::create(); for (auto& item : m_vector) result->pushValue(ValueConversions::toValue(item)); return result; } private: std::vector m_vector; }; template<> class Array : public ArrayBase {}; template<> class Array : public ArrayBase {}; template<> class Array : public ArrayBase {}; template<> class Array : public ArrayBase {}; {% for namespace in config.protocol.namespace %} } // namespace {{namespace}} {% endfor %} #endif // !defined({{"_".join(config.protocol.namespace)}}_Array_h)