[pygccxml-commit] SF.net SVN: pygccxml:[1802] pyplusplus_dev
Brought to you by:
mbaas,
roman_yakovenko
From: <rom...@us...> - 2010-01-14 08:50:30
|
Revision: 1802 http://pygccxml.svn.sourceforge.net/pygccxml/?rev=1802&view=rev Author: roman_yakovenko Date: 2010-01-14 08:50:23 +0000 (Thu, 14 Jan 2010) Log Message: ----------- integrating input_static_array FT Modified Paths: -------------- pyplusplus_dev/docs/documentation/functions/transformation/transformation.rest pyplusplus_dev/pyplusplus/function_transformers/transformer.py pyplusplus_dev/unittests/test_all.py Added Paths: ----------- pyplusplus_dev/docs/documentation/functions/transformation/inout_static_array.rest pyplusplus_dev/unittests/data/ft_inout_static_array_to_be_exported.hpp pyplusplus_dev/unittests/ft_inout_static_array_tester.py Added: pyplusplus_dev/docs/documentation/functions/transformation/inout_static_array.rest =================================================================== --- pyplusplus_dev/docs/documentation/functions/transformation/inout_static_array.rest (rev 0) +++ pyplusplus_dev/docs/documentation/functions/transformation/inout_static_array.rest 2010-01-14 08:50:23 UTC (rev 1802) @@ -0,0 +1,72 @@ +==================================== +``inout_static_array`` transformer +==================================== + +---------- +Definition +---------- + +``inout_static_array`` transformer is a combination of :doc:`input <input_static_array>` +and :doc:`output <output_static_array>` transformers. +It allows to call a C++ function, which takes an array using Python ``list`` class + +"inout_static_array" transformer takes as first argument name or index of the +original function argument. The argument should have "array" or "pointer" type. +The second argument specifies the array size. + +------- +Example +------- + +.. code-block:: c++ + + int sum_and_fill( int v[3], int value ){ + int result = v[0] + v[1] + v[2]; + v[0] = value; + v[1] = value; + v[2] = 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_array('v', 3) ) + +What you see below is the relevant pieces of generated code: + + .. code-block:: c++ + + static boost::python::tuple sum_and_fill_2dd285a3344dbf7d71ffb7c78dd614c5( boost::python::object v, int value ){ + int native_v[3]; + boost::python::list py_v; + pyplus_conv::ensure_uniform_sequence< int >( v, 3 ); + pyplus_conv::copy_sequence( v, pyplus_conv::array_inserter( native_v, 3 ) ); + int result = ::sum_and_fill(native_v, value); + pyplus_conv::copy_container( native_v, native_v + 3, pyplus_conv::list_inserter( py_v ) ); + return bp::make_tuple( result, py_v ); + } + + BOOST_PYTHON_MODULE(ft_inout_static_array){ + { //::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_2dd285a3344dbf7d71ffb7c78dd614c5 ) + , ( bp::arg("v"), 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 12:05:21 UTC (rev 1801) +++ pyplusplus_dev/docs/documentation/functions/transformation/transformation.rest 2010-01-14 08:50:23 UTC (rev 1802) @@ -92,6 +92,7 @@ modify_type.rest input_static_array.rest output_static_array.rest + inout_static_array.rest transfer_ownership.rest input_c_buffer.rest from_address.rest Modified: pyplusplus_dev/pyplusplus/function_transformers/transformer.py =================================================================== --- pyplusplus_dev/pyplusplus/function_transformers/transformer.py 2010-01-13 12:05:21 UTC (rev 1801) +++ pyplusplus_dev/pyplusplus/function_transformers/transformer.py 2010-01-14 08:50:23 UTC (rev 1802) @@ -39,7 +39,7 @@ found = filter( lambda arg: arg.name == reference, self.function.arguments ) if len( found ) == 1: return found[0] - raise RuntimeError( "Argument with %s was not found" % reference ) + raise RuntimeError( "Argument with name \"%s\" was not found" % reference ) else: assert isinstance( reference, int ) if transformer_t.USE_1_BASED_INDEXING: Added: pyplusplus_dev/unittests/data/ft_inout_static_array_to_be_exported.hpp =================================================================== --- pyplusplus_dev/unittests/data/ft_inout_static_array_to_be_exported.hpp (rev 0) +++ pyplusplus_dev/unittests/data/ft_inout_static_array_to_be_exported.hpp 2010-01-14 08:50:23 UTC (rev 1802) @@ -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) + +#ifndef __ft_inout_static_array_to_be_exported_hpp__ +#define __ft_inout_static_array_to_be_exported_hpp__ + +#include <cmath> +#include <string> +#include <iostream> + + +int sum_and_fill( int v[3], int value ){ + int result = v[0] + v[1] + v[2]; + v[0] = value; + v[1] = value; + v[2] = value; + return result; +} + +struct point3d_t{ + + point3d_t() + : x( 0 ), y(0), z(0) + {} + + int initialize( int v[3] ){ + x = v[0]; + y = v[1]; + z = v[2]; + return x*y*z; + } + + virtual void position( int v[3] ){ + v[0] = x; + v[1] = y; + v[2] = z; + } + + void swap( int v[3] ){ + std::swap( x, v[0] ); + std::swap( y, v[1] ); + std::swap( z, v[2] ); + } + + virtual void swap_derived( int v[3] ){ + this->swap( v ); + } + + int x, y, z; +}; + + +#endif//__ft_inout_static_array_to_be_exported_hpp__ Added: pyplusplus_dev/unittests/ft_inout_static_array_tester.py =================================================================== --- pyplusplus_dev/unittests/ft_inout_static_array_tester.py (rev 0) +++ pyplusplus_dev/unittests/ft_inout_static_array_tester.py 2010-01-14 08:50:23 UTC (rev 1802) @@ -0,0 +1,70 @@ +# 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) + +import os +import sys +import math +import unittest +import fundamental_tester_base +from pygccxml import declarations +from pyplusplus import function_transformers as ft +from pyplusplus.module_builder import call_policies + + +def remove_const_ref(type): + """Converts "T const&" into "T &" """ + if declarations.type_traits.is_reference(type): + t = declarations.cpptypes.reference_t(declarations.type_traits.remove_const(type.base)) + return t + return type + + +class tester_t(fundamental_tester_base.fundamental_tester_base_t): + EXTENSION_NAME = 'ft_inout_static_array' + + def __init__( self, *args ): + fundamental_tester_base.fundamental_tester_base_t.__init__( + self + , tester_t.EXTENSION_NAME + , *args ) + + def customize( self, mb ): + mb.global_ns.calldefs().create_with_signature = True + + sum_and_fill = mb.global_ns.calldef( 'sum_and_fill' ) + sum_and_fill.add_transformation( ft.inout_static_array( 'v', 3 ) ) + + point3d = mb.class_( 'point3d_t' ) + point3d.add_wrapper_code( '' ) + + swap = point3d.calldefs( lambda d: 'swap' in d.name ) + swap.add_transformation( ft.inout_static_array( 'v', 3 ) ) + + + point3d.mem_fun( 'initialize' ).add_transformation( ft.input_static_array(0, size=3) ) + point3d.mem_fun( 'position' ).add_transformation( ft.output_static_array(0, size=3) ) + + def run_tests(self, module): + """Run the actual unit tests. + """ + point3d = module.point3d_t() + result = point3d.initialize( [ 1,2,3 ] ) + self.failUnless( result== 1*2*3 and point3d.x == 1 and point3d.y==2 and point3d.z==3 ) + self.failUnless( [1,2,3] == point3d.position() ) + + self.failUnless( ( 12, [10,10,10] ) == module.sum_and_fill( [2,4,6], 10 ) ) + self.failUnless( [1,2,3] == point3d.swap( [4,5,6] ) + and [4,5,6] == point3d.position() ) + +def create_suite(): + suite = unittest.TestSuite() + suite.addTest( unittest.makeSuite(tester_t)) + return suite + +def run_suite(): + unittest.TextTestRunner(verbosity=2).run( create_suite() ) + +if __name__ == "__main__": + run_suite() Modified: pyplusplus_dev/unittests/test_all.py =================================================================== --- pyplusplus_dev/unittests/test_all.py 2010-01-13 12:05:21 UTC (rev 1801) +++ pyplusplus_dev/unittests/test_all.py 2010-01-14 08:50:23 UTC (rev 1802) @@ -125,6 +125,7 @@ import ft_input_static_matrix_tester import ft_output_static_matrix_tester import ft_inout_static_matrix_tester +import ft_inout_static_array_tester testers = [ algorithms_tester @@ -238,6 +239,7 @@ , ft_input_static_matrix_tester , ft_output_static_matrix_tester , ft_inout_static_matrix_tester + , ft_inout_static_array_tester # , ogre_generate_tester too much time ] This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |