[C++-sig] Re: Arrays

David Abrahams dave at boost-consulting.com
Tue Sep 21 18:09:29 CEST 2004


Gareth McCaughan <gmccaughan at synaptics-uk.com> writes:

> I have some C++ software that I'm wrapping using Boost.Python .
> Some bits of it make use of plain ol' C-style arrays. Typical
> (but lightly fictionalized) situation: I have a method that
> talks to some hardware and fills a buffer with measurements.
>
>     unsigned short buffer[N_SAMPLES];
>     instrument.measure(&buffer[0], N_SAMPLES);
>
> And another that sends data to another piece of hardware.


If your functions accepted array references, like this:

   void measure(unsigned short (&buffer)[N_SAMPLES]);

it would be easier to give you a convenient solution.  As it stands,
the best thing I can suggest is to write thin wrappers:

   void measure(Instrument& i, boost::python::list b)
   {
       std::size_t len = extract<std::size_t>(b.attr("__len__")());
       boost::scoped_array<unsigned> b2 = new unsigned[len];
       for (unsigned i = 0; i < len; ++i)
          b2[i] = extract<unsigned>(b[i]);
       i.measure(b, len);
   }

Then wrap the measure function above.

HTH,
-- 
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com




More information about the Cplusplus-sig mailing list