Chromium Code Reviews| Index: blimp/net/helium/syncable_primitive_serializer.h |
| diff --git a/blimp/net/helium/syncable_primitive_serializer.h b/blimp/net/helium/syncable_primitive_serializer.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..31130b275aa13940f30fdb7c3d4de627ec00c592 |
| --- /dev/null |
| +++ b/blimp/net/helium/syncable_primitive_serializer.h |
| @@ -0,0 +1,67 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
|
Kevin M
2016/10/14 17:12:35
Can you add unit tests for every supported datatyp
steimel
2016/10/17 21:46:00
Done.
|
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef BLIMP_NET_HELIUM_SYNCABLE_PRIMITIVE_SERIALIZER_H_ |
| +#define BLIMP_NET_HELIUM_SYNCABLE_PRIMITIVE_SERIALIZER_H_ |
| + |
| +#include <string> |
| + |
| +#include "third_party/protobuf/src/google/protobuf/io/coded_stream.h" |
| +#include "third_party/protobuf/src/google/protobuf/io/zero_copy_stream_impl_lite.h" |
| + |
| +namespace blimp { |
| + |
| +// Syncable CRDTs need to serialize their data into CodedOutputStreams. This |
| +// helper class centralizes that logic into two templated functions, one for |
| +// serialization and one for deserialization that can be called for any |
| +// primitive type. |
| +template <class RegisterType> |
|
Kevin M
2016/10/13 22:03:36
Recommend making these standalone templated functi
steimel
2016/10/17 21:46:00
Discussed offline. Changing to overloaded instead.
|
| +class SyncablePrimitiveSerializer { |
| + public: |
| + static void SerializeSyncablePrimitive( |
| + RegisterType& value, |
| + google::protobuf::io::ZeroCopyOutputStream* output_stream); |
| + static void DeserializeSyncablePrimitive( |
|
Kevin M
2016/10/13 22:03:36
Should this return a bool?
steimel
2016/10/17 21:46:00
Done.
|
| + RegisterType& value, |
|
Kevin M
2016/10/13 22:03:36
Output parameters should be pointers and located a
steimel
2016/10/17 21:46:00
Done.
|
| + google::protobuf::io::ZeroCopyInputStream* input_stream); |
| +}; |
| + |
| +// For each type that needs to be (de)serialized, override the template default |
| +// with a custom implementation. |
| +template <> |
| +void SyncablePrimitiveSerializer<int32_t>::SerializeSyncablePrimitive( |
|
CJ
2016/10/12 22:32:44
Pretty sure we could simplify these function names
steimel
2016/10/17 21:46:00
Done.
|
| + int32_t& value, |
| + google::protobuf::io::ZeroCopyOutputStream* output_stream) { |
| + google::protobuf::io::CodedOutputStream(output_stream) |
| + .WriteVarint32((uint32_t)value); |
| +} |
| +template <> |
| +void SyncablePrimitiveSerializer<int32_t>::DeserializeSyncablePrimitive( |
| + int32_t& value, |
| + google::protobuf::io::ZeroCopyInputStream* input_stream) { |
| + google::protobuf::io::CodedInputStream(input_stream) |
| + .ReadVarint32((uint32_t*)&value); |
|
scf
2016/10/12 23:05:12
add check?
steimel
2016/10/17 21:46:00
Discussed with Kevin offline. Returning bool inste
|
| +} |
| + |
| +template <> |
| +void SyncablePrimitiveSerializer<std::string>::SerializeSyncablePrimitive( |
| + std::string& value, |
| + google::protobuf::io::ZeroCopyOutputStream* output_stream) { |
| + google::protobuf::io::CodedOutputStream coded_output_stream(output_stream); |
|
Kevin M
2016/10/13 22:03:36
These should take CodedStreams as parameters (ther
steimel
2016/10/17 21:46:00
Done.
|
| + coded_output_stream.WriteVarint32(value.length()); |
| + coded_output_stream.WriteString(value); |
| +} |
| +template <> |
| +void SyncablePrimitiveSerializer<std::string>::DeserializeSyncablePrimitive( |
| + std::string& value, |
| + google::protobuf::io::ZeroCopyInputStream* input_stream) { |
| + google::protobuf::io::CodedInputStream coded_input_stream(input_stream); |
| + uint32_t length; |
| + coded_input_stream.ReadVarint32(&length); |
|
scf
2016/10/12 23:05:12
add check
|
| + coded_input_stream.ReadString(&value, length); |
| +} |
| + |
| +} // namespace blimp |
| + |
| +#endif // BLIMP_NET_HELIUM_SYNCABLE_PRIMITIVE_SERIALIZER_H_ |