[pygccxml-commit] SF.net SVN: pygccxml:[1801] pyplusplus_dev
Brought to you by:
mbaas,
roman_yakovenko
From: <rom...@us...> - 2010-01-13 12:05:28
|
Revision: 1801 http://pygccxml.svn.sourceforge.net/pygccxml/?rev=1801&view=rev Author: roman_yakovenko Date: 2010-01-13 12:05:21 +0000 (Wed, 13 Jan 2010) Log Message: ----------- integrating inout_static_matrix implementation Modified Paths: -------------- pyplusplus_dev/docs/documentation/functions/transformation/inout.rest pyplusplus_dev/docs/documentation/functions/transformation/transformation.rest Added Paths: ----------- pyplusplus_dev/docs/documentation/functions/transformation/inout_static_matrix.rest pyplusplus_dev/pyplusplus/code_repository/indexing_suite/registry_utils_header.py Modified: pyplusplus_dev/docs/documentation/functions/transformation/inout.rest =================================================================== --- pyplusplus_dev/docs/documentation/functions/transformation/inout.rest 2010-01-13 11:45:48 UTC (rev 1800) +++ pyplusplus_dev/docs/documentation/functions/transformation/inout.rest 2010-01-13 12:05:21 UTC (rev 1801) @@ -6,7 +6,7 @@ Definition ---------- -``inout`` transformer is a combination of :doc:`input <inout>` and :doc:`output <output>` transformers. +``inout`` transformer is a combination of :doc:`input <input>` and :doc:`output <output>` transformers. It removes a "reference" type from the function argument and then adds the "returned", by the original function, value to the return statement of the function-wrapper. Added: pyplusplus_dev/docs/documentation/functions/transformation/inout_static_matrix.rest =================================================================== --- pyplusplus_dev/docs/documentation/functions/transformation/inout_static_matrix.rest (rev 0) +++ pyplusplus_dev/docs/documentation/functions/transformation/inout_static_matrix.rest 2010-01-13 12:05:21 UTC (rev 1801) @@ -0,0 +1,91 @@ +==================================== +``inout_static_matrix`` transformer +==================================== + +---------- +Definition +---------- + +``inout_static_matrix`` transformer is a combination of :doc:`input <input_static_matrix>` +and :doc:`output <output_static_matrix>` transformers. +It allows to call a C++ function, which takes 2D array using Python ``list`` class + +"input_static_matrix" transformer takes as first argument name or index of the +original function argument. The argument should have "array" or "pointer" type. +The second and the third arguments specify rows and columns sizes. + +----------- +Limitations +----------- + +This transformer could not be applied on virtual functions. + +------- +Example +------- + +.. code-block:: c++ + + int sum_and_fill( int m[2][3], int value ){ + int result = 0; + for( int r = 0; r < 2; ++r ){ + for( int c = 0; c < 3; ++c ){ + result += m[r][c]; + m[r][c] *= value; + } + } + return result; + } + +In order to expose ``sum_and_fill`` function we need to create a small wrapper. +The following :doc:`Py++ <../../../pyplusplus>` code does it for you: + + .. code-block:: python + + from pyplusplus import module_builder + from pyplusplus import function_transformers as FT + + mb = module_builder.module_builder_t( ... ) + sum_and_fill = mb.free_fun( 'sum_and_fill' ) + sum_and_fill.add_transformation( ft.inout_static_matrix('m', rows=2, columns=3) ) + +What you see below is the relevant pieces of generated code: + + .. code-block:: c++ + + static boost::python::tuple sum_and_fill_ec4892ec81f672fe151a0a2caa3215f4( boost::python::object m, int value ){ + int native_m[2][3]; + boost::python::list py_m; + pyplus_conv::ensure_uniform_sequence< boost::python::list >( m, 2 ); + for( size_t row = 0; row < 2; ++row ){ + pyplus_conv::ensure_uniform_sequence< int >( m[row], 3 ); + pyplus_conv::copy_sequence( m[row], pyplus_conv::array_inserter( native_m[row], 3 ) ); + } + int result = ::ft::sum_and_fill(native_m, value); + for (int row2 = 0; row2 < 2; ++row2 ){ + boost::python::list pyrow; + pyplus_conv::copy_container( native_m[row2] + , native_m[row2] + 3 + , pyplus_conv::list_inserter( pyrow ) ); + py_m.append( pyrow ); + } + return bp::make_tuple( result, py_m ); + } + + BOOST_PYTHON_MODULE(ft_inout_static_matrix){ + { //::ft::sum_and_fill + + typedef boost::python::tuple ( *sum_and_fill_function_type )( boost::python::object,int ); + + bp::def( + "sum_and_fill" + , sum_and_fill_function_type( &sum_and_fill_ec4892ec81f672fe151a0a2caa3215f4 ) + , ( bp::arg("m"), bp::arg("value") ) ); + + } + } + +.. _`Boost.Python`: http://www.boost.org/libs/python/doc/index.html +.. _`Python`: http://www.python.org +.. _`GCC-XML`: http://www.gccxml.org + Modified: pyplusplus_dev/docs/documentation/functions/transformation/transformation.rest =================================================================== --- pyplusplus_dev/docs/documentation/functions/transformation/transformation.rest 2010-01-13 11:45:48 UTC (rev 1800) +++ pyplusplus_dev/docs/documentation/functions/transformation/transformation.rest 2010-01-13 12:05:21 UTC (rev 1801) @@ -97,6 +97,7 @@ from_address.rest input_static_matrix.rest output_static_matrix.rest + inout_static_matrix.rest Added: pyplusplus_dev/pyplusplus/code_repository/indexing_suite/registry_utils_header.py =================================================================== --- pyplusplus_dev/pyplusplus/code_repository/indexing_suite/registry_utils_header.py (rev 0) +++ pyplusplus_dev/pyplusplus/code_repository/indexing_suite/registry_utils_header.py 2010-01-13 12:05:21 UTC (rev 1801) @@ -0,0 +1,55 @@ +# Copyright 2004-2008 Roman Yakovenko. +# Distributed under the Boost Software License, Version 1.0. (See +# accompanying file LICENSE_1_0.txt or copy at +# http://www.boost.org/LICENSE_1_0.txt) + +""" +This file contains indexing suite v2 code +""" + +file_name = "indexing_suite/registry_utils.hpp" + +code = """// Header file registry_utils.hpp +// +// provides few convenience functions for dealing with boost:python registry +// +// Copyright (c) 2010 Roman Yakovenko +// +// Use, modification and distribution is subject to the Boost Software +// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy +// at http://www.boost.org/LICENSE_1_0.txt) +// +// History +// ======= +// 2010/01/12 Roman File creation +// + +#ifndef REGISTRY_UTILS_12_01_2010_HPP +#define REGISTRY_UTILS_12_01_2010_HPP + +#include <boost/config.hpp> +#include "boost/python.hpp" +#include "boost/python/converter/registry.hpp" + +namespace boost{ namespace python{ namespace registry{ namespace utils{ + +template<class T> +bool is_registered(){ + namespace bpl = boost::python; + bpl::handle<> class_obj( bpl::objects::registered_class_object( bpl::type_id< T >())); + return class_obj.get() ? true : false; +} + +template< class T > +void register_alias( const char* name ){ + namespace bpl = boost::python; + bpl::handle<> class_obj( bpl::objects::registered_class_object( bpl::type_id< T >())); + boost::python::scope().attr( name ) = bpl::object( class_obj ); +} + +}}}} + +#endif // REGISTRY_UTILS_12_01_2010_HPP + + +""" This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |