Chromium Code Reviews| 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/coded_value_serializer.h" | |
| 6 | |
| 7 #include <stdint.h> | |
| 8 | |
| 9 #include <string> | |
| 10 | |
| 11 #include "base/logging.h" | |
| 12 #include "blimp/helium/version_vector.h" | |
| 13 #include "third_party/protobuf/src/google/protobuf/io/coded_stream.h" | |
| 14 | |
| 15 namespace blimp { | |
| 16 namespace helium { | |
| 17 namespace { | |
| 18 constexpr uint32_t kMaxStringLength = (10 << 20); // 10MB | |
|
Kevin M
2016/10/20 17:11:22
Add a comment saying this constant is arbitrary an
steimel
2016/10/20 17:20:41
Done.
| |
| 19 } | |
| 20 | |
| 21 // ----------------------------- | |
| 22 // int32_t serialization methods | |
| 23 | |
| 24 void CodedValueSerializer::Serialize( | |
| 25 const int32_t& value, | |
| 26 google::protobuf::io::CodedOutputStream* output_stream) { | |
| 27 output_stream->WriteVarint32(static_cast<uint32_t>(value)); | |
| 28 } | |
| 29 | |
| 30 bool CodedValueSerializer::Deserialize( | |
| 31 google::protobuf::io::CodedInputStream* input_stream, | |
| 32 int32_t* value) { | |
| 33 return input_stream->ReadVarint32(reinterpret_cast<uint32_t*>(value)); | |
| 34 } | |
| 35 | |
| 36 // --------------------------------- | |
| 37 // std::string serialization methods | |
| 38 | |
| 39 void CodedValueSerializer::Serialize( | |
| 40 const std::string& value, | |
| 41 google::protobuf::io::CodedOutputStream* output_stream) { | |
| 42 DCHECK_LT(value.length(), kMaxStringLength); | |
| 43 output_stream->WriteVarint32(value.length()); | |
| 44 output_stream->WriteString(value); | |
| 45 } | |
| 46 | |
| 47 bool CodedValueSerializer::Deserialize( | |
| 48 google::protobuf::io::CodedInputStream* input_stream, | |
| 49 std::string* value) { | |
| 50 uint32_t length; | |
| 51 if (input_stream->ReadVarint32(&length) && length < kMaxStringLength) { | |
| 52 return input_stream->ReadString(value, length); | |
| 53 } | |
| 54 return false; | |
| 55 } | |
| 56 | |
| 57 // ----------------------------------- | |
| 58 // VersionVector serialization methods | |
| 59 | |
| 60 void CodedValueSerializer::Serialize( | |
| 61 const VersionVector& value, | |
| 62 google::protobuf::io::CodedOutputStream* output_stream) { | |
| 63 output_stream->WriteVarint64(value.local_revision()); | |
| 64 output_stream->WriteVarint64(value.remote_revision()); | |
| 65 } | |
| 66 | |
| 67 bool CodedValueSerializer::Deserialize( | |
| 68 google::protobuf::io::CodedInputStream* input_stream, | |
| 69 VersionVector* value) { | |
| 70 Revision local_revision; | |
| 71 Revision remote_revision; | |
| 72 if (!input_stream->ReadVarint64(&local_revision) || | |
| 73 !input_stream->ReadVarint64(&remote_revision)) { | |
| 74 return false; | |
| 75 } | |
| 76 value->set_local_revision(local_revision); | |
| 77 value->set_remote_revision(remote_revision); | |
| 78 return true; | |
| 79 } | |
| 80 | |
| 81 } // namespace helium | |
| 82 } // namespace blimp | |
| OLD | NEW |