Re: help with typemap

William S Fulton <[email protected]> Sat, 30 Jan 2021 18:18:52 +0000
Newsgroups gmane.comp.programming.swig
Message-ID <CANGqftAE8Oh=sRfeu9z9mmNnC+EjX7LsqHLW2TXSa+k8qQk+Ag@mail.gmail.com>
On Thu, 24 Dec 2020 at 20:24, Robert Arkiletian <[email protected]> wrote:

> On Wed, Dec 23, 2020 at 11:52 AM William S Fulton
> <[email protected]> wrote:
> >
> > Looks like you are using it in an overloaded C++ function. You need to
> also provide a 'typecheck' typemap in addition to the 'in' typemap.
> >>
> >>
>
> Thanks for the help William. I looked for examples of how to create a
> typecheck for a "const unsigned char * " and unfortunately I didn't
> find much. So I copied an example from the SWIG docs and used
> SWIG_TYPECHECK_UINT8_PTR.
>
> %typemap(typecheck, precedence=SWIG_TYPECHECK_UINT8_PTR, noblock=1)
> SWIGTYPE * {
>  void *vptr = 0;
>  int res = SWIG_ConvertPtr($input, &vptr, $1_descriptor, 0);
>  $1 = SWIG_IsOK(res) ? 1 : 0;
> }
>
> %typemap(in) const unsigned char *buffer {   //used buffer instead of data
> ...
> }
>
> I don't know if the typecheck is correct. However, it did not work. So
> I looked more closely at the overloaded C++ header for the function.
>
>  Fl_JPEG_Image(const char *filename);
>  Fl_JPEG_Image(const char *name, const unsigned char *data); // this
> is the function
>
> Then I noticed I used the variable "buffer" not "data" in typemap(in)
> (see above). So I simply changed the var name to data
>
> %typemap(in) const unsigned char *data {  //used data instead of buffer
>
> and it worked, even without the typecheck!!
> I found this in the swig docs
> http://www.swig.org/Doc4.0/Typemaps.html#Typemaps_nn5
> "In addition to tracking typenames, typemaps may also be specialized
> to match against a specific argument name"
>
> So I solved my issue, but is my typecheck correct? If not, how should I
> fix it?
>

Your typecheck typemap targets all pointers because it uses the special
type SWIGTYPE *. Generally one would declare a 'typecheck' typemap's type
to match the type for your 'in' typemap, so you would change it to:

%typemap(typecheck, precedence=SWIG_TYPECHECK_UINT8_PTR, noblock=1) const
unsigned char *data {
...
}

Check the generated code to make sure it gets used, or use the typemap
debugging features documented in the Typemaps chapter you linked to above.

William

_______________________________________________
Swig-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/swig-user