Chromium Code Reviews| Index: blimp/helium/syncable_primitive_serializer.cc |
| diff --git a/blimp/helium/syncable_primitive_serializer.cc b/blimp/helium/syncable_primitive_serializer.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e48b2da387bd9c695d0b134e109c84a7fb3a02f1 |
| --- /dev/null |
| +++ b/blimp/helium/syncable_primitive_serializer.cc |
| @@ -0,0 +1,82 @@ |
| +// Copyright 2016 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 "blimp/helium/syncable_primitive_serializer.h" |
| + |
| +#include <stdint.h> |
| + |
| +#include <string> |
| + |
| +#include "base/logging.h" |
| +#include "blimp/helium/version_vector.h" |
| +#include "third_party/protobuf/src/google/protobuf/io/coded_stream.h" |
| + |
| +namespace blimp { |
| +namespace helium { |
| +namespace { |
| +constexpr uint32_t kMaxStringLength = (10 << 20); // 10MB |
| +} |
| + |
| +// ----------------------------- |
| +// int32_t serialization methods |
| + |
| +void SyncablePrimitiveSerializer::Serialize( |
| + const int32_t& value, |
| + google::protobuf::io::CodedOutputStream* output_stream) { |
| + output_stream->WriteVarint32(static_cast<uint32_t>(value)); |
| +} |
| + |
| +bool SyncablePrimitiveSerializer::Deserialize( |
| + google::protobuf::io::CodedInputStream* input_stream, |
| + int32_t* value) { |
| + return input_stream->ReadVarint32(reinterpret_cast<uint32_t*>(value)); |
| +} |
| + |
| +// --------------------------------- |
| +// std::string serialization methods |
| + |
| +void SyncablePrimitiveSerializer::Serialize( |
| + const std::string& value, |
| + google::protobuf::io::CodedOutputStream* output_stream) { |
| + DCHECK(value.length() < kMaxStringLength); |
|
Kevin M
2016/10/20 00:38:24
DCHECK_LT
steimel
2016/10/20 00:57:38
Done.
|
| + output_stream->WriteVarint32(value.length()); |
| + output_stream->WriteString(value); |
| +} |
| + |
| +bool SyncablePrimitiveSerializer::Deserialize( |
| + google::protobuf::io::CodedInputStream* input_stream, |
| + std::string* value) { |
| + uint32_t length; |
| + if (input_stream->ReadVarint32(&length) && length < kMaxStringLength) { |
| + return input_stream->ReadString(value, length); |
| + } |
| + return false; |
| +} |
| + |
| +// ------ |
|
Kevin M
2016/10/20 00:38:24
Moar dashes?
steimel
2016/10/20 00:57:38
Done.
|
| +// VersionVector serialization methods |
| + |
| +void SyncablePrimitiveSerializer::Serialize( |
| + const VersionVector& value, |
| + google::protobuf::io::CodedOutputStream* output_stream) { |
| + output_stream->WriteVarint64(value.local_revision()); |
| + output_stream->WriteVarint64(value.remote_revision()); |
| +} |
| + |
| +bool SyncablePrimitiveSerializer::Deserialize( |
| + google::protobuf::io::CodedInputStream* input_stream, |
| + VersionVector* value) { |
| + Revision local_revision; |
| + Revision remote_revision; |
| + if (input_stream->ReadVarint64(&local_revision) && |
| + input_stream->ReadVarint64(&remote_revision)) { |
| + value->set_local_revision(local_revision); |
| + value->set_remote_revision(remote_revision); |
| + return true; |
|
Kevin M
2016/10/20 00:38:24
Can you flip the conditional so that "return true"
steimel
2016/10/20 00:57:38
Done.
|
| + } |
| + return false; |
| +} |
| + |
| +} // namespace helium |
| +} // namespace blimp |