[pygccxml-commit] SF.net SVN: pygccxml:[1797] pyplusplus_dev
Brought to you by:
mbaas,
roman_yakovenko
|
From: <rom...@us...> - 2010-01-13 09:48:27
|
Revision: 1797
http://pygccxml.svn.sourceforge.net/pygccxml/?rev=1797&view=rev
Author: roman_yakovenko
Date: 2010-01-13 09:48:10 +0000 (Wed, 13 Jan 2010)
Log Message:
-----------
integrate output_static_matrix
Modified Paths:
--------------
pyplusplus_dev/docs/documentation/functions/transformation/input_static_matrix.rest
pyplusplus_dev/docs/documentation/functions/transformation/transformation.rest
pyplusplus_dev/pyplusplus/function_transformers/transformers.py
pyplusplus_dev/unittests/test_all.py
Added Paths:
-----------
pyplusplus_dev/docs/documentation/functions/transformation/output_static_matrix.rest
pyplusplus_dev/unittests/data/ft_output_static_matrix_to_be_exported.hpp
pyplusplus_dev/unittests/ft_output_static_matrix_tester.py
Modified: pyplusplus_dev/docs/documentation/functions/transformation/input_static_matrix.rest
===================================================================
--- pyplusplus_dev/docs/documentation/functions/transformation/input_static_matrix.rest 2010-01-13 09:02:23 UTC (rev 1796)
+++ pyplusplus_dev/docs/documentation/functions/transformation/input_static_matrix.rest 2010-01-13 09:48:10 UTC (rev 1797)
@@ -13,7 +13,7 @@
"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 specfies rows and columns size.
+The second and the third arguments specify rows and columns size.
-----------
Limitations
@@ -43,7 +43,7 @@
}
-In order to expose ``sum`` function we need to create small wrapper:
+In order to expose ``sum`` function we need to create a small wrapper:
The following :doc:`Py++ <../../../pyplusplus>` code does it for you:
.. code-block:: python
Added: pyplusplus_dev/docs/documentation/functions/transformation/output_static_matrix.rest
===================================================================
--- pyplusplus_dev/docs/documentation/functions/transformation/output_static_matrix.rest (rev 0)
+++ pyplusplus_dev/docs/documentation/functions/transformation/output_static_matrix.rest 2010-01-13 09:48:10 UTC (rev 1797)
@@ -0,0 +1,85 @@
+=====================================
+``output_static_matrix`` transformer
+=====================================
+
+----------
+Definition
+----------
+
+"output_static_matrix" transformer works on native 2D static arrays.
+It handles the translation between a matrix and `Python`_ list object.
+The matrix row and column sizes should be known in advance.
+
+"output_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 size.
+
+-----------
+Limitations
+-----------
+
+This transformer could not be applied on virtual functions.
+
+
+-------
+Example
+-------
+
+.. code-block:: c++
+
+ void filler( int m[2][3], int value ){
+ for( int r = 0; r < 2; ++r ){
+ for( int c = 0; c < 3; ++c ){
+ m[r][c] = value;
+ }
+ }
+ }
+
+In order to expose ``filler`` 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( ... )
+ filler = mb.free_fun( 'filler' )
+ filler.add_transformation( ft.output_static_matrix('m', rows=2, columns=3) )
+
+What you see below is the relevant pieces of generated code:
+
+ .. code-block:: c++
+
+ #include "__convenience.pypp.hpp" //Py++ header file, which contains few convenience function
+
+ namespace bp = boost::python;
+
+ static boost::python::object filler_7b0a7cb8f4000f0474aa44d21c2e4917( int value ){
+ int native_m[2][3];
+ boost::python::list py_m;
+ ::ft::filler(native_m, value);
+ for (int row = 0; row < 2; ++row ){
+ boost::python::list pyrow;
+ pyplus_conv::copy_container( native_m[row]
+ , native_m[row] + 3
+ , pyplus_conv::list_inserter( pyrow ) );
+ py_m.append( pyrow );
+ }
+ return bp::object( py_m );
+ }
+
+ BOOST_PYTHON_MODULE(...){
+ ...
+ typedef boost::python::object ( *filler_function_type )( int );
+
+ bp::def(
+ "filler"
+ , filler_function_type( &filler_7b0a7cb8f4000f0474aa44d21c2e4917 )
+ , ( 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 09:02:23 UTC (rev 1796)
+++ pyplusplus_dev/docs/documentation/functions/transformation/transformation.rest 2010-01-13 09:48:10 UTC (rev 1797)
@@ -96,6 +96,7 @@
input_c_buffer.rest
from_address.rest
input_static_matrix.rest
+ output_static_matrix.rest
Modified: pyplusplus_dev/pyplusplus/function_transformers/transformers.py
===================================================================
--- pyplusplus_dev/pyplusplus/function_transformers/transformers.py 2010-01-13 09:02:23 UTC (rev 1796)
+++ pyplusplus_dev/pyplusplus/function_transformers/transformers.py 2010-01-13 09:48:10 UTC (rev 1797)
@@ -262,11 +262,6 @@
_arr2seq = string.Template(
'pyplus_conv::copy_container( $native_array, $native_array + $array_size, pyplus_conv::list_inserter( $pylist ) );' )
-_mat2seq = string.Template(
- 'for (int i=0; i<$rows; ++i) { boost::python::list ${pylist}_i; pyplus_conv::copy_container( $native_matrix[i], $native_matrix[i] + $columns, pyplus_conv::list_inserter( ${pylist}_i ) ); $pylist.append(${pylist}_i); }' )
-
-
-
class input_static_array_t(transformer.transformer_t):
"""Handles an input array with fixed size.
@@ -534,6 +529,17 @@
, ' pyplus_conv::copy_sequence( $pymatrix[$row], pyplus_conv::array_inserter( $native_matrix[$row], $columns ) );'
, '}']))
+
+_cmatrix2pymatrix = string.Template( os.linesep.join([
+ 'for (int $row = 0; $row < $rows; ++$row ){'
+ , ' boost::python::list $pyrow;'
+ , ' pyplus_conv::copy_container( $native_matrix[$row]'
+ , ' , $native_matrix[$row] + $columns'
+ , ' , pyplus_conv::list_inserter( $pyrow ) );'
+ , ' $pymatrix.append( $pyrow ); '
+ , '}' ]))
+
+
# input_static_matrix_t
class input_static_matrix_t(transformer.transformer_t):
"""Handles an input matrix with fixed size.
@@ -654,31 +660,32 @@
return [ code_repository.convenience.file_name ]
def __configure_sealed(self, controller):
- global _mat2seq
+ global _cmatrix2pymatrix
#removing arg from the function wrapper definition
controller.remove_wrapper_arg( self.arg.name )
# Declare a variable that will hold the C matrix...
native_matrix = controller.declare_variable( self.matrix_item_type
, "native_" + self.arg.name
- , '[%d][%d]' % (self.rows, self.columns)
- )
+ , '[%d][%d]' % (self.rows, self.columns ) )
#adding just declared variable to the original function call expression
controller.modify_arg_expression( self.arg_index, native_matrix )
# Declare a Python list which will receive the output...
- pylist = controller.declare_variable( declarations.dummy_type_t( "boost::python::list" )
+ pymatrix = controller.declare_variable( declarations.dummy_type_t( "boost::python::list" )
, 'py_' + self.arg.name )
- copy_mat2pylist = _mat2seq.substitute( native_matrix = native_matrix,
- rows=self.rows,
- columns=self.columns,
- pylist=pylist)
+ conversion_code = _cmatrix2pymatrix.substitute( pymatrix=pymatrix
+ , columns='%d' % self.columns
+ , row=controller.register_variable_name( "row" )
+ , pyrow=controller.register_variable_name( "pyrow" )
+ , rows='%d' % self.rows
+ , native_matrix=native_matrix )
- controller.add_post_call_code( copy_mat2pylist )
+ controller.add_post_call_code( conversion_code )
#adding the variable to return variables list
- controller.return_variable( pylist )
+ controller.return_variable( pymatrix )
def __configure_v_mem_fun_default( self, controller ):
self.__configure_sealed( controller )
Added: pyplusplus_dev/unittests/data/ft_output_static_matrix_to_be_exported.hpp
===================================================================
--- pyplusplus_dev/unittests/data/ft_output_static_matrix_to_be_exported.hpp (rev 0)
+++ pyplusplus_dev/unittests/data/ft_output_static_matrix_to_be_exported.hpp 2010-01-13 09:48:10 UTC (rev 1797)
@@ -0,0 +1,25 @@
+// 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_output_static_matrix_to_be_exported_hpp__
+#define __ft_output_static_matrix_to_be_exported_hpp__
+
+#include <cmath>
+#include <string>
+#include <iostream>
+
+namespace ft{
+
+void filler( int m[2][3], int value ){
+ for( int r = 0; r < 2; ++r ){
+ for( int c = 0; c < 3; ++c ){
+ m[r][c] = value;
+ }
+ }
+}
+
+}
+
+#endif//__ft_output_static_matrix_to_be_exported_hpp__
Added: pyplusplus_dev/unittests/ft_output_static_matrix_tester.py
===================================================================
--- pyplusplus_dev/unittests/ft_output_static_matrix_tester.py (rev 0)
+++ pyplusplus_dev/unittests/ft_output_static_matrix_tester.py 2010-01-13 09:48:10 UTC (rev 1797)
@@ -0,0 +1,47 @@
+# 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
+
+class tester_t(fundamental_tester_base.fundamental_tester_base_t):
+ EXTENSION_NAME = 'ft_output_static_matrix'
+
+ 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 = mb.free_fun( 'filler' )
+ sum.add_transformation( ft.output_static_matrix('m', rows=2, columns=3) )
+
+
+ #calculate = mb.mem_fun( 'calculate' )
+ #calculate.add_transformation( ft.input_static_matrix('m', rows=3, columns=5) )
+
+ def run_tests(self, module):
+ """Run the actual unit tests"""
+ self.failUnless( [[23,23,23],[23,23,23]] == module.filler( 23 ) )
+
+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 09:02:23 UTC (rev 1796)
+++ pyplusplus_dev/unittests/test_all.py 2010-01-13 09:48:10 UTC (rev 1797)
@@ -123,6 +123,7 @@
import ft_from_address_tester
import indexing_suites2_support_tester
import ft_input_static_matrix_tester
+import ft_output_static_matrix_tester
testers = [
algorithms_tester
@@ -234,6 +235,7 @@
, ft_from_address_tester
, indexing_suites2_support_tester
, ft_input_static_matrix_tester
+ , ft_output_static_matrix_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.
|