Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(101)

Side by Side Diff: blimp/helium/lazy_syncable_adapter.h

Issue 2522633004: Add LazySyncableAdapter (Closed)
Patch Set: scf feedback Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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_LAZY_SYNCABLE_ADAPTER_H_
6 #define BLIMP_HELIUM_LAZY_SYNCABLE_ADAPTER_H_
7
8 #include <string>
9
10 #include "base/memory/ptr_util.h"
11 #include "blimp/helium/blimp_helium_export.h"
12 #include "blimp/helium/syncable.h"
13 #include "third_party/protobuf/src/google/protobuf/io/coded_stream.h"
14 #include "third_party/protobuf/src/google/protobuf/io/zero_copy_stream_impl_lite .h"
15
16 namespace blimp {
17 namespace helium {
18
19 namespace {
20 template <class ChangesetType>
21 std::unique_ptr<ChangesetType> ParseChangesetFromString(
Kevin M 2016/12/02 18:28:03 Anonymous namespaced functions in the .h aren't ac
steimel 2016/12/03 00:29:41 Good point. Done.
22 const std::string& changeset) {
23 google::protobuf::io::ArrayInputStream raw_input_stream(changeset.data(),
24 changeset.size());
25 google::protobuf::io::CodedInputStream input_stream(&raw_input_stream);
26 std::unique_ptr<ChangesetType> parsed_changeset =
27 base::MakeUnique<ChangesetType>();
28 parsed_changeset->Parse(&input_stream);
29 return parsed_changeset;
30 }
31 } // namespace
32
33 // The LazySyncableAdapter is used to wrap LazySyncables of any changeset type
34 // into a LazySyncable with a string changeset by serializing the changesets
35 // into a string and parsing them from strings. That way, ObjectSyncState can
36 // just work with LazySyncables of type string.
37 template <class ChangesetType>
38 class BLIMP_HELIUM_EXPORT LazySyncableAdapter
39 : public LazySyncable<std::string> {
40 public:
41 explicit LazySyncableAdapter(LazySyncable<ChangesetType>* inner_syncable)
42 : inner_syncable_(inner_syncable) {}
43 ~LazySyncableAdapter() = default;
44
45 // LazySyncable implementation.
46 std::unique_ptr<std::string> CreateChangeset(Revision from) const override;
47 void ApplyChangeset(const std::string& changeset) override;
48 bool ValidateChangeset(const std::string& changeset) const override;
49 void SetLocalUpdateCallback(
50 const base::Closure& local_update_callback) override;
51 void ReleaseBefore(Revision before) override;
52 Revision GetRevision() const override;
53 void PrepareToCreateChangeset(Revision from, base::Closure done) override;
54
55 private:
56 LazySyncable<ChangesetType>* inner_syncable_;
57
58 DISALLOW_COPY_AND_ASSIGN(LazySyncableAdapter);
59 };
60
61 template <class ChangesetType>
62 std::unique_ptr<std::string>
63 LazySyncableAdapter<ChangesetType>::CreateChangeset(Revision from) const {
64 std::unique_ptr<ChangesetType> changeset =
65 inner_syncable_->CreateChangeset(from);
66 std::unique_ptr<std::string> output = base::MakeUnique<std::string>();
67 google::protobuf::io::StringOutputStream raw_output_stream(output.get());
68 google::protobuf::io::CodedOutputStream output_stream(&raw_output_stream);
69 changeset->Serialize(&output_stream);
70 return output;
71 }
72
73 template <class ChangesetType>
74 void LazySyncableAdapter<ChangesetType>::ApplyChangeset(
75 const std::string& changeset) {
76 std::unique_ptr<ChangesetType> parsed_changeset =
77 ParseChangesetFromString<ChangesetType>(changeset);
78 inner_syncable_->ApplyChangeset(*parsed_changeset);
79 }
80
81 template <class ChangesetType>
82 bool LazySyncableAdapter<ChangesetType>::ValidateChangeset(
83 const std::string& changeset) const {
84 std::unique_ptr<ChangesetType> parsed_changeset =
85 ParseChangesetFromString<ChangesetType>(changeset);
86 return inner_syncable_->ValidateChangeset(*parsed_changeset);
87 }
88
89 template <class ChangesetType>
90 void LazySyncableAdapter<ChangesetType>::SetLocalUpdateCallback(
91 const base::Closure& local_update_callback) {
92 inner_syncable_->SetLocalUpdateCallback(local_update_callback);
93 }
94
95 template <class ChangesetType>
96 void LazySyncableAdapter<ChangesetType>::ReleaseBefore(Revision before) {
97 inner_syncable_->ReleaseBefore(before);
98 }
99
100 template <class ChangesetType>
101 Revision LazySyncableAdapter<ChangesetType>::GetRevision() const {
102 return inner_syncable_->GetRevision();
103 }
104
105 template <class ChangesetType>
106 void LazySyncableAdapter<ChangesetType>::PrepareToCreateChangeset(
107 Revision from,
108 base::Closure done) {
109 inner_syncable_->PrepareToCreateChangeset(from, done);
110 }
111
112 } // namespace helium
113 } // namespace blimp
114 #endif // BLIMP_HELIUM_LAZY_SYNCABLE_ADAPTER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698