=========
Variables
=========

.. contents:: Table of contents

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

  .. code-block:: C++

     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``:

  .. code-block:: Python

    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:

  .. code-block:: Python

    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


.. _`ctypes` : http://docs.python.org/lib/module-ctypes.html
.. _`from_address` : http://docs.python.org/lib/ctypes-data-types.html
.. _`Py++` : ./../../pyplusplus.html
.. _`pygccxml` : ./../../../pygccxml/pygccxml.html
.. _`Boost.Python`: http://www.boost.org/libs/python/doc/index.html
.. _`Python`: http://www.python.org
.. _`GCC-XML`: http://www.gccxml.org

