Пример #1
0
    def __call__(self, variable, hint=None):
        if not isinstance(variable, declarations.variable_t):
            return None

        assert hint in ('get', 'set')

        if not declarations.is_reference(variable.decl_type):
            return None

        no_ref = declarations.remove_reference(variable.decl_type)
        base_type = declarations.remove_const(no_ref)
        if python_traits.is_immutable(base_type):
            #the relevant code creator will generate code, that will return this member variable
            #by value
            return decl_wrappers.default_call_policies()

        if not isinstance(base_type, declarations.declarated_t):
            return None

        base_type = declarations.remove_alias(base_type)
        declaration = base_type.declaration

        if declarations.is_class_declaration(declaration):
            return None

        if declaration.is_abstract:
            return None
        if declarations.has_destructor(
                declaration
        ) and not declarations.has_public_destructor(declaration):
            return None
        if not declarations.has_copy_constructor(declaration):
            return None
        if hint == 'get':
            #if we rich this line, it means that we are able to create an obect using
            #copy constructor.
            if declarations.is_const(no_ref):
                return decl_wrappers.return_value_policy(
                    decl_wrappers.copy_const_reference)
            else:
                return decl_wrappers.return_value_policy(
                    decl_wrappers.copy_non_const_reference)
        else:
            return decl_wrappers.default_call_policies()
Пример #2
0
    def _get_has_setter( self ):
        if declarations.is_const( declarations.remove_reference( self.declaration.type ) ):
            return False
        elif python_traits.is_immutable( self._get_exported_var_type() ):
            return True
        else:
            pass

        no_ref = declarations.remove_reference( self.declaration.type )
        no_const = declarations.remove_const( no_ref )
        base_type = declarations.remove_alias( no_const )
        if not isinstance( base_type, declarations.declarated_t ):
            return True #TODO ????
        decl = base_type.declaration
        if decl.is_abstract:
            return False
        if declarations.has_destructor( decl ) and not declarations.has_public_destructor( decl ):
            return False
        if not declarations.has_copy_constructor(decl):
            return False
        return True
    def _get_has_setter( self ):
        if declarations.is_const( declarations.remove_reference( self.declaration.type ) ):
            return False
        elif python_traits.is_immutable( self._get_exported_var_type() ):
            return True
        else:
            pass

        no_ref = declarations.remove_reference( self.declaration.type )
        no_const = declarations.remove_const( no_ref )
        base_type = declarations.remove_alias( no_const )
        if not isinstance( base_type, declarations.declarated_t ):
            return True #TODO ????
        decl = base_type.declaration
        if decl.is_abstract:
            return False
        if declarations.has_destructor( decl ) and not declarations.has_public_destructor( decl ):
            return False
        if not declarations.has_copy_constructor(decl):
            return False
        return True
Пример #4
0
    def __call__( self, variable, hint=None ):
        if not isinstance( variable, declarations.variable_t ):
            return None

        assert hint in ( 'get', 'set' )
        
        if not declarations.is_reference( variable.type ):
            return None
        
        no_ref = declarations.remove_reference( variable.type )
        base_type = declarations.remove_const( no_ref )
        if python_traits.is_immutable( base_type ):
            #the relevant code creator will generate code, that will return this member variable
            #by value
            return decl_wrappers.default_call_policies()
        
        if not isinstance( base_type, declarations.declarated_t ):
            return None
        
        base_type = declarations.remove_alias( base_type )
        decl = base_type.declaration
        
        if declarations.is_class_declaration( decl ):
            return None
        
        if decl.is_abstract:
            return None
        if declarations.has_destructor( decl ) and not declarations.has_public_destructor( decl ): 
            return None
        if not declarations.has_copy_constructor(decl):
            return None
        if hint == 'get':
            #if we rich this line, it means that we are able to create an obect using
            #copy constructor. 
            if declarations.is_const( no_ref ):
                return decl_wrappers.return_value_policy( decl_wrappers.copy_const_reference )
            else:
                return decl_wrappers.return_value_policy( decl_wrappers.copy_non_const_reference )
        else:
            return decl_wrappers.default_call_policies()
