Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // 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.
| |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef BLIMP_NET_HELIUM_SYNCABLE_PRIMITIVE_SERIALIZER_H_ | |
| 6 #define BLIMP_NET_HELIUM_SYNCABLE_PRIMITIVE_SERIALIZER_H_ | |
| 7 | |
| 8 #include <string> | |
| 9 | |
| 10 #include "third_party/protobuf/src/google/protobuf/io/coded_stream.h" | |
| 11 #include "third_party/protobuf/src/google/protobuf/io/zero_copy_stream_impl_lite .h" | |
| 12 | |
| 13 namespace blimp { | |
| 14 | |
| 15 // Syncable CRDTs need to serialize their data into CodedOutputStreams. This | |
| 16 // helper class centralizes that logic into two templated functions, one for | |
| 17 // serialization and one for deserialization that can be called for any | |
| 18 // primitive type. | |
| 19 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.
| |
| 20 class SyncablePrimitiveSerializer { | |
| 21 public: | |
| 22 static void SerializeSyncablePrimitive( | |
| 23 RegisterType& value, | |
| 24 google::protobuf::io::ZeroCopyOutputStream* output_stream); | |
| 25 static void DeserializeSyncablePrimitive( | |
|
Kevin M
2016/10/13 22:03:36
Should this return a bool?
steimel
2016/10/17 21:46:00
Done.
| |
| 26 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.
| |
| 27 google::protobuf::io::ZeroCopyInputStream* input_stream); | |
| 28 }; | |
| 29 | |
| 30 // For each type that needs to be (de)serialized, override the template default | |
| 31 // with a custom implementation. | |
| 32 template <> | |
| 33 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.
| |
| 34 int32_t& value, | |
| 35 google::protobuf::io::ZeroCopyOutputStream* output_stream) { | |
| 36 google::protobuf::io::CodedOutputStream(output_stream) | |
| 37 .WriteVarint32((uint32_t)value); | |
| 38 } | |
| 39 template <> | |
| 40 void SyncablePrimitiveSerializer<int32_t>::DeserializeSyncablePrimitive( | |
| 41 int32_t& value, | |
| 42 google::protobuf::io::ZeroCopyInputStream* input_stream) { | |
| 43 google::protobuf::io::CodedInputStream(input_stream) | |
| 44 .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
| |
| 45 } | |
| 46 | |
| 47 template <> | |
| 48 void SyncablePrimitiveSerializer<std::string>::SerializeSyncablePrimitive( | |
| 49 std::string& value, | |
| 50 google::protobuf::io::ZeroCopyOutputStream* output_stream) { | |
| 51 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.
| |
| 52 coded_output_stream.WriteVarint32(value.length()); | |
| 53 coded_output_stream.WriteString(value); | |
| 54 } | |
| 55 template <> | |
| 56 void SyncablePrimitiveSerializer<std::string>::DeserializeSyncablePrimitive( | |
| 57 std::string& value, | |
| 58 google::protobuf::io::ZeroCopyInputStream* input_stream) { | |
| 59 google::protobuf::io::CodedInputStream coded_input_stream(input_stream); | |
| 60 uint32_t length; | |
| 61 coded_input_stream.ReadVarint32(&length); | |
|
scf
2016/10/12 23:05:12
add check
| |
| 62 coded_input_stream.ReadString(&value, length); | |
| 63 } | |
| 64 | |
| 65 } // namespace blimp | |
| 66 | |
| 67 #endif // BLIMP_NET_HELIUM_SYNCABLE_PRIMITIVE_SERIALIZER_H_ | |
| OLD | NEW |