| 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; |
| 11 // | 11 // |
| 12 // void ProcessStuff(const LargeObjectList& stuff) { | 12 // void ProcessStuff(const LargeObjectList& stuff) { |
| 13 // for (LargeObjectList::const_iterator it = stuff.begin(); | 13 // for (LargeObjectList::const_iterator it = stuff.begin(); |
| 14 // it != stuff.end(); ++it) { | 14 // it != stuff.end(); ++it) { |
| 15 // ... process it ... | 15 // ... process it ... |
| 16 // } | 16 // } |
| 17 // } | 17 // } |
| 18 // | 18 // |
| 19 // ... | 19 // ... |
| 20 // | 20 // |
| 21 // LargeObjectList my_stuff; | 21 // LargeObjectList my_stuff; |
| 22 // ... fill my_stuff with lots of LargeObjects ... | 22 // ... fill my_stuff with lots of LargeObjects ... |
| 23 // some_loop->PostTask(FROM_HERE, base::Bind(&ProcessStuff, my_stuff)); | 23 // some_loop->PostTask(FROM_HERE, base::Bind(&ProcessStuff, my_stuff)); |
| 24 // | 24 // |
| 25 // The last line incurs the cost of copying my_stuff, which is | 25 // The last line incurs the cost of copying my_stuff, which is |
| 26 // undesirable. Here's the above code re-written using Immutable<T>: | 26 // undesirable. Here's the above code re-written using Immutable<T>: |
| 27 // | 27 // |
| 28 // void ProcessStuff( | 28 // void ProcessStuff( |
| 29 // const csync::Immutable<LargeObjectList>& stuff) { | 29 // const syncer::Immutable<LargeObjectList>& stuff) { |
| 30 // for (LargeObjectList::const_iterator it = stuff.Get().begin(); | 30 // for (LargeObjectList::const_iterator it = stuff.Get().begin(); |
| 31 // it != stuff.Get().end(); ++it) { | 31 // it != stuff.Get().end(); ++it) { |
| 32 // ... process it ... | 32 // ... process it ... |
| 33 // } | 33 // } |
| 34 // } | 34 // } |
| 35 // | 35 // |
| 36 // ... | 36 // ... |
| 37 // | 37 // |
| 38 // LargeObjectList my_stuff; | 38 // LargeObjectList my_stuff; |
| 39 // ... fill my_stuff with lots of LargeObjects ... | 39 // ... fill my_stuff with lots of LargeObjects ... |
| (...skipping 25 matching lines...) Expand all Loading... |
| 65 #ifndef SYNC_UTIL_IMMUTABLE_H_ | 65 #ifndef SYNC_UTIL_IMMUTABLE_H_ |
| 66 #define SYNC_UTIL_IMMUTABLE_H_ | 66 #define SYNC_UTIL_IMMUTABLE_H_ |
| 67 #pragma once | 67 #pragma once |
| 68 | 68 |
| 69 // For std::swap(). | 69 // For std::swap(). |
| 70 #include <algorithm> | 70 #include <algorithm> |
| 71 | 71 |
| 72 #include "base/basictypes.h" | 72 #include "base/basictypes.h" |
| 73 #include "base/memory/ref_counted.h" | 73 #include "base/memory/ref_counted.h" |
| 74 | 74 |
| 75 namespace csync { | 75 namespace syncer { |
| 76 | 76 |
| 77 namespace internal { | 77 namespace internal { |
| 78 // This class is part of the Immutable implementation. DO NOT USE | 78 // This class is part of the Immutable implementation. DO NOT USE |
| 79 // THIS CLASS DIRECTLY YOURSELF. | 79 // THIS CLASS DIRECTLY YOURSELF. |
| 80 | 80 |
| 81 template <typename T, typename Traits> | 81 template <typename T, typename Traits> |
| 82 class ImmutableCore | 82 class ImmutableCore |
| 83 : public base::RefCountedThreadSafe<ImmutableCore<T, Traits> > { | 83 : public base::RefCountedThreadSafe<ImmutableCore<T, Traits> > { |
| 84 public: | 84 public: |
| 85 // wrapper_ is always explicitly default-initialized to handle | 85 // wrapper_ is always explicitly default-initialized to handle |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 250 private: | 250 private: |
| 251 scoped_refptr<const internal::ImmutableCore<T, Traits> > core_; | 251 scoped_refptr<const internal::ImmutableCore<T, Traits> > core_; |
| 252 }; | 252 }; |
| 253 | 253 |
| 254 // Helper function to avoid having to write out template arguments. | 254 // Helper function to avoid having to write out template arguments. |
| 255 template <typename T> | 255 template <typename T> |
| 256 Immutable<T> MakeImmutable(T* t) { | 256 Immutable<T> MakeImmutable(T* t) { |
| 257 return Immutable<T>(t); | 257 return Immutable<T>(t); |
| 258 } | 258 } |
| 259 | 259 |
| 260 } // namespace csync | 260 } // namespace syncer |
| 261 | 261 |
| 262 #endif // SYNC_UTIL_IMMUTABLE_H_ | 262 #endif // SYNC_UTIL_IMMUTABLE_H_ |
| OLD | NEW |