Add libwx-perl
[pkg-perl] / deb-src / libwx-perl / libwx-perl-0.96 / cpp / overload.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        cpp/overload.cpp
3 // Purpose:     C++ implementation for a function to match a function's
4 //              argument list against a prototype
5 // Author:      Mattia Barbon
6 // Modified by:
7 // Created:     07/08/2002
8 // RCS-ID:      $Id: overload.cpp 2192 2007-08-21 21:27:40Z mbarbon $
9 // Copyright:   (c) 2002-2004, 2006-2007 Mattia Barbon
10 // Licence:     This program is free software; you can redistribute it and/or
11 //              modify it under the same terms as Perl itself
12 /////////////////////////////////////////////////////////////////////////////
13
14 #include "cpp/overload.h"
15
16 #if 0
17 class wxPliArgArray
18 {
19 public:
20     virtual ~wxPliArgArray() {};
21
22     virtual SV* operator[]( size_t index ) = 0;
23     virtual size_t GetCount() const = 0;
24 };
25
26 class wxPliStackArray
27 {
28 public:
29     wxPliStackArray();
30
31 private:
32     SV*** sp;
33     
34 };
35 #endif
36
37 bool wxPli_match_arguments_offset( pTHX_ const wxPliPrototype& prototype,
38                                    int required,
39                                    bool allow_more, size_t offset );
40
41 bool wxPli_match_arguments_skipfirst( pTHX_ const wxPliPrototype& prototype,
42                                       int required /* = -1 */,
43                                       bool allow_more /* = false */ )
44 {
45     return wxPli_match_arguments_offset( aTHX_ prototype, required,
46                                          allow_more, 1 );
47 }
48
49 bool wxPli_match_arguments( pTHX_ const wxPliPrototype& prototype,
50                             int required /* = -1 */,
51                             bool allow_more /* = false */ )
52 {
53     return wxPli_match_arguments_offset( aTHX_ prototype, required,
54                                          allow_more, 0 );
55 }
56
57 static inline bool IsGV( SV* sv ) { return SvTYPE( sv ) == SVt_PVGV; }
58
59 bool wxPli_match_arguments_offset( pTHX_ const wxPliPrototype& prototype,
60                                    int required,
61                                    bool allow_more, size_t offset )
62 {
63     dXSARGS; // restore the mark we implicitly popped in dMARK!
64     int argc = items - int(offset);
65
66     if( required != -1 )
67     {
68         if(  allow_more && argc <  required )
69             { PUSHMARK(MARK); return false; }
70         if( !allow_more && argc != required )
71             { PUSHMARK(MARK); return false; }
72     }
73     else if( argc < int(prototype.count) )
74         { PUSHMARK(MARK); return false; }
75
76     size_t max = wxMin( prototype.count, size_t(argc) ) + offset;
77     for( size_t i = offset; i < max; ++i )
78     {
79         unsigned char p = prototype.args[i - offset];
80         // everything is a string or a boolean
81         if( p == wxPliOvlstr ||
82             p == wxPliOvlbool )
83             continue;
84
85         SV* t = ST(i);
86
87         // want a number
88         if( p == wxPliOvlnum )
89         {
90             if( my_looks_like_number( aTHX_ t ) ) continue;
91             else { PUSHMARK(MARK); return false; }
92         }
93         // want an object/package name, accept undef, too
94         const char* cstr =
95           p > wxPliOvlzzz   ? prototype.tnames[p - wxPliOvlzzz] :
96           p == wxPliOvlwpos ? "Wx::Position" :
97           p == wxPliOvlwpoi ? "Wx::Point" :
98           p == wxPliOvlwsiz ? "Wx::Size"  :
99                               NULL;
100         if(    !IsGV( t )
101             && (    !SvOK( t )
102                  || (    cstr != NULL
103                       && sv_isobject( t )
104                       && sv_derived_from( t, CHAR_P cstr )
105                       )
106                  )
107             )
108             continue;
109         // want an array reference
110         if( p == wxPliOvlarr && wxPli_avref_2_av( t ) ) continue;
111         // want a wxPoint/wxSize, accept an array reference, too
112         if( ( p == wxPliOvlwpoi || p == wxPliOvlwsiz || p == wxPliOvlwpos )
113             && wxPli_avref_2_av( t ) ) continue;
114         // want an input/output stream, accept any reference
115         if( ( p == wxPliOvlwist || p == wxPliOvlwost ) &&
116             ( SvROK( t ) || IsGV( t ) ) ) continue;
117
118         // type clash: return false
119         PUSHMARK(MARK);
120         return false;
121     }
122
123     PUSHMARK(MARK);
124     return true;
125 }
126
127 void wxPli_set_ovl_constant( const char* name, const wxPliPrototype* value )
128 {
129     dTHX;
130     char buffer[1024];
131     strcpy( buffer, "Wx::_" );
132     strcat( buffer, name );
133
134     SV* sv = get_sv( buffer, 1 );
135     sv_setiv( sv, PTR2IV( value ) );
136 }