File: e_cback.cpp

package info (click to toggle)
libwx-perl 1%3A0.9909-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,912 kB
  • sloc: cpp: 9,728; perl: 8,182; ansic: 626; makefile: 41
file content (136 lines) | stat: -rw-r--r-- 3,334 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/////////////////////////////////////////////////////////////////////////////
// Name:        cpp/e_cback.cpp
// Purpose:     implementation for e_cback.h
// Author:      Mattia Barbon
// Modified by:
// Created:     29/10/2000
// RCS-ID:      $Id: e_cback.cpp 2454 2008-08-31 11:12:47Z mbarbon $
// Copyright:   (c) 2000-2002, 2004-2008 Mattia Barbon
// Licence:     This program is free software; you can redistribute it and/or
//              modify it under the same terms as Perl itself
/////////////////////////////////////////////////////////////////////////////

wxPliEventCallback::wxPliEventCallback( SV* method, SV* self ) 
{
    dTHX;
    m_method = method;
    SvREFCNT_inc( m_method );
    m_self = self;
    SvREFCNT_inc( m_self );
    m_is_method = !SvROK( m_method ) || !SvRV( m_method );
}

wxPliEventCallback::~wxPliEventCallback() 
{
    dTHX;
    SvREFCNT_dec( m_method );
    SvREFCNT_dec( m_self );
}

class wxPliGuard
{
public:
    wxPliGuard()
    {
        m_sv = NULL;
    }

    ~wxPliGuard()
    {
        if( m_sv )
        {
            dTHX;

            wxPli_thread_sv_unregister( aTHX_ wxPli_get_class( aTHX_ m_sv ),
                                        (void*)SvIV( m_sv ), m_sv );
            sv_setiv( m_sv, 0 );
        }
    }

    void SetSV( SV* sv ) { m_sv = sv; }
private:
    SV* m_sv;
};

void wxPliEventCallback::Handler( wxEvent& event ) 
{
    wxPliEventCallback* This = (wxPliEventCallback*) event.m_callbackUserData;

    dTHX;
    dSP;

    ENTER;
    SAVETMPS;

    {
        // similar to wxPli_object_2_sv
        wxPliGuard guard;
        SV* e = 0;
        SV* rv = 0;
        wxClassInfo *ci = event.GetClassInfo();
        const wxChar* classname = ci->GetClassName();

#if wxUSE_UNICODE
        if( wcsncmp( classname, wxT("wxPl"), 4 ) == 0 ) 
#else
        if( strnEQ( classname, "wxPl", 4 ) ) 
#endif
        {
            wxPliClassInfo* cci = (wxPliClassInfo*)ci;
            wxPliSelfRef* sr = cci->m_func( &event );
        
            if( sr )
            {
                // this needs to have the refcount incremented, otherwise
                // the refcount will be decremented one time too much when
                // exiting from the handler
                e = sv_2mortal( newRV_inc( SvRV( sr->m_self ) ) );
            }
        }

        if( !e )
        {
            char buffer[WXPL_BUF_SIZE];
            const char* CLASS = wxPli_cpp_class_2_perl( classname, buffer );

            e = sv_newmortal();
            sv_setref_pv( e, CHAR_P CLASS, &event );
            rv = SvRV( e );
            // corner case: prevent destruction if referrer is
            // destroyed
            SvREFCNT_inc( rv );
            sv_2mortal( rv );
            guard.SetSV( rv );
            wxPli_thread_sv_register( aTHX_ CLASS, &event, e );
        }

        PUSHMARK( SP );
        XPUSHs( This->m_self );
        XPUSHs( e );
        PUTBACK;

        if( This->m_is_method )
        {
            call_method( SvPV_nolen( This->m_method ), G_EVAL|G_VOID|G_DISCARD );
        }
        else
        {
            call_sv( This->m_method, G_EVAL|G_VOID|G_DISCARD );
        }

        SPAGAIN;
    }

    if( SvTRUE( ERRSV ) )
    {
        croak( Nullch );
    }

    PUTBACK;
    FREETMPS;
    LEAVE;
}

// Local variables: //
// mode: c++ //
// End: //