[C++-sig] Extracting constant pointer to char* array from list

Alex Leach beamesleach at gmail.com
Wed Mar 20 16:41:34 CET 2013


Hi,

I was wondering the best way to solve this problem:-

void WrapperClass::parse_args(boost::python::list argv)
{
     int argc = boost::python::len(argv);
     const char* const* pargv = boost::python::extract<const char*  
const*>(argv); /// <----
     WrappedClass::parse_args(argc, pargv);
}

This doesn't work unfortunately. (Sorry, that's not very helpful, I know;  
I can't remember the exact error right now).

I've had a look through boost/python/extract.hpp and frankly have no idea  
how I'd extend it for this use case, but I have hacked together a rather  
bloated and repetitive solution that I copy and paste each time I have to  
do the same thing. e.g.

void WrapperClass::parse_args(const boost::python::list& argv)
{
     int argc = boost::python::len(argv);

     // - Could be a one-liner call to extract<>():-

     const char** pyargv = new const char*[argc+1];
     boost::python::stl_input_iterator<std::string> begin(argv), end;
     int i=0;
     while (begin != end)
     {
         pyargv[i++] = (*begin).c_str();
         begin++;
     }
     pyargv[i] = '\0';
     const char* const* p_argv = &pyargv[0];
     /// ----------

     WrappedClass::parse_args(argc, p_argv);
     delete[] pyargv;
}


This took quite a lot of trial and error to get running correctly, but is  
there a way to perform this type of extraction already? Could I extend  
boost::python::extract to do it for me? If so, please could someone show  
me how I could?

Cheers,
Alex


-- 
Using Opera's mail client: http://www.opera.com/mail/


More information about the Cplusplus-sig mailing list