[C++-sig] Some questions when making a C++ project as python externsions
Binbin Shen
binbinsh at gmail.com
Tue Sep 7 09:19:40 CEST 2010
Hi,
I am new to boost.python, and want to provide python interface of a
large lab application written in C++. I have written some wrappers and
make some classes works in python, but also encounter some problems
that cannot be solved by Googling, and I do not want to modify the
source of the C++ application, some of the problems are as follows:
1. Some C++ functions have return the result by parameters like this:
void add(std::wstring& a, const wstring b) {
a+=b;
}
>>> a = unicode()
>>> b = u"test"
>>> add(a, b) # error when compiling, unicode can not convert to wstring automatically
I should pass a python unicode into this function and return by "a" implicitly.
2. Some C++ functions show pass a "void*" pointer that pointing a
pre-allocated memory into the function, like this:
void read(void *memory, int size, int count)
{
// copy something into memory
}
How can I make a void pointer in python and pass it to read()?
3. Some virtual function with default arguments, like this:
class foo {
public:
virtual int add(int x, int y = 10) { return x+y; }
};
and the wrapper looks like:
struct fooWrap {
int add(int x, int y) {
if (override add_ = this->get_override("add")
return add_(x, y);
return this->foo::add(x, y);
}
int default add(int x, int y) {
return this->foo::add(x, y);
}
}
and
.def("add", &foo::add, &fooWrap::default_add);
but when dealing with overloads, it looks like:
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(add_overloads, add, 1, 2)
and
.def("add", &foo::add, add_overloads())
How can I write these two together?
4. Some functions have variable length parameter:
std::wstring format(const wchar_t* strFormat, ...) {
// use va_list, va_start, va_end...
}
how boost.python deal with it?
5. The last question is that I do not quiet understand the policies,
:-(, is there any detail documents introduce them:
with_custodian_and_ward: Ties lifetimes of the arguments
with_custodian_and_ward_postcall: Ties lifetimes of the arguments and results
return_internal_reference: Ties lifetime of one argument to that of result
return_value_policy<T> with T one of:
reference_existing_object: naive (dangerous) approach object
copy_const_reference: Boost.Python v1 approach
copy_non_const_reference:
manage_new_object: Adopt a pointer and hold the instance
return_by_value
return_by_reference
return_opaque_pointer
I use boost 1.42 with gcc 4.4.3 in Gentoo Linux.
Any advice would be gratefully received.
Best regards
Binbin
More information about the Cplusplus-sig
mailing list