Chromium Code Reviews| Index: blimp/net/helium/lww_register_serializer.h |
| diff --git a/blimp/net/helium/lww_register_serializer.h b/blimp/net/helium/lww_register_serializer.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c1329ba92065b350d221f4dd7958afd9195af768 |
| --- /dev/null |
| +++ b/blimp/net/helium/lww_register_serializer.h |
| @@ -0,0 +1,71 @@ |
| +// 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. |
| + |
| +#ifndef BLIMP_NET_HELIUM_LWW_REGISTER_SERIALIZER_H_ |
| +#define BLIMP_NET_HELIUM_LWW_REGISTER_SERIALIZER_H_ |
| + |
| +#include "base/logging.h" |
| +#include "blimp/common/proto/helium.pb.h" |
| + |
| +namespace blimp { |
| + |
| +// Each type of LwwRegister has it's own way of serializing and deserializing |
| +// data into/from the LwwRegisterChangesetMessage. This template class extracts |
| +// that logic out into this file. |
| +template <class RegisterType> |
| +class LwwRegisterSerializer { |
| + public: |
| + static void SerializeLwwRegister( |
| + RegisterType& state, |
| + proto::LwwRegisterChangesetMessage* changeset); |
| + static void DeserializeLwwRegister( |
| + RegisterType& state, |
| + proto::LwwRegisterChangesetMessage* changeset); |
| +}; |
| + |
| +// NOTREACHED for any unsupported types. |
| +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.
|
| +void LwwRegisterSerializer<RegisterType>::SerializeLwwRegister( |
| + RegisterType& state, |
| + proto::LwwRegisterChangesetMessage* changeset) { |
| + NOTREACHED() << "Serializing unsupported LwwRegister type."; |
| +} |
| +template <class RegisterType> |
| +void LwwRegisterSerializer<RegisterType>::DeserializeLwwRegister( |
| + RegisterType& state, |
| + proto::LwwRegisterChangesetMessage* changeset) { |
| + NOTREACHED() << "Deserializing unsupported LwwRegister type."; |
| +} |
| + |
| +// For each type that needs to be (de)serialized, override the template default |
| +// with a custom implementation. |
| +template <> |
| +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
|
| + int32_t& state, |
| + proto::LwwRegisterChangesetMessage* changeset) { |
| + changeset->set_integer_value(state); |
| +} |
| +template <> |
| +void LwwRegisterSerializer<int32_t>::DeserializeLwwRegister( |
| + int32_t& state, |
| + proto::LwwRegisterChangesetMessage* changeset) { |
| + state = changeset->integer_value(); |
| +} |
| + |
| +template <> |
| +void LwwRegisterSerializer<std::string>::SerializeLwwRegister( |
| + std::string& state, |
| + proto::LwwRegisterChangesetMessage* changeset) { |
| + changeset->set_string_value(state); |
| +} |
| +template <> |
| +void LwwRegisterSerializer<std::string>::DeserializeLwwRegister( |
| + std::string& state, |
| + proto::LwwRegisterChangesetMessage* changeset) { |
| + state = changeset->string_value(); |
| +} |
| + |
| +} // namespace blimp |
| + |
| +#endif // BLIMP_NET_HELIUM_LWW_REGISTER_SERIALIZER_H_ |