Variables¶
expose_address¶
variable_t
declarations have got new property expose_address
. If you set
it value to True
, Py++ will register new property with the same name, but
the type of it will be unsigned int
and the value is address of the variable.
Py++ will take care and generate the right code for global, static and member variables.
Show me the code¶
Lets say you have the following C++ code:
struct bytes_t{ bytes_t(){ data = new int[5]; for(int i=0; i<5; i++){ data[i] = i; } } ... int* data; static int* x; }; //somewhere in a cpp file int* bytes_t::x = new int( 1997 );
In order to get access to the bytes_t::data
and bytes_t::x
you
have to turn on expose_address
property to True
:
mb = module_builder_t( ... ) bytes = mb.class_( 'bytes_t' ) bytes.vars().expose_address = True
Py++ will generate code, which will expose the address of the variables.
and now it is a time to show some ctypes magic:
import ctypes import your_module as m bytes = m.bytes_t() data_type = ctypes.POINTER( ctypes.c_int ) data = data_type.from_address( bytes.data ) for j in range(5): print '%d : %d' % ( j, data[j] ) data_type = ctypes.POINTER( ctypes.c_int ) data = data_type.from_address( m.bytes_t.x ) print x.contents.value