1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
// This file is generated by ValueConversions_cpp.template.
// Copyright 2020 The Chromium Authors
// 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 <algorithm>
#include <climits>
#include <string>
//#include "ValueConversions.h"
//#include "Values.h"
{% for namespace in config.protocol.namespace %}
namespace {{namespace}} {
{% endfor %}
{% for namespace in config.protocol.namespace %}
} // namespace
{% 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<Value> 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<const uint16_t*>(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<std::unique_ptr<Value>>::Deserialize(
DeserializerState* state, std::unique_ptr<Value>* value) {
auto result = ReadValue(state);
if (!result)
return false;
*value = std::move(result);
return true;
}
// static
void ProtocolTypeTraits<std::unique_ptr<Value>>::Serialize(
const std::unique_ptr<Value>& value, std::vector<uint8_t>* bytes) {
value->AppendSerialized(bytes);
}
// static
bool ProtocolTypeTraits<std::unique_ptr<DictionaryValue>>::Deserialize(
DeserializerState* state, std::unique_ptr<DictionaryValue>* value) {
std::unique_ptr<Value> res;
if (!ProtocolTypeTraits<std::unique_ptr<Value>>::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<std::unique_ptr<DictionaryValue>>::Serialize(
const std::unique_ptr<DictionaryValue>& value, std::vector<uint8_t>* bytes) {
value->AppendSerialized(bytes);
}
// static
bool ProtocolTypeTraits<std::unique_ptr<Object>>::Deserialize(DeserializerState* state, std::unique_ptr<Object>* value) {
auto res = DictionaryValue::create();
if (ProtocolTypeTraits<std::unique_ptr<DictionaryValue>>::Deserialize(state, &res)) {
*value = std::make_unique<Object>(std::move(res));
return true;
}
return false;
}
void ProtocolTypeTraits<std::unique_ptr<Object>>::Serialize(const std::unique_ptr<Object>& value, std::vector<uint8_t>* bytes) {
value->AppendSerialized(bytes);
}
} // namespace {{config.crdtp.namespace}}
|