|
Boost : |
From: David Abrahams (abrahams_at_[hidden])
Date: 2000-11-04 04:42:10
----- Original Message -----
From: "Prabhu Ramachandran" <prabhu_at_[hidden]>
> >>>>> "David" == David Abrahams <abrahams_at_[hidden]> writes:
>
> >> 1) The type unsigned char is not wrapped. So things like this
> >> dont work.
> >>
> >> unsigned char GetDebug();
>
> David> True. What is the right Python representation for unsigned
> David> char? I suppose an int would fine. Unless you disagree with
> David> this I will add the appropriate from_python()/to_python()
> David> functions for unsigned char to py.h
>
> I guess this should do. What are the lines I need to add to py.h?
Just copy the corresponding from_python/to_python functions for unsigned
short from py.cpp and py.h, replacing the string "short" with "char". Or you
can try grabbing
http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/boost/development/py_cpp/py.cp
p?cvsroot=boost
and
http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/boost/development/py_cpp/py.h?
cvsroot=boost
> >> 2) The class I am wrapping is called vtkObject. It is a base
> >> class from which every other class inherits. There are a few
> >> methods in this class that require one to pass a vtkObject
> >> *obj. These do not work. I do not know the best approach to
> >> solve this problem. The objects passed are not arrays just
> >> plain pointers to the base object. What do I do? Typical
> >> methods are
> >>
> >> static vtkObject *New() {return new vtkObject;};
>
> David> I am not surprised you are having trouble with this one.
>
> Yes, so how do I wrap it?? This is a very important function. :)
Have you been watching the boost list? I have answered this question in
several ways over the past few days in the following messages:
http://www.egroups.com/message/boost/6212
http://www.egroups.com/message/boost/6196
I don't know how to give you more information than that... well, on second
thought maybe in this case I do. It looks to me like this function is
passing back ownership of the vtkObject to the caller. The semantics of
ownership are not implicit in a raw pointer, but they /are/ in a
std::auto_ptr<vtkObject>. If you expose this wrapper function instead, I
think you'll get what you're looking for:
#include <memory>
static std::auto_ptr<vtkObject> vtkObject_New() {
return std::auto_ptr<vtkObject>(vtkObject::New());
}
> My bad! Sorry, I cut and pasted the wrong function. The function
>
> virtual void UnRegister(vtkObject* o);
>
> needs to be wrapped and hence is in the vtkObject_cb (callback struct)
> and defining it there creates problems because vtkObject* is not yet
> wrapped... What do you suggest I do?
One way to deal with this is covered in detail in
http://www.egroups.com/message/boost/6224
> David> Umm... OK, I'm not surprised. If vtkObject has virtual
> David> functions with default implementations, we are creating a
> David> "phantom" base class. Try
> David> dir(vtkObject.__bases__[0]). Ullrich and I are currently
> David> discussing a way to avoid this phantom base class so that
> David> dir(vtkObject) has all the methods you expect to see.
>
> Yup, that works. The phantom base class shows the methods. Besides,
> calling any static methods of vtkObject give errors because "self" is
> passed to the static c++ function when none is expected. Therefore
> something like this happens for a static func.
> static int GetGlobalWarningDisplay();
> In python
> >>> a = vtkObject ()
> >>> a.GetGlobalWarningDisplay ()
> Traceback (innermost last):
> File "<stdin>", line 1, in ?
> TypeError: function requires exactly 0 arguments; 1 given
> # the following works though
> >>> vtkObject.GetGlobalWarningDisplay ()
> 1
This is normal for Python. Unlike in C++, you can't call a static member
function through an instance. Qualification with an instance causes implicit
binding of the first argument.
> Here is a summary of what I'd like some help with.
>
> 1) What are the functions I need to add to handle the unsigned
> char.
answered
> 2) How do I wrap the c++ function
>
> static vtkObject *New() {return new vtkObject;};
answered
> 3) How does one wrap a virtual function that returns/requires a
> pointer to an instance of the class being wrapped. Like
>
> virtual void UnRegister(vtkObject* o);
Good question. It will go in the docs.
The answer is to use explicit instantiation as described in
http://www.egroups.com/message/boost/6224
> 4) A fix for the phantom class would be useful.
I have done that and checked it into CVS
> It would be nice to
> be able to call the static methods just like any non static methods.
That's a separate issue, and one that I don't expect to to fix. It's just
not "pythonesque" to do things that way.
-Dave
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk