Hi all,
I'm working on PySide[1] and we have a problem with map between c++
pointers and PyObject.
In this section of FAQ:
http://www.boost.org/doc/libs/1_40_0/libs/python/doc/v2/faq.html#xref,
they give some examples on how to handle this kind of problem. But the
point is: dynamic_cast does not work between shared libraries. My
wrapper class is not visible to another library then the dynamic_cast
fails.
Then I tried implement a hash table to map c++ pointers and python
objects, but in some point of the code I received the pointer in
another class representation and this moves the pointer address to
another region of memory, causing a mess in my hash. I would like to
know if someone has any idea about how to solve this problem. How can
I map a c++ pointer to a PyObject between libraries without use
dynamic_cast.
I have attached a small use case for this case.
Renato Araujo Oliveira Filho
[1] PySide = http://pyside.org
#include <stdio.h>
#include <map>
class py_object;
typedef std::map<void*, py_object*> pointers_t;
static pointers_t pointers;
/*
* this function demostrate how we assosiate a c++ pointer to a python object
*/
/*
* use the ptr as a key in a map to store py in the value
*/
void register_wrapper(void *ptr, py_object *py)
{
pointers[ptr] = py;
}
/*
* try find for a py_object associate with this c++ pointer
*/
py_object* get_py_object(void *ptr)
{
pointers_t::iterator found = pointers.find(ptr);
if (found != pointers.end()) {
return found->second;
}
return 0;
}
/* fake classes to demonstrate de problem */
struct classA
{
virtual ~classA(){};
};
struct classB
{
virtual ~classB(){};
};
struct classC: public classA, public classB
{
virtual ~classC(){};
};
struct py_object
{
py_object(void *cpp)
{
register_wrapper(cpp, this);
}
};
/*
* main function with some tests
*/
int main(int argc, char **argv)
{
classA *a = new classA();
py_object *a_py = new py_object(a);
classB *b = new classB();
py_object *b_py = new py_object(b);
classC *c = new classC();
py_object *c_py = new py_object(c);
/* This tests work very well */
printf("result test1: %d\n", a_py == get_py_object(a));
printf("result test2: %d\n", a_py == get_py_object(a));
printf("result test3: %d\n", a_py == get_py_object(a));
/*
* some times you have the same c++ pointer but in another format
* this change the pointer to another address and cause a mess in map association
*/
classA *ca = dynamic_cast<classA*>(c);
printf("result test4: %d\n", a_py == get_py_object(ca));
}
_______________________________________________
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig