// This file is generated by ValueConversions_h.template. // Copyright 2016 The Chromium Authors // 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)}}_ValueConversions_h #define {{"_".join(config.protocol.namespace)}}_ValueConversions_h //#include "ErrorSupport.h" //#include "Forward.h" //#include "Values.h" {% for namespace in config.protocol.namespace %} namespace {{namespace}} { {% endfor %} template struct ValueConversions { static std::unique_ptr fromValue(protocol::Value* value, ErrorSupport* errors) { return T::fromValue(value, errors); } static std::unique_ptr toValue(T* value) { return value->toValue(); } static std::unique_ptr toValue(const std::unique_ptr& value) { return value->toValue(); } }; template<> struct ValueConversions { static bool fromValue(protocol::Value* value, ErrorSupport* errors) { bool result = false; bool success = value ? value->asBoolean(&result) : false; if (!success) errors->AddError("boolean value expected"); return result; } static std::unique_ptr toValue(bool value) { return FundamentalValue::create(value); } }; template<> struct ValueConversions { static int fromValue(protocol::Value* value, ErrorSupport* errors) { int result = 0; bool success = value ? value->asInteger(&result) : false; if (!success) errors->AddError("integer value expected"); return result; } static std::unique_ptr toValue(int value) { return FundamentalValue::create(value); } }; template<> struct ValueConversions { static double fromValue(protocol::Value* value, ErrorSupport* errors) { double result = 0; bool success = value ? value->asDouble(&result) : false; if (!success) errors->AddError("double value expected"); return result; } static std::unique_ptr toValue(double value) { return FundamentalValue::create(value); } }; template<> struct ValueConversions { static String fromValue(protocol::Value* value, ErrorSupport* errors) { String result; bool success = value ? value->asString(&result) : false; if (!success) errors->AddError("string value expected"); return result; } static std::unique_ptr toValue(const String& value) { return StringValue::create(value); } }; template<> struct ValueConversions { static Binary fromValue(protocol::Value* value, ErrorSupport* errors) { if (!value || (value->type() != Value::TypeBinary && value->type() != Value::TypeString)) { errors->AddError("Either string base64 or binary value expected"); return Binary(); } Binary binary; if (value->asBinary(&binary)) return binary; String result; value->asString(&result); bool success; Binary out = Binary::fromBase64(result, &success); if (!success) errors->AddError("base64 decoding error"); return out; } static std::unique_ptr toValue(const Binary& value) { return BinaryValue::create(value); } }; template struct ValueConversions>> { 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 std::vector>()); result->reserve(array->size()); for (size_t i = 0; i < array->size(); ++i) { errors->SetIndex(i); auto item = ValueConversions::fromValue(array->at(i), errors); result->emplace_back(std::move(item)); } errors->Pop(); if (!errors->Errors().empty()) return nullptr; return result; } static std::unique_ptr toValue(std::vector>* v) { std::unique_ptr result = ListValue::create(); result->reserve(v->size()); for (auto& item : *v) result->pushValue(ValueConversions::toValue(item.get())); return result; } }; template struct ValueConversions> { 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 std::vector()); result->reserve(array->size()); for (size_t i = 0; i < array->size(); ++i) { errors->SetIndex(i); auto item = ValueConversions::fromValue(array->at(i), errors); result->emplace_back(std::move(item)); } errors->Pop(); if (!errors->Errors().empty()) return nullptr; return result; } static std::unique_ptr toValue(std::vector* v) { std::unique_ptr result = ListValue::create(); result->reserve(v->size()); for (auto& item : *v) result->pushValue(ValueConversions::toValue(item)); return result; } }; template<> struct ValueConversions { static std::unique_ptr fromValue(protocol::Value* value, ErrorSupport* errors) { bool success = !!value; if (!success) { errors->AddError("value expected"); return nullptr; } return value->clone(); } static std::unique_ptr toValue(Value* value) { return value->clone(); } static std::unique_ptr toValue(const std::unique_ptr& value) { return value->clone(); } }; template<> struct ValueConversions { static std::unique_ptr fromValue(protocol::Value* value, ErrorSupport* errors) { bool success = value && value->type() == protocol::Value::TypeObject; if (!success) errors->AddError("object expected"); return DictionaryValue::cast(value->clone()); } static std::unique_ptr toValue(DictionaryValue* value) { return value->clone(); } static std::unique_ptr toValue(const std::unique_ptr& value) { return value->clone(); } }; template<> struct ValueConversions { static std::unique_ptr fromValue(protocol::Value* value, ErrorSupport* errors) { bool success = value && value->type() == protocol::Value::TypeArray; if (!success) errors->AddError("list expected"); return ListValue::cast(value->clone()); } static std::unique_ptr toValue(ListValue* value) { return value->clone(); } static std::unique_ptr toValue(const std::unique_ptr& value) { return value->clone(); } }; template struct ValueTypeConverter { static std::unique_ptr FromValue(const protocol::Value& value) { std::vector bytes; value.AppendSerialized(&bytes); return T::FromBinary(bytes.data(), bytes.size()); } static std::unique_ptr ToValue(const T& obj) { std::vector bytes; obj.AppendSerialized(&bytes); auto result = Value::parseBinary(bytes.data(), bytes.size()); return DictionaryValue::cast(std::move(result)); } }; {% for namespace in config.protocol.namespace %} } // namespace {{namespace}} {% endfor %} namespace {{config.crdtp.namespace}} { template struct ProtocolTypeTraits::value>::type> { static void Serialize(const {{"::".join(config.protocol.namespace)}}::Value& value, std::vector* bytes) { value.AppendSerialized(bytes); } }; template <> struct ProtocolTypeTraits> { static bool Deserialize(DeserializerState* state, std::unique_ptr<{{"::".join(config.protocol.namespace)}}::Value>* value); static void Serialize(const std::unique_ptr<{{"::".join(config.protocol.namespace)}}::Value>& value, std::vector* bytes); }; template <> struct ProtocolTypeTraits> { static bool Deserialize(DeserializerState* state, std::unique_ptr<{{"::".join(config.protocol.namespace)}}::DictionaryValue>* value); static void Serialize(const std::unique_ptr<{{"::".join(config.protocol.namespace)}}::DictionaryValue>& value, std::vector* bytes); }; // TODO(caseq): get rid of it, it's just a DictionaryValue really. template <> struct ProtocolTypeTraits> { static bool Deserialize(DeserializerState* state, std::unique_ptr<{{"::".join(config.protocol.namespace)}}::Object>* value); static void Serialize(const std::unique_ptr<{{"::".join(config.protocol.namespace)}}::Object>& value, std::vector* bytes); }; template<> struct ProtocolTypeTraits<{{"::".join(config.protocol.namespace)}}::Object> { static void Serialize(const {{"::".join(config.protocol.namespace)}}::Object& value, std::vector* bytes) { value.AppendSerialized(bytes); } }; } // namespace {{config.crdtp.namespace}} #endif // !defined({{"_".join(config.protocol.namespace)}}_ValueConversions_h)