| 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_HELIUM_SERIALIZABLE_STRUCT_H_ | |
| 6 #define BLIMP_HELIUM_SERIALIZABLE_STRUCT_H_ | |
| 7 | |
| 8 #include <memory> | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "blimp/helium/blimp_helium_export.h" | |
| 12 #include "blimp/helium/serializable.h" | |
| 13 #include "blimp/helium/stream_helpers.h" | |
| 14 #include "blimp/helium/syncable.h" | |
| 15 #include "third_party/protobuf/src/google/protobuf/io/coded_stream.h" | |
| 16 | |
| 17 namespace blimp { | |
| 18 namespace helium { | |
| 19 | |
| 20 // SerializableStruct is a base class which manages registration, serialization, | |
| 21 // and deserialization of its contents. Optional values are not permitted; | |
| 22 // SerializableStructs are therefore emitted/parsed in their entirety. | |
| 23 // | |
| 24 // Example usage: | |
| 25 // | |
| 26 // struct MyStruct : public SerializableStruct { | |
| 27 // MyStruct() : some_str(this, "initial value!"), some_int(this) {} | |
| 28 // Field<std::string> some_str; | |
| 29 // Field<int> some_int; | |
| 30 // }; | |
| 31 // | |
| 32 // MyStruct s; | |
| 33 // s.some_str.Set("foo"); | |
| 34 // s.some_int.Set(123); | |
| 35 class BLIMP_HELIUM_EXPORT SerializableStruct : public Serializable { | |
| 36 public: | |
| 37 // Provides template type-agnostic methods to SerializableStruct. | |
| 38 class FieldBase : public Serializable { | |
| 39 public: | |
| 40 FieldBase(); | |
| 41 ~FieldBase(); | |
| 42 | |
| 43 void SetLocalUpdateCallback(const base::Closure& local_update_callback); | |
| 44 | |
| 45 void RegisterWith(SerializableStruct* container); | |
| 46 | |
| 47 private: | |
| 48 base::Closure local_update_callback_; | |
| 49 }; | |
| 50 | |
| 51 // Holds arbitrarily-typed Serializable values and registers itself with the | |
| 52 // SerializableStruct. | |
| 53 template <typename FieldType> | |
| 54 class Field : public FieldBase { | |
| 55 public: | |
| 56 Field() = delete; | |
| 57 | |
| 58 explicit Field(SerializableStruct* changeset, const FieldType& value = {}) | |
| 59 : value_(value) { | |
| 60 DCHECK(changeset); | |
| 61 RegisterWith(changeset); | |
| 62 } | |
| 63 | |
| 64 ~Field() {} | |
| 65 | |
| 66 void Set(const FieldType& value); | |
| 67 const FieldType& operator()() const; | |
| 68 | |
| 69 // Serializable implementation. | |
| 70 void Serialize( | |
| 71 google::protobuf::io::CodedOutputStream* output_stream) const override; | |
| 72 bool Parse(google::protobuf::io::CodedInputStream* input_stream) override; | |
| 73 | |
| 74 private: | |
| 75 FieldType value_; | |
| 76 | |
| 77 DISALLOW_COPY_AND_ASSIGN(Field); | |
| 78 }; | |
| 79 | |
| 80 SerializableStruct(); | |
| 81 virtual ~SerializableStruct(); | |
| 82 | |
| 83 void SetLocalUpdateCallback(const base::Closure& local_update_callback); | |
| 84 | |
| 85 // Serializable implementation. | |
| 86 void Serialize( | |
| 87 google::protobuf::io::CodedOutputStream* output_stream) const override; | |
| 88 bool Parse(google::protobuf::io::CodedInputStream* input_stream) override; | |
| 89 | |
| 90 private: | |
| 91 friend class FieldBase; | |
| 92 | |
| 93 std::vector<FieldBase*> fields_; | |
| 94 | |
| 95 DISALLOW_COPY_AND_ASSIGN(SerializableStruct); | |
| 96 }; | |
| 97 | |
| 98 template <typename FieldType> | |
| 99 void SerializableStruct::Field<FieldType>::Set(const FieldType& value) { | |
| 100 value_ = value; | |
| 101 } | |
| 102 | |
| 103 template <typename FieldType> | |
| 104 const FieldType& SerializableStruct::Field<FieldType>::operator()() const { | |
| 105 return value_; | |
| 106 } | |
| 107 | |
| 108 template <typename FieldType> | |
| 109 void SerializableStruct::Field<FieldType>::Serialize( | |
| 110 google::protobuf::io::CodedOutputStream* output_stream) const { | |
| 111 DCHECK(output_stream); | |
| 112 WriteToStream(value_, output_stream); | |
| 113 } | |
| 114 | |
| 115 template <typename FieldType> | |
| 116 bool SerializableStruct::Field<FieldType>::Parse( | |
| 117 google::protobuf::io::CodedInputStream* input_stream) { | |
| 118 DCHECK(input_stream); | |
| 119 return ReadFromStream(input_stream, &value_); | |
| 120 } | |
| 121 | |
| 122 } // namespace helium | |
| 123 } // namespace blimp | |
| 124 | |
| 125 #endif // BLIMP_HELIUM_SERIALIZABLE_STRUCT_H_ | |
| OLD | NEW |