Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(194)

Side by Side Diff: third_party/WebKit/Source/wtf/WeakPtr.h

Issue 1611343002: wtf reformat test Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: pydent Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2013 Google, Inc. All Rights Reserved. 2 * Copyright (C) 2013 Google, Inc. All Rights Reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
(...skipping 16 matching lines...) Expand all
27 #define WTF_WeakPtr_h 27 #define WTF_WeakPtr_h
28 28
29 #include "wtf/Noncopyable.h" 29 #include "wtf/Noncopyable.h"
30 #include "wtf/PassRefPtr.h" 30 #include "wtf/PassRefPtr.h"
31 #include "wtf/RefPtr.h" 31 #include "wtf/RefPtr.h"
32 #include "wtf/ThreadSafeRefCounted.h" 32 #include "wtf/ThreadSafeRefCounted.h"
33 #include "wtf/Threading.h" 33 #include "wtf/Threading.h"
34 34
35 namespace WTF { 35 namespace WTF {
36 36
37 template<typename T> 37 template <typename T>
38 class WeakReference : public ThreadSafeRefCounted<WeakReference<T>> { 38 class WeakReference : public ThreadSafeRefCounted<WeakReference<T>> {
39 WTF_MAKE_NONCOPYABLE(WeakReference<T>); 39 WTF_MAKE_NONCOPYABLE(WeakReference<T>);
40 USING_FAST_MALLOC(WeakReference); 40 USING_FAST_MALLOC(WeakReference);
41 public:
42 static PassRefPtr<WeakReference<T>> create(T* ptr) { return adoptRef(new Wea kReference(ptr)); }
43 static PassRefPtr<WeakReference<T>> createUnbound() { return adoptRef(new We akReference()); }
44 41
45 T* get() const 42 public:
46 { 43 static PassRefPtr<WeakReference<T>> create(T* ptr) {
47 ASSERT(m_boundThread == currentThread()); 44 return adoptRef(new WeakReference(ptr));
48 return m_ptr; 45 }
49 } 46 static PassRefPtr<WeakReference<T>> createUnbound() {
47 return adoptRef(new WeakReference());
48 }
50 49
51 void clear() 50 T* get() const {
52 { 51 ASSERT(m_boundThread == currentThread());
53 ASSERT(m_boundThread == currentThread()); 52 return m_ptr;
54 m_ptr = 0; 53 }
55 }
56 54
57 void bindTo(T* ptr) 55 void clear() {
58 { 56 ASSERT(m_boundThread == currentThread());
59 ASSERT(!m_ptr); 57 m_ptr = 0;
58 }
59
60 void bindTo(T* ptr) {
61 ASSERT(!m_ptr);
60 #if ENABLE(ASSERT) 62 #if ENABLE(ASSERT)
61 m_boundThread = currentThread(); 63 m_boundThread = currentThread();
62 #endif 64 #endif
63 m_ptr = ptr; 65 m_ptr = ptr;
64 } 66 }
65 67
66 private: 68 private:
67 WeakReference() : m_ptr(0) { } 69 WeakReference() : m_ptr(0) {}
68 70
69 explicit WeakReference(T* ptr) 71 explicit WeakReference(T* ptr)
70 : m_ptr(ptr) 72 : m_ptr(ptr)
71 #if ENABLE(ASSERT) 73 #if ENABLE(ASSERT)
72 , m_boundThread(currentThread()) 74 ,
75 m_boundThread(currentThread())
73 #endif 76 #endif
74 { 77 {
75 } 78 }
76 79
77 T* m_ptr; 80 T* m_ptr;
78 #if ENABLE(ASSERT) 81 #if ENABLE(ASSERT)
79 ThreadIdentifier m_boundThread; 82 ThreadIdentifier m_boundThread;
80 #endif 83 #endif
81 }; 84 };
82 85
83 template<typename T> 86 template <typename T>
84 class WeakPtr { 87 class WeakPtr {
85 USING_FAST_MALLOC(WeakPtr); 88 USING_FAST_MALLOC(WeakPtr);
86 public:
87 WeakPtr() { }
88 WeakPtr(std::nullptr_t) { }
89 WeakPtr(PassRefPtr<WeakReference<T>> ref) : m_ref(ref) { }
90 89
91 T* get() const { return m_ref ? m_ref->get() : 0; } 90 public:
92 void clear() { m_ref.clear(); } 91 WeakPtr() {}
92 WeakPtr(std::nullptr_t) {}
93 WeakPtr(PassRefPtr<WeakReference<T>> ref) : m_ref(ref) {}
93 94
94 T* operator->() const 95 T* get() const { return m_ref ? m_ref->get() : 0; }
95 { 96 void clear() { m_ref.clear(); }
96 ASSERT(get());
97 return get();
98 }
99 97
100 typedef RefPtr<WeakReference<T>> (WeakPtr::*UnspecifiedBoolType); 98 T* operator->() const {
101 operator UnspecifiedBoolType() const { return get() ? &WeakPtr::m_ref : 0; } 99 ASSERT(get());
100 return get();
101 }
102 102
103 private: 103 typedef RefPtr<WeakReference<T>>(WeakPtr::*UnspecifiedBoolType);
104 RefPtr<WeakReference<T>> m_ref; 104 operator UnspecifiedBoolType() const { return get() ? &WeakPtr::m_ref : 0; }
105
106 private:
107 RefPtr<WeakReference<T>> m_ref;
105 }; 108 };
106 109
107 template<typename T, typename U> inline bool operator==(const WeakPtr<T>& a, con st WeakPtr<U>& b) 110 template <typename T, typename U>
108 { 111 inline bool operator==(const WeakPtr<T>& a, const WeakPtr<U>& b) {
109 return a.get() == b.get(); 112 return a.get() == b.get();
110 } 113 }
111 114
112 template<typename T, typename U> inline bool operator!=(const WeakPtr<T>& a, con st WeakPtr<U>& b) 115 template <typename T, typename U>
113 { 116 inline bool operator!=(const WeakPtr<T>& a, const WeakPtr<U>& b) {
114 return a.get() != b.get(); 117 return a.get() != b.get();
115 } 118 }
116 119
117 template<typename T> 120 template <typename T>
118 class WeakPtrFactory { 121 class WeakPtrFactory {
119 WTF_MAKE_NONCOPYABLE(WeakPtrFactory<T>); 122 WTF_MAKE_NONCOPYABLE(WeakPtrFactory<T>);
120 USING_FAST_MALLOC(WeakPtrFactory); 123 USING_FAST_MALLOC(WeakPtrFactory);
121 public:
122 explicit WeakPtrFactory(T* ptr) : m_ref(WeakReference<T>::create(ptr)) { }
123 124
124 WeakPtrFactory(PassRefPtr<WeakReference<T>> ref, T* ptr) 125 public:
125 : m_ref(ref) 126 explicit WeakPtrFactory(T* ptr) : m_ref(WeakReference<T>::create(ptr)) {}
126 {
127 m_ref->bindTo(ptr);
128 }
129 127
130 ~WeakPtrFactory() { m_ref->clear(); } 128 WeakPtrFactory(PassRefPtr<WeakReference<T>> ref, T* ptr) : m_ref(ref) {
129 m_ref->bindTo(ptr);
130 }
131 131
132 // We should consider having createWeakPtr populate m_ref the first time cre ateWeakPtr is called. 132 ~WeakPtrFactory() { m_ref->clear(); }
133 WeakPtr<T> createWeakPtr() { return WeakPtr<T>(m_ref); }
134 133
135 void revokeAll() 134 // We should consider having createWeakPtr populate m_ref the first time creat eWeakPtr is called.
136 { 135 WeakPtr<T> createWeakPtr() { return WeakPtr<T>(m_ref); }
137 T* ptr = m_ref->get();
138 m_ref->clear();
139 // We create a new WeakReference so that future calls to createWeakPtr() create nonzero WeakPtrs.
140 m_ref = WeakReference<T>::create(ptr);
141 }
142 136
143 bool hasWeakPtrs() const 137 void revokeAll() {
144 { 138 T* ptr = m_ref->get();
145 return m_ref->refCount() > 1; 139 m_ref->clear();
146 } 140 // We create a new WeakReference so that future calls to createWeakPtr() cre ate nonzero WeakPtrs.
141 m_ref = WeakReference<T>::create(ptr);
142 }
147 143
148 private: 144 bool hasWeakPtrs() const { return m_ref->refCount() > 1; }
149 RefPtr<WeakReference<T>> m_ref; 145
146 private:
147 RefPtr<WeakReference<T>> m_ref;
150 }; 148 };
151 149
152 } // namespace WTF 150 } // namespace WTF
153 151
154 using WTF::WeakPtr; 152 using WTF::WeakPtr;
155 using WTF::WeakPtrFactory; 153 using WTF::WeakPtrFactory;
156 using WTF::WeakReference; 154 using WTF::WeakReference;
157 155
158 #endif 156 #endif
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/wtf/WTFThreadData.cpp ('k') | third_party/WebKit/Source/wtf/asm/SaturatedArithmeticARM.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698