Boost Python objects and object identity

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

Boost Python objects and object identity

by Pertti Kellomäki-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I have Python bindings for a C++ library that among other
things contains classes for describing the static structure
of a microprocessor.

My background is in Lisp, so I am used to using object
identities (references) as keys. The C++ library can
be used in this fashion, e.g. looking up a register file
by name always returns the same object reference.
However, the C++ to Python mapping does not preserve object
identity, as a new Python object is created for every query,
even if the underlying C++ reference is the same.

Is there any easy way to preserve object identity in this
sense with Boost Python? My first thought was to use some
kind of caching scheme so that consequent queries with a
particular name return the same object, but the problem is
that objects are not only looked up by name, they can also
be returned e.g. by asking for a parent object in a containment
hierarchy.
--
Pertti
_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig@...
http://mail.python.org/mailman/listinfo/cplusplus-sig

Re: Boost Python objects and object identity

by Renato Araujo :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Pertti

I had the same problem as you in my binding implementation, then I
solve the problem using this function:

//
// a generator with an execute() function which, given a source type
// and a pointer to an object of that type, returns its most-derived
// /reachable/ type identifier and object pointer.
//

// first, the case where T has virtual functions
template <class T>
struct polymorphic_id_generator
{
    static dynamic_id_t execute(void* p_)
    {
        T* p = static_cast<T*>(p_);
        return std::make_pair(dynamic_cast<void*>(p), class_id(typeid(*p)));
    }
};

I use this function to get a pointer to most-derived type and use this
as key in my hash table.


BR
Renato Araujo Oliveira Filho




2009/9/29 Pertti Kellomäki <pertti.kellomaki@...>:

> I have Python bindings for a C++ library that among other
> things contains classes for describing the static structure
> of a microprocessor.
>
> My background is in Lisp, so I am used to using object
> identities (references) as keys. The C++ library can
> be used in this fashion, e.g. looking up a register file
> by name always returns the same object reference.
> However, the C++ to Python mapping does not preserve object
> identity, as a new Python object is created for every query,
> even if the underlying C++ reference is the same.
>
> Is there any easy way to preserve object identity in this
> sense with Boost Python? My first thought was to use some
> kind of caching scheme so that consequent queries with a
> particular name return the same object, but the problem is
> that objects are not only looked up by name, they can also
> be returned e.g. by asking for a parent object in a containment
> hierarchy.
> --
> Pertti
> _______________________________________________
> Cplusplus-sig mailing list
> Cplusplus-sig@...
> http://mail.python.org/mailman/listinfo/cplusplus-sig
>
_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig@...
http://mail.python.org/mailman/listinfo/cplusplus-sig

Re: Boost Python objects and object identity

by Alex Mohr-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Renato Araujo wrote:

> I had the same problem as you in my binding implementation, then I
> solve the problem using this function:
>
> //
> // a generator with an execute() function which, given a source type
> // and a pointer to an object of that type, returns its most-derived
> // /reachable/ type identifier and object pointer.
> //
>
> // first, the case where T has virtual functions
> template <class T>
> struct polymorphic_id_generator
> {
>     static dynamic_id_t execute(void* p_)
>     {
>         T* p = static_cast<T*>(p_);
>         return std::make_pair(dynamic_cast<void*>(p), class_id(typeid(*p)));
>     }
> };
>
> I use this function to get a pointer to most-derived type and use this
> as key in my hash table.

Cool -- how do you clean the table when an object is destroyed?

Alex
_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig@...
http://mail.python.org/mailman/listinfo/cplusplus-sig

Re: Boost Python objects and object identity

by Roman Yakovenko :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

2009/9/29 Pertti Kellomäki <pertti.kellomaki@...>:

> I have Python bindings for a C++ library that among other
> things contains classes for describing the static structure
> of a microprocessor.
>
> My background is in Lisp, so I am used to using object
> identities (references) as keys. The C++ library can
> be used in this fashion, e.g. looking up a register file
> by name always returns the same object reference.
> However, the C++ to Python mapping does not preserve object
> identity, as a new Python object is created for every query,
> even if the underlying C++ reference is the same.
>
> Is there any easy way to preserve object identity in this
> sense with Boost Python? My first thought was to use some
> kind of caching scheme so that consequent queries with a
> particular name return the same object, but the problem is
> that objects are not only looked up by name, they can also
> be returned e.g. by asking for a parent object in a containment
> hierarchy.

A while ago I introduce to Py++ some kind of functionality, which
integrates with CTYPES package.

For example, you can expose "this" pointer to Python and use it as your id:

http://language-binding.net/pyplusplus/documentation/ctypes/this_and_sizeof.html

HTH

--
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/
_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig@...
http://mail.python.org/mailman/listinfo/cplusplus-sig

Re: Boost Python objects and object identity

by Pertti Kellomäki-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Roman Yakovenko wrote:
> For example, you can expose "this" pointer to Python and use it as your id:

Thanks, this sounds like what I was looking for.
--
Pertti
_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig@...
http://mail.python.org/mailman/listinfo/cplusplus-sig