1/*
2 * Copyright (C) 2011 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
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,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
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
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
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef WTF_Functional_h
27#define WTF_Functional_h
28
29#include "wtf/Assertions.h"
30#include "wtf/PassRefPtr.h"
31#include "wtf/RefPtr.h"
32#include "wtf/ThreadSafeRefCounted.h"
33#include "wtf/WeakPtr.h"
34
35namespace WTF {
36
37// Functional.h provides a very simple way to bind a function pointer and arguments together into a function object
38// that can be stored, copied and invoked, similar to how boost::bind and std::bind in C++11.
39
40// A FunctionWrapper is a class template that can wrap a function pointer or a member function pointer and
41// provide a unified interface for calling that function.
42template<typename>
43class FunctionWrapper;
44
45// Bound static functions:
46
47template<typename R>
48class FunctionWrapper<R(*)()> {
49public:
50    typedef R ResultType;
51
52    explicit FunctionWrapper(R(*function)())
53        : m_function(function)
54    {
55    }
56
57    R operator()()
58    {
59        return m_function();
60    }
61
62private:
63    R(*m_function)();
64};
65
66template<typename R, typename P1>
67class FunctionWrapper<R(*)(P1)> {
68public:
69    typedef R ResultType;
70
71    explicit FunctionWrapper(R(*function)(P1))
72        : m_function(function)
73    {
74    }
75
76    R operator()(P1 p1)
77    {
78        return m_function(p1);
79    }
80
81private:
82    R(*m_function)(P1);
83};
84
85template<typename R, typename P1, typename P2>
86class FunctionWrapper<R(*)(P1, P2)> {
87public:
88    typedef R ResultType;
89
90    explicit FunctionWrapper(R(*function)(P1, P2))
91        : m_function(function)
92    {
93    }
94
95    R operator()(P1 p1, P2 p2)
96    {
97        return m_function(p1, p2);
98    }
99
100private:
101    R(*m_function)(P1, P2);
102};
103
104template<typename R, typename P1, typename P2, typename P3>
105class FunctionWrapper<R(*)(P1, P2, P3)> {
106public:
107    typedef R ResultType;
108
109    explicit FunctionWrapper(R(*function)(P1, P2, P3))
110        : m_function(function)
111    {
112    }
113
114    R operator()(P1 p1, P2 p2, P3 p3)
115    {
116        return m_function(p1, p2, p3);
117    }
118
119private:
120    R(*m_function)(P1, P2, P3);
121};
122
123template<typename R, typename P1, typename P2, typename P3, typename P4>
124class FunctionWrapper<R(*)(P1, P2, P3, P4)> {
125public:
126    typedef R ResultType;
127
128    explicit FunctionWrapper(R(*function)(P1, P2, P3, P4))
129        : m_function(function)
130    {
131    }
132
133    R operator()(P1 p1, P2 p2, P3 p3, P4 p4)
134    {
135        return m_function(p1, p2, p3, p4);
136    }
137
138private:
139    R(*m_function)(P1, P2, P3, P4);
140};
141
142template<typename R, typename P1, typename P2, typename P3, typename P4, typename P5>
143class FunctionWrapper<R(*)(P1, P2, P3, P4, P5)> {
144public:
145    typedef R ResultType;
146
147    explicit FunctionWrapper(R(*function)(P1, P2, P3, P4, P5))
148        : m_function(function)
149    {
150    }
151
152    R operator()(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5)
153    {
154        return m_function(p1, p2, p3, p4, p5);
155    }
156
157private:
158    R(*m_function)(P1, P2, P3, P4, P5);
159};
160
161template<typename R, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6>
162class FunctionWrapper<R(*)(P1, P2, P3, P4, P5, P6)> {
163public:
164    typedef R ResultType;
165
166    explicit FunctionWrapper(R(*function)(P1, P2, P3, P4, P5, P6))
167        : m_function(function)
168    {
169    }
170
171    R operator()(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6)
172    {
173        return m_function(p1, p2, p3, p4, p5, p6);
174    }
175
176private:
177    R(*m_function)(P1, P2, P3, P4, P5, P6);
178};
179
180// Bound member functions:
181
182template<typename R, typename C>
183class FunctionWrapper<R(C::*)()> {
184public:
185    typedef R ResultType;
186
187    explicit FunctionWrapper(R(C::*function)())
188        : m_function(function)
189    {
190    }
191
192    R operator()(C* c)
193    {
194        return (c->*m_function)();
195    }
196
197    R operator()(const WeakPtr<C>& c)
198    {
199        C* obj = c.get();
200        if (!obj)
201            return R();
202        return (obj->*m_function)();
203    }
204
205private:
206    R(C::*m_function)();
207};
208
209template<typename R, typename C, typename P1>
210class FunctionWrapper<R(C::*)(P1)> {
211public:
212    typedef R ResultType;
213
214    explicit FunctionWrapper(R(C::*function)(P1))
215        : m_function(function)
216    {
217    }
218
219    R operator()(C* c, P1 p1)
220    {
221        return (c->*m_function)(p1);
222    }
223
224    R operator()(const WeakPtr<C>& c, P1 p1)
225    {
226        C* obj = c.get();
227        if (!obj)
228            return R();
229        return (obj->*m_function)(p1);
230    }
231
232private:
233    R(C::*m_function)(P1);
234};
235
236template<typename R, typename C, typename P1, typename P2>
237class FunctionWrapper<R(C::*)(P1, P2)> {
238public:
239    typedef R ResultType;
240
241    explicit FunctionWrapper(R(C::*function)(P1, P2))
242        : m_function(function)
243    {
244    }
245
246    R operator()(C* c, P1 p1, P2 p2)
247    {
248        return (c->*m_function)(p1, p2);
249    }
250
251    R operator()(const WeakPtr<C>& c, P1 p1, P2 p2)
252    {
253        C* obj = c.get();
254        if (!obj)
255            return R();
256        return (obj->*m_function)(p1, p2);
257    }
258
259private:
260    R(C::*m_function)(P1, P2);
261};
262
263template<typename R, typename C, typename P1, typename P2, typename P3>
264class FunctionWrapper<R(C::*)(P1, P2, P3)> {
265public:
266    typedef R ResultType;
267
268    explicit FunctionWrapper(R(C::*function)(P1, P2, P3))
269        : m_function(function)
270    {
271    }
272
273    R operator()(C* c, P1 p1, P2 p2, P3 p3)
274    {
275        return (c->*m_function)(p1, p2, p3);
276    }
277
278    R operator()(const WeakPtr<C>& c, P1 p1, P2 p2, P3 p3)
279    {
280        C* obj = c.get();
281        if (!obj)
282            return R();
283        return (obj->*m_function)(p1, p2, p3);
284    }
285
286private:
287    R(C::*m_function)(P1, P2, P3);
288};
289
290template<typename R, typename C, typename P1, typename P2, typename P3, typename P4>
291class FunctionWrapper<R(C::*)(P1, P2, P3, P4)> {
292public:
293    typedef R ResultType;
294
295    explicit FunctionWrapper(R(C::*function)(P1, P2, P3, P4))
296        : m_function(function)
297    {
298    }
299
300    R operator()(C* c, P1 p1, P2 p2, P3 p3, P4 p4)
301    {
302        return (c->*m_function)(p1, p2, p3, p4);
303    }
304
305    R operator()(const WeakPtr<C>& c, P1 p1, P2 p2, P3 p3, P4 p4)
306    {
307        C* obj = c.get();
308        if (!obj)
309            return R();
310        return (obj->*m_function)(p1, p2, p3, p4);
311    }
312
313private:
314    R(C::*m_function)(P1, P2, P3, P4);
315};
316
317template<typename R, typename C, typename P1, typename P2, typename P3, typename P4, typename P5>
318class FunctionWrapper<R(C::*)(P1, P2, P3, P4, P5)> {
319public:
320    typedef R ResultType;
321
322    explicit FunctionWrapper(R(C::*function)(P1, P2, P3, P4, P5))
323        : m_function(function)
324    {
325    }
326
327    R operator()(C* c, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5)
328    {
329        return (c->*m_function)(p1, p2, p3, p4, p5);
330    }
331
332    R operator()(const WeakPtr<C>& c, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5)
333    {
334        C* obj = c.get();
335        if (!obj)
336            return R();
337        return (obj->*m_function)(p1, p2, p3, p4, p5);
338    }
339
340private:
341    R(C::*m_function)(P1, P2, P3, P4, P5);
342};
343
344template<typename T> struct ParamStorageTraits {
345    typedef T StorageType;
346
347    static StorageType wrap(const T& value) { return value; }
348    static const T& unwrap(const StorageType& value) { return value; }
349};
350
351template<typename T> struct ParamStorageTraits<PassRefPtr<T> > {
352    typedef RefPtr<T> StorageType;
353
354    static StorageType wrap(PassRefPtr<T> value) { return value; }
355    static T* unwrap(const StorageType& value) { return value.get(); }
356};
357
358template<typename T> struct ParamStorageTraits<RefPtr<T> > {
359    typedef RefPtr<T> StorageType;
360
361    static StorageType wrap(RefPtr<T> value) { return value.release(); }
362    static T* unwrap(const StorageType& value) { return value.get(); }
363};
364
365template<typename> class RetainPtr;
366
367template<typename T> struct ParamStorageTraits<RetainPtr<T> > {
368    typedef RetainPtr<T> StorageType;
369
370    static StorageType wrap(const RetainPtr<T>& value) { return value; }
371    static typename RetainPtr<T>::PtrType unwrap(const StorageType& value) { return value.get(); }
372};
373
374class FunctionImplBase : public ThreadSafeRefCounted<FunctionImplBase> {
375public:
376    virtual ~FunctionImplBase() { }
377};
378
379template<typename>
380class FunctionImpl;
381
382template<typename R>
383class FunctionImpl<R()> : public FunctionImplBase {
384public:
385    virtual R operator()() = 0;
386};
387
388template<typename R, typename A1>
389class FunctionImpl<R(A1)> : public FunctionImplBase {
390public:
391    virtual R operator()(A1 a1) = 0;
392};
393
394template<typename R, typename A1, typename A2>
395class FunctionImpl<R(A1, A2)> : public FunctionImplBase {
396public:
397    virtual R operator()(A1 a1, A2 a2) = 0;
398};
399
400template<typename R, typename A1, typename A2, typename A3>
401class FunctionImpl<R(A1, A2, A3)> : public FunctionImplBase {
402public:
403    virtual R operator()(A1 a1, A2 a2, A3 a3) = 0;
404};
405
406template<typename R, typename A1, typename A2, typename A3, typename A4>
407class FunctionImpl<R(A1, A2, A3, A4)> : public FunctionImplBase {
408public:
409    virtual R operator()(A1 a1, A2 a2, A3 a3, A4 a4) = 0;
410};
411
412template<typename R, typename A1, typename A2, typename A3, typename A4, typename A5>
413class FunctionImpl<R(A1, A2, A3, A4, A5)> : public FunctionImplBase {
414public:
415    virtual R operator()(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5) = 0;
416};
417
418template<typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6>
419class FunctionImpl<R(A1, A2, A3, A4, A5, A6)> : public FunctionImplBase {
420public:
421    virtual R operator()(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6) = 0;
422};
423
424template<typename FunctionWrapper, typename FunctionType>
425class UnboundFunctionImpl;
426
427template<typename FunctionWrapper, typename R, typename P1>
428class UnboundFunctionImpl<FunctionWrapper, R(P1)> final : public FunctionImpl<typename FunctionWrapper::ResultType(P1)> {
429public:
430    UnboundFunctionImpl(FunctionWrapper functionWrapper)
431        : m_functionWrapper(functionWrapper)
432    {
433    }
434
435    virtual typename FunctionWrapper::ResultType operator()(P1 p1) override
436    {
437        return m_functionWrapper(p1);
438    }
439
440private:
441    FunctionWrapper m_functionWrapper;
442};
443
444template<typename FunctionWrapper, typename R, typename P1, typename P2>
445class UnboundFunctionImpl<FunctionWrapper, R(P1, P2)> final : public FunctionImpl<typename FunctionWrapper::ResultType(P1, P2)> {
446public:
447    UnboundFunctionImpl(FunctionWrapper functionWrapper)
448        : m_functionWrapper(functionWrapper)
449    {
450    }
451
452    virtual typename FunctionWrapper::ResultType operator()(P1 p1, P2 p2) override
453    {
454        return m_functionWrapper(p1, p2);
455    }
456
457private:
458    FunctionWrapper m_functionWrapper;
459};
460
461template<typename FunctionWrapper, typename R, typename P1, typename P2, typename P3>
462class UnboundFunctionImpl<FunctionWrapper, R(P1, P2, P3)> final : public FunctionImpl<typename FunctionWrapper::ResultType(P1, P2, P3)> {
463public:
464    UnboundFunctionImpl(FunctionWrapper functionWrapper)
465        : m_functionWrapper(functionWrapper)
466    {
467    }
468
469    virtual typename FunctionWrapper::ResultType operator()(P1 p1, P2 p2, P3 p3) override
470    {
471        return m_functionWrapper(p1, p2, p3);
472    }
473
474private:
475    FunctionWrapper m_functionWrapper;
476};
477
478template<typename FunctionWrapper, typename R, typename P1, typename P2, typename P3, typename P4>
479class UnboundFunctionImpl<FunctionWrapper, R(P1, P2, P3, P4)> final : public FunctionImpl<typename FunctionWrapper::ResultType(P1, P2, P3, P4)> {
480public:
481    UnboundFunctionImpl(FunctionWrapper functionWrapper)
482        : m_functionWrapper(functionWrapper)
483    {
484    }
485
486    virtual typename FunctionWrapper::ResultType operator()(P1 p1, P2 p2, P3 p3, P4 p4) override
487    {
488        return m_functionWrapper(p1, p2, p3, p4);
489    }
490
491private:
492    FunctionWrapper m_functionWrapper;
493};
494
495template<typename FunctionWrapper, typename R, typename P1, typename P2, typename P3, typename P4, typename P5>
496class UnboundFunctionImpl<FunctionWrapper, R(P1, P2, P3, P4, P5)> final : public FunctionImpl<typename FunctionWrapper::ResultType(P1, P2, P3, P4, P5)> {
497public:
498    UnboundFunctionImpl(FunctionWrapper functionWrapper)
499        : m_functionWrapper(functionWrapper)
500    {
501    }
502
503    virtual typename FunctionWrapper::ResultType operator()(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) override
504    {
505        return m_functionWrapper(p1, p2, p3, p4, p5);
506    }
507
508private:
509    FunctionWrapper m_functionWrapper;
510};
511
512template<typename FunctionWrapper, typename R, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6>
513class UnboundFunctionImpl<FunctionWrapper, R(P1, P2, P3, P4, P5, P6)> final : public FunctionImpl<typename FunctionWrapper::ResultType(P1, P2, P3, P4, P5, P6)> {
514public:
515    UnboundFunctionImpl(FunctionWrapper functionWrapper)
516        : m_functionWrapper(functionWrapper)
517    {
518    }
519
520    virtual typename FunctionWrapper::ResultType operator()(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6) override
521    {
522        return m_functionWrapper(p1, p2, p3, p4, p5, p6);
523    }
524
525private:
526    FunctionWrapper m_functionWrapper;
527};
528
529template<typename FunctionWrapper, typename FunctionType>
530class OneArgPartBoundFunctionImpl;
531
532template<typename FunctionWrapper, typename R, typename P1, typename P2>
533class OneArgPartBoundFunctionImpl<FunctionWrapper, R(P1, P2)> final : public FunctionImpl<typename FunctionWrapper::ResultType(P2)> {
534public:
535    OneArgPartBoundFunctionImpl(FunctionWrapper functionWrapper, const P1& p1)
536        : m_functionWrapper(functionWrapper)
537        , m_p1(ParamStorageTraits<P1>::wrap(p1))
538    {
539    }
540
541    virtual typename FunctionWrapper::ResultType operator()(P2 p2) override
542    {
543        return m_functionWrapper(m_p1, p2);
544    }
545
546private:
547    FunctionWrapper m_functionWrapper;
548    typename ParamStorageTraits<P1>::StorageType m_p1;
549};
550
551template<typename FunctionWrapper, typename R, typename P1, typename P2, typename P3>
552class OneArgPartBoundFunctionImpl<FunctionWrapper, R(P1, P2, P3)> final : public FunctionImpl<typename FunctionWrapper::ResultType(P2, P3)> {
553public:
554    OneArgPartBoundFunctionImpl(FunctionWrapper functionWrapper, const P1& p1)
555        : m_functionWrapper(functionWrapper)
556        , m_p1(ParamStorageTraits<P1>::wrap(p1))
557    {
558    }
559
560    virtual typename FunctionWrapper::ResultType operator()(P2 p2, P3 p3) override
561    {
562        return m_functionWrapper(m_p1, p2, p3);
563    }
564
565private:
566    FunctionWrapper m_functionWrapper;
567    typename ParamStorageTraits<P1>::StorageType m_p1;
568};
569
570template<typename FunctionWrapper, typename R, typename P1, typename P2, typename P3, typename P4>
571class OneArgPartBoundFunctionImpl<FunctionWrapper, R(P1, P2, P3, P4)> final : public FunctionImpl<typename FunctionWrapper::ResultType(P2, P3, P4)> {
572public:
573    OneArgPartBoundFunctionImpl(FunctionWrapper functionWrapper, const P1& p1)
574        : m_functionWrapper(functionWrapper)
575        , m_p1(ParamStorageTraits<P1>::wrap(p1))
576    {
577    }
578
579    virtual typename FunctionWrapper::ResultType operator()(P2 p2, P3 p3, P4 p4) override
580    {
581        return m_functionWrapper(m_p1, p2, p3, p4);
582    }
583
584private:
585    FunctionWrapper m_functionWrapper;
586    typename ParamStorageTraits<P1>::StorageType m_p1;
587};
588
589template<typename FunctionWrapper, typename R, typename P1, typename P2, typename P3, typename P4, typename P5>
590class OneArgPartBoundFunctionImpl<FunctionWrapper, R(P1, P2, P3, P4, P5)> final : public FunctionImpl<typename FunctionWrapper::ResultType(P2, P3, P4, P5)> {
591public:
592    OneArgPartBoundFunctionImpl(FunctionWrapper functionWrapper, const P1& p1)
593        : m_functionWrapper(functionWrapper)
594        , m_p1(ParamStorageTraits<P1>::wrap(p1))
595    {
596    }
597
598    virtual typename FunctionWrapper::ResultType operator()(P2 p2, P3 p3, P4 p4, P5 p5) override
599    {
600        return m_functionWrapper(m_p1, p2, p3, p4, p5);
601    }
602
603private:
604    FunctionWrapper m_functionWrapper;
605    typename ParamStorageTraits<P1>::StorageType m_p1;
606};
607
608template<typename FunctionWrapper, typename R, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6>
609class OneArgPartBoundFunctionImpl<FunctionWrapper, R(P1, P2, P3, P4, P5, P6)> final : public FunctionImpl<typename FunctionWrapper::ResultType(P2, P3, P4, P5, P6)> {
610public:
611    OneArgPartBoundFunctionImpl(FunctionWrapper functionWrapper, const P1& p1)
612        : m_functionWrapper(functionWrapper)
613        , m_p1(ParamStorageTraits<P1>::wrap(p1))
614    {
615    }
616
617    virtual typename FunctionWrapper::ResultType operator()(P2 p2, P3 p3, P4 p4, P5 p5, P6 p6) override
618    {
619        return m_functionWrapper(m_p1, p2, p3, p4, p5, p6);
620    }
621
622private:
623    FunctionWrapper m_functionWrapper;
624    typename ParamStorageTraits<P1>::StorageType m_p1;
625};
626
627template<typename FunctionWrapper, typename FunctionType>
628class TwoArgPartBoundFunctionImpl;
629
630template<typename FunctionWrapper, typename R, typename P1, typename P2, typename P3>
631class TwoArgPartBoundFunctionImpl<FunctionWrapper, R(P1, P2, P3)> final : public FunctionImpl<typename FunctionWrapper::ResultType(P3)> {
632public:
633    TwoArgPartBoundFunctionImpl(FunctionWrapper functionWrapper, const P1& p1, const P2& p2)
634        : m_functionWrapper(functionWrapper)
635        , m_p1(ParamStorageTraits<P1>::wrap(p1))
636        , m_p2(ParamStorageTraits<P2>::wrap(p2))
637    {
638    }
639
640    virtual typename FunctionWrapper::ResultType operator()(P3 p3) override
641    {
642        return m_functionWrapper(m_p1, m_p2, p3);
643    }
644
645private:
646    FunctionWrapper m_functionWrapper;
647    typename ParamStorageTraits<P1>::StorageType m_p1;
648    typename ParamStorageTraits<P2>::StorageType m_p2;
649};
650
651template<typename FunctionWrapper, typename R, typename P1, typename P2, typename P3, typename P4>
652class TwoArgPartBoundFunctionImpl<FunctionWrapper, R(P1, P2, P3, P4)> final : public FunctionImpl<typename FunctionWrapper::ResultType(P3, P4)> {
653public:
654    TwoArgPartBoundFunctionImpl(FunctionWrapper functionWrapper, const P1& p1, const P2& p2)
655        : m_functionWrapper(functionWrapper)
656        , m_p1(ParamStorageTraits<P1>::wrap(p1))
657        , m_p2(ParamStorageTraits<P2>::wrap(p2))
658    {
659    }
660
661    virtual typename FunctionWrapper::ResultType operator()(P3 p3, P4 p4) override
662    {
663        return m_functionWrapper(m_p1, m_p2, p3, p4);
664    }
665
666private:
667    FunctionWrapper m_functionWrapper;
668    typename ParamStorageTraits<P1>::StorageType m_p1;
669    typename ParamStorageTraits<P2>::StorageType m_p2;
670};
671
672template<typename FunctionWrapper, typename R, typename P1, typename P2, typename P3, typename P4, typename P5>
673class TwoArgPartBoundFunctionImpl<FunctionWrapper, R(P1, P2, P3, P4, P5)> final : public FunctionImpl<typename FunctionWrapper::ResultType(P3, P4, P5)> {
674public:
675    TwoArgPartBoundFunctionImpl(FunctionWrapper functionWrapper, const P1& p1, const P2& p2)
676        : m_functionWrapper(functionWrapper)
677        , m_p1(ParamStorageTraits<P1>::wrap(p1))
678        , m_p2(ParamStorageTraits<P2>::wrap(p2))
679    {
680    }
681
682    virtual typename FunctionWrapper::ResultType operator()(P3 p3, P4 p4, P5 p5) override
683    {
684        return m_functionWrapper(m_p1, m_p2, p3, p4, p5);
685    }
686
687private:
688    FunctionWrapper m_functionWrapper;
689    typename ParamStorageTraits<P1>::StorageType m_p1;
690    typename ParamStorageTraits<P2>::StorageType m_p2;
691};
692
693template<typename FunctionWrapper, typename R, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6>
694class TwoArgPartBoundFunctionImpl<FunctionWrapper, R(P1, P2, P3, P4, P5, P6)> final : public FunctionImpl<typename FunctionWrapper::ResultType(P3, P4, P5, P6)> {
695public:
696    TwoArgPartBoundFunctionImpl(FunctionWrapper functionWrapper, const P1& p1, const P2& p2)
697        : m_functionWrapper(functionWrapper)
698        , m_p1(ParamStorageTraits<P1>::wrap(p1))
699        , m_p2(ParamStorageTraits<P2>::wrap(p2))
700    {
701    }
702
703    virtual typename FunctionWrapper::ResultType operator()(P3 p3, P4 p4, P5 p5, P6 p6) override
704    {
705        return m_functionWrapper(m_p1, m_p2, p3, p4, p5, p6);
706    }
707
708private:
709    FunctionWrapper m_functionWrapper;
710    typename ParamStorageTraits<P1>::StorageType m_p1;
711    typename ParamStorageTraits<P2>::StorageType m_p2;
712};
713
714template<typename FunctionWrapper, typename FunctionType>
715class ThreeArgPartBoundFunctionImpl;
716
717template<typename FunctionWrapper, typename R, typename P1, typename P2, typename P3, typename P4>
718class ThreeArgPartBoundFunctionImpl<FunctionWrapper, R(P1, P2, P3, P4)> final : public FunctionImpl<typename FunctionWrapper::ResultType(P4)> {
719public:
720    ThreeArgPartBoundFunctionImpl(FunctionWrapper functionWrapper, const P1& p1, const P2& p2, const P3& p3)
721        : m_functionWrapper(functionWrapper)
722        , m_p1(ParamStorageTraits<P1>::wrap(p1))
723        , m_p2(ParamStorageTraits<P2>::wrap(p2))
724        , m_p3(ParamStorageTraits<P3>::wrap(p3))
725    {
726    }
727
728    virtual typename FunctionWrapper::ResultType operator()(P4 p4) override
729    {
730        return m_functionWrapper(m_p1, m_p2, m_p3, p4);
731    }
732
733private:
734    FunctionWrapper m_functionWrapper;
735    typename ParamStorageTraits<P1>::StorageType m_p1;
736    typename ParamStorageTraits<P2>::StorageType m_p2;
737    typename ParamStorageTraits<P3>::StorageType m_p3;
738};
739
740template<typename FunctionWrapper, typename R, typename P1, typename P2, typename P3, typename P4, typename P5>
741class ThreeArgPartBoundFunctionImpl<FunctionWrapper, R(P1, P2, P3, P4, P5)> final : public FunctionImpl<typename FunctionWrapper::ResultType(P4, P5)> {
742public:
743    ThreeArgPartBoundFunctionImpl(FunctionWrapper functionWrapper, const P1& p1, const P2& p2, const P3& p3)
744        : m_functionWrapper(functionWrapper)
745        , m_p1(ParamStorageTraits<P1>::wrap(p1))
746        , m_p2(ParamStorageTraits<P2>::wrap(p2))
747        , m_p3(ParamStorageTraits<P3>::wrap(p3))
748    {
749    }
750
751    virtual typename FunctionWrapper::ResultType operator()(P4 p4, P5 p5) override
752    {
753        return m_functionWrapper(m_p1, m_p2, m_p3, p4, p5);
754    }
755
756private:
757    FunctionWrapper m_functionWrapper;
758    typename ParamStorageTraits<P1>::StorageType m_p1;
759    typename ParamStorageTraits<P2>::StorageType m_p2;
760    typename ParamStorageTraits<P3>::StorageType m_p3;
761};
762
763template<typename FunctionWrapper, typename R, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6>
764class ThreeArgPartBoundFunctionImpl<FunctionWrapper, R(P1, P2, P3, P4, P5, P6)> final : public FunctionImpl<typename FunctionWrapper::ResultType(P4, P5, P6)> {
765public:
766    ThreeArgPartBoundFunctionImpl(FunctionWrapper functionWrapper, const P1& p1, const P2& p2, const P3& p3)
767        : m_functionWrapper(functionWrapper)
768        , m_p1(ParamStorageTraits<P1>::wrap(p1))
769        , m_p2(ParamStorageTraits<P2>::wrap(p2))
770        , m_p3(ParamStorageTraits<P3>::wrap(p3))
771    {
772    }
773
774    virtual typename FunctionWrapper::ResultType operator()(P4 p4, P5 p5, P6 p6) override
775    {
776        return m_functionWrapper(m_p1, m_p2, m_p3, p4, p5, p6);
777    }
778
779private:
780    FunctionWrapper m_functionWrapper;
781    typename ParamStorageTraits<P1>::StorageType m_p1;
782    typename ParamStorageTraits<P2>::StorageType m_p2;
783    typename ParamStorageTraits<P3>::StorageType m_p3;
784};
785
786template<typename FunctionWrapper, typename FunctionType>
787class FourArgPartBoundFunctionImpl;
788
789template<typename FunctionWrapper, typename R, typename P1, typename P2, typename P3, typename P4, typename P5>
790class FourArgPartBoundFunctionImpl<FunctionWrapper, R(P1, P2, P3, P4, P5)> final : public FunctionImpl<typename FunctionWrapper::ResultType(P5)> {
791public:
792    FourArgPartBoundFunctionImpl(FunctionWrapper functionWrapper, const P1& p1, const P2& p2, const P3& p3, const P4& p4)
793        : m_functionWrapper(functionWrapper)
794        , m_p1(ParamStorageTraits<P1>::wrap(p1))
795        , m_p2(ParamStorageTraits<P2>::wrap(p2))
796        , m_p3(ParamStorageTraits<P3>::wrap(p3))
797        , m_p4(ParamStorageTraits<P4>::wrap(p4))
798    {
799    }
800
801    virtual typename FunctionWrapper::ResultType operator()(P5 p5) override
802    {
803        return m_functionWrapper(m_p1, m_p2, m_p3, m_p4, p5);
804    }
805
806private:
807    FunctionWrapper m_functionWrapper;
808    typename ParamStorageTraits<P1>::StorageType m_p1;
809    typename ParamStorageTraits<P2>::StorageType m_p2;
810    typename ParamStorageTraits<P3>::StorageType m_p3;
811    typename ParamStorageTraits<P4>::StorageType m_p4;
812};
813
814template<typename FunctionWrapper, typename R, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6>
815class FourArgPartBoundFunctionImpl<FunctionWrapper, R(P1, P2, P3, P4, P5, P6)> final : public FunctionImpl<typename FunctionWrapper::ResultType(P5, P6)> {
816public:
817    FourArgPartBoundFunctionImpl(FunctionWrapper functionWrapper, const P1& p1, const P2& p2, const P3& p3, const P4& p4)
818        : m_functionWrapper(functionWrapper)
819        , m_p1(ParamStorageTraits<P1>::wrap(p1))
820        , m_p2(ParamStorageTraits<P2>::wrap(p2))
821        , m_p3(ParamStorageTraits<P3>::wrap(p3))
822        , m_p4(ParamStorageTraits<P4>::wrap(p4))
823    {
824    }
825
826    virtual typename FunctionWrapper::ResultType operator()(P5 p5, P6 p6) override
827    {
828        return m_functionWrapper(m_p1, m_p2, m_p3, m_p4, p5, p6);
829    }
830
831private:
832    FunctionWrapper m_functionWrapper;
833    typename ParamStorageTraits<P1>::StorageType m_p1;
834    typename ParamStorageTraits<P2>::StorageType m_p2;
835    typename ParamStorageTraits<P3>::StorageType m_p3;
836    typename ParamStorageTraits<P4>::StorageType m_p4;
837};
838
839template<typename FunctionWrapper, typename FunctionType>
840class FiveArgPartBoundFunctionImpl;
841
842template<typename FunctionWrapper, typename R, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6>
843class FiveArgPartBoundFunctionImpl<FunctionWrapper, R(P1, P2, P3, P4, P5, P6)> final : public FunctionImpl<typename FunctionWrapper::ResultType(P6)> {
844public:
845    FiveArgPartBoundFunctionImpl(FunctionWrapper functionWrapper, const P1& p1, const P2& p2, const P3& p3, const P4& p4, const P5& p5)
846        : m_functionWrapper(functionWrapper)
847        , m_p1(ParamStorageTraits<P1>::wrap(p1))
848        , m_p2(ParamStorageTraits<P2>::wrap(p2))
849        , m_p3(ParamStorageTraits<P3>::wrap(p3))
850        , m_p4(ParamStorageTraits<P4>::wrap(p4))
851        , m_p5(ParamStorageTraits<P5>::wrap(p5))
852    {
853    }
854
855    virtual typename FunctionWrapper::ResultType operator()(P6 p6) override
856    {
857        return m_functionWrapper(m_p1, m_p2, m_p3, m_p4, m_p5, p6);
858    }
859
860private:
861    FunctionWrapper m_functionWrapper;
862    typename ParamStorageTraits<P1>::StorageType m_p1;
863    typename ParamStorageTraits<P2>::StorageType m_p2;
864    typename ParamStorageTraits<P3>::StorageType m_p3;
865    typename ParamStorageTraits<P4>::StorageType m_p4;
866    typename ParamStorageTraits<P5>::StorageType m_p5;
867};
868
869template<typename FunctionWrapper, typename FunctionType>
870class BoundFunctionImpl;
871
872template<typename FunctionWrapper, typename R>
873class BoundFunctionImpl<FunctionWrapper, R()> final : public FunctionImpl<typename FunctionWrapper::ResultType()> {
874public:
875    explicit BoundFunctionImpl(FunctionWrapper functionWrapper)
876        : m_functionWrapper(functionWrapper)
877    {
878    }
879
880    virtual typename FunctionWrapper::ResultType operator()() override
881    {
882        return m_functionWrapper();
883    }
884
885private:
886    FunctionWrapper m_functionWrapper;
887};
888
889template<typename FunctionWrapper, typename R, typename P1>
890class BoundFunctionImpl<FunctionWrapper, R(P1)> final : public FunctionImpl<typename FunctionWrapper::ResultType()> {
891public:
892    BoundFunctionImpl(FunctionWrapper functionWrapper, const P1& p1)
893        : m_functionWrapper(functionWrapper)
894        , m_p1(ParamStorageTraits<P1>::wrap(p1))
895    {
896    }
897
898    virtual typename FunctionWrapper::ResultType operator()() override
899    {
900        return m_functionWrapper(ParamStorageTraits<P1>::unwrap(m_p1));
901    }
902
903private:
904    FunctionWrapper m_functionWrapper;
905    typename ParamStorageTraits<P1>::StorageType m_p1;
906};
907
908template<typename FunctionWrapper, typename R, typename P1, typename P2>
909class BoundFunctionImpl<FunctionWrapper, R(P1, P2)> final : public FunctionImpl<typename FunctionWrapper::ResultType()> {
910public:
911    BoundFunctionImpl(FunctionWrapper functionWrapper, const P1& p1, const P2& p2)
912        : m_functionWrapper(functionWrapper)
913        , m_p1(ParamStorageTraits<P1>::wrap(p1))
914        , m_p2(ParamStorageTraits<P2>::wrap(p2))
915    {
916    }
917
918    virtual typename FunctionWrapper::ResultType operator()() override
919    {
920        return m_functionWrapper(ParamStorageTraits<P1>::unwrap(m_p1), ParamStorageTraits<P2>::unwrap(m_p2));
921    }
922
923private:
924    FunctionWrapper m_functionWrapper;
925    typename ParamStorageTraits<P1>::StorageType m_p1;
926    typename ParamStorageTraits<P2>::StorageType m_p2;
927};
928
929template<typename FunctionWrapper, typename R, typename P1, typename P2, typename P3>
930class BoundFunctionImpl<FunctionWrapper, R(P1, P2, P3)> final : public FunctionImpl<typename FunctionWrapper::ResultType()> {
931public:
932    BoundFunctionImpl(FunctionWrapper functionWrapper, const P1& p1, const P2& p2, const P3& p3)
933        : m_functionWrapper(functionWrapper)
934        , m_p1(ParamStorageTraits<P1>::wrap(p1))
935        , m_p2(ParamStorageTraits<P2>::wrap(p2))
936        , m_p3(ParamStorageTraits<P3>::wrap(p3))
937    {
938    }
939
940    virtual typename FunctionWrapper::ResultType operator()() override
941    {
942        return m_functionWrapper(ParamStorageTraits<P1>::unwrap(m_p1), ParamStorageTraits<P2>::unwrap(m_p2), ParamStorageTraits<P3>::unwrap(m_p3));
943    }
944
945private:
946    FunctionWrapper m_functionWrapper;
947    typename ParamStorageTraits<P1>::StorageType m_p1;
948    typename ParamStorageTraits<P2>::StorageType m_p2;
949    typename ParamStorageTraits<P3>::StorageType m_p3;
950};
951
952template<typename FunctionWrapper, typename R, typename P1, typename P2, typename P3, typename P4>
953class BoundFunctionImpl<FunctionWrapper, R(P1, P2, P3, P4)> final : public FunctionImpl<typename FunctionWrapper::ResultType()> {
954public:
955    BoundFunctionImpl(FunctionWrapper functionWrapper, const P1& p1, const P2& p2, const P3& p3, const P4& p4)
956        : m_functionWrapper(functionWrapper)
957        , m_p1(ParamStorageTraits<P1>::wrap(p1))
958        , m_p2(ParamStorageTraits<P2>::wrap(p2))
959        , m_p3(ParamStorageTraits<P3>::wrap(p3))
960        , m_p4(ParamStorageTraits<P4>::wrap(p4))
961    {
962    }
963
964    virtual typename FunctionWrapper::ResultType operator()() override
965    {
966        return m_functionWrapper(ParamStorageTraits<P1>::unwrap(m_p1), ParamStorageTraits<P2>::unwrap(m_p2), ParamStorageTraits<P3>::unwrap(m_p3), ParamStorageTraits<P4>::unwrap(m_p4));
967    }
968
969private:
970    FunctionWrapper m_functionWrapper;
971    typename ParamStorageTraits<P1>::StorageType m_p1;
972    typename ParamStorageTraits<P2>::StorageType m_p2;
973    typename ParamStorageTraits<P3>::StorageType m_p3;
974    typename ParamStorageTraits<P4>::StorageType m_p4;
975};
976
977template<typename FunctionWrapper, typename R, typename P1, typename P2, typename P3, typename P4, typename P5>
978class BoundFunctionImpl<FunctionWrapper, R(P1, P2, P3, P4, P5)> final : public FunctionImpl<typename FunctionWrapper::ResultType()> {
979public:
980    BoundFunctionImpl(FunctionWrapper functionWrapper, const P1& p1, const P2& p2, const P3& p3, const P4& p4, const P5& p5)
981        : m_functionWrapper(functionWrapper)
982        , m_p1(ParamStorageTraits<P1>::wrap(p1))
983        , m_p2(ParamStorageTraits<P2>::wrap(p2))
984        , m_p3(ParamStorageTraits<P3>::wrap(p3))
985        , m_p4(ParamStorageTraits<P4>::wrap(p4))
986        , m_p5(ParamStorageTraits<P5>::wrap(p5))
987    {
988    }
989
990    virtual typename FunctionWrapper::ResultType operator()() override
991    {
992        return m_functionWrapper(ParamStorageTraits<P1>::unwrap(m_p1), ParamStorageTraits<P2>::unwrap(m_p2), ParamStorageTraits<P3>::unwrap(m_p3), ParamStorageTraits<P4>::unwrap(m_p4), ParamStorageTraits<P5>::unwrap(m_p5));
993    }
994
995private:
996    FunctionWrapper m_functionWrapper;
997    typename ParamStorageTraits<P1>::StorageType m_p1;
998    typename ParamStorageTraits<P2>::StorageType m_p2;
999    typename ParamStorageTraits<P3>::StorageType m_p3;
1000    typename ParamStorageTraits<P4>::StorageType m_p4;
1001    typename ParamStorageTraits<P5>::StorageType m_p5;
1002};
1003
1004template<typename FunctionWrapper, typename R, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6>
1005class BoundFunctionImpl<FunctionWrapper, R(P1, P2, P3, P4, P5, P6)> final : public FunctionImpl<typename FunctionWrapper::ResultType()> {
1006public:
1007    BoundFunctionImpl(FunctionWrapper functionWrapper, const P1& p1, const P2& p2, const P3& p3, const P4& p4, const P5& p5, const P6& p6)
1008        : m_functionWrapper(functionWrapper)
1009        , m_p1(ParamStorageTraits<P1>::wrap(p1))
1010        , m_p2(ParamStorageTraits<P2>::wrap(p2))
1011        , m_p3(ParamStorageTraits<P3>::wrap(p3))
1012        , m_p4(ParamStorageTraits<P4>::wrap(p4))
1013        , m_p5(ParamStorageTraits<P5>::wrap(p5))
1014        , m_p6(ParamStorageTraits<P6>::wrap(p6))
1015    {
1016    }
1017
1018    virtual typename FunctionWrapper::ResultType operator()() override
1019    {
1020        return m_functionWrapper(ParamStorageTraits<P1>::unwrap(m_p1), ParamStorageTraits<P2>::unwrap(m_p2), ParamStorageTraits<P3>::unwrap(m_p3), ParamStorageTraits<P4>::unwrap(m_p4), ParamStorageTraits<P5>::unwrap(m_p5), ParamStorageTraits<P6>::unwrap(m_p6));
1021    }
1022
1023private:
1024    FunctionWrapper m_functionWrapper;
1025    typename ParamStorageTraits<P1>::StorageType m_p1;
1026    typename ParamStorageTraits<P2>::StorageType m_p2;
1027    typename ParamStorageTraits<P3>::StorageType m_p3;
1028    typename ParamStorageTraits<P4>::StorageType m_p4;
1029    typename ParamStorageTraits<P5>::StorageType m_p5;
1030    typename ParamStorageTraits<P6>::StorageType m_p6;
1031};
1032
1033class FunctionBase {
1034public:
1035    bool isNull() const
1036    {
1037        return !m_impl;
1038    }
1039
1040protected:
1041    FunctionBase()
1042    {
1043    }
1044
1045    explicit FunctionBase(PassRefPtr<FunctionImplBase> impl)
1046        : m_impl(impl)
1047    {
1048    }
1049
1050    template<typename FunctionType> FunctionImpl<FunctionType>* impl() const
1051    {
1052        return static_cast<FunctionImpl<FunctionType>*>(m_impl.get());
1053    }
1054
1055private:
1056    RefPtr<FunctionImplBase> m_impl;
1057};
1058
1059template<typename>
1060class Function;
1061
1062template<typename R>
1063class Function<R()> : public FunctionBase {
1064public:
1065    Function()
1066    {
1067    }
1068
1069    Function(PassRefPtr<FunctionImpl<R()> > impl)
1070        : FunctionBase(impl)
1071    {
1072    }
1073
1074    R operator()() const
1075    {
1076        ASSERT(!isNull());
1077        return impl<R()>()->operator()();
1078    }
1079};
1080
1081template<typename R, typename A1>
1082class Function<R(A1)> : public FunctionBase {
1083public:
1084    Function()
1085    {
1086    }
1087
1088    Function(PassRefPtr<FunctionImpl<R(A1)> > impl)
1089        : FunctionBase(impl)
1090    {
1091    }
1092
1093    R operator()(A1 a1) const
1094    {
1095        ASSERT(!isNull());
1096        return impl<R(A1)>()->operator()(a1);
1097    }
1098};
1099
1100template<typename R, typename A1, typename A2>
1101class Function<R(A1, A2)> : public FunctionBase {
1102public:
1103    Function()
1104    {
1105    }
1106
1107    Function(PassRefPtr<FunctionImpl<R(A1, A2)> > impl)
1108        : FunctionBase(impl)
1109    {
1110    }
1111
1112    R operator()(A1 a1, A2 a2) const
1113    {
1114        ASSERT(!isNull());
1115        return impl<R(A1, A2)>()->operator()(a1, a2);
1116    }
1117};
1118
1119template<typename R, typename A1, typename A2, typename A3>
1120class Function<R(A1, A2, A3)> : public FunctionBase {
1121public:
1122    Function()
1123    {
1124    }
1125
1126    Function(PassRefPtr<FunctionImpl<R(A1, A2, A3)> > impl)
1127        : FunctionBase(impl)
1128    {
1129    }
1130
1131    R operator()(A1 a1, A2 a2, A3 a3) const
1132    {
1133        ASSERT(!isNull());
1134        return impl<R(A1, A2, A3)>()->operator()(a1, a2, a3);
1135    }
1136};
1137
1138template<typename R, typename A1, typename A2, typename A3, typename A4>
1139class Function<R(A1, A2, A3, A4)> : public FunctionBase {
1140public:
1141    Function()
1142    {
1143    }
1144
1145    Function(PassRefPtr<FunctionImpl<R(A1, A2, A3, A4)> > impl)
1146        : FunctionBase(impl)
1147    {
1148    }
1149
1150    R operator()(A1 a1, A2 a2, A3 a3, A4 a4) const
1151    {
1152        ASSERT(!isNull());
1153        return impl<R(A1, A2, A3, A4)>()->operator()(a1, a2, a3, a4);
1154    }
1155};
1156
1157template<typename R, typename A1, typename A2, typename A3, typename A4, typename A5>
1158class Function<R(A1, A2, A3, A4, A5)> : public FunctionBase {
1159public:
1160    Function()
1161    {
1162    }
1163
1164    Function(PassRefPtr<FunctionImpl<R(A1, A2, A3, A4, A5)> > impl)
1165        : FunctionBase(impl)
1166    {
1167    }
1168
1169    R operator()(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5) const
1170    {
1171        ASSERT(!isNull());
1172        return impl<R(A1, A2, A3, A4, A5)>()->operator()(a1, a2, a3, a4, a5);
1173    }
1174};
1175
1176template<typename R, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6>
1177class Function<R(A1, A2, A3, A4, A5, A6)> : public FunctionBase {
1178public:
1179    Function()
1180    {
1181    }
1182
1183    Function(PassRefPtr<FunctionImpl<R(A1, A2, A3, A4, A5, A6)> > impl)
1184        : FunctionBase(impl)
1185    {
1186    }
1187
1188    R operator()(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6) const
1189    {
1190        ASSERT(!isNull());
1191        return impl<R(A1, A2, A3, A4, A5, A6)>()->operator()(a1, a2, a3, a4, a5, a6);
1192    }
1193};
1194
1195template<typename FunctionType>
1196Function<typename FunctionWrapper<FunctionType>::ResultType()> bind(FunctionType function)
1197{
1198    return Function<typename FunctionWrapper<FunctionType>::ResultType()>(adoptRef(new BoundFunctionImpl<FunctionWrapper<FunctionType>, typename FunctionWrapper<FunctionType>::ResultType()>(FunctionWrapper<FunctionType>(function))));
1199}
1200
1201template<typename FunctionType, typename A1>
1202Function<typename FunctionWrapper<FunctionType>::ResultType()> bind(FunctionType function, const A1& a1)
1203{
1204    return Function<typename FunctionWrapper<FunctionType>::ResultType()>(adoptRef(new BoundFunctionImpl<FunctionWrapper<FunctionType>, typename FunctionWrapper<FunctionType>::ResultType (A1)>(FunctionWrapper<FunctionType>(function), a1)));
1205}
1206
1207template<typename FunctionType, typename A1, typename A2>
1208Function<typename FunctionWrapper<FunctionType>::ResultType()> bind(FunctionType function, const A1& a1, const A2& a2)
1209{
1210    return Function<typename FunctionWrapper<FunctionType>::ResultType()>(adoptRef(new BoundFunctionImpl<FunctionWrapper<FunctionType>, typename FunctionWrapper<FunctionType>::ResultType (A1, A2)>(FunctionWrapper<FunctionType>(function), a1, a2)));
1211}
1212
1213template<typename FunctionType, typename A1, typename A2, typename A3>
1214Function<typename FunctionWrapper<FunctionType>::ResultType()> bind(FunctionType function, const A1& a1, const A2& a2, const A3& a3)
1215{
1216    return Function<typename FunctionWrapper<FunctionType>::ResultType()>(adoptRef(new BoundFunctionImpl<FunctionWrapper<FunctionType>, typename FunctionWrapper<FunctionType>::ResultType (A1, A2, A3)>(FunctionWrapper<FunctionType>(function), a1, a2, a3)));
1217}
1218
1219template<typename FunctionType, typename A1, typename A2, typename A3, typename A4>
1220Function<typename FunctionWrapper<FunctionType>::ResultType()> bind(FunctionType function, const A1& a1, const A2& a2, const A3& a3, const A4& a4)
1221{
1222    return Function<typename FunctionWrapper<FunctionType>::ResultType()>(adoptRef(new BoundFunctionImpl<FunctionWrapper<FunctionType>, typename FunctionWrapper<FunctionType>::ResultType (A1, A2, A3, A4)>(FunctionWrapper<FunctionType>(function), a1, a2, a3, a4)));
1223}
1224
1225template<typename FunctionType, typename A1, typename A2, typename A3, typename A4, typename A5>
1226Function<typename FunctionWrapper<FunctionType>::ResultType()> bind(FunctionType function, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5)
1227{
1228    return Function<typename FunctionWrapper<FunctionType>::ResultType()>(adoptRef(new BoundFunctionImpl<FunctionWrapper<FunctionType>, typename FunctionWrapper<FunctionType>::ResultType (A1, A2, A3, A4, A5)>(FunctionWrapper<FunctionType>(function), a1, a2, a3, a4, a5)));
1229}
1230
1231template<typename FunctionType, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6>
1232Function<typename FunctionWrapper<FunctionType>::ResultType()> bind(FunctionType function, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6)
1233{
1234    return Function<typename FunctionWrapper<FunctionType>::ResultType()>(adoptRef(new BoundFunctionImpl<FunctionWrapper<FunctionType>, typename FunctionWrapper<FunctionType>::ResultType (A1, A2, A3, A4, A5, A6)>(FunctionWrapper<FunctionType>(function), a1, a2, a3, a4, a5, a6)));
1235}
1236
1237
1238// Partial parameter binding.
1239
1240template<typename A1, typename FunctionType>
1241Function<typename FunctionWrapper<FunctionType>::ResultType(A1)> bind(FunctionType function)
1242{
1243    return Function<typename FunctionWrapper<FunctionType>::ResultType(A1)>(adoptRef(new UnboundFunctionImpl<FunctionWrapper<FunctionType>, typename FunctionWrapper<FunctionType>::ResultType (A1)>(FunctionWrapper<FunctionType>(function))));
1244}
1245
1246template<typename A1, typename A2, typename FunctionType>
1247Function<typename FunctionWrapper<FunctionType>::ResultType(A1, A2)> bind(FunctionType function)
1248{
1249    return Function<typename FunctionWrapper<FunctionType>::ResultType(A1, A2)>(adoptRef(new UnboundFunctionImpl<FunctionWrapper<FunctionType>, typename FunctionWrapper<FunctionType>::ResultType (A1, A2)>(FunctionWrapper<FunctionType>(function))));
1250}
1251
1252template<typename A2, typename FunctionType, typename A1>
1253Function<typename FunctionWrapper<FunctionType>::ResultType(A2)> bind(FunctionType function, const A1& a1)
1254{
1255    return Function<typename FunctionWrapper<FunctionType>::ResultType(A2)>(adoptRef(new OneArgPartBoundFunctionImpl<FunctionWrapper<FunctionType>, typename FunctionWrapper<FunctionType>::ResultType (A1, A2)>(FunctionWrapper<FunctionType>(function), a1)));
1256}
1257
1258template<typename A1, typename A2, typename A3, typename FunctionType>
1259Function<typename FunctionWrapper<FunctionType>::ResultType(A1, A2, A3)> bind(FunctionType function)
1260{
1261    return Function<typename FunctionWrapper<FunctionType>::ResultType(A1, A2, A3)>(adoptRef(new UnboundFunctionImpl<FunctionWrapper<FunctionType>, typename FunctionWrapper<FunctionType>::ResultType (A1, A2, A3)>(FunctionWrapper<FunctionType>(function))));
1262}
1263
1264template<typename A2, typename A3, typename FunctionType, typename A1>
1265Function<typename FunctionWrapper<FunctionType>::ResultType(A2, A3)> bind(FunctionType function, const A1& a1)
1266{
1267    return Function<typename FunctionWrapper<FunctionType>::ResultType(A2, A3)>(adoptRef(new OneArgPartBoundFunctionImpl<FunctionWrapper<FunctionType>, typename FunctionWrapper<FunctionType>::ResultType (A1, A2, A3)>(FunctionWrapper<FunctionType>(function), a1)));
1268}
1269
1270template<typename A3, typename FunctionType, typename A1, typename A2>
1271Function<typename FunctionWrapper<FunctionType>::ResultType(A3)> bind(FunctionType function, const A1& a1, const A2& a2)
1272{
1273    return Function<typename FunctionWrapper<FunctionType>::ResultType(A3)>(adoptRef(new TwoArgPartBoundFunctionImpl<FunctionWrapper<FunctionType>, typename FunctionWrapper<FunctionType>::ResultType (A1, A2, A3)>(FunctionWrapper<FunctionType>(function), a1, a2)));
1274}
1275
1276template<typename A1, typename A2, typename A3, typename A4, typename FunctionType>
1277Function<typename FunctionWrapper<FunctionType>::ResultType(A1, A2, A3, A4)> bind(FunctionType function)
1278{
1279    return Function<typename FunctionWrapper<FunctionType>::ResultType(A1, A2, A3, A4)>(adoptRef(new UnboundFunctionImpl<FunctionWrapper<FunctionType>, typename FunctionWrapper<FunctionType>::ResultType (A1, A2, A3, A4)>(FunctionWrapper<FunctionType>(function))));
1280}
1281
1282template<typename A2, typename A3, typename A4, typename FunctionType, typename A1>
1283Function<typename FunctionWrapper<FunctionType>::ResultType(A2, A3, A4)> bind(FunctionType function, const A1& a1)
1284{
1285    return Function<typename FunctionWrapper<FunctionType>::ResultType(A2, A3, A4)>(adoptRef(new OneArgPartBoundFunctionImpl<FunctionWrapper<FunctionType>, typename FunctionWrapper<FunctionType>::ResultType (A1, A2, A3, A4)>(FunctionWrapper<FunctionType>(function), a1)));
1286}
1287
1288template<typename A3, typename A4, typename FunctionType, typename A1, typename A2>
1289Function<typename FunctionWrapper<FunctionType>::ResultType(A3, A4)> bind(FunctionType function, const A1& a1, const A2& a2)
1290{
1291    return Function<typename FunctionWrapper<FunctionType>::ResultType(A3, A4)>(adoptRef(new TwoArgPartBoundFunctionImpl<FunctionWrapper<FunctionType>, typename FunctionWrapper<FunctionType>::ResultType (A1, A2, A3, A4)>(FunctionWrapper<FunctionType>(function), a1, a2)));
1292}
1293
1294template<typename A4, typename FunctionType, typename A1, typename A2, typename A3>
1295Function<typename FunctionWrapper<FunctionType>::ResultType(A4)> bind(FunctionType function, const A1& a1, const A2& a2, const A3& a3)
1296{
1297    return Function<typename FunctionWrapper<FunctionType>::ResultType(A4)>(adoptRef(new ThreeArgPartBoundFunctionImpl<FunctionWrapper<FunctionType>, typename FunctionWrapper<FunctionType>::ResultType (A1, A2, A3, A4)>(FunctionWrapper<FunctionType>(function), a1, a2, a3)));
1298}
1299
1300template<typename A1, typename A2, typename A3, typename A4, typename A5, typename FunctionType>
1301Function<typename FunctionWrapper<FunctionType>::ResultType(A1, A2, A3, A4, A5)> bind(FunctionType function)
1302{
1303    return Function<typename FunctionWrapper<FunctionType>::ResultType(A1, A2, A3, A4, A5)>(adoptRef(new UnboundFunctionImpl<FunctionWrapper<FunctionType>, typename FunctionWrapper<FunctionType>::ResultType (A1, A2, A3, A4, A5)>(FunctionWrapper<FunctionType>(function))));
1304}
1305
1306template<typename A2, typename A3, typename A4, typename A5, typename FunctionType, typename A1>
1307Function<typename FunctionWrapper<FunctionType>::ResultType(A2, A3, A4, A5)> bind(FunctionType function, const A1& a1)
1308{
1309    return Function<typename FunctionWrapper<FunctionType>::ResultType(A2, A3, A4, A5)>(adoptRef(new OneArgPartBoundFunctionImpl<FunctionWrapper<FunctionType>, typename FunctionWrapper<FunctionType>::ResultType (A1, A2, A3, A4, A5)>(FunctionWrapper<FunctionType>(function), a1)));
1310}
1311
1312template<typename A3, typename A4, typename A5, typename FunctionType, typename A1, typename A2>
1313Function<typename FunctionWrapper<FunctionType>::ResultType(A3, A4, A5)> bind(FunctionType function, const A1& a1, const A2& a2)
1314{
1315    return Function<typename FunctionWrapper<FunctionType>::ResultType(A3, A4, A5)>(adoptRef(new TwoArgPartBoundFunctionImpl<FunctionWrapper<FunctionType>, typename FunctionWrapper<FunctionType>::ResultType (A1, A2, A3, A4, A5)>(FunctionWrapper<FunctionType>(function), a1, a2)));
1316}
1317
1318template<typename A4, typename A5, typename FunctionType, typename A1, typename A2, typename A3>
1319Function<typename FunctionWrapper<FunctionType>::ResultType(A4, A5)> bind(FunctionType function, const A1& a1, const A2& a2, const A3& a3)
1320{
1321    return Function<typename FunctionWrapper<FunctionType>::ResultType(A4, A5)>(adoptRef(new ThreeArgPartBoundFunctionImpl<FunctionWrapper<FunctionType>, typename FunctionWrapper<FunctionType>::ResultType (A1, A2, A3, A4, A5)>(FunctionWrapper<FunctionType>(function), a1, a2, a3)));
1322}
1323
1324template<typename A5, typename FunctionType, typename A1, typename A2, typename A3, typename A4>
1325Function<typename FunctionWrapper<FunctionType>::ResultType(A5)> bind(FunctionType function, const A1& a1, const A2& a2, const A3& a3, const A4& a4)
1326{
1327    return Function<typename FunctionWrapper<FunctionType>::ResultType(A5)>(adoptRef(new FourArgPartBoundFunctionImpl<FunctionWrapper<FunctionType>, typename FunctionWrapper<FunctionType>::ResultType (A1, A2, A3, A4, A5)>(FunctionWrapper<FunctionType>(function), a1, a2, a3, a4)));
1328}
1329
1330template<typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename FunctionType>
1331Function<typename FunctionWrapper<FunctionType>::ResultType(A1, A2, A3, A4, A5, A6)> bind(FunctionType function)
1332{
1333    return Function<typename FunctionWrapper<FunctionType>::ResultType(A1, A2, A3, A4, A5, A6)>(adoptRef(new UnboundFunctionImpl<FunctionWrapper<FunctionType>, typename FunctionWrapper<FunctionType>::ResultType (A1, A2, A3, A4, A5, A6)>(FunctionWrapper<FunctionType>(function))));
1334}
1335
1336template<typename A2, typename A3, typename A4, typename A5, typename A6, typename FunctionType, typename A1>
1337Function<typename FunctionWrapper<FunctionType>::ResultType(A2, A3, A4, A5, A6)> bind(FunctionType function, const A1& a1)
1338{
1339    return Function<typename FunctionWrapper<FunctionType>::ResultType(A2, A3, A4, A5, A6)>(adoptRef(new OneArgPartBoundFunctionImpl<FunctionWrapper<FunctionType>, typename FunctionWrapper<FunctionType>::ResultType (A1, A2, A3, A4, A5, A6)>(FunctionWrapper<FunctionType>(function), a1)));
1340}
1341
1342template<typename A3, typename A4, typename A5, typename A6, typename FunctionType, typename A1, typename A2>
1343Function<typename FunctionWrapper<FunctionType>::ResultType(A3, A4, A5, A6)> bind(FunctionType function, const A1& a1, const A2& a2)
1344{
1345    return Function<typename FunctionWrapper<FunctionType>::ResultType(A3, A4, A5, A6)>(adoptRef(new TwoArgPartBoundFunctionImpl<FunctionWrapper<FunctionType>, typename FunctionWrapper<FunctionType>::ResultType (A1, A2, A3, A4, A5, A6)>(FunctionWrapper<FunctionType>(function), a1, a2)));
1346}
1347
1348template<typename A4, typename A5, typename A6, typename FunctionType, typename A1, typename A2, typename A3>
1349Function<typename FunctionWrapper<FunctionType>::ResultType(A4, A5, A6)> bind(FunctionType function, const A1& a1, const A2& a2, const A3& a3)
1350{
1351    return Function<typename FunctionWrapper<FunctionType>::ResultType(A4, A5, A6)>(adoptRef(new ThreeArgPartBoundFunctionImpl<FunctionWrapper<FunctionType>, typename FunctionWrapper<FunctionType>::ResultType (A1, A2, A3, A4, A5, A6)>(FunctionWrapper<FunctionType>(function), a1, a2, a3)));
1352}
1353
1354template<typename A5, typename A6, typename FunctionType, typename A1, typename A2, typename A3, typename A4>
1355Function<typename FunctionWrapper<FunctionType>::ResultType(A5, A6)> bind(FunctionType function, const A1& a1, const A2& a2, const A3& a3, const A4& a4)
1356{
1357    return Function<typename FunctionWrapper<FunctionType>::ResultType(A5, A6)>(adoptRef(new FourArgPartBoundFunctionImpl<FunctionWrapper<FunctionType>, typename FunctionWrapper<FunctionType>::ResultType (A1, A2, A3, A4, A5, A6)>(FunctionWrapper<FunctionType>(function), a1, a2, a3, a4)));
1358}
1359
1360template<typename A6, typename FunctionType, typename A1, typename A2, typename A3, typename A4, typename A5>
1361Function<typename FunctionWrapper<FunctionType>::ResultType(A6)> bind(FunctionType function, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5)
1362{
1363    return Function<typename FunctionWrapper<FunctionType>::ResultType(A6)>(adoptRef(new FiveArgPartBoundFunctionImpl<FunctionWrapper<FunctionType>, typename FunctionWrapper<FunctionType>::ResultType (A1, A2, A3, A4, A5, A6)>(FunctionWrapper<FunctionType>(function), a1, a2, a3, a4, a5)));
1364}
1365
1366typedef Function<void()> Closure;
1367
1368}
1369
1370using WTF::Function;
1371using WTF::bind;
1372using WTF::Closure;
1373
1374#endif // WTF_Functional_h
1375