| OLD | NEW |
| 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 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 } | 232 } |
| 233 | 233 |
| 234 template<typename Signature, FunctionThreadAffinity threadAffinity = SameThreadA
ffinity> | 234 template<typename Signature, FunctionThreadAffinity threadAffinity = SameThreadA
ffinity> |
| 235 class Function; | 235 class Function; |
| 236 | 236 |
| 237 template<typename R, typename... Args, FunctionThreadAffinity threadAffinity> | 237 template<typename R, typename... Args, FunctionThreadAffinity threadAffinity> |
| 238 class Function<R(Args...), threadAffinity> { | 238 class Function<R(Args...), threadAffinity> { |
| 239 USING_FAST_MALLOC(Function); | 239 USING_FAST_MALLOC(Function); |
| 240 WTF_MAKE_NONCOPYABLE(Function); | 240 WTF_MAKE_NONCOPYABLE(Function); |
| 241 public: | 241 public: |
| 242 Function(base::Callback<R(Args...)> callback) | |
| 243 : m_callback(std::move(callback)) { } | |
| 244 | |
| 245 ~Function() | 242 ~Function() |
| 246 { | 243 { |
| 247 DCHECK(m_threadChecker.CalledOnValidThread()); | 244 DCHECK(m_threadChecker.CalledOnValidThread()); |
| 248 } | 245 } |
| 249 | 246 |
| 250 R operator()(Args... args) | 247 R operator()(Args... args) |
| 251 { | 248 { |
| 252 DCHECK(m_threadChecker.CalledOnValidThread()); | 249 DCHECK(m_threadChecker.CalledOnValidThread()); |
| 253 return m_callback.Run(std::forward<Args>(args)...); | 250 return m_callback.Run(std::forward<Args>(args)...); |
| 254 } | 251 } |
| 255 | 252 |
| 256 explicit operator base::Callback<R(Args...)>() | 253 explicit operator base::Callback<R(Args...)>() |
| 257 { | 254 { |
| 258 return m_callback; | 255 return m_callback; |
| 259 } | 256 } |
| 260 | 257 |
| 261 private: | 258 private: |
| 259 Function(base::Callback<R(Args...)> callback) |
| 260 : m_callback(std::move(callback)) { } |
| 261 |
| 262 template <typename FunctionType, typename... BoundParameters> |
| 263 static std::unique_ptr<Function<base::MakeUnboundRunType<FunctionType, Bound
Parameters...>, threadAffinity>> bindInternal(FunctionType function, BoundParame
ters&&... boundParameters) |
| 264 { |
| 265 using UnboundRunType = base::MakeUnboundRunType<FunctionType, BoundParam
eters...>; |
| 266 return wrapUnique(new Function<UnboundRunType, threadAffinity>(base::Bin
d(function, typename ParamStorageTraits<typename std::decay<BoundParameters>::ty
pe>::StorageType(std::forward<BoundParameters>(boundParameters))...))); |
| 267 } |
| 268 |
| 269 template <typename FunctionType, typename... BoundParameters> |
| 270 friend std::unique_ptr<Function<base::MakeUnboundRunType<FunctionType, Bound
Parameters...>, SameThreadAffinity>> bind(FunctionType, BoundParameters&&...); |
| 271 |
| 272 template <typename FunctionType, typename... BoundParameters> |
| 273 friend std::unique_ptr<Function<base::MakeUnboundRunType<FunctionType, Bound
Parameters...>, CrossThreadAffinity>> crossThreadBind(FunctionType, BoundParamet
ers&&...); |
| 274 |
| 262 using MaybeThreadChecker = typename std::conditional< | 275 using MaybeThreadChecker = typename std::conditional< |
| 263 threadAffinity == SameThreadAffinity, | 276 threadAffinity == SameThreadAffinity, |
| 264 base::ThreadChecker, | 277 base::ThreadChecker, |
| 265 base::ThreadCheckerDoNothing>::type; | 278 base::ThreadCheckerDoNothing>::type; |
| 266 MaybeThreadChecker m_threadChecker; | 279 MaybeThreadChecker m_threadChecker; |
| 267 base::Callback<R(Args...)> m_callback; | 280 base::Callback<R(Args...)> m_callback; |
| 268 }; | 281 }; |
| 269 | 282 |
| 270 template <FunctionThreadAffinity threadAffinity, typename FunctionType, typename
... BoundParameters> | |
| 271 std::unique_ptr<Function<base::MakeUnboundRunType<FunctionType, BoundParameters.
..>, threadAffinity>> bindInternal(FunctionType function, BoundParameters&&... b
oundParameters) | |
| 272 { | |
| 273 using UnboundRunType = base::MakeUnboundRunType<FunctionType, BoundParameter
s...>; | |
| 274 return wrapUnique(new Function<UnboundRunType, threadAffinity>(base::Bind(fu
nction, typename ParamStorageTraits<typename std::decay<BoundParameters>::type>:
:StorageType(std::forward<BoundParameters>(boundParameters))...))); | |
| 275 } | |
| 276 | |
| 277 template <typename FunctionType, typename... BoundParameters> | 283 template <typename FunctionType, typename... BoundParameters> |
| 278 std::unique_ptr<Function<base::MakeUnboundRunType<FunctionType, BoundParameters.
..>, SameThreadAffinity>> bind(FunctionType function, BoundParameters&&... bound
Parameters) | 284 std::unique_ptr<Function<base::MakeUnboundRunType<FunctionType, BoundParameters.
..>, SameThreadAffinity>> bind(FunctionType function, BoundParameters&&... bound
Parameters) |
| 279 { | 285 { |
| 280 return bindInternal<SameThreadAffinity>(function, std::forward<BoundParamete
rs>(boundParameters)...); | 286 return Function<base::MakeUnboundRunType<FunctionType, BoundParameters...>,
SameThreadAffinity>::bindInternal(function, std::forward<BoundParameters>(boundP
arameters)...); |
| 281 } | 287 } |
| 282 | 288 |
| 283 template <typename FunctionType, typename... BoundParameters> | 289 template <typename FunctionType, typename... BoundParameters> |
| 284 std::unique_ptr<Function<base::MakeUnboundRunType<FunctionType, BoundParameters.
..>, CrossThreadAffinity>> crossThreadBind(FunctionType function, BoundParameter
s&&... boundParameters) | 290 std::unique_ptr<Function<base::MakeUnboundRunType<FunctionType, BoundParameters.
..>, CrossThreadAffinity>> crossThreadBind(FunctionType function, BoundParameter
s&&... boundParameters) |
| 285 { | 291 { |
| 286 return bindInternal<CrossThreadAffinity>(function, CrossThreadCopier<typenam
e std::decay<BoundParameters>::type>::copy(std::forward<BoundParameters>(boundPa
rameters))...); | 292 return Function<base::MakeUnboundRunType<FunctionType, BoundParameters...>,
CrossThreadAffinity>::bindInternal(function, CrossThreadCopier<typename std::dec
ay<BoundParameters>::type>::copy(std::forward<BoundParameters>(boundParameters))
...); |
| 287 } | 293 } |
| 288 | 294 |
| 289 typedef Function<void(), SameThreadAffinity> Closure; | 295 typedef Function<void(), SameThreadAffinity> Closure; |
| 290 typedef Function<void(), CrossThreadAffinity> CrossThreadClosure; | 296 typedef Function<void(), CrossThreadAffinity> CrossThreadClosure; |
| 291 | 297 |
| 292 } // namespace WTF | 298 } // namespace WTF |
| 293 | 299 |
| 294 using WTF::crossThreadBind; | 300 using WTF::crossThreadBind; |
| 295 | 301 |
| 296 using WTF::passed; | 302 using WTF::passed; |
| 297 using WTF::unretained; | 303 using WTF::unretained; |
| 298 using WTF::crossThreadUnretained; | 304 using WTF::crossThreadUnretained; |
| 299 | 305 |
| 300 using WTF::Function; | 306 using WTF::Function; |
| 301 using WTF::CrossThreadClosure; | 307 using WTF::CrossThreadClosure; |
| 302 | 308 |
| 303 #endif // WTF_Functional_h | 309 #endif // WTF_Functional_h |
| OLD | NEW |