[C++-sig] Re: Arrays

Gareth McCaughan gmccaughan at synaptics-uk.com
Tue Sep 21 18:38:32 CEST 2004


On Tuesday 2004-09-21 17:09, David Abrahams wrote:

[I said:]
> > 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.

[David:]
> 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.

Alas... :-)

>                                                        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.

That sounds like an effective solution. (I assume that last line
should refer to b2 rather than b.)

... But, in fact, I changed my mind about how much pain
was involved in moving to vectors everywhere, and made
the change. It turned out not to be too bad. So now the
indexing_suite works fine for my needs. Sorry to have
wasted your time.

-- 
g




More information about the Cplusplus-sig mailing list