OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 DataPersistent_h | 5 #ifndef DataPersistent_h |
6 #define DataPersistent_h | 6 #define DataPersistent_h |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include "platform/heap/Handle.h" | 9 #include "platform/heap/Handle.h" |
10 #include "platform/wtf/Allocator.h" | 10 #include "platform/wtf/Allocator.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 // on a copy of the underlying T object. | 22 // on a copy of the underlying T object. |
23 // | 23 // |
24 // DataPersistent<T> does assume that no one keeps non-DataPersistent<> shared | 24 // DataPersistent<T> does assume that no one keeps non-DataPersistent<> shared |
25 // references to the underlying T object that it manages, and is mutating the | 25 // references to the underlying T object that it manages, and is mutating the |
26 // object via those. | 26 // object via those. |
27 template <typename T> | 27 template <typename T> |
28 class DataPersistent { | 28 class DataPersistent { |
29 USING_FAST_MALLOC(DataPersistent); | 29 USING_FAST_MALLOC(DataPersistent); |
30 | 30 |
31 public: | 31 public: |
32 DataPersistent() : own_copy_(false) {} | 32 DataPersistent() |
| 33 : data_(WTF::WrapUnique(new Persistent<T>(T::Create()))), |
| 34 own_copy_(true) {} |
| 35 |
| 36 DataPersistent(std::nullptr_t) : data_(nullptr), own_copy_(false) {} |
33 | 37 |
34 DataPersistent(const DataPersistent& other) : own_copy_(false) { | 38 DataPersistent(const DataPersistent& other) : own_copy_(false) { |
35 if (other.data_) | 39 if (other.data_) |
36 data_ = WTF::WrapUnique(new Persistent<T>(other.data_->Get())); | 40 data_ = WTF::WrapUnique(new Persistent<T>(other.data_->Get())); |
37 | 41 |
38 // Invalidated, subsequent mutations will happen on a new copy. | 42 // Invalidated, subsequent mutations will happen on a new copy. |
39 // | 43 // |
40 // (Clearing |m_ownCopy| will not be observable over T, hence | 44 // (Clearing |m_ownCopy| will not be observable over T, hence |
41 // the const_cast<> is considered acceptable here.) | 45 // the const_cast<> is considered acceptable here.) |
42 const_cast<DataPersistent&>(other).own_copy_ = false; | 46 const_cast<DataPersistent&>(other).own_copy_ = false; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 | 81 |
78 private: | 82 private: |
79 // Reduce size of DataPersistent<> by delaying creation of Persistent<>. | 83 // Reduce size of DataPersistent<> by delaying creation of Persistent<>. |
80 std::unique_ptr<Persistent<T>> data_; | 84 std::unique_ptr<Persistent<T>> data_; |
81 unsigned own_copy_ : 1; | 85 unsigned own_copy_ : 1; |
82 }; | 86 }; |
83 | 87 |
84 } // namespace blink | 88 } // namespace blink |
85 | 89 |
86 #endif // DataPersistent_h | 90 #endif // DataPersistent_h |
OLD | NEW |