camelo | 1 Jun 2004 01:07

Reseting swigCMemOwn inside a method


Hi! 

<< Sorry if you get a duplicate copy of this message. My first
attempt didn't got through. >>

I`m trying to reset the swigMemOwn flag of an object after it has
been passed to a method that aquires ownership of its parameter.

I'm trying to use a javain typemap, but I confess I haven't got the
grip of  typemaps. For our python bindings, I`m using the following
syntax (borrowed from a previus post I've found in the archives):

%typemap(python,in) core::ServerUniverseObject *p_obj_ptr {
 if ((SWIG_ConvertPtr(obj1,(void **) &arg2, 
SWIGTYPE_p_core__ServerUniverseObject,SWIG_POINTER_EXCEPTION | 0 )) == 
-1) SWIG_fail;
 PyObject_SetAttrString( $input, "thisown", PyInt_FromLong((long)0) );
}

In java I'd like to do something like this:

%typemap(javain) core::ServerSyncObject *p_obj_ptr {
  corelibsjJNI.ServerUniverseData_addSyncObject(swigCPtr, p_obj_ptr);
  p_obj_ptr.swigCMemOwn = false;
}

But the resulting wrapper code is rather strange to me:

public void addSyncObject(ServerSyncObject p_obj_ptr) {
(Continue reading)

William.Fulton | 1 Jun 2004 10:44

RE: Reseting swigCMemOwn inside a method


>-----Original Message-----
>From: swig-admin <at> cs.uchicago.edu [mailto:swig-admin <at> cs.uchicago.edu]On
>Behalf Of camelo <at> hoplon.com
>Sent: 01 June 2004 00:08
>To: swig <at> cs.uchicago.edu
>Subject: [Swig] Reseting swigCMemOwn inside a method
>
>
>
>Hi! 
>
><< Sorry if you get a duplicate copy of this message. My first
>attempt didn't got through. >>
>
>I`m trying to reset the swigMemOwn flag of an object after it has
>been passed to a method that aquires ownership of its parameter.
>
>I'm trying to use a javain typemap, but I confess I haven't got the
>grip of  typemaps. For our python bindings, I`m using the following
>syntax (borrowed from a previus post I've found in the archives):
>
>%typemap(python,in) core::ServerUniverseObject *p_obj_ptr {
> if ((SWIG_ConvertPtr(obj1,(void **) &arg2, 
>SWIGTYPE_p_core__ServerUniverseObject,SWIG_POINTER_EXCEPTION | 0 )) == 
>-1) SWIG_fail;
> PyObject_SetAttrString( $input, "thisown", PyInt_FromLong((long)0) );
>}
>
>In java I'd like to do something like this:
(Continue reading)

David E. Konerding | 1 Jun 2004 17:46
Favicon

Re: derived types

David E. Konerding DSD Staff wrote:

>
> I've confirmed (yuck) that explictly changing the type of t2.base.this 
> to a _p_Derived (by string replacement) works
> fine.  So I'm guessing that my attempt at casting isn't the right 
> method.  I'm also guessing the %pointer_cast and %pointer_class aren't
> the right approach.  Is this some other (swig-elegant) way of casting 
> from a Base to Derived type?
>
> Dave

Over the weekend I took a little more time to look at this.

The 'pointer_cast' feature is indeed able to autogenerate (for each 
listed base/derived type pair I use this directive for)
casting functions; so, for my original example below:
class Base {
public:
 int foo;
 Base(int foo_);
 int basemethod() { return foo; }
};

class Derived: public Base {
public:
 int bar;
 Derived(int foo_, int bar_);
 int derivedmethod() { return bar; }
};
(Continue reading)

Ivan R. Judson | 2 Jun 2004 14:57
Favicon

Python Callback problem


I'm having a problem with the following scenario:

I have an "object oriented" C library, that I've created C++ objects to
encapsulate. The reason I've done this is that the C "object" in the library
uses two callbacks, and rather than have static callbacks (and therefore
only one callback per process), I want more flexibility.

So I've created C++ objects that wrap the C, with wrappers that allow me to
create per object callbacks, etc. This is all very nice, very very clean
from the outside, exactly what I want.

However, the problem I'm running into is illustrated with this code:

import os
import common
import pprint

class MbusTest:
    def __init__(self, address):
        self.address = address
        self.mbus = common.Mbus(address,
                                lambda s,c,a,d: self.cmd(s,c,a,d),
                                lambda s,e: self.err(s,e))

    def cmd(self, src, cmd, arg, data):
        print "IN CALLBACK"
        prefix = "Got%s MBUS Command:"
        print "%s <%s> <%s> <%s> <%s>" % (prefix, src, cmd, arg, data)

(Continue reading)

Mr stephane conversy | 2 Jun 2004 17:21
Picon
Favicon

reference of pointer handling

hello,

I have a problem with reference of pointer handling.
say I want to wrap this fonction:
void foo(bar*&);
bar being a struct defines elsewhere.
if I write in my python code something like:
b = bar()
foo(b)
the the interpreter complains that it was waiting for
a _p_p_b though it received a _p_b.

I know this has something to do with swig politics to
consider reference to be a 'passed by adress' method,
even though it's not the purpose of foo. In fact, foo
can be a template, and the referecence is here to
prevent the copy of an object onto the stack, as it
often the case  in the std lib.

For example, if I want to wrap an std::list, and use
it as a list of pointer, I would write:

%template(listfoop) std::list<foo*>;

but when I use it in python, I code something like:
f = foo()
listfoop.push_back(f)

then the interpreter will complain that it was waiting
for _p_p_foo and it received a _p_foo.
(Continue reading)

Dave Marquardt | 2 Jun 2004 23:33
Picon

Tcl array reference of C arrays ?

I've searched the documentation and the mailing list archives at the
SWIG website, but haven't quite found what I'm looking for.

I have a C array of char *, i.e.

char *foo[1024];

I'd like to see this as an array in Tcl, and be able to use the
"array" command on it and to reference individual elements, e.g.

        set foo(0)
        array names foo
        array get foo

All the advice I see about arrays in the manuals suggests accessing
them using %inline get and set functions, or through conversion
functions.  I think typemaps *might* be able to help me here, but I
haven't seen an example quite like what I'm trying to do.

So, can I do this?  How?  Thanks.
--

-- 
Dave Marquardt
Sun Microsystems, Inc.
Austin, TX
+1 512 401-1077
_______________________________________________
Swig maillist  -  Swig <at> cs.uchicago.edu
http://mailman.cs.uchicago.edu/mailman/listinfo/swig

(Continue reading)

Mr stephane conversy | 3 Jun 2004 13:02
Picon
Favicon

Re: reference of pointer handling


--- Marcelo Matus <mmatus <at> dinha.acms.arizona.edu>
wrote:
> The problem you have is related to the std_list.i,
> or is with your own code?

my own code.
actually, I wasn't aware that the swig dist comes with
interfaces to std::lib.
A suggestion : make a reference of that work in the
documentation, under "swig and c++", not only in SWIG
library...

anyway, I looked through the std_lib.i code, but it
doesn't suite my need. I have my own templated list
structure, which is a template (it comes from the svg
idl definition):

template <class X>
struct SVGList {
...
X& appendItem(const X&);
}

if I use it with a pointer to foo:
struct foo {...};

I can't just use it that way (in python)
f = foo()
l = SVGListfoop()
(Continue reading)

Marcelo Matus | 3 Jun 2004 20:19
Picon

Re: reference of pointer handling

Ok, then you can do three things:

1.- declare a pointer specialized SVGList, like

        template <class X>
        strict SVGList <X*> {

         X* appendItem(X*);

        };

       note that you probably will need to declare it
       in the swig interface, and you can use the %extend
       directive to change the methods signature, ie, from

          X& appendItem(const X&);

       to

      %extend  X* appendItem(X*) {

            return  self->appendItem(x);
           }

        if needed.

     See the pointer specialization for std_vector.i in python/Lib.

2.- Try to define a typemap, ie

(Continue reading)

Mark Gossage | 4 Jun 2004 09:53

Re: reference of pointer handling (Mr stephane conversy)

I tested this on 1.3.21 its the same issue, it seems to work of for plain old data types eg void add(const int&
x) but not for objects.

> 
> template <class X>
> struct SVGList {
> ...
> X& appendItem(const X&);
> }
> 
> if I use it with a pointer to foo:
> struct foo {...};
> 
> I can't just use it that way (in python)
> f = foo()
> l = SVGListfoop()
> l.appendItem(f)
> 
> because appendItem signature is a ref to a pointer
> which is turned into a pointer of pointer by swig, and
> I use it with a pointer : there is a mitchmatch
> between _p_foo and _p_p_foo...
> 

Here is a work around:

template <class X>
struct SVGList {
..
X& appendItem(const X&);
(Continue reading)

Mr stephane conversy | 4 Jun 2004 16:41
Picon
Favicon

Re: reference of pointer handling

thanks for the answer. Removing the '&' in the
interface seems to work also, o I stay with it.

I don't know if it's really a bug, that's swig
behavior.
Maybe there can be an addition of a keyword, like the
one that prevents de default destructor (%nodefault,
if I remember well), that would be, I don't know,
%noinoutparameter...

--- Marcelo Matus <mmatus <at> dinha.acms.arizona.edu>
wrote:
> Ok, then you can do three things:
> 
> 1.- declare a pointer specialized SVGList, like
> 
>         template <class X>
>         strict SVGList <X*> {
> 
>          X* appendItem(X*);
> 
>         };
>    
>        note that you probably will need to declare
> it
>        in the swig interface, and you can use the
> %extend
>        directive to change the methods signature,
> ie, from
>  
(Continue reading)


Gmane