Esempio n. 1
0
def beautify_func_list(func_list):
    func_list = [f for f in func_list if not f.ignore]

    # fix default values
    # don't remove std::vector default values, old compilers _need_ std::allocator removed
    for f in func_list:
        for arg in f.arguments:
            if isinstance(arg.default_value, str):
                repl_list = {
                    'std::basic_string<char, std::char_traits<char>, std::allocator<char> >':
                    'std::string',
                    'std::vector<cv::Point_<int>, std::allocator<cv::Point_<int> > >':
                    'std::vector<cv::Point>',
                    'std::vector<cv::Scalar_<double>, std::allocator<cv::Scalar_<double> > >':
                    'std::vector<cv::Scalar>',
                    'std::vector<int, std::allocator<int> >':
                    'std::vector<int>',
                    'std::vector<cv::Vec<int, 4>, std::allocator<cv::Vec<int, 4> > >':
                    'std::vector<cv::Vec4i>',
                }
                for z in repl_list:
                    arg.default_value = arg.default_value.replace(
                        z, repl_list[z])

    # one-to-one function argument
    for f in func_list:
        for arg in f.arguments:
            if is_arg_touched(f, arg.name):
                continue
            pds = _c.unique_pds(arg.type.partial_decl_string)
            if pds in _c.c2cpp:
                f._transformer_creators.append(
                    _FT.input_as_FixType(pds, _c.c2cpp[pds], arg.name))
            elif pds in [
                    'CvRNG *', 'CvRNG &', 'CvRNG cosnt *', 'CvRNG const &'
            ]:
                f._transformer_creators.append(_FT.input_asRNG(arg.name))
            elif pds in ['CvFileStorage *', 'CvFileStorage const *']:
                f._transformer_creators.append(
                    _FT.input_as_FileStorage(arg.name))
            elif pds in ['CvFileNode *', 'CvFileNode const *']:
                f._transformer_creators.append(_FT.input_as_FileNode(arg.name))
            elif pds in ['CvMemStorage *', 'CvMemStorage const *']:
                f._transformer_creators.append(
                    _FT.input_as_MemStorage(arg.name))
            elif pds in [
                    'CvSparseMat *', 'CvSparseMat &', 'CvSparseMat const *',
                    'CvSparseMat const &'
            ]:
                f._transformer_creators.append(_FT.input_asSparseMat(arg.name))
            elif pds in [
                    "IplImage *", "IplImage const *", "CvArr *",
                    "CvArr const *", "CvMat *", "CvMat const *",
                    "cv::Range const *"
            ]:
                f._transformer_creators.append(_FT.input_as_Mat(arg.name))

    # function argument int *sizes and int dims
    for f in func_list:
        for arg in f.arguments:
            if is_arg_touched(f, arg.name):
                continue
            if arg.name == 'sizes' and _D.is_pointer(arg.type):
                for arg2 in f.arguments:
                    if arg2.name == 'dims' and _D.is_integral(arg2.type):
                        f._transformer_creators.append(
                            _FT.input_array1d('sizes', 'dims'))
                        break
            if arg.name == '_sizes' and _D.is_pointer(arg.type):
                for arg2 in f.arguments:
                    if arg2.name == '_ndims' and _D.is_integral(arg2.type):
                        f._transformer_creators.append(
                            _FT.input_array1d('_sizes', '_ndims'))
                        break
                    if arg2.name == 'dims' and _D.is_integral(arg2.type):
                        f._transformer_creators.append(
                            _FT.input_array1d('_sizes', 'dims'))
                        break
            if arg.name == '_newsz' and _D.is_pointer(arg.type):
                for arg2 in f.arguments:
                    if arg2.name == '_newndims' and _D.is_integral(arg2.type):
                        f._transformer_creators.append(
                            _FT.input_array1d('_newsz', '_newndims'))
                        break

    # function argument const CvPoint2D32f * src and const CvPoint2D32f * dst
    for f in func_list:
        for arg in f.arguments:
            if is_arg_touched(f, arg.name):
                continue
            if arg.name == 'src' and _D.is_pointer(
                    arg.type) and 'CvPoint2D32f' in arg.type.decl_string:
                for arg2 in f.arguments:
                    if arg2.name == 'dst' and _D.is_pointer(
                            arg2.type
                    ) and 'CvPoint2D32f' in arg2.type.decl_string:
                        f._transformer_creators.append(
                            _FT.input_array1d('src'))
                        f._transformer_creators.append(
                            _FT.input_array1d('dst'))
                        break

    #  argument 'void *data'
    for f in func_list:
        for arg in f.arguments:
            if is_arg_touched(f, arg.name):
                continue
            if arg.name == 'data' and _D.is_void_pointer(arg.type):
                f._transformer_creators.append(_FT.input_string(arg.name))
                self.add_doc(f.name, "'data' is represented by a string")

    # final step: apply all the function transformations
    for f in func_list:
        if len(f._transformer_creators) > 0:
            sort_transformers(f)

            f.add_transformation(*f._transformer_creators,
                                 **f._transformer_kwds)
            if 'unique_function_name' in f._transformer_kwds:
                f.transformations[0].unique_name = f._transformer_kwds[
                    'unique_function_name']
            else:
                s = f.transformations[0].unique_name
                repl_dict = {
                    'operator()': '__call__',
                }
                for t in repl_dict:
                    if t in s:
                        s = s.replace(t, repl_dict[t])
                        f.transformations[0].unique_name = s
                        f.transformations[0].alias = repl_dict[t]
                        break

        _c.add_decl_desc(f)
Esempio n. 2
0
def beautify_func_list(func_list):
    func_list = [f for f in func_list if not f.ignore]

    # fix default values
    # don't remove std::vector default values, old compilers _need_ std::allocator removed
    for f in func_list:
        for arg in f.arguments:
            if isinstance(arg.default_value, str):
                repl_list = {
                    'std::basic_string<char, std::char_traits<char>, std::allocator<char> >': 'std::string',
                    'std::vector<cv::Point_<int>, std::allocator<cv::Point_<int> > >': 'std::vector<cv::Point>',
                    'std::vector<cv::Scalar_<double>, std::allocator<cv::Scalar_<double> > >': 'std::vector<cv::Scalar>',
                    'std::vector<int, std::allocator<int> >': 'std::vector<int>',
                    'std::vector<cv::Vec<int, 4>, std::allocator<cv::Vec<int, 4> > >': 'std::vector<cv::Vec4i>',
                }
                for z in repl_list:
                    arg.default_value = arg.default_value.replace(z, repl_list[z])

    # one-to-one function argument
    for f in func_list:
        for arg in f.arguments:
            if is_arg_touched(f, arg.name):
                continue
            pds = _c.unique_pds(arg.type.partial_decl_string)
            if pds in _c.c2cpp:
                f._transformer_creators.append(_FT.input_as_FixType(pds, _c.c2cpp[pds], arg.name))
            elif pds in ['CvRNG *', 'CvRNG &', 'CvRNG cosnt *', 'CvRNG const &']:
                f._transformer_creators.append(_FT.input_asRNG(arg.name))
            elif pds in ['CvFileStorage *', 'CvFileStorage const *']:
                f._transformer_creators.append(_FT.input_as_FileStorage(arg.name))
            elif pds in ['CvFileNode *', 'CvFileNode const *']:
                f._transformer_creators.append(_FT.input_as_FileNode(arg.name))
            elif pds in ['CvMemStorage *', 'CvMemStorage const *']:
                f._transformer_creators.append(_FT.input_as_MemStorage(arg.name))
            elif pds in ['CvSparseMat *', 'CvSparseMat &', 'CvSparseMat const *', 'CvSparseMat const &']:
                f._transformer_creators.append(_FT.input_asSparseMat(arg.name))
            elif pds in ["IplImage *", "IplImage const *", "CvArr *", "CvArr const *",
                "CvMat *", "CvMat const *", "cv::Range const *"]:
                f._transformer_creators.append(_FT.input_as_Mat(arg.name))

    # function argument int *sizes and int dims
    for f in func_list:
        for arg in f.arguments:
            if is_arg_touched(f, arg.name):
                continue
            if arg.name == 'sizes' and _D.is_pointer(arg.type):
                for arg2 in f.arguments:
                    if arg2.name == 'dims' and _D.is_integral(arg2.type):
                        f._transformer_creators.append(_FT.input_array1d('sizes', 'dims'))
                        break
            if arg.name == '_sizes' and _D.is_pointer(arg.type):
                for arg2 in f.arguments:
                    if arg2.name == '_ndims' and _D.is_integral(arg2.type):
                        f._transformer_creators.append(_FT.input_array1d('_sizes', '_ndims'))
                        break
                    if arg2.name == 'dims' and _D.is_integral(arg2.type):
                        f._transformer_creators.append(_FT.input_array1d('_sizes', 'dims'))
                        break
            if arg.name == '_newsz' and _D.is_pointer(arg.type):
                for arg2 in f.arguments:
                    if arg2.name == '_newndims' and _D.is_integral(arg2.type):
                        f._transformer_creators.append(_FT.input_array1d('_newsz', '_newndims'))
                        break

    # function argument const CvPoint2D32f * src and const CvPoint2D32f * dst
    for f in func_list:
        for arg in f.arguments:
            if is_arg_touched(f, arg.name):
                continue
            if arg.name == 'src' and _D.is_pointer(arg.type) and 'CvPoint2D32f' in arg.type.decl_string:
                for arg2 in f.arguments:
                    if arg2.name == 'dst' and _D.is_pointer(arg2.type) and 'CvPoint2D32f' in arg2.type.decl_string:
                        f._transformer_creators.append(_FT.input_array1d('src'))
                        f._transformer_creators.append(_FT.input_array1d('dst'))
                        break

    #  argument 'void *data'
    for f in func_list:
        for arg in f.arguments:
            if is_arg_touched(f, arg.name):
                continue
            if arg.name == 'data' and _D.is_void_pointer(arg.type):
                f._transformer_creators.append(_FT.input_string(arg.name))
                self.add_doc(f.name, "'data' is represented by a string")

    # final step: apply all the function transformations
    for f in func_list:
        if len(f._transformer_creators) > 0:
            sort_transformers(f)

            f.add_transformation(*f._transformer_creators, **f._transformer_kwds)
            if 'unique_function_name' in f._transformer_kwds:
                f.transformations[0].unique_name = f._transformer_kwds['unique_function_name']
            else:
                s = f.transformations[0].unique_name
                repl_dict = {
                    'operator()': '__call__',
                }
                for t in repl_dict:
                    if t in s:
                        s = s.replace(t, repl_dict[t])
                        f.transformations[0].unique_name = s
                        f.transformations[0].alias = repl_dict[t]
                        break

        _c.add_decl_desc(f)
Esempio n. 3
0
def customize_decls( mb ):
    classes = mb.classes()
    classes.always_expose_using_scope = True #better error reporting from compiler
    classes.redefine_operators = True #redefine all operators found in base classes
    for class_ in classes:
        if class_.name in aliases.db:
            class_.alias = aliases.db[ class_.name ]
            class_.wrapper_alias = class_.alias + '_wrapper'

    # Fix up default parameter use of null strings
    for func in mb.calldefs():
        for arg in func.arguments:
            if arg.default_value == 'FX::FXString::null':
                arg.default_value = 'FX::FXString::nullStr()'

    # Insert custom code into wrappers for FXObject subclasses
    fxobject = mb.class_( 'FXObject' )
    fxobjectclasses = mb.classes( lambda decl: decl==fxobject or declarations.is_base_and_derived( fxobject, decl ) )
    for fxobjectclass in fxobjectclasses:
        # Redirect handle() to point to python dispatcher
        fxobjectclass.member_function( 'handle' ).exclude()
        fxobjectclass.add_code( 'def("handle", &::FX::'+fxobjectclass.name+'::handle, &'+fxobjectclass.name+'_wrapper::default_handle, bp::default_call_policies() )' )

        # Make it so TnFOX objects and their python mirrors can be detached
        fxobjectclass.held_type = 'std::auto_ptr<'+fxobjectclass.name+'_wrapper>'

        # Add in runtime support for detaching and msg handling
        fxobjectclass.set_constructors_body( '    FX::FXPython::int_pythonObjectCreated((int_decouplingFunctor=FX::Generic::BindObjN(*this, &FX::'+fxobjectclass.name+'::decouplePythonObject)));' )
        fxobjectclass.add_wrapper_code("""    // TnFOX runtime support
    virtual long int handle( ::FX::FXObject * sender, ::FX::FXSelector sel, void * ptr ) {
        if( bp::override func_handle = this->get_override( "handle" ) )
            return func_handle( boost::python::ptr(sender), sel, ptr );
        else
            return default_handle( sender, sel, ptr );
    }
    
    long int default_handle( ::FX::FXObject * sender, ::FX::FXSelector sel, void * ptr ) {
        long ret=0;
        if( FX::FXPython::int_FXObjectHandle( &ret, this, sender, sel, ptr ) )
            return ret;
        return FX::"""+fxobjectclass.name+"""::handle( sender, sel, ptr );
    }


    ~"""+fxobjectclass.name+"""_wrapper( ) {
        FX::FXPython::int_pythonObjectDeleted(int_decouplingFunctor);
        using namespace boost::python;
        using namespace std;
        PyObject *py_self=get_owner(*this);
        // We must be careful not to reinvoke object destruction!
        py_self->ob_refcnt=1<<16;
        {
            object me(boost::python::handle<>(borrowed(py_self)));
            auto_ptr<"""+fxobjectclass.name+"""_wrapper> &autoptr=extract<auto_ptr<"""+fxobjectclass.name+"""_wrapper> &>(me);
            autoptr.release();
        }
        py_self->ob_refcnt=0;
    }

    virtual void *getPythonObject() const {
        return (void *) get_owner(*this);
    }
    virtual void decouplePythonObject() const {
        using namespace boost::python;
        using namespace std;
        PyObject *py_self=get_owner(*this);
        object me(boost::python::handle<>(borrowed(py_self)));
        auto_ptr<"""+fxobjectclass.name+"""_wrapper> &autoptr=extract<auto_ptr<"""+fxobjectclass.name+"""_wrapper> &>(me);
        autoptr.reset(0);
    }
private:
    Generic::BoundFunctorV *int_decouplingFunctor;""")

    # Patch in custom FXApp::init() implementation
    fxapp = mb.class_( 'FXApp' )
    fxapp.member_functions( 'init' ).exclude()
    fxapp.add_code( 'def("init", &FXApp_init)' )
    fxapp.add_code( 'def("init", &FXApp_init2)' )
    fxapp.add_code( 'def("getArgv", &FXApp_getArgv)' )

    # Patch in custom FXGLTriangleMesh implementations
    fxgltrianglemesh = mb.class_( 'FXGLTriangleMesh' )
    fxgltrianglemesh.member_functions( 'getVertexBuffer' ).exclude()
    fxgltrianglemesh.add_code   ( 'def("getVertexBuffer",       &FXGLTriangleMesh_getVertexBuffer)' )
    fxgltrianglemesh.member_functions( 'getColorBuffer' ).exclude()
    fxgltrianglemesh.add_code   ( 'def("getColorBuffer",        &FXGLTriangleMesh_getColorBuffer)' )
    fxgltrianglemesh.member_functions( 'getNormalBuffer' ).exclude()
    fxgltrianglemesh.add_code   ( 'def("getNormalBuffer",       &FXGLTriangleMesh_getNormalBuffer)' )
    fxgltrianglemesh.member_functions( 'getTextureCoordBuffer' ).exclude()
    fxgltrianglemesh.add_code   ( 'def("getTextureCoordBuffer", &FXGLTriangleMesh_getTextureCoordBuffer)' )

    # Patch in custom FXGLViewer implementations
    fxglviewer = mb.class_( 'FXGLViewer' )
    fxglviewer.member_functions( 'lasso' ).exclude()
    fxglviewer.add_code   ( 'def("lasso",  &FXGLViewer_lasso, bp::return_value_policy< bp::manage_new_object, bp::default_call_policies >() )' )
    fxglviewer.member_functions( 'select' ).exclude()
    fxglviewer.add_code   ( 'def("select", &FXGLViewer_select, bp::return_value_policy< bp::manage_new_object, bp::default_call_policies >() )' )

    # Patch image & bitmap getData() functions
    getdatafuncs = mb.calldefs( lambda decl: decl.name == 'getData' and not declarations.is_void_pointer( decl.return_type ) and ('Icon' in decl.parent.name or 'Image' in decl.parent.name or 'Bitmap' in decl.parent.name) )
    getdatafuncs.exclude()
    for getdatafunc in getdatafuncs:
        getdatafunc.parent.add_code( 'def("getData", &'+getdatafunc.parent.name+'_getData)' )
    
    # Patch sort functions
    sortfuncs = mb.calldefs( lambda decl: 'SortFunc' in decl.name and 'set' in decl.name )
    for sortfunc in sortfuncs:
        sortfunc.parent.add_code( 'def("'+sortfunc.name+'", &FX::FXPython::set'+sortfunc.parent.name+'SortFunc)' )
    
    # Patch QIODevice wrapper with missing pure virtual functions
    qiodevice = mb.class_( 'QIODevice' )
    qiodevice.add_wrapper_code("""    virtual ::FX::FXuval readBlock( char * data, ::FX::FXuval maxlen ){
        bp::override func_readBlock = this->get_override( "readBlock" );
        return func_readBlock( data, maxlen );
    }

    virtual ::FX::FXuval writeBlock( char const * data, ::FX::FXuval maxlen ){
        bp::override func_writeBlock = this->get_override( "writeBlock" );
        return func_writeBlock( data, maxlen );
    }

    virtual ::FX::FXuval readBlockFrom( char * data, ::FX::FXuval maxlen, ::FX::FXfval pos ){
        bp::override func_readBlockFrom = this->get_override( "readBlockFrom" );
        return func_readBlockFrom( data, maxlen, pos );
    }

    virtual ::FX::FXuval writeBlockTo( ::FX::FXfval pos, char const * data, ::FX::FXuval maxlen ){
        bp::override func_writeBlockTo = this->get_override( "writeBlockTo" );
        return func_writeBlockTo( pos, data, maxlen );
    }
""")
    qiodevices = mb.class_( 'QIODeviceS' )
    qiodevices.add_wrapper_code("""    virtual ::FX::FXuval readBlock( char * data, ::FX::FXuval maxlen ){
        bp::override func_readBlock = this->get_override( "readBlock" );
        return func_readBlock( data, maxlen );
    }

    virtual ::FX::FXuval writeBlock( char const * data, ::FX::FXuval maxlen ){
        bp::override func_writeBlock = this->get_override( "writeBlock" );
        return func_writeBlock( data, maxlen );
    }

    virtual void *int_getOSHandle() const{
        return 0;
    }
""")