[C++-SIG] How to use CXX...
Barry Scott
barry at scottb.demon.co.uk
Wed May 17 20:43:54 CEST 2000
> This is what I'm interested in. The examples in the Demo directory
> present what are essentially Python types implemented in C++, not
> wrappers around existing C++ types. You say that you know of "a
> solution" to this class of problem. Can you share some of what you've
> discovered?
Here is the code to find out if a T object is in Py:Object o.
if( T::check( o ) ) // is o of class T?
{
T *t = static_cast<T *>o.ptr(); // get the T out of the Py::Object o
os << t.str(); // call a method on the C++ object T
}
> At present, I'm trying to learn CXX by wrapping a hypothetical
> "POD-like" C++ type that doesn't know anything about Python. Some of
> the problems I'm having are:
>
> 1. Should the Python type wrap around a reference to the C++ type,
Depends on the life time of the C++ type. IF the C++
lives longer then the Python object then you have to use a pointer
or refernece.
OTherwise you can use the Py:Object's life time to control the
life time of the C++ type. Your wrappers d'tor will be called
once Python is finished with the object.
> 2. Should the Python type be constructible from Python? If the wrapper
> holds the wrapped by reference, this construction becomes difficult,
> because then you'd have to create two variables. How do you know when
> you can delete the wrapped C++ variable?
Basically you expose a method that will return instances of
the python wrapper objects. Typically this look like a traditional
factory.
> 3. If you're wrapping a type, you may not have access to pieces that
> would be good for methods like str(). Does that just mean that the
> type isn't printable in the Python sense?
You control the str() method - throw an exception or return whatever
you think is reasonable. For repr() return something even if its only
<Steve Harris Object at 0x80012131> you will need this for debugging
the C++ and the python code built on it.
> 4. I haven't gotten to trying to wrap containers yet. What if your C++
> type has a method like
> const std::list<foo>& get_foos() const;
> where the type exposes a nested container of some other type. That
> looks like you could coax some Pythonic behavior out of the pattern,
> but how would you go about wrapping it? Do you have to copy the whole
> list for each request for the Python-wrapped sequence?
No copy needed. You can implement the methods required for a
sequence type and be a proxy between Python and the std::list.
> > Going back to const. If adding const meets the goal then PyCXX
> > should be changed. If it does not then I'd leave as is.
>
> I think it meets the higher-level aim of making use of wrapped Python
> types "correct" in the C++ sense, which is consistent with aims like
> avoiding bald pointers, casting, and such.
Agreed.
> --
> Steven E. Harris
> Primus Knowledge Solutions, Inc.
> http://www.primus.com
>
> _______________________________________________
> C++-SIG maillist - C++-SIG at python.org
> http://www.python.org/mailman/listinfo/c++-sig
>
More information about the Cplusplus-sig
mailing list