Hi,

We have a simple data structure like this, that we would like to expose via 
boost python:

struct Layer
{
    double value;
};

struct Document
{
    std::vector< Layer > layer;
};

If we were writing this in pure python, then it could be written as follows:

class Layer:
    def __init__( self ):
        self.value = 0.0

class Document:
    def __init__( self ):
        self.layers = []

We would like our C++ bindings to match the behaviour of python as closely as 
possible. But we have encountered a scenario in which it doesn't, and we wanted 
to get some advice on the best way forward.

In pure python, the following code produces an output of '123.0':

l = Layer( )
d = Document( )
d.layers.append( l )
l.value = 123.0
print d.layers[0].value

When exposing C++ classes via boost python (using the vector_indexing_suite for 
Document.layers), it produces a result of '0.0'.

The 'append' is making a copy such that 'l' and 'd.layers[0]' are two separate 
objects. We've noticed that one way to make this work is by using shared_ptr in 
our C++ data structure, but we feel we would have to compromise on our C++ API 
as a result. Do we have any other options?

Thanks,

James.
_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig@python.org
https://mail.python.org/mailman/listinfo/cplusplus-sig

Reply via email to