[C++-sig] Arrays

Gareth McCaughan gmccaughan at synaptics-uk.com
Tue Sep 21 16:44:01 CEST 2004


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.

    long waveform[N_SAMPLES];
    for (int i=0; i<N_SAMPLES; ++i)
      waveform[i] = static_cast<long>((1<<24)*sin(0.01*i));
    instrument.drive(&waveform[0], N_SAMPLES);

I would like to be able to provide access to all this from
Python.

One approach would be to change everything to use vectors
rather than arrays; after all, they are guaranteed to store
their data contiguously. Then I could use the indexing_suite.
That would probably be manageable, but I'd rather not have to
change all the code.

Is there anything easier I can do? The ideal would be to
be able to say

    buffer = [0]*N_SAMPLES
    instrument.measure(buffer, N_SAMPLES)

and

    waveform = [(1<<24)*math.sin(0.01*i) for i in range(N_SAMPLES)]
    instrument.drive(waveform, N_SAMPLES)

and have it Just Work (tm). I'd settle for something nearer to

    buffer = c_array_short(N_SAMPLES)
    instrument.measure(buffer, N_SAMPLES)

and

    waveform = c_array_long(N_SAMPLES)
    for i in range(N_SAMPLES):
      waveform[i] = (1<<24)*math.sin(0.01*i)
    instrument.drive(waveform, N_SAMPLES)

if that's more realistic. (I don't need to be able to change the
sizes of these arrays; I'm content if I can create them, destroy
them, store data in them, and get it out again.) Is any of this
possible with Boost.Python as it currently stands, and if so how?

I understand that someone has worked on a new version of the
indexing_suite that somehow makes it easier to work with arrays,
but (unless I'm confused) this is not in any released version
of Boost.Python, and the webpage that describes it[1] shows some
signs of abandonment: last update in February, talking about
what the author is proposing to do "this week". What's the
actual state of this work?

    [1] http://home.clara.net/raoulgough/boost/

Thanks in advance,

-- 
Gareth McCaughan




More information about the Cplusplus-sig mailing list