File: overload.cpp

package info (click to toggle)
libwx-perl 1%3A0.9932-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,300 kB
  • sloc: cpp: 11,064; perl: 8,603; ansic: 711; makefile: 53
file content (213 lines) | stat: -rw-r--r-- 6,561 bytes parent folder | download | duplicates (6)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/////////////////////////////////////////////////////////////////////////////
// Name:        cpp/overload.cpp
// Purpose:     C++ implementation for a function to match a function's
//              argument list against a prototype
// Author:      Mattia Barbon
// Modified by:
// Created:     07/08/2002
// RCS-ID:      $Id: overload.cpp 2953 2010-08-15 14:29:24Z mbarbon $
// Copyright:   (c) 2002-2004, 2006-2007, 2010 Mattia Barbon
// Licence:     This program is free software; you can redistribute it and/or
//              modify it under the same terms as Perl itself
/////////////////////////////////////////////////////////////////////////////

#include "cpp/overload.h"

#if 0
class wxPliArgArray
{
public:
    virtual ~wxPliArgArray() {};

    virtual SV* operator[]( size_t index ) = 0;
    virtual size_t GetCount() const = 0;
};

class wxPliStackArray
{
public:
    wxPliStackArray();

private:
    SV*** sp;
    
};
#endif

bool wxPli_match_arguments_offset( pTHX_ const wxPliPrototype& prototype,
                                   int required,
                                   bool allow_more, size_t offset );

bool wxPli_match_arguments_skipfirst( pTHX_ const wxPliPrototype& prototype,
                                      int required /* = -1 */,
                                      bool allow_more /* = false */ )
{
    return wxPli_match_arguments_offset( aTHX_ prototype, required,
                                         allow_more, 1 );
}

bool wxPli_match_arguments( pTHX_ const wxPliPrototype& prototype,
                            int required /* = -1 */,
                            bool allow_more /* = false */ )
{
    return wxPli_match_arguments_offset( aTHX_ prototype, required,
                                         allow_more, 0 );
}

static inline bool IsGV( SV* sv ) { return SvTYPE( sv ) == SVt_PVGV; }

bool wxPli_match_arguments_offset( pTHX_ const wxPliPrototype& prototype,
                                   int required,
                                   bool allow_more, size_t offset )
{
    dXSARGS; // restore the mark we implicitly popped in dMARK!
    int argc = items - int(offset);

    if( required != -1 )
    {
        if(  allow_more && argc <  required )
            { PUSHMARK(MARK); return false; }
        if( !allow_more && argc != required )
            { PUSHMARK(MARK); return false; }
    }
    else if( argc < int(prototype.count) )
        { PUSHMARK(MARK); return false; }

    size_t max = wxMin( prototype.count, size_t(argc) ) + offset;
    for( size_t i = offset; i < max; ++i )
    {
        const char* p = prototype.args[i - offset];
        // everything is a string or a boolean
        if( p == wxPliOvlstr ||
            p == wxPliOvlbool )
            continue;

        SV* t = ST(i);

        // want a number
        if( p == wxPliOvlnum )
        {
            if( my_looks_like_number( aTHX_ t ) ) continue;
            else { PUSHMARK(MARK); return false; }
        }
        // want an object/package name, accept undef, too
        const char* cstr =
          p > wxPliOvlzzz   ? p :
          p == wxPliOvlwpos ? "Wx::Position" :
          p == wxPliOvlwpoi ? "Wx::Point" :
          p == wxPliOvlwsiz ? "Wx::Size"  :
                              NULL;
        if(    !IsGV( t )
            && (    !SvOK( t )
                 || (    cstr != NULL
                      && sv_isobject( t )
                      && sv_derived_from( t, CHAR_P cstr )
                      )
                 )
            )
            continue;
        // want an array reference
        if( p == wxPliOvlarr && wxPli_avref_2_av( t ) ) continue;
        // want a wxPoint/wxSize, accept an array reference, too
        if( ( p == wxPliOvlwpoi || p == wxPliOvlwsiz || p == wxPliOvlwpos )
            && wxPli_avref_2_av( t ) ) continue;
        // want an input/output stream, accept any reference
        if( ( p == wxPliOvlwist || p == wxPliOvlwost ) &&
            ( SvROK( t ) || IsGV( t ) ) ) continue;

        // type clash: return false
        PUSHMARK(MARK);
        return false;
    }

    PUSHMARK(MARK);
    return true;
}

void wxPli_set_ovl_constant( const char* name, const wxPliPrototype* value )
{
    dTHX;
    char buffer[1024];
    strcpy( buffer, "Wx::_" );
    strcat( buffer, name );

    SV* sv = get_sv( buffer, 1 );
    sv_setiv( sv, PTR2IV( value ) );
}

static const char *overload_descriptions[] =
{
    NULL, "array", "boolean", "number", "string/scalar", "input stream",
    "output stream", "Wx::Point/array", "Wx::Position/array", "Wx::Size/array"
};

void wxPli_overload_error( pTHX_ const char* function,
                           wxPliPrototype* prototypes[] )
{
    dXSARGS; // restore the mark we implicitly popped in dMARK!
    SV* message = newSVpv( "Available methods:\n", 0 );
    sv_2mortal( message );

    for( int j = 0; prototypes[j]; ++j )
    {
        wxPliPrototype* p = prototypes[j];

        sv_catpv( message, function );
        sv_catpv( message, "(" );

        for( int i = 0; i < p->count; ++i )
        {
            if( p->args[i] < wxPliOvlzzz )
                sv_catpv( message, overload_descriptions[wxUIntPtr(p->args[i])] );
            else
                sv_catpv( message, p->args[i] );

            if( i != p->count - 1 )
                sv_catpv( message, ", " );                
        }

        sv_catpv( message, ")\n" );
    }

    sv_catpvf( message, "unable to resolve overload for %s(", function );

    for( size_t i = 1; i < items; ++i )
    {
        SV* t = ST(i);
        const char* type;

        if( !SvOK( t ) )
            type = "undef";
        else if( sv_isobject( t ) )
            type = HvNAME( SvSTASH( SvRV( t ) ) );
        else if( SvROK( t ) )
        {
            SV* r = SvRV( t );

            if( SvTYPE( r ) == SVt_PVAV )
                type = "array";
            else if( SvTYPE( r ) == SVt_PVHV )
                type = "hash";
            else
                type = "reference";
        }
        else if( IsGV( t ) )
            type = "glob/handle";
        else if( my_looks_like_number( aTHX_ t ) )
            type = "number";
        else
            type = "scalar";

        sv_catpv( message, type );
        if( i != items - 1 )
            sv_catpv( message, ", " );                
    }

    sv_catpv( message, ")" );

    PUSHMARK(MARK); // probably not necessary

    require_pv( "Carp.pm" );
    const char* argv[2]; argv[0] = SvPV_nolen( message ); argv[1] = NULL;
    call_argv( "Carp::croak", G_VOID|G_DISCARD, (char**) argv ); \
}