WebKit2/Platform/WorkItem.h
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 /*
       
     2  * Copyright (C) 2010 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 WorkItem_h
       
    27 #define WorkItem_h
       
    28 
       
    29 #include <wtf/PassOwnPtr.h>
       
    30 
       
    31 class WorkItem {
       
    32 public:
       
    33     template<typename C> 
       
    34     static PassOwnPtr<WorkItem> create(C*, void (C::*)());
       
    35 
       
    36     template<typename C, typename T0, typename T1>
       
    37     static PassOwnPtr<WorkItem> create(C*, void (C::*)(T0, T1), T0, T1);
       
    38 
       
    39     virtual ~WorkItem() { }
       
    40     virtual void execute() = 0;
       
    41 
       
    42 protected:
       
    43     WorkItem() { }
       
    44 
       
    45 private:
       
    46     WorkItem(const WorkItem&);
       
    47     WorkItem& operator=(const WorkItem&);
       
    48 };
       
    49 
       
    50 template <typename C>
       
    51 class MemberFunctionWorkItem0 : private WorkItem {
       
    52     // We only allow WorkItem to create this.
       
    53     friend class WorkItem;
       
    54 
       
    55     typedef void (C::*FunctionType)();
       
    56     
       
    57     MemberFunctionWorkItem0(C* ptr, FunctionType function)
       
    58         : m_ptr(ptr)
       
    59         , m_function(function)
       
    60     {
       
    61         m_ptr->ref();
       
    62     }
       
    63 
       
    64     ~MemberFunctionWorkItem0()
       
    65     {
       
    66         m_ptr->deref();
       
    67     }
       
    68 
       
    69     void execute()
       
    70     {
       
    71         (m_ptr->*m_function)();
       
    72     }
       
    73 
       
    74     C* m_ptr;
       
    75     FunctionType m_function;
       
    76 };
       
    77 
       
    78 template<typename C, typename T0, typename T1>
       
    79 class MemberFunctionWorkItem2 : private WorkItem {
       
    80     // We only allow WorkItem to create this.
       
    81     friend class WorkItem;
       
    82     
       
    83     typedef void (C::*FunctionType)(T0, T1);
       
    84     
       
    85     MemberFunctionWorkItem2(C* ptr, FunctionType function, T0 t0, T1 t1)
       
    86         : m_ptr(ptr)
       
    87         , m_function(function)
       
    88         , m_t0(t0)
       
    89         , m_t1(t1)
       
    90     {
       
    91         m_ptr->ref();
       
    92     }
       
    93     
       
    94     ~MemberFunctionWorkItem2()
       
    95     {
       
    96         m_ptr->deref();
       
    97     }
       
    98 
       
    99     void execute()
       
   100     {
       
   101         (m_ptr->*m_function)(m_t0, m_t1);
       
   102     }
       
   103     
       
   104     C* m_ptr;
       
   105     FunctionType m_function;
       
   106     T0 m_t0;
       
   107     T1 m_t1;
       
   108 };
       
   109 
       
   110 template<typename C>
       
   111 PassOwnPtr<WorkItem> WorkItem::create(C* ptr, void (C::*function)())
       
   112 {
       
   113     return adoptPtr(static_cast<WorkItem*>(new MemberFunctionWorkItem0<C>(ptr, function)));
       
   114 }
       
   115 
       
   116 template<typename C, typename T0, typename T1>
       
   117 PassOwnPtr<WorkItem> WorkItem::create(C* ptr, void (C::*function)(T0, T1), T0 t0, T1 t1)
       
   118 {
       
   119     return adoptPtr(static_cast<WorkItem*>(new MemberFunctionWorkItem2<C, T0, T1>(ptr, function, t0, t1)));
       
   120 }
       
   121 
       
   122 #endif // WorkItem_h