| 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(const Immutable<LargeObjectList>& stuff) { |
| 29 // const syncer::Immutable<LargeObjectList>& stuff) { | |
| 30 // for (LargeObjectList::const_iterator it = stuff.Get().begin(); | 29 // for (LargeObjectList::const_iterator it = stuff.Get().begin(); |
| 31 // it != stuff.Get().end(); ++it) { | 30 // it != stuff.Get().end(); ++it) { |
| 32 // ... process it ... | 31 // ... process it ... |
| 33 // } | 32 // } |
| 34 // } | 33 // } |
| 35 // | 34 // |
| 36 // ... | 35 // ... |
| 37 // | 36 // |
| 38 // LargeObjectList my_stuff; | 37 // LargeObjectList my_stuff; |
| 39 // ... fill my_stuff with lots of LargeObjects ... | 38 // ... fill my_stuff with lots of LargeObjects ... |
| (...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 | 251 |
| 253 // Helper function to avoid having to write out template arguments. | 252 // Helper function to avoid having to write out template arguments. |
| 254 template <typename T> | 253 template <typename T> |
| 255 Immutable<T> MakeImmutable(T* t) { | 254 Immutable<T> MakeImmutable(T* t) { |
| 256 return Immutable<T>(t); | 255 return Immutable<T>(t); |
| 257 } | 256 } |
| 258 | 257 |
| 259 } // namespace syncer | 258 } // namespace syncer |
| 260 | 259 |
| 261 #endif // SYNC_UTIL_IMMUTABLE_H_ | 260 #endif // SYNC_UTIL_IMMUTABLE_H_ |
| OLD | NEW |