Changeset 56194

Show
Ignore:
Timestamp:
10/09/08 04:26:50 (6 weeks ago)
Author:
VZ
Message:

add 3 parameter scope guard; improve its documentation

Location:
wxWidgets/trunk
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • wxWidgets/trunk/include/wx/scopeguard.h

    r52857 r56194  
    227227} 
    228228 
     229// ---------------------------------------------------------------------------- 
     230// wxScopeGuardImpl3: scope guard for actions with 3 parameters 
     231// ---------------------------------------------------------------------------- 
     232 
     233template <class F, class P1, class P2, class P3> 
     234class wxScopeGuardImpl3 : public wxScopeGuardImplBase 
     235{ 
     236public: 
     237    static wxScopeGuardImpl3<F, P1, P2, P3> MakeGuard(F fun, P1 p1, P2 p2, P3 p3) 
     238    { 
     239        return wxScopeGuardImpl3<F, P1, P2, P3>(fun, p1, p2, p3); 
     240    } 
     241 
     242    ~wxScopeGuardImpl3() { wxPrivateOnScopeExit(*this); } 
     243 
     244    void Execute() { m_fun(m_p1, m_p2, m_p3); } 
     245 
     246protected: 
     247    wxScopeGuardImpl3(F fun, P1 p1, P2 p2, P3 p3) 
     248        : m_fun(fun), m_p1(p1), m_p2(p2), m_p3(p3) { } 
     249 
     250    F m_fun; 
     251    const P1 m_p1; 
     252    const P2 m_p2; 
     253    const P3 m_p3; 
     254 
     255    wxScopeGuardImpl3& operator=(const wxScopeGuardImpl3&); 
     256}; 
     257 
     258template <class F, class P1, class P2, class P3> 
     259inline wxScopeGuardImpl3<F, P1, P2, P3> wxMakeGuard(F fun, P1 p1, P2 p2, P3 p3) 
     260{ 
     261    return wxScopeGuardImpl3<F, P1, P2, P3>::MakeGuard(fun, p1, p2, p3); 
     262} 
     263 
    229264// ============================================================================ 
    230265// wxScopeGuards for object methods 
     
    323358    return wxObjScopeGuardImpl2<Obj, MemFun, P1, P2>:: 
    324359                                            MakeObjGuard(obj, memFun, p1, p2); 
     360} 
     361 
     362template <class Obj, class MemFun, class P1, class P2, class P3> 
     363class wxObjScopeGuardImpl3 : public wxScopeGuardImplBase 
     364{ 
     365public: 
     366    static wxObjScopeGuardImpl3<Obj, MemFun, P1, P2, P3> 
     367        MakeObjGuard(Obj& obj, MemFun memFun, P1 p1, P2 p2, P3 p3) 
     368    { 
     369        return wxObjScopeGuardImpl3<Obj, MemFun, P1, P3>(obj, memFun, p1, p2, p3); 
     370    } 
     371 
     372    ~wxObjScopeGuardImpl3() { wxPrivateOnScopeExit(*this); } 
     373 
     374    void Execute() { (m_obj.*m_memfun)(m_p1, m_p2, m_p3); } 
     375 
     376protected: 
     377    wxObjScopeGuardImpl3(Obj& obj, MemFun memFun, P1 p1, P2 p2, P3 p3) 
     378        : m_obj(obj), m_memfun(memFun), m_p1(p1), m_p2(p2), m_p3(p3) { } 
     379 
     380    Obj& m_obj; 
     381    MemFun m_memfun; 
     382    const P1 m_p1; 
     383    const P2 m_p2; 
     384    const P3 m_p3; 
     385}; 
     386 
     387template <class Obj, class MemFun, class P1, class P2, class P3> 
     388inline wxObjScopeGuardImpl3<Obj, MemFun, P1, P2, P3> 
     389wxMakeObjGuard(Obj& obj, MemFun memFun, P1 p1, P2 p2, P3 p3) 
     390{ 
     391    return wxObjScopeGuardImpl3<Obj, MemFun, P1, P2, P3>:: 
     392                                        MakeObjGuard(obj, memFun, p1, p2, p3); 
    325393} 
    326394 
     
    466534 
    467535 
     536#define wxON_BLOCK_EXIT3_IMPL(n, f, p1, p2, p3) \ 
     537    wxScopeGuard n = wxMakeGuard(f, p1, p2, p3); \ 
     538    wxPrivateUse(n) 
     539#define wxON_BLOCK_EXIT3(f, p1, p2, p3) \ 
     540    wxON_BLOCK_EXIT3_IMPL(wxGuardName, f, p1, p2, p3) 
     541 
     542#define wxON_BLOCK_EXIT_OBJ3_IMPL(n, o, m, p1, p2, p3) \ 
     543    wxScopeGuard n = wxMakeObjGuard(o, m, p1, p2, p3); \ 
     544    wxPrivateUse(n) 
     545#define wxON_BLOCK_EXIT_OBJ3(o, m, p1, p2, p3) \ 
     546    wxON_BLOCK_EXIT_OBJ3_IMPL(wxGuardName, o, &m, p1, p2, p3) 
     547 
     548#define wxON_BLOCK_EXIT_THIS3(m, p1, p2, p3) \ 
     549    wxON_BLOCK_EXIT_OBJ3(*this, m, p1, p2, p3) 
     550 
     551 
    468552#define wxSetterName wxMAKE_UNIQUE_NAME(wxVarSetter) 
    469553 
  • wxWidgets/trunk/interface/wx/scopeguard.h

    r54385 r56194  
    77///////////////////////////////////////////////////////////////////////////// 
    88 
     9/** 
     10    Scope guard is an object which allows executing an action on scope exit. 
     11 
     12    The objects of this class must be constructed using wxMakeGuard() function. 
     13 */ 
     14class wxScopeGuard 
     15{ 
     16public: 
     17    /** 
     18        Call this method to dismiss the execution of the action on scope exit. 
     19 
     20        A typical example: 
     21        @code 
     22            Update1(); 
     23 
     24            // ensure that changes done so far are rolled back if the next 
     25            // operation throws 
     26            wxScopeGuard guard = wxMakeGuard(RollBack); 
     27            Update2(); 
     28 
     29            // it didn't throw so commit the changes, i.e. avoid rolling back 
     30            guard.Dismiss(); 
     31        @endcode 
     32     */ 
     33    void Dismiss(); 
     34}; 
     35 
    936/** @ingroup group_funcmacro_misc */ 
    1037//@{ 
    1138/** 
    12     This macro ensures that the global @a function with 0, 1, 2 or more 
    13     parameters (up to some implementation-defined limit) is executed on scope 
    14     exit, whether due to a normal function return or because an exception has 
    15     been thrown. A typical example of its usage: 
     39    Returns a scope guard object which will call the specified function with 
     40    the given parameters on scope exit. 
     41 
     42    This function is overloaded to take several parameters up to some 
     43    implementation-defined (but relatively low) limit. 
     44 
     45    The @a func should be a functor taking parameters of the types P1, ..., PN, 
     46    i.e. the expression @c func(p1, ..., pN) should be valid. 
     47 */ 
     48template <typename F, typename P1, ..., typename PN> 
     49wxScopeGuard wxMakeGuard(F func, P1 p1, ..., PN pN); 
     50 
     51//@} 
     52 
     53/** @ingroup group_funcmacro_misc */ 
     54//@{ 
     55/** 
     56    Ensure that the global @a function with a few (up to some 
     57    implementation-defined limit) is executed on scope exit, whether due to a 
     58    normal function return or because an exception has been thrown. 
     59 
     60    A typical example of its usage: 
    1661 
    1762    @code 
     
    2772    @header{wx/scopeguard.h} 
    2873*/ 
     74#define wxON_BLOCK_EXIT(function, ...) 
    2975#define wxON_BLOCK_EXIT0(function) 
    3076#define wxON_BLOCK_EXIT1(function, p1) 
    3177#define wxON_BLOCK_EXIT2(function, p1, p2) 
     78#define wxON_BLOCK_EXIT3(function, p1, p2, p3) 
    3279//@} 
    3380 
     
    3582//@{ 
    3683/** 
    37     This family of macros is similar to wxON_BLOCK_EXIT0(), but calls a method 
     84    This family of macros is similar to wxON_BLOCK_EXIT(), but calls a method 
    3885    of the given object instead of a free function. 
    3986 
    4087    @header{wx/scopeguard.h} 
    4188*/ 
     89#define wxON_BLOCK_EXIT_OBJ(object, method, ...) 
    4290#define wxON_BLOCK_EXIT_OBJ0(object, method) 
    4391#define wxON_BLOCK_EXIT_OBJ1(object, method, p1) 
    4492#define wxON_BLOCK_EXIT_OBJ2(object, method, p1, p2) 
     93#define wxON_BLOCK_EXIT_OBJ3(object, method, p1, p2, p3) 
    4594//@} 
    4695 
     
    4897//@{ 
    4998/** 
    50     This family of macros is similar to wxON_BLOCK_OBJ0(), but calls a method 
     99    This family of macros is similar to wxON_BLOCK_OBJ(), but calls a method 
    51100    of @c this object instead of a method of the specified object. 
    52101 
    53102    @header{wx/scopeguard.h} 
    54103*/ 
     104#define wxON_BLOCK_EXIT_THIS(method, ...) 
    55105#define wxON_BLOCK_EXIT_THIS0(method) 
    56106#define wxON_BLOCK_EXIT_THIS1(method, p1) 
    57107#define wxON_BLOCK_EXIT_THIS2(method, p1, p2) 
     108#define wxON_BLOCK_EXIT_THIS3(method, p1, p2, p3) 
    58109//@} 
    59110