OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // Immutable<T> provides an easy, cheap, and thread-safe way to pass | 5 // Immutable<T> provides an easy, cheap, and thread-safe way to pass |
6 // large immutable data around. | 6 // large immutable data around. |
7 // | 7 // |
8 // For example, consider the following code: | 8 // For example, consider the following code: |
9 // | 9 // |
10 // typedef std::vector<LargeObject> LargeObjectList; | 10 // typedef std::vector<LargeObject> LargeObjectList; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 // for discussion) Immutable<T> should be able to find it. | 54 // for discussion) Immutable<T> should be able to find it. |
55 // | 55 // |
56 // Alternatively, you could explicitly control which swap function is | 56 // Alternatively, you could explicitly control which swap function is |
57 // used by providing your own traits class or using one of the | 57 // used by providing your own traits class or using one of the |
58 // pre-defined ones below. See comments on traits below for details. | 58 // pre-defined ones below. See comments on traits below for details. |
59 // | 59 // |
60 // NOTE: Some complexity is necessary in order to use Immutable<T> | 60 // NOTE: Some complexity is necessary in order to use Immutable<T> |
61 // with forward-declared types. See comments on traits below for | 61 // with forward-declared types. See comments on traits below for |
62 // details. | 62 // details. |
63 | 63 |
64 #ifndef SYNC_INTERNAL_API_PUBLIC_UTIL_IMMUTABLE_H_ | 64 #ifndef COMPONENTS_SYNC_BASE_IMMUTABLE_H_ |
65 #define SYNC_INTERNAL_API_PUBLIC_UTIL_IMMUTABLE_H_ | 65 #define COMPONENTS_SYNC_BASE_IMMUTABLE_H_ |
66 | 66 |
67 // For std::swap(). | 67 // For std::swap(). |
68 #include <algorithm> | 68 #include <algorithm> |
69 | 69 |
70 #include "base/macros.h" | 70 #include "base/macros.h" |
71 #include "base/memory/ref_counted.h" | 71 #include "base/memory/ref_counted.h" |
72 | 72 |
73 namespace syncer { | 73 namespace syncer { |
74 | 74 |
75 namespace internal { | 75 namespace internal { |
76 // This class is part of the Immutable implementation. DO NOT USE | 76 // This class is part of the Immutable implementation. DO NOT USE |
77 // THIS CLASS DIRECTLY YOURSELF. | 77 // THIS CLASS DIRECTLY YOURSELF. |
78 | 78 |
79 template <typename T, typename Traits> | 79 template <typename T, typename Traits> |
80 class ImmutableCore | 80 class ImmutableCore |
81 : public base::RefCountedThreadSafe<ImmutableCore<T, Traits> > { | 81 : public base::RefCountedThreadSafe<ImmutableCore<T, Traits>> { |
82 public: | 82 public: |
83 // wrapper_ is always explicitly default-initialized to handle | 83 // wrapper_ is always explicitly default-initialized to handle |
84 // primitive types and the case where Traits::Wrapper == T. | 84 // primitive types and the case where Traits::Wrapper == T. |
85 | 85 |
86 ImmutableCore() : wrapper_() { | 86 ImmutableCore() : wrapper_() { Traits::InitializeWrapper(&wrapper_); } |
87 Traits::InitializeWrapper(&wrapper_); | |
88 } | |
89 | 87 |
90 explicit ImmutableCore(T* t) : wrapper_() { | 88 explicit ImmutableCore(T* t) : wrapper_() { |
91 Traits::InitializeWrapper(&wrapper_); | 89 Traits::InitializeWrapper(&wrapper_); |
92 Traits::Swap(Traits::UnwrapMutable(&wrapper_), t); | 90 Traits::Swap(Traits::UnwrapMutable(&wrapper_), t); |
93 } | 91 } |
94 | 92 |
95 const T& Get() const { | 93 const T& Get() const { return Traits::Unwrap(wrapper_); } |
96 return Traits::Unwrap(wrapper_); | |
97 } | |
98 | 94 |
99 private: | 95 private: |
100 friend class base::RefCountedThreadSafe<ImmutableCore<T, Traits>>; | 96 friend class base::RefCountedThreadSafe<ImmutableCore<T, Traits>>; |
101 | 97 |
102 ~ImmutableCore() { | 98 ~ImmutableCore() { Traits::DestroyWrapper(&wrapper_); } |
103 Traits::DestroyWrapper(&wrapper_); | |
104 } | |
105 | 99 |
106 // This is semantically const, but we can't mark it a such as we | 100 // This is semantically const, but we can't mark it a such as we |
107 // modify it in the constructor. | 101 // modify it in the constructor. |
108 typename Traits::Wrapper wrapper_; | 102 typename Traits::Wrapper wrapper_; |
109 | 103 |
110 DISALLOW_COPY_AND_ASSIGN(ImmutableCore); | 104 DISALLOW_COPY_AND_ASSIGN(ImmutableCore); |
111 }; | 105 }; |
112 | 106 |
113 } // namespace internal | 107 } // namespace internal |
114 | 108 |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 // http://en.wikipedia.org/wiki/Argument-dependent_name_lookup). | 203 // http://en.wikipedia.org/wiki/Argument-dependent_name_lookup). |
210 using std::swap; | 204 using std::swap; |
211 swap(*t1, *t2); | 205 swap(*t1, *t2); |
212 } | 206 } |
213 }; | 207 }; |
214 | 208 |
215 // Most STL containers have by-reference swap() member functions, | 209 // Most STL containers have by-reference swap() member functions, |
216 // although they usually already overload std::swap() to use those. | 210 // although they usually already overload std::swap() to use those. |
217 template <typename T> | 211 template <typename T> |
218 struct HasSwapMemFnByRef : public DefaultImmutableTraits<T> { | 212 struct HasSwapMemFnByRef : public DefaultImmutableTraits<T> { |
219 static void Swap(T* t1, T* t2) { | 213 static void Swap(T* t1, T* t2) { t1->swap(*t2); } |
220 t1->swap(*t2); | |
221 } | |
222 }; | 214 }; |
223 | 215 |
224 // Most Google-style objects have by-pointer Swap() member functions | 216 // Most Google-style objects have by-pointer Swap() member functions |
225 // (for example, generated protocol buffer classes). | 217 // (for example, generated protocol buffer classes). |
226 template <typename T> | 218 template <typename T> |
227 struct HasSwapMemFnByPtr : public DefaultImmutableTraits<T> { | 219 struct HasSwapMemFnByPtr : public DefaultImmutableTraits<T> { |
228 static void Swap(T* t1, T* t2) { | 220 static void Swap(T* t1, T* t2) { t1->Swap(t2); } |
229 t1->Swap(t2); | |
230 } | |
231 }; | 221 }; |
232 | 222 |
233 template <typename T, typename Traits = DefaultImmutableTraits<T> > | 223 template <typename T, typename Traits = DefaultImmutableTraits<T>> |
234 class Immutable { | 224 class Immutable { |
235 public: | 225 public: |
236 // Puts the underlying object in a default-initialized state. | 226 // Puts the underlying object in a default-initialized state. |
237 Immutable() : core_(new internal::ImmutableCore<T, Traits>()) {} | 227 Immutable() : core_(new internal::ImmutableCore<T, Traits>()) {} |
238 | 228 |
239 // Copy constructor and assignment welcome. | 229 // Copy constructor and assignment welcome. |
240 | 230 |
241 // Resets |t| to a default-initialized state. | 231 // Resets |t| to a default-initialized state. |
242 explicit Immutable(T* t) | 232 explicit Immutable(T* t) : core_(new internal::ImmutableCore<T, Traits>(t)) {} |
243 : core_(new internal::ImmutableCore<T, Traits>(t)) {} | |
244 | 233 |
245 const T& Get() const { | 234 const T& Get() const { return core_->Get(); } |
246 return core_->Get(); | |
247 } | |
248 | 235 |
249 private: | 236 private: |
250 scoped_refptr<const internal::ImmutableCore<T, Traits> > core_; | 237 scoped_refptr<const internal::ImmutableCore<T, Traits>> core_; |
251 }; | 238 }; |
252 | 239 |
253 // Helper function to avoid having to write out template arguments. | 240 // Helper function to avoid having to write out template arguments. |
254 template <typename T> | 241 template <typename T> |
255 Immutable<T> MakeImmutable(T* t) { | 242 Immutable<T> MakeImmutable(T* t) { |
256 return Immutable<T>(t); | 243 return Immutable<T>(t); |
257 } | 244 } |
258 | 245 |
259 } // namespace syncer | 246 } // namespace syncer |
260 | 247 |
261 #endif // SYNC_INTERNAL_API_PUBLIC_UTIL_IMMUTABLE_H_ | 248 #endif // COMPONENTS_SYNC_BASE_IMMUTABLE_H_ |
OLD | NEW |