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 "platform/heap/Handle.h" | 8 #include "platform/heap/Handle.h" |
9 #include "wtf/Allocator.h" | 9 #include "wtf/Allocator.h" |
10 #include "wtf/PtrUtil.h" | 10 #include "wtf/PtrUtil.h" |
(...skipping 15 matching lines...) Expand all Loading... |
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() : m_ownCopy(false) {} | 32 DataPersistent() : m_ownCopy(false) {} |
33 | 33 |
34 DataPersistent(const DataPersistent& other) : m_ownCopy(false) { | 34 DataPersistent(const DataPersistent& other) : m_ownCopy(false) { |
35 if (other.m_data) | 35 if (other.m_data) |
36 m_data = wrapUnique(new Persistent<T>(other.m_data->get())); | 36 m_data = WTF::wrapUnique(new Persistent<T>(other.m_data->get())); |
37 | 37 |
38 // Invalidated, subsequent mutations will happen on a new copy. | 38 // Invalidated, subsequent mutations will happen on a new copy. |
39 // | 39 // |
40 // (Clearing |m_ownCopy| will not be observable over T, hence | 40 // (Clearing |m_ownCopy| will not be observable over T, hence |
41 // the const_cast<> is considered acceptable here.) | 41 // the const_cast<> is considered acceptable here.) |
42 const_cast<DataPersistent&>(other).m_ownCopy = false; | 42 const_cast<DataPersistent&>(other).m_ownCopy = false; |
43 } | 43 } |
44 | 44 |
45 const T* get() const { return m_data ? m_data->get() : nullptr; } | 45 const T* get() const { return m_data ? m_data->get() : nullptr; } |
46 | 46 |
47 const T& operator*() const { return m_data ? *get() : nullptr; } | 47 const T& operator*() const { return m_data ? *get() : nullptr; } |
48 const T* operator->() const { return get(); } | 48 const T* operator->() const { return get(); } |
49 | 49 |
50 T* access() { | 50 T* access() { |
51 if (m_data && !m_ownCopy) { | 51 if (m_data && !m_ownCopy) { |
52 *m_data = (*m_data)->copy(); | 52 *m_data = (*m_data)->copy(); |
53 m_ownCopy = true; | 53 m_ownCopy = true; |
54 } | 54 } |
55 return m_data ? m_data->get() : nullptr; | 55 return m_data ? m_data->get() : nullptr; |
56 } | 56 } |
57 | 57 |
58 void init() { | 58 void init() { |
59 ASSERT(!m_data); | 59 ASSERT(!m_data); |
60 m_data = wrapUnique(new Persistent<T>(T::create())); | 60 m_data = WTF::wrapUnique(new Persistent<T>(T::create())); |
61 m_ownCopy = true; | 61 m_ownCopy = true; |
62 } | 62 } |
63 | 63 |
64 bool operator==(const DataPersistent<T>& o) const { | 64 bool operator==(const DataPersistent<T>& o) const { |
65 ASSERT(m_data); | 65 ASSERT(m_data); |
66 ASSERT(o.m_data); | 66 ASSERT(o.m_data); |
67 return m_data->get() == o.m_data->get() || | 67 return m_data->get() == o.m_data->get() || |
68 *m_data->get() == *o.m_data->get(); | 68 *m_data->get() == *o.m_data->get(); |
69 } | 69 } |
70 | 70 |
71 bool operator!=(const DataPersistent<T>& o) const { | 71 bool operator!=(const DataPersistent<T>& o) const { |
72 ASSERT(m_data); | 72 ASSERT(m_data); |
73 ASSERT(o.m_data); | 73 ASSERT(o.m_data); |
74 return m_data->get() != o.m_data->get() && | 74 return m_data->get() != o.m_data->get() && |
75 *m_data->get() != *o.m_data->get(); | 75 *m_data->get() != *o.m_data->get(); |
76 } | 76 } |
77 | 77 |
78 void operator=(std::nullptr_t) { m_data.clear(); } | 78 void operator=(std::nullptr_t) { m_data.clear(); } |
79 | 79 |
80 private: | 80 private: |
81 // Reduce size of DataPersistent<> by delaying creation of Persistent<>. | 81 // Reduce size of DataPersistent<> by delaying creation of Persistent<>. |
82 std::unique_ptr<Persistent<T>> m_data; | 82 std::unique_ptr<Persistent<T>> m_data; |
83 unsigned m_ownCopy : 1; | 83 unsigned m_ownCopy : 1; |
84 }; | 84 }; |
85 | 85 |
86 } // namespace blink | 86 } // namespace blink |
87 | 87 |
88 #endif // DataPersistent_h | 88 #endif // DataPersistent_h |
OLD | NEW |