Recognizing the same objects

View: New views
9 Messages — Rating Filter:   Alert me  

Recognizing the same objects

by Daniel K. O.-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello everybody.

Is it possible to make SWIG recognize that 2 objects are actually the
same [wrapped object]? Like:

void setFoo(Foo *f);
Foo* getFoo();

So in the wrapped code (e.g. python):

x = Foo()
x.name = "some data" # data appended in the python's side
setFoo(x)

a = getFoo()
b = getFoo()

print a.name
print b.name

And I would like the print output to be "some data".
Assuming I can add data members to Foo itself (in the original C++
class), would it be possible?

Otherwise I might need to redesign the API (any suggestion?) or stick
with python+pyrex.


Regards


--
Daniel K. O.
"The only way to succeed is to build success yourself"

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Re: Recognizing the same objects

by Dustin J. Mitchell-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Thu, May 15, 2008 at 7:16 PM, Daniel K. O. <danielko.listas@...> wrote:
> Is it possible to make SWIG recognize that 2 objects are actually the
> same [wrapped object]? Like:

I had similar difficulties with Perl bindings.

The basic answer is, "no."  SWIG's bindings will create a new Python
object every time they are handed a pointer from the C/C++ layer.

You can work around this by writing typemaps that will keep some kind
of (weak-ref) dictionary of extant wrapped objects, and look up any
new pointer values in that dictionary.  SWIG doesn't implement this
itself as it's really fairly domain-specific.

Dustin

--
Storage Software Engineer
http://www.zmanda.com

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Re: Recognizing the same objects

by Daniel K. O.-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

2008/5/15 Dustin J. Mitchell <dustin@...>:
> I had similar difficulties with Perl bindings.
>
> The basic answer is, "no."  SWIG's bindings will create a new Python
> object every time they are handed a pointer from the C/C++ layer.
>
> You can work around this by writing typemaps that will keep some kind
> of (weak-ref) dictionary of extant wrapped objects, and look up any
> new pointer values in that dictionary.  SWIG doesn't implement this
> itself as it's really fairly domain-specific.


I was hoping for something along these lines:

struct Foo {
    void *data;
    ...
};

And make the [SWIG] Foo constructor store a pointer to the SWIG object
in the [C++] Foo object. So the getFoo() function would be overwritten
as:

SwigFoo* getFooSwig()
{
    Foo *f = getFoo();
    swigobj = f->data;
    return swigobj;
}

Could this be done with the help of typemaps?


--
Daniel K. O.
"The only way to succeed is to build success yourself"

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Re: Recognizing the same objects

by Dustin J. Mitchell-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Thu, May 15, 2008 at 9:09 PM, Daniel K. O. <danielko.listas@...> wrote:
> Could this be done with the help of typemaps?

Yes -- but the problem is that this leads to a reference loop -- your
python object has a ref to the C++ object, and your C++ object has a
ref to the python object.  You can get around this with weakrefs, but
it will make it more complicated.

I'm pretty sure this is possbile, but I haven't done it, so I don't
have much more detail :(

Dustin

--
Storage Software Engineer
http://www.zmanda.com

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Re: Recognizing the same objects

by Daniel K. O.-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Dustin J. Mitchell escreveu:
> Yes -- but the problem is that this leads to a reference loop -- your
> python object has a ref to the C++ object, and your C++ object has a
> ref to the python object.

Hm... for Python, with ref counting, it whouldn't cause problems in my case.

So far this is what I able to do, mostly based on the output from the
python target:

---
%typemap(in) Foo* {
    void *arg = 0;
    int res = SWIG_ConvertPtr($input, &arg, $1_descriptor, 0 |  0 );
    if (!SWIG_IsOK(res)) {
        SWIG_exception_fail(SWIG_ArgError(res), "shouldn't happen");
    }
    $1 = reinterpret_cast< $1_ltype > ( arg );
    $1->data = $input;
}

%typemap(out) Foo* {
    if ($1->data)
        $result = reinterpret_cast< PyObject* > ( $1->data );
    else
        $1->data =
        $result = SWIG_NewPointerObj(SWIG_as_voidptr($1),
$1_descriptor, SWIG_POINTER_NEW |  0 );
}

struct Foo {
    void *data;
    Foo() : data(0) {}
};

struct Bar {
    Foo *ptr;
    void setFoo(Foo* f)
    {
        ptr = f;
    }
    Foo* getFoo()
    {
        return ptr;
    }
};
---
I tried to use those SWIG_* macros hoping it would work for other
targets; then I read the manual bit that said it is supposed to be
language-dependent anyways. :(


>From a high-level, all I want is 1) extract the C++ pointer from the
SWIG wrapper, 2) cast a raw pointer to a pointer to a SWIG wrapper, and
3) create a new wrapper from the raw pointer. Aren't those operations
abstract and generic enough to deserve a built-in, portable [SWIG]
language construct? Maybe through some $whatever special variable. The
functionality to generate code for 1) and 3) is probably implemented
already, just (maybe) not visible; and 2) would be (I believe) trivial
(when possible at all).



