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

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

Issue 2031463002: Add WTF::WeakPtr::operator*() (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix Created 4 years, 6 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 26 matching lines...) Expand all
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: 41 public:
42 static PassRefPtr<WeakReference<T>> create(T* ptr) { return adoptRef(new Wea kReference(ptr)); } 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()); } 43 static PassRefPtr<WeakReference<T>> createUnbound() { return adoptRef(new We akReference()); }
44 44
45 T* get() const 45 T* get() const
46 { 46 {
47 ASSERT(m_boundThread == currentThread()); 47 #if DCHECK_IS_ON()
48 DCHECK(m_boundThread == currentThread());
49 #endif
48 return m_ptr; 50 return m_ptr;
49 } 51 }
50 52
51 void clear() 53 void clear()
52 { 54 {
53 ASSERT(m_boundThread == currentThread()); 55 #if DCHECK_IS_ON()
56 DCHECK(m_boundThread == currentThread());
57 #endif
54 m_ptr = 0; 58 m_ptr = 0;
55 } 59 }
56 60
57 void bindTo(T* ptr) 61 void bindTo(T* ptr)
58 { 62 {
59 ASSERT(!m_ptr); 63 DCHECK(!m_ptr);
60 #if ENABLE(ASSERT) 64 #if DCHECK_IS_ON()
61 m_boundThread = currentThread(); 65 m_boundThread = currentThread();
62 #endif 66 #endif
63 m_ptr = ptr; 67 m_ptr = ptr;
64 } 68 }
65 69
66 private: 70 private:
67 WeakReference() : m_ptr(0) { } 71 WeakReference() : m_ptr(0) { }
68 72
69 explicit WeakReference(T* ptr) 73 explicit WeakReference(T* ptr)
70 : m_ptr(ptr) 74 : m_ptr(ptr)
71 #if ENABLE(ASSERT) 75 #if DCHECK_IS_ON()
72 , m_boundThread(currentThread()) 76 , m_boundThread(currentThread())
73 #endif 77 #endif
74 { 78 {
75 } 79 }
76 80
77 T* m_ptr; 81 T* m_ptr;
78 #if ENABLE(ASSERT) 82 #if DCHECK_IS_ON()
79 ThreadIdentifier m_boundThread; 83 ThreadIdentifier m_boundThread;
80 #endif 84 #endif
81 }; 85 };
82 86
83 template<typename T> 87 template<typename T>
84 class WeakPtr { 88 class WeakPtr {
85 USING_FAST_MALLOC(WeakPtr); 89 USING_FAST_MALLOC(WeakPtr);
86 public: 90 public:
87 WeakPtr() { } 91 WeakPtr() { }
88 WeakPtr(std::nullptr_t) { } 92 WeakPtr(std::nullptr_t) { }
89 WeakPtr(PassRefPtr<WeakReference<T>> ref) : m_ref(ref) { } 93 WeakPtr(PassRefPtr<WeakReference<T>> ref) : m_ref(ref) { }
90 94
91 T* get() const { return m_ref ? m_ref->get() : 0; } 95 T* get() const { return m_ref ? m_ref->get() : 0; }
92 void clear() { m_ref.clear(); } 96 void clear() { m_ref.clear(); }
93 97
98 T& operator*() const
99 {
100 DCHECK(get());
101 return *get();
102 }
103
94 T* operator->() const 104 T* operator->() const
95 { 105 {
96 ASSERT(get()); 106 DCHECK(get());
97 return get(); 107 return get();
98 } 108 }
99 109
100 typedef RefPtr<WeakReference<T>> (WeakPtr::*UnspecifiedBoolType); 110 typedef RefPtr<WeakReference<T>> (WeakPtr::*UnspecifiedBoolType);
101 operator UnspecifiedBoolType() const { return get() ? &WeakPtr::m_ref : 0; } 111 operator UnspecifiedBoolType() const { return get() ? &WeakPtr::m_ref : 0; }
102 112
103 private: 113 private:
104 RefPtr<WeakReference<T>> m_ref; 114 RefPtr<WeakReference<T>> m_ref;
105 }; 115 };
106 116
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
149 RefPtr<WeakReference<T>> m_ref; 159 RefPtr<WeakReference<T>> m_ref;
150 }; 160 };
151 161
152 } // namespace WTF 162 } // namespace WTF
153 163
154 using WTF::WeakPtr; 164 using WTF::WeakPtr;
155 using WTF::WeakPtrFactory; 165 using WTF::WeakPtrFactory;
156 using WTF::WeakReference; 166 using WTF::WeakReference;
157 167
158 #endif 168 #endif
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698