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

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

Issue 2091713002: Specialize base::IsWeakReceiver for Blink weak pointers to support base::Bind (1/5) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: +#include for build 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
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2011 Apple Inc. All rights reserved. 2 * Copyright (C) 2011 Apple 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 25 matching lines...) Expand all
36 #include "wtf/ThreadSafeRefCounted.h" 36 #include "wtf/ThreadSafeRefCounted.h"
37 #include "wtf/TypeTraits.h" 37 #include "wtf/TypeTraits.h"
38 #include "wtf/WeakPtr.h" 38 #include "wtf/WeakPtr.h"
39 #include <tuple> 39 #include <tuple>
40 #include <utility> 40 #include <utility>
41 41
42 namespace blink { 42 namespace blink {
43 template<typename T> class CrossThreadPersistent; 43 template<typename T> class CrossThreadPersistent;
44 } 44 }
45 45
46 namespace base {
47
48 template <typename T>
49 struct IsWeakReceiver<WTF::WeakPtr<T>> : std::true_type {};
50
51 }
52
46 namespace WTF { 53 namespace WTF {
47 54
48 // Functional.h provides a very simple way to bind a function pointer and argume nts together into a function object 55 // Functional.h provides a very simple way to bind a function pointer and argume nts together into a function object
49 // that can be stored, copied and invoked, similar to how boost::bind and std::b ind in C++11. 56 // that can be stored, copied and invoked, similar to how boost::bind and std::b ind in C++11.
50 57
51 // Thread Safety: 58 // Thread Safety:
52 // 59 //
53 // WTF::bind() and SameThreadClosure should be used for same-thread closures 60 // WTF::bind() and SameThreadClosure should be used for same-thread closures
54 // only, i.e. the closures must be created, executed and destructed on 61 // only, i.e. the closures must be created, executed and destructed on
55 // the same thread. 62 // the same thread.
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 public: 190 public:
184 typedef R ResultType; 191 typedef R ResultType;
185 // + 1 is for |this| as an argument. 192 // + 1 is for |this| as an argument.
186 static const size_t numberOfArguments = sizeof...(Parameters) + 1; 193 static const size_t numberOfArguments = sizeof...(Parameters) + 1;
187 194
188 explicit FunctionWrapper(R(C::*function)(Parameters...)) 195 explicit FunctionWrapper(R(C::*function)(Parameters...))
189 : m_function(function) 196 : m_function(function)
190 { 197 {
191 } 198 }
192 199
193 template <typename... IncomingParameters> 200 template <typename Receiver, typename... IncomingParameters>
194 R operator()(C* c, IncomingParameters&&... parameters) 201 R operator()(Receiver&& receiver, IncomingParameters&&... parameters)
195 { 202 {
196 return (c->*m_function)(std::forward<IncomingParameters>(parameters)...) ; 203 if (base::IsWeakReceiver<typename std::decay<Receiver>::type>::value && !receiver) {
197 } 204 return R();
205 }
198 206
199 template <typename... IncomingParameters> 207 C& c = *receiver;
200 R operator()(const std::unique_ptr<C>& c, IncomingParameters&&... parameters ) 208 return (c.*m_function)(std::forward<IncomingParameters>(parameters)...);
201 {
202 return (c.get()->*m_function)(std::forward<IncomingParameters>(parameter s)...);
203 }
204
205 template <typename... IncomingParameters>
206 R operator()(const WeakPtr<C>& c, IncomingParameters&&... parameters)
207 {
208 C* obj = c.get();
209 if (!obj)
210 return R();
211 return (obj->*m_function)(std::forward<IncomingParameters>(parameters).. .);
212 } 209 }
213 210
214 private: 211 private:
215 R(C::*m_function)(Parameters...); 212 R(C::*m_function)(Parameters...);
216 }; 213 };
217 214
218 template <typename T> 215 template <typename T>
219 struct ParamStorageTraits { 216 struct ParamStorageTraits {
220 typedef T StorageType; 217 typedef T StorageType;
221 218
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
409 406
410 using WTF::passed; 407 using WTF::passed;
411 using WTF::unretained; 408 using WTF::unretained;
412 using WTF::crossThreadUnretained; 409 using WTF::crossThreadUnretained;
413 410
414 using WTF::Function; 411 using WTF::Function;
415 using WTF::SameThreadClosure; 412 using WTF::SameThreadClosure;
416 using WTF::CrossThreadClosure; 413 using WTF::CrossThreadClosure;
417 414
418 #endif // WTF_Functional_h 415 #endif // WTF_Functional_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698