| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "blimp/helium/syncable_primitive_serializer.h" |
| 6 |
| 7 #include <stdint.h> |
| 8 |
| 9 #include <string> |
| 10 |
| 11 #include "third_party/protobuf/src/google/protobuf/io/coded_stream.h" |
| 12 |
| 13 namespace blimp { |
| 14 namespace helium { |
| 15 namespace { |
| 16 constexpr uint32_t kMaxStringLength = (10 << 20); // 10MB |
| 17 } |
| 18 |
| 19 // ----------------------------- |
| 20 // int32_t serialization methods |
| 21 |
| 22 void SyncablePrimitiveSerializer::Serialize( |
| 23 const int32_t& value, |
| 24 google::protobuf::io::CodedOutputStream* output_stream) { |
| 25 output_stream->WriteVarint32(static_cast<uint32_t>(value)); |
| 26 } |
| 27 |
| 28 bool SyncablePrimitiveSerializer::Deserialize( |
| 29 google::protobuf::io::CodedInputStream* input_stream, |
| 30 int32_t* value) { |
| 31 return input_stream->ReadVarint32(reinterpret_cast<uint32_t*>(value)); |
| 32 } |
| 33 |
| 34 // --------------------------------- |
| 35 // std::string serialization methods |
| 36 |
| 37 void SyncablePrimitiveSerializer::Serialize( |
| 38 const std::string& value, |
| 39 google::protobuf::io::CodedOutputStream* output_stream) { |
| 40 output_stream->WriteVarint32(value.length()); |
| 41 output_stream->WriteString(value); |
| 42 } |
| 43 |
| 44 bool SyncablePrimitiveSerializer::Deserialize( |
| 45 google::protobuf::io::CodedInputStream* input_stream, |
| 46 std::string* value) { |
| 47 uint32_t length; |
| 48 if (input_stream->ReadVarint32(&length) && length < kMaxStringLength) { |
| 49 return input_stream->ReadString(value, length); |
| 50 } |
| 51 return false; |
| 52 } |
| 53 |
| 54 } // namespace helium |
| 55 } // namespace blimp |
| OLD | NEW |