--
Daniel K. O.
"The only way to succeed is to build success yourself"


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Re: Recognizing the same objects

by nitro-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Am 16.05.2008, 01:16 Uhr, schrieb Daniel K. O. <danielko.listas@...>:

> Hello everybody.
>
> Is it possible to make SWIG recognize that 2 objects are actually the
> same [wrapped object]? Like:
>
> void setFoo(Foo *f);
> Foo* getFoo();
>
> So in the wrapped code (e.g. python):
>
> x = Foo()
> x.name = "some data" # data appended in the python's side
> setFoo(x)
>
> a = getFoo()
> b = getFoo()
>
> print a.name
> print b.name
>
> And I would like the print output to be "some data".
> Assuming I can add data members to Foo itself (in the original C++
> class), would it be possible?
>
> Otherwise I might need to redesign the API (any suggestion?) or stick
> with python+pyrex.

I haven't tried this, but maybe you can do

a = getFoo()
b = getFoo()

print 'Same objects: ', (a.this == b.this)

-Matthias

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Re: Recognizing the same objects

by Bugzilla from w.richert@gmx.net :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

Basically it is possible if your wrappable function uses a PyObject* instead
of Foo* in the signature. Swig then does not perform any "intelligent"
conversion. Inside the function you can, of course, then extract the Foo
object. But this has to be done in your wrappable function, not in your swig
file.

You can then even check the objects in Python via "is" for euality.

Regards,
wr

On Friday 16 May 2008 01:16:25 Daniel K. O. wrote:

> Hello everybody.
>
> Is it possible to make SWIG recognize that 2 objects are actually the
> same [wrapped object]? Like:
>
> void setFoo(Foo *f);
> Foo* getFoo();
>
> So in the wrapped code (e.g. python):
>
> x = Foo()
> x.name = "some data" # data appended in the python's side
> setFoo(x)
>
> a = getFoo()
> b = getFoo()
>
> print a.name
> print b.name
>
> And I would like the print output to be "some data".
> Assuming I can add data members to Foo itself (in the original C++
> class), would it be possible?
>
> Otherwise I might need to redesign the API (any suggestion?) or stick
> with python+pyrex.
>
>
> Regards



-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Re: Recognizing the same objects

by wsfulton :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Daniel K. O. wrote:

> Hello everybody.
>
> Is it possible to make SWIG recognize that 2 objects are actually the
> same [wrapped object]? Like:
>
> void setFoo(Foo *f);
> Foo* getFoo();
>
> So in the wrapped code (e.g. python):
>
> x = Foo()
> x.name = "some data" # data appended in the python's side
> setFoo(x)
>
> a = getFoo()
> b = getFoo()
>
> print a.name
> print b.name
>
> And I would like the print output to be "some data".
> Assuming I can add data members to Foo itself (in the original C++
> class), would it be possible?
>
> Otherwise I might need to redesign the API (any suggestion?) or stick
> with python+pyrex.
>

Have you thought of not appending the data on the python side and
instead using the C++ side only? You can enforce that the Python objects
are not added to by using %pythonnondynamic (see CHANGES file). Also
consider directors where you can derive from the C++ objects using pure
Python objects. If you put the 'name' member variable into the derived
type, then you will get the full Python object back from the C++ layer.

William

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Re: Recognizing the same objects

by wsfulton :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

William S Fulton wrote:

> Daniel K. O. wrote:
>> Hello everybody.
>>
>> Is it possible to make SWIG recognize that 2 objects are actually the
>> same [wrapped object]? Like:
>>
>> void setFoo(Foo *f);
>> Foo* getFoo();
>>
>> So in the wrapped code (e.g. python):
>>
>> x = Foo()
>> x.name = "some data" # data appended in the python's side
>> setFoo(x)
>>
>> a = getFoo()
>> b = getFoo()
>>
>> print a.name
>> print b.name
>>
>> And I would like the print output to be "some data".
>> Assuming I can add data members to Foo itself (in the original C++
>> class), would it be possible?
>>
>> Otherwise I might need to redesign the API (any suggestion?) or stick
>> with python+pyrex.
>>
>
> Have you thought of not appending the data on the python side and
> instead using the C++ side only? You can enforce that the Python objects
> are not added to by using %pythonnondynamic (see CHANGES file). Also
> consider directors where you can derive from the C++ objects using pure
> Python objects. If you put the 'name' member variable into the derived
> type, then you will get the full Python object back from the C++ layer.
>

Oh and I meant to mention if you didn't know already that you can see if
two objects are the same by using:

equal = a.this == b.this

which basically compares their underlying c++ pointers.

William

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user