Пример #5
0
    def visit_class(self ):
        self.__dependencies_manager.add_exported( self.curr_decl )
        cls_decl = self.curr_decl
        cls_parent_cc = self.curr_code_creator
        exportable_members = self.curr_decl.get_exportable_members(sort_algorithms.sort)

        wrapper = None
        cls_cc = code_creators.class_t( class_inst=self.curr_decl )

        if self.curr_decl.is_wrapper_needed():
            wrapper = code_creators.class_wrapper_t( declaration=self.curr_decl
                                                     , class_creator=cls_cc )
            cls_cc.wrapper = wrapper
            cls_cc.associated_decl_creators.append( wrapper )
            #insert wrapper before module body
            if isinstance( self.curr_decl.parent, declarations.class_t ):
                #we deal with internal class
                self.curr_code_creator.wrapper.adopt_creator( wrapper )
            else:
                self.__extmodule.adopt_declaration_creator( wrapper )
            #next constructors are not present in code, but compiler generated
            #Boost.Python requiers them to be declared in the wrapper class
            if '0.9' in self.curr_decl.compiler:
                copy_constr = self.curr_decl.find_copy_constructor()
                add_to_wrapper = False
                if declarations.has_copy_constructor( self.curr_decl ):
                    #find out whether user or compiler defined it
                    if self.curr_decl.noncopyable:
                        add_to_wrapper = False
                    elif not copy_constr:
                        add_to_wrapper = True #compiler defined will not be exposed manually later
                    elif copy_constr.is_artificial:
                        add_to_wrapper = True #compiler defined will not be exposed manually later
                if add_to_wrapper:
                    cccc = code_creators.copy_constructor_wrapper_t( class_=self.curr_decl)
                    wrapper.adopt_creator( cccc )
                trivial_constr = self.curr_decl.find_trivial_constructor()
                add_to_wrapper = False
                if declarations.has_trivial_constructor( self.curr_decl ):
                    if not trivial_constr:
                        add_to_wrapper = True
                    elif trivial_constr.is_artificial:
                        add_to_wrapper = True
                if add_to_wrapper:
                    tcons = code_creators.null_constructor_wrapper_t( class_=self.curr_decl )
                    wrapper.adopt_creator( tcons )
            else:
                if declarations.has_copy_constructor( self.curr_decl ):
                    copy_constr = self.curr_decl.find_copy_constructor()
                    if not self.curr_decl.noncopyable and copy_constr.is_artificial:
                        cccc = code_creators.copy_constructor_wrapper_t( class_=self.curr_decl)
                        wrapper.adopt_creator( cccc )
                    null_constr = self.curr_decl.find_trivial_constructor()
                    if null_constr and null_constr.is_artificial:
                        #this constructor is not going to be exposed
                        tcons = code_creators.null_constructor_wrapper_t( class_=self.curr_decl )
                        wrapper.adopt_creator( tcons )

        exposed = self.expose_overloaded_mem_fun_using_macro( cls_decl, cls_cc )

        cls_parent_cc.adopt_creator( cls_cc )
        self.curr_code_creator = cls_cc
        for decl in exportable_members:
            if decl in exposed:
                continue
            self.curr_decl = decl
            declarations.apply_visitor( self, decl )

        for redefined_func in cls_decl.redefined_funcs():
            if isinstance( redefined_func, declarations.operator_t ):
                continue
            self.curr_decl = redefined_func
            declarations.apply_visitor( self, redefined_func )

        #all static_methods_t should be moved to the end
        #better approach is to move them after last def of relevant function
        static_methods = filter( lambda creator: isinstance( creator, code_creators.static_method_t )
                                 , cls_cc.creators )
        for static_method in static_methods:
            cls_cc.remove_creator( static_method )
            cls_cc.adopt_creator( static_method )

        if cls_decl.exception_translation_code:
            translator = code_creators.exception_translator_t( cls_decl )
            self.__extmodule.adopt_declaration_creator( translator )
            cls_cc.associated_decl_creators.append( translator )
            translator_register \
                = code_creators.exception_translator_register_t( cls_decl, translator )
            cls_cc.adopt_creator( translator_register )

        for property_def in cls_decl.properties:
            cls_cc.adopt_creator( code_creators.property_t(property_def) )

        self.curr_decl = cls_decl
        self.curr_code_creator = cls_parent_cc