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 |
11 * documentation and/or other materials provided with the distribution. | 11 * documentation and/or other materials provided with the distribution. |
12 * | 12 * |
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' | 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS | 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
23 * THE POSSIBILITY OF SUCH DAMAGE. | 23 * THE POSSIBILITY OF SUCH DAMAGE. |
24 */ | 24 */ |
25 | 25 |
26 #ifndef WTF_Functional_h | 26 #ifndef WTF_Functional_h |
27 #define WTF_Functional_h | 27 #define WTF_Functional_h |
28 | 28 |
29 #include "wtf/Assertions.h" | 29 #include "wtf/Assertions.h" |
| 30 #include "wtf/PassOwnPtr.h" |
30 #include "wtf/PassRefPtr.h" | 31 #include "wtf/PassRefPtr.h" |
31 #include "wtf/RefPtr.h" | 32 #include "wtf/RefPtr.h" |
32 #include "wtf/ThreadSafeRefCounted.h" | 33 #include "wtf/ThreadSafeRefCounted.h" |
33 #include "wtf/WeakPtr.h" | 34 #include "wtf/WeakPtr.h" |
34 | 35 |
35 namespace WTF { | 36 namespace WTF { |
36 | 37 |
37 // Functional.h provides a very simple way to bind a function pointer and argume
nts together into a function object | 38 // Functional.h provides a very simple way to bind a function pointer and argume
nts together into a function object |
38 // that can be stored, copied and invoked, similar to how boost::bind and std::b
ind in C++11. | 39 // that can be stored, copied and invoked, similar to how boost::bind and std::b
ind in C++11. |
39 | 40 |
(...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
429 FunctionWrapper m_functionWrapper; | 430 FunctionWrapper m_functionWrapper; |
430 typename ParamStorageTraits<P1>::StorageType m_p1; | 431 typename ParamStorageTraits<P1>::StorageType m_p1; |
431 typename ParamStorageTraits<P2>::StorageType m_p2; | 432 typename ParamStorageTraits<P2>::StorageType m_p2; |
432 typename ParamStorageTraits<P3>::StorageType m_p3; | 433 typename ParamStorageTraits<P3>::StorageType m_p3; |
433 typename ParamStorageTraits<P4>::StorageType m_p4; | 434 typename ParamStorageTraits<P4>::StorageType m_p4; |
434 typename ParamStorageTraits<P5>::StorageType m_p5; | 435 typename ParamStorageTraits<P5>::StorageType m_p5; |
435 typename ParamStorageTraits<P6>::StorageType m_p6; | 436 typename ParamStorageTraits<P6>::StorageType m_p6; |
436 }; | 437 }; |
437 | 438 |
438 class FunctionBase { | 439 class FunctionBase { |
| 440 WTF_MAKE_NONCOPYABLE(FunctionBase); |
439 public: | 441 public: |
440 bool isNull() const | 442 bool isNull() const |
441 { | 443 { |
442 return !m_impl; | 444 return !m_impl; |
443 } | 445 } |
444 | 446 |
445 protected: | 447 protected: |
446 FunctionBase() | 448 FunctionBase() |
447 { | 449 { |
448 } | 450 } |
(...skipping 28 matching lines...) Expand all Loading... |
477 } | 479 } |
478 | 480 |
479 R operator()(A... args) const | 481 R operator()(A... args) const |
480 { | 482 { |
481 ASSERT(!isNull()); | 483 ASSERT(!isNull()); |
482 return impl<R(A...)>()->operator()(args...); | 484 return impl<R(A...)>()->operator()(args...); |
483 } | 485 } |
484 }; | 486 }; |
485 | 487 |
486 template<typename FunctionType, typename... A> | 488 template<typename FunctionType, typename... A> |
487 Function<typename FunctionWrapper<FunctionType>::ResultType()> bind(FunctionType
function, const A... args) | 489 PassOwnPtr<Function<typename FunctionWrapper<FunctionType>::ResultType()>> bind(
FunctionType function, const A... args) |
488 { | 490 { |
489 return Function<typename FunctionWrapper<FunctionType>::ResultType()>(adoptR
ef(new BoundFunctionImpl<FunctionWrapper<FunctionType>, typename FunctionWrapper
<FunctionType>::ResultType (A...)>(FunctionWrapper<FunctionType>(function), args
...))); | 491 return adoptPtr(new Function<typename FunctionWrapper<FunctionType>::ResultT
ype()>(adoptRef(new BoundFunctionImpl<FunctionWrapper<FunctionType>, typename Fu
nctionWrapper<FunctionType>::ResultType (A...)>(FunctionWrapper<FunctionType>(fu
nction), args...)))); |
490 } | 492 } |
491 | 493 |
492 // Partial parameter binding. | 494 // Partial parameter binding. |
493 | 495 |
494 template<typename A1, typename FunctionType, typename... A> | 496 template<typename A1, typename FunctionType, typename... A> |
495 Function<typename FunctionWrapper<FunctionType>::ResultType(A1)> bind(FunctionTy
pe function, const A&... args) | 497 PassOwnPtr<Function<typename FunctionWrapper<FunctionType>::ResultType(A1)>> bin
d(FunctionType function, const A&... args) |
496 { | 498 { |
497 const int boundArgsCount = sizeof...(A); | 499 const int boundArgsCount = sizeof...(A); |
498 return Function<typename FunctionWrapper<FunctionType>::ResultType(A1)>(adop
tRef(new PartBoundFunctionImpl<boundArgsCount, FunctionWrapper<FunctionType>, ty
pename FunctionWrapper<FunctionType>::ResultType (A..., A1)>(FunctionWrapper<Fun
ctionType>(function), args...))); | 500 return adoptPtr(new Function<typename FunctionWrapper<FunctionType>::ResultT
ype(A1)>(adoptRef(new PartBoundFunctionImpl<boundArgsCount, FunctionWrapper<Func
tionType>, typename FunctionWrapper<FunctionType>::ResultType (A..., A1)>(Functi
onWrapper<FunctionType>(function), args...)))); |
499 } | 501 } |
500 | 502 |
501 template<typename A1, typename A2, typename FunctionType, typename... A> | 503 template<typename A1, typename A2, typename FunctionType, typename... A> |
502 Function<typename FunctionWrapper<FunctionType>::ResultType(A1, A2)> bind(Functi
onType function, const A&... args) | 504 PassOwnPtr<Function<typename FunctionWrapper<FunctionType>::ResultType(A1, A2)>>
bind(FunctionType function, const A&... args) |
503 { | 505 { |
504 const int boundArgsCount = sizeof...(A); | 506 const int boundArgsCount = sizeof...(A); |
505 return Function<typename FunctionWrapper<FunctionType>::ResultType(A1, A2)>(
adoptRef(new PartBoundFunctionImpl<boundArgsCount, FunctionWrapper<FunctionType>
, typename FunctionWrapper<FunctionType>::ResultType (A..., A1, A2)>(FunctionWra
pper<FunctionType>(function), args...))); | 507 return adoptPtr(new Function<typename FunctionWrapper<FunctionType>::ResultT
ype(A1, A2)>(adoptRef(new PartBoundFunctionImpl<boundArgsCount, FunctionWrapper<
FunctionType>, typename FunctionWrapper<FunctionType>::ResultType (A..., A1, A2)
>(FunctionWrapper<FunctionType>(function), args...)))); |
506 } | 508 } |
507 | 509 |
508 template<typename A1, typename A2, typename A3, typename FunctionType, typename.
.. A> | 510 template<typename A1, typename A2, typename A3, typename FunctionType, typename.
.. A> |
509 Function<typename FunctionWrapper<FunctionType>::ResultType(A1, A2, A3)> bind(Fu
nctionType function, const A&... args) | 511 PassOwnPtr<Function<typename FunctionWrapper<FunctionType>::ResultType(A1, A2, A
3)>> bind(FunctionType function, const A&... args) |
510 { | 512 { |
511 const int boundArgsCount = sizeof...(A); | 513 const int boundArgsCount = sizeof...(A); |
512 return Function<typename FunctionWrapper<FunctionType>::ResultType(A1, A2, A
3)>(adoptRef(new PartBoundFunctionImpl<boundArgsCount, FunctionWrapper<FunctionT
ype>, typename FunctionWrapper<FunctionType>::ResultType (A..., A1, A2, A3)>(Fun
ctionWrapper<FunctionType>(function), args...))); | 514 return adoptPtr(new Function<typename FunctionWrapper<FunctionType>::ResultT
ype(A1, A2, A3)>(adoptRef(new PartBoundFunctionImpl<boundArgsCount, FunctionWrap
per<FunctionType>, typename FunctionWrapper<FunctionType>::ResultType (A..., A1,
A2, A3)>(FunctionWrapper<FunctionType>(function), args...)))); |
513 } | 515 } |
514 | 516 |
515 template<typename A1, typename A2, typename A3, typename A4, typename FunctionTy
pe, typename... A> | 517 template<typename A1, typename A2, typename A3, typename A4, typename FunctionTy
pe, typename... A> |
516 Function<typename FunctionWrapper<FunctionType>::ResultType(A1, A2, A3, A4)> bin
d(FunctionType function, const A&... args) | 518 PassOwnPtr<Function<typename FunctionWrapper<FunctionType>::ResultType(A1, A2, A
3, A4)>> bind(FunctionType function, const A&... args) |
517 { | 519 { |
518 const int boundArgsCount = sizeof...(A); | 520 const int boundArgsCount = sizeof...(A); |
519 return Function<typename FunctionWrapper<FunctionType>::ResultType(A1, A2, A
3, A4)>(adoptRef(new PartBoundFunctionImpl<boundArgsCount, FunctionWrapper<Funct
ionType>, typename FunctionWrapper<FunctionType>::ResultType (A..., A1, A2, A3,
A4)>(FunctionWrapper<FunctionType>(function), args...))); | 521 return adoptPtr(new Function<typename FunctionWrapper<FunctionType>::ResultT
ype(A1, A2, A3, A4)>(adoptRef(new PartBoundFunctionImpl<boundArgsCount, Function
Wrapper<FunctionType>, typename FunctionWrapper<FunctionType>::ResultType (A...,
A1, A2, A3, A4)>(FunctionWrapper<FunctionType>(function), args...)))); |
520 } | 522 } |
521 | 523 |
522 template<typename A1, typename A2, typename A3, typename A4, typename A5, typena
me FunctionType, typename... A> | 524 template<typename A1, typename A2, typename A3, typename A4, typename A5, typena
me FunctionType, typename... A> |
523 Function<typename FunctionWrapper<FunctionType>::ResultType(A1, A2, A3, A4, A5)>
bind(FunctionType function, const A&... args) | 525 PassOwnPtr<Function<typename FunctionWrapper<FunctionType>::ResultType(A1, A2, A
3, A4, A5)>> bind(FunctionType function, const A&... args) |
524 { | 526 { |
525 const int boundArgsCount = sizeof...(A); | 527 const int boundArgsCount = sizeof...(A); |
526 return Function<typename FunctionWrapper<FunctionType>::ResultType(A1, A2, A
3, A4, A5)>(adoptRef(new PartBoundFunctionImpl<boundArgsCount, FunctionWrapper<F
unctionType>, typename FunctionWrapper<FunctionType>::ResultType (A..., A1, A2,
A3, A4, A5)>(FunctionWrapper<FunctionType>(function), args...))); | 528 return adoptPtr(new Function<typename FunctionWrapper<FunctionType>::ResultT
ype(A1, A2, A3, A4, A5)>(adoptRef(new PartBoundFunctionImpl<boundArgsCount, Func
tionWrapper<FunctionType>, typename FunctionWrapper<FunctionType>::ResultType (A
..., A1, A2, A3, A4, A5)>(FunctionWrapper<FunctionType>(function), args...)))); |
527 } | 529 } |
528 | 530 |
529 template<typename A1, typename A2, typename A3, typename A4, typename A5, typena
me A6, typename FunctionType, typename... A> | 531 template<typename A1, typename A2, typename A3, typename A4, typename A5, typena
me A6, typename FunctionType, typename... A> |
530 Function<typename FunctionWrapper<FunctionType>::ResultType(A1, A2, A3, A4, A5,
A6)> bind(FunctionType function, const A&... args) | 532 PassOwnPtr<Function<typename FunctionWrapper<FunctionType>::ResultType(A1, A2, A
3, A4, A5, A6)>> bind(FunctionType function, const A&... args) |
531 { | 533 { |
532 const int boundArgsCount = sizeof...(A); | 534 const int boundArgsCount = sizeof...(A); |
533 return Function<typename FunctionWrapper<FunctionType>::ResultType(A1, A2, A
3, A4, A5, A6)>(adoptRef(new PartBoundFunctionImpl<boundArgsCount, FunctionWrapp
er<FunctionType>, typename FunctionWrapper<FunctionType>::ResultType (A..., A1,
A2, A3, A4, A5, A6)>(FunctionWrapper<FunctionType>(function), args...))); | 535 return adoptPtr(new Function<typename FunctionWrapper<FunctionType>::ResultT
ype(A1, A2, A3, A4, A5, A6)>(adoptRef(new PartBoundFunctionImpl<boundArgsCount,
FunctionWrapper<FunctionType>, typename FunctionWrapper<FunctionType>::ResultTyp
e (A..., A1, A2, A3, A4, A5, A6)>(FunctionWrapper<FunctionType>(function), args.
..)))); |
534 } | 536 } |
535 | 537 |
536 typedef Function<void()> Closure; | 538 typedef Function<void()> Closure; |
537 | 539 |
538 } | 540 } |
539 | 541 |
540 using WTF::Function; | 542 using WTF::Function; |
541 using WTF::bind; | 543 using WTF::bind; |
542 using WTF::Closure; | 544 using WTF::Closure; |
543 | 545 |
544 #endif // WTF_Functional_h | 546 #endif // WTF_Functional_h |
OLD | NEW |