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 #ifndef BLIMP_NET_HELIUM_LWW_REGISTER_SERIALIZER_H_ | |
| 6 #define BLIMP_NET_HELIUM_LWW_REGISTER_SERIALIZER_H_ | |
| 7 | |
| 8 #include "base/logging.h" | |
| 9 #include "blimp/common/proto/helium.pb.h" | |
| 10 | |
| 11 namespace blimp { | |
| 12 | |
| 13 // Each type of LwwRegister has it's own way of serializing and deserializing | |
| 14 // data into/from the LwwRegisterChangesetMessage. This template class extracts | |
| 15 // that logic out into this file. | |
| 16 template <class RegisterType> | |
| 17 class LwwRegisterSerializer { | |
| 18 public: | |
| 19 static void SerializeLwwRegister( | |
| 20 RegisterType& state, | |
| 21 proto::LwwRegisterChangesetMessage* changeset); | |
| 22 static void DeserializeLwwRegister( | |
| 23 RegisterType& state, | |
| 24 proto::LwwRegisterChangesetMessage* changeset); | |
| 25 }; | |
| 26 | |
| 27 // NOTREACHED for any unsupported types. | |
| 28 template <class RegisterType> | |
|
scf
2016/10/10 19:45:47
I prefer removing this code so you get a compiler
Kevin M
2016/10/10 20:50:21
+1
steimel
2016/10/11 16:39:17
Done.
| |
| 29 void LwwRegisterSerializer<RegisterType>::SerializeLwwRegister( | |
| 30 RegisterType& state, | |
| 31 proto::LwwRegisterChangesetMessage* changeset) { | |
| 32 NOTREACHED() << "Serializing unsupported LwwRegister type."; | |
| 33 } | |
| 34 template <class RegisterType> | |
| 35 void LwwRegisterSerializer<RegisterType>::DeserializeLwwRegister( | |
| 36 RegisterType& state, | |
| 37 proto::LwwRegisterChangesetMessage* changeset) { | |
| 38 NOTREACHED() << "Deserializing unsupported LwwRegister type."; | |
| 39 } | |
| 40 | |
| 41 // For each type that needs to be (de)serialized, override the template default | |
| 42 // with a custom implementation. | |
| 43 template <> | |
| 44 void LwwRegisterSerializer<int32_t>::SerializeLwwRegister( | |
|
Kevin M
2016/10/10 20:50:21
Since we'll be serializing primitive data types al
steimel
2016/10/11 16:39:17
Gotcha. I'll do that when I make the change to Cod
| |
| 45 int32_t& state, | |
| 46 proto::LwwRegisterChangesetMessage* changeset) { | |
| 47 changeset->set_integer_value(state); | |
| 48 } | |
| 49 template <> | |
| 50 void LwwRegisterSerializer<int32_t>::DeserializeLwwRegister( | |
| 51 int32_t& state, | |
| 52 proto::LwwRegisterChangesetMessage* changeset) { | |
| 53 state = changeset->integer_value(); | |
| 54 } | |
| 55 | |
| 56 template <> | |
| 57 void LwwRegisterSerializer<std::string>::SerializeLwwRegister( | |
| 58 std::string& state, | |
| 59 proto::LwwRegisterChangesetMessage* changeset) { | |
| 60 changeset->set_string_value(state); | |
| 61 } | |
| 62 template <> | |
| 63 void LwwRegisterSerializer<std::string>::DeserializeLwwRegister( | |
| 64 std::string& state, | |
| 65 proto::LwwRegisterChangesetMessage* changeset) { | |
| 66 state = changeset->string_value(); | |
| 67 } | |
| 68 | |
| 69 } // namespace blimp | |
| 70 | |
| 71 #endif // BLIMP_NET_HELIUM_LWW_REGISTER_SERIALIZER_H_ | |
| OLD | NEW |