OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef SYNC_INTERNAL_API_PUBLIC_UTIL_PROTO_VALUE_PTR_H_ | 5 #ifndef SYNC_INTERNAL_API_PUBLIC_UTIL_PROTO_VALUE_PTR_H_ |
6 #define SYNC_INTERNAL_API_PUBLIC_UTIL_PROTO_VALUE_PTR_H_ | 6 #define SYNC_INTERNAL_API_PUBLIC_UTIL_PROTO_VALUE_PTR_H_ |
7 | 7 |
8 #include "base/gtest_prod_util.h" | 8 #include "base/gtest_prod_util.h" |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 | 10 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 // Copying ProtoValuePtr results in ref-counted sharing of the | 46 // Copying ProtoValuePtr results in ref-counted sharing of the |
47 // underlying wrapper and the value contained in the wrapper. | 47 // underlying wrapper and the value contained in the wrapper. |
48 // | 48 // |
49 // The public interface includes only immutable access to the wrapped value. | 49 // The public interface includes only immutable access to the wrapped value. |
50 // The only way to assign a value to ProtoValuePtr is through a | 50 // The only way to assign a value to ProtoValuePtr is through a |
51 // private SetValue function which is called from EntryKernel. That results | 51 // private SetValue function which is called from EntryKernel. That results |
52 // in stopping sharing the previous value and creating a wrapper to the new | 52 // in stopping sharing the previous value and creating a wrapper to the new |
53 // value. | 53 // value. |
54 template <typename T, typename Traits = DefaultProtoValuePtrTraits<T>> | 54 template <typename T, typename Traits = DefaultProtoValuePtrTraits<T>> |
55 class ProtoValuePtr { | 55 class ProtoValuePtr { |
56 TYPE_WITH_MOVE_CONSTRUCTOR_FOR_CPP_03(ProtoValuePtr) | |
57 | |
58 private: | 56 private: |
59 // Immutable shareable ref-counted wrapper that embeds the value. | 57 // Immutable shareable ref-counted wrapper that embeds the value. |
60 class Wrapper : public base::RefCountedThreadSafe<Wrapper> { | 58 class Wrapper : public base::RefCountedThreadSafe<Wrapper> { |
61 public: | 59 public: |
62 explicit Wrapper(const T& value) { Traits::CopyValue(&value_, value); } | 60 explicit Wrapper(const T& value) { Traits::CopyValue(&value_, value); } |
63 explicit Wrapper(T* value) { Traits::SwapValue(&value_, value); } | 61 explicit Wrapper(T* value) { Traits::SwapValue(&value_, value); } |
64 | 62 |
65 const T& value() const { return value_; } | 63 const T& value() const { return value_; } |
66 // Create wrapper by deserializing a BLOB. | 64 // Create wrapper by deserializing a BLOB. |
67 static Wrapper* ParseFromBlob(const void* blob, int length) { | 65 static Wrapper* ParseFromBlob(const void* blob, int length) { |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 void load(const void* blob, int length) { | 123 void load(const void* blob, int length) { |
126 wrapper_ = Wrapper::ParseFromBlob(blob, length); | 124 wrapper_ = Wrapper::ParseFromBlob(blob, length); |
127 } | 125 } |
128 | 126 |
129 scoped_refptr<Wrapper> wrapper_; | 127 scoped_refptr<Wrapper> wrapper_; |
130 }; | 128 }; |
131 | 129 |
132 } // namespace syncer | 130 } // namespace syncer |
133 | 131 |
134 #endif // SYNC_INTERNAL_API_PUBLIC_UTIL_PROTO_VALUE_PTR_H_ | 132 #endif // SYNC_INTERNAL_API_PUBLIC_UTIL_PROTO_VALUE_PTR_H_ |
OLD | NEW |