arguments to c++ functions

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

arguments to c++ functions

Josh Stratton
Where can I find docs for sending arguments to C\C++ functions?  I've
followed the hello world example, which seems to automatically convert
a python string to a char*, but I'm going to need more conversions
later like how to handle a char**.  Something like ["a","b","c"]
doesn't seem to work.
_______________________________________________
Cplusplus-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/cplusplus-sig
Reply | Threaded
Open this post in threaded view
|

Re: arguments to c++ functions

Jim Bosch-2
On 08/09/2011 08:33 PM, Josh Stratton wrote:
> Where can I find docs for sending arguments to C\C++ functions?  I've
> followed the hello world example, which seems to automatically convert
> a python string to a char*, but I'm going to need more conversions
> later like how to handle a char**.  Something like ["a","b","c"]
> doesn't seem to work.

I don't think there are automatic conversions for char** - it can mean
too many things, and there's no automatic way to pass in the length.

If you want to pass in a sequence of Python strings, there are plenty of
ways to get C++ code to accept that.  But if the C++ code you're dealing
with has char** arguments, you'll probably have to provide manual
wrappers for those functions; something like this:


namespace bp = boost::python;

void original(char** s, int n);

void wrapper(bp::object const & o) {
     int n = bp::len(o);
     std::vector<std::string> v(n);
     boost::scoped_array<char*> s(new char*[n]);
     for (int i = 0; i < n; ++i) {
         v[i] = bp::extract<std::string>(o[i]);
         s[i] = v[i].c_str();
     }
     original(s, n);
}


Good luck!

Jim Bosch
_______________________________________________
Cplusplus-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/cplusplus-sig