// This file is generated by ValueConversions_cpp.template. // Copyright 2020 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. #include {{format_include(config.protocol.package, "Protocol")}} #include #include #include //#include "ValueConversions.h" //#include "Values.h" {% for namespace in config.protocol.namespace %} namespace {{namespace}} { {% endfor %} {% for namespace in config.protocol.namespace %} } // namespce {% endfor %} namespace {{config.crdtp.namespace}} { namespace { using {{"::".join(config.protocol.namespace)}}::Binary; using {{"::".join(config.protocol.namespace)}}::Object; using {{"::".join(config.protocol.namespace)}}::Value; using {{"::".join(config.protocol.namespace)}}::String; using {{"::".join(config.protocol.namespace)}}::DictionaryValue; using {{"::".join(config.protocol.namespace)}}::FundamentalValue; using {{"::".join(config.protocol.namespace)}}::StringValue; using {{"::".join(config.protocol.namespace)}}::StringUtil; //using {{"::".join(config.protocol.namespace)}}::EncodeString; std::unique_ptr ReadValue(DeserializerState* state) { cbor::CBORTokenizer* tokenizer = state->tokenizer(); switch (tokenizer->TokenTag()) { case cbor::CBORTokenTag::TRUE_VALUE: return FundamentalValue::create(true); case cbor::CBORTokenTag::FALSE_VALUE: return FundamentalValue::create(false); case cbor::CBORTokenTag::NULL_VALUE: return Value::null(); case cbor::CBORTokenTag::INT32: return FundamentalValue::create(tokenizer->GetInt32()); case cbor::CBORTokenTag::DOUBLE: return FundamentalValue::create(tokenizer->GetDouble()); case cbor::CBORTokenTag::STRING8: { const auto str = tokenizer->GetString8(); return StringValue::create(StringUtil::fromUTF8(str.data(), str.size())); } case cbor::CBORTokenTag::STRING16: { const auto str = tokenizer->GetString16WireRep(); return StringValue::create(StringUtil::fromUTF16LE(reinterpret_cast(str.data()), str.size() / 2)); } case cbor::CBORTokenTag::ENVELOPE: { const auto env = tokenizer->GetEnvelope(); return Value::parseBinary(env.data(), env.size()); } // Intentionally not supported. case cbor::CBORTokenTag::BINARY: // Should not be encountered outside of envelope. case cbor::CBORTokenTag::MAP_START: case cbor::CBORTokenTag::ARRAY_START: default: state->RegisterError(Error::CBOR_UNSUPPORTED_VALUE); return nullptr; } } } // namespace // static bool ProtocolTypeTraits>::Deserialize( DeserializerState* state, std::unique_ptr* value) { auto result = ReadValue(state); if (!result) return false; *value = std::move(result); return true; } // static void ProtocolTypeTraits>::Serialize( const std::unique_ptr& value, std::vector* bytes) { value->AppendSerialized(bytes); } // static bool ProtocolTypeTraits>::Deserialize( DeserializerState* state, std::unique_ptr* value) { std::unique_ptr res; if (!ProtocolTypeTraits>::Deserialize(state, &res)) return false; if (res->type() != Value::TypeObject) { state->RegisterError(Error::BINDINGS_DICTIONARY_VALUE_EXPECTED); return false; } *value = DictionaryValue::cast(std::move(res)); return true; } // static void ProtocolTypeTraits>::Serialize( const std::unique_ptr& value, std::vector* bytes) { value->AppendSerialized(bytes); } // static bool ProtocolTypeTraits>::Deserialize(DeserializerState* state, std::unique_ptr* value) { auto res = DictionaryValue::create(); if (ProtocolTypeTraits>::Deserialize(state, &res)) { *value = std::make_unique(std::move(res)); return true; } return false; } void ProtocolTypeTraits>::Serialize(const std::unique_ptr& value, std::vector* bytes) { value->AppendSerialized(bytes); } } // namespace {{config.crdtp.namespace}}