Exemplo n.º 1
0
def addWrapperTypedefs(class_name, namespaces):

    short_wrapper_class_name = classutils.toWrapperType(class_name['short'])

    indent = ' ' * cfg.indent * len(namespaces)

    wrapper_typedef_code = ''
    wrapper_typedef_code += utils.constrNamespace(namespaces, 'open')

    temp_namespace_list = [gb.gambit_backend_namespace] + namespaces
    wrapper_typedef_code += indent + 'typedef ' + '::'.join(
        temp_namespace_list
    ) + '::' + class_name['short'] + ' ' + short_wrapper_class_name + ';\n'

    wrapper_typedef_code += utils.constrNamespace(namespaces, 'close')
    wrapper_typedef_code += '\n'

    frw_decl_include_statement = '#include "' + os.path.join(
        gb.backend_types_basedir, gb.gambit_backend_name_full,
        gb.frwd_decls_wrp_fname + cfg.header_extension) + '"\n'
    identification_include_statement = '#include "' + os.path.join(
        gb.backend_types_basedir, gb.gambit_backend_name_full,
        'identification.hpp') + '"\n\n'
    undef_include_statement = '#include "' + os.path.join(
        gb.gambit_backend_incl_dir, 'backend_undefs.hpp') + '"\n'

    wrapper_typedefs_path = os.path.join(
        gb.boss_output_dir, gb.wrapper_typedefs_fname + cfg.header_extension)

    if wrapper_typedefs_path not in gb.new_code.keys():
        gb.new_code[wrapper_typedefs_path] = {
            'code_tuples': [],
            'add_include_guard': False
        }

        gb.new_code[wrapper_typedefs_path]['code_tuples'].append(
            (0, frw_decl_include_statement))
        gb.new_code[wrapper_typedefs_path]['code_tuples'].append(
            (len(frw_decl_include_statement),
             identification_include_statement))
        gb.new_code[wrapper_typedefs_path]['code_tuples'].append(
            (-1, undef_include_statement))

    gb.new_code[wrapper_typedefs_path]['code_tuples'].append(
        (-len(undef_include_statement), wrapper_typedef_code))
Exemplo n.º 2
0
def constrWrapperUtils(class_name):

    wrapper_class_name = classutils.toWrapperType(class_name['long'],
                                                  include_namespace=True)
    abstr_class_name = classutils.toAbstractType(class_name['long'],
                                                 include_namespace=True)

    # Include statement for the header file
    wrapper_include_statement_decl = '#include "' + gb.new_header_files[
        class_name['long']]['wrapper_fullpath'] + '"\n'

    wr_utils_decl = ''
    wr_utils_impl = ''

    #
    # wrapper_creator
    #

    # Function declaration
    wr_utils_decl = '\n'
    wr_utils_decl += wrapper_class_name + '* wrapper_creator(' + abstr_class_name + '*);\n'

    # Function implementation
    wr_utils_impl = '\n'
    wr_utils_impl += wrapper_class_name + '* wrapper_creator(' + abstr_class_name + '* abs_ptr)\n'
    wr_utils_impl += '{\n'
    wr_utils_impl += ' ' * cfg.indent + 'return new ' + wrapper_class_name + '(abs_ptr);\n'
    wr_utils_impl += '}\n'

    # #
    # # wrapper_creator
    # #

    # # Function declaration
    # wr_utils_decl  = '\n'
    # wr_utils_decl += 'void wrapper_creator(' + abstr_class_name + '*);\n'

    # # Function implementation
    # wr_utils_impl  = '\n'
    # wr_utils_impl += 'void wrapper_creator(' + abstr_class_name + '* abs_ptr)\n'
    # wr_utils_impl += '{\n'
    # wr_utils_impl += ' '*cfg.indent + 'abs_ptr->set_wptr( new ' + wrapper_class_name + '(abs_ptr) );\n'
    # wr_utils_impl += '}\n'

    #
    # wrapper_deleter
    #

    # Function declaration
    wr_utils_decl += '\n'
    wr_utils_decl += 'void wrapper_deleter(' + wrapper_class_name + '*);\n'

    # Function implementation
    wr_utils_impl += '\n'
    wr_utils_impl += 'void wrapper_deleter(' + wrapper_class_name + '* wptr)\n'
    wr_utils_impl += '{\n'
    wr_utils_impl += ' ' * cfg.indent + 'wptr->set_delete_BEptr(false);\n'
    wr_utils_impl += ' ' * cfg.indent + 'delete wptr;\n'
    wr_utils_impl += '}\n'

    #
    # set_delete_BEptr
    #

    # Function declaration
    wr_utils_decl += '\n'
    wr_utils_decl += 'void set_delete_BEptr(' + wrapper_class_name + '*, bool);\n'

    # Function implementation
    wr_utils_impl += '\n'
    wr_utils_impl += 'void set_delete_BEptr(' + wrapper_class_name + '* wptr, bool setting)\n'
    wr_utils_impl += '{\n'
    wr_utils_impl += ' ' * cfg.indent + 'wptr->set_delete_BEptr(setting);\n'
    wr_utils_impl += '}\n'

    # Register code
    w_creator_header_path = os.path.join(
        gb.boss_output_dir, gb.wrapper_utils_fname + cfg.header_extension)
    w_creator_source_path = os.path.join(
        gb.boss_output_dir, gb.wrapper_utils_fname + cfg.source_extension)

    if w_creator_header_path not in gb.new_code.keys():
        gb.new_code[w_creator_header_path] = {
            'code_tuples': [],
            'add_include_guard': True
        }

        gb.new_code[w_creator_header_path]['code_tuples'].append(
            (0, '#include "' +
             os.path.join(gb.gambit_backend_incl_dir,
                          gb.wrapper_typedefs_fname + cfg.header_extension) +
             '"\n'))
        gb.new_code[w_creator_header_path]['code_tuples'].append(
            (0, '#include "' +
             os.path.join(gb.gambit_backend_incl_dir,
                          gb.abstract_typedefs_fname + cfg.header_extension) +
             '"\n'))

    gb.new_code[w_creator_header_path]['code_tuples'].append(
        (0, wrapper_include_statement_decl))
    gb.new_code[w_creator_header_path]['code_tuples'].append(
        (-1, wr_utils_decl))

    if w_creator_source_path not in gb.new_code.keys():
        w_creator_include = '#include "' + os.path.join(
            gb.gambit_backend_incl_dir,
            gb.wrapper_utils_fname + cfg.header_extension) + '"\n'
        gb.new_code[w_creator_source_path] = {
            'code_tuples': [(0, w_creator_include)],
            'add_include_guard': False
        }
    gb.new_code[w_creator_source_path]['code_tuples'].append(
        (-1, wr_utils_impl))
Exemplo n.º 3
0
def generateFunctionWrapperClassVersion(func_el, wr_func_name, namespaces,
                                        n_overloads):

    new_code = ''

    # Check if this function makes use of any loaded types
    uses_loaded_type = funcutils.usesLoadedType(func_el)

    # Function name
    func_name = func_el.get('name')

    # Determine return type
    return_type_dict = utils.findType(func_el)
    return_el = return_type_dict['el']
    pointerness = return_type_dict['pointerness']
    is_ref = return_type_dict['is_reference']
    return_type_kw = return_type_dict['cv_qualifiers']

    return_kw_str = ' '.join(return_type_kw) + ' ' * bool(len(return_type_kw))

    return_is_loaded = utils.isLoadedClass(return_el)

    return_type = return_type_dict['name'] + '*' * pointerness + '&' * is_ref

    # If return type is a known class, add '::' for absolute namespace.
    if (not return_is_loaded) and utils.isKnownClass(return_el):
        return_type = '::' + return_type

    # If return-by-value, then a const qualifier on the return value is meaningless
    # (will result in a compiler warning)
    if (pointerness == 0) and (is_ref == False):
        if 'const' in return_type_kw:
            return_type_kw.remove('const')

    # Arguments
    args = funcutils.getArgs(func_el)

    # One function for each set of default arguments
    n_overloads = funcutils.numberOfDefaultArgs(func_el)
    for remove_n_args in range(n_overloads + 1):

        # Check that the function is acceptable
        if funcutils.ignoreFunction(func_el,
                                    limit_pointerness=True,
                                    remove_n_args=remove_n_args):
            continue

        if remove_n_args == 0:
            use_args = args
        else:
            use_args = args[:-remove_n_args]

        # Argument bracket
        args_bracket = funcutils.constrArgsBracket(use_args,
                                                   include_arg_name=True,
                                                   include_arg_type=True,
                                                   include_namespace=True,
                                                   use_wrapper_class=True)

        # Name of original function to call
        call_func_name = func_name

        # Convert return type if loaded class
        if utils.isLoadedClass(return_el):
            wrapper_return_type = classutils.toWrapperType(
                return_type, remove_reference=True)
        else:
            wrapper_return_type = return_type

        # Write declaration line
        new_code += return_kw_str + wrapper_return_type + ' ' + wr_func_name + args_bracket + '\n'

        # Write function body
        indent = ' ' * cfg.indent
        new_code += '{\n'

        if return_type == 'void':
            new_code += indent
        else:
            new_code += indent + 'return '

        # args_bracket_notypes = funcutils.constrArgsBracket(use_args, include_arg_name=True, include_arg_type=False, wrapper_to_pointer=True)
        args_bracket_notypes = funcutils.constrArgsBracket(
            use_args,
            include_arg_name=True,
            include_arg_type=False,
            cast_to_original=True,
            wrapper_to_pointer=True)

        if return_is_loaded:

            abs_return_type_simple = classutils.toAbstractType(
                return_type,
                include_namespace=True,
                remove_reference=True,
                remove_pointers=True)
            wrapper_return_type_simple = wrapper_return_type.replace(
                '*', '').replace('&', '')

            if is_ref:  # Return-by-reference
                new_code += 'reference_returner< ' + wrapper_return_type_simple + ', ' + abs_return_type_simple + ' >( ' + call_func_name + args_bracket_notypes + ' );\n'

            elif (not is_ref) and (pointerness > 0):  # Return-by-pointer
                new_code += 'pointer_returner< ' + wrapper_return_type_simple + ', ' + abs_return_type_simple + ' >( ' + call_func_name + args_bracket_notypes + ' );\n'

            else:  # Return-by-value
                new_code += wrapper_return_type + '( ' + call_func_name + args_bracket_notypes + ' );\n'

        else:
            new_code += call_func_name + args_bracket_notypes + ';\n'

        new_code += '}\n'
        new_code += '\n'

    # Add 'extern "C" {...}' block
    new_code = 'extern "C"\n{\n' + new_code + '}\n'

    return new_code
Exemplo n.º 4
0
def constrArgsBracket(args, include_arg_name=True, include_arg_type=True, include_namespace=False,
                      cast_to_original=False, use_wrapper_class=False, wrapper_to_pointer=False, 
                      add_namespace_to_loaded=''):

    #
    # Requires a list of dicts as input, as returned by 'getArgs' or 'constrWrapperArgs'.
    #

    import modules.classutils as classutils

    # Construct bracket with input arguments
    args_seq = ''
    argc = 1
    for i in range(len(args)):

        # We must create a new copy since we may be altering the content later
        arg_dict = OrderedDict(args[i])

        if arg_dict['loaded_class'] and (add_namespace_to_loaded != ''):
            add_namespaces = add_namespace_to_loaded.split('::')
            arg_dict['type_namespaces'] = add_namespaces + arg_dict['type_namespaces']
            arg_dict['type'] = add_namespace_to_loaded + '::' + arg_dict['type']

        if include_arg_name and cast_to_original:

            if arg_dict['loaded_class']:

                # We assume that arg_dict['type'] *is* the original type!
                cast_to_type = arg_dict['type']
        
                if include_namespace:
                    namespaces = arg_dict['type_namespaces']
                    if len(namespaces)>0:
                        cast_to_type = '::'.join(namespaces) + '::' + cast_to_type

                # If argument type is not pointer or reference, add a reference operator '&'
                check_type = cast_to_type.split('<')[0]
                if ('*' not in check_type) and ('&' not in check_type):
                    cast_to_type = cast_to_type + '&'

                # Add qualifiers
                if len(arg_dict['kw']) > 0:
                    qualifiers = ' '.join(arg_dict['kw'])
                    cast_to_type = qualifiers + ' ' + cast_to_type

                # Determine what argument name to use (arg_name or *arg_name.get_BEptr() or ...)
                if wrapper_to_pointer:
                    if arg_dict['type'].count('*') == 0:
                        use_name = '*' + arg_dict['name'] + '.get_BEptr()'
                    elif arg_dict['type'].count('*') == 1:
                        use_name = '(*' + arg_dict['name'] + ')' + '.get_BEptr()'
                    args_seq += 'dynamic_cast< ' + cast_to_type + ' >(' + use_name + ')'
                else:
                    args_seq += 'dynamic_cast< ' + cast_to_type + ' >(' + arg_dict['name'] + ')'

            else:

                args_seq += arg_dict['name']


        else:
            if include_arg_type:
                args_seq += ''.join([ kw+' ' for kw in arg_dict['kw'] ])

                if use_wrapper_class and arg_dict['loaded_class'] == True:
                    args_seq += classutils.toWrapperType(arg_dict['type'], include_namespace=include_namespace)

                else:
                    if include_namespace:
                        # If known class, add '::' for absolute namespace
                        if arg_dict['known_class']:
                            args_seq += '::' + arg_dict['type']                       
                        else:
                            args_seq += arg_dict['type']
                    else:
                        args_seq += utils.removeNamespace(arg_dict['type'])

            if include_arg_type and include_arg_name:
                args_seq += ' '

            if include_arg_name:
                if utils.isLoadedClass(arg_dict['type'], byname=True) and wrapper_to_pointer:
                    if arg_dict['type'].count('*') == 0:
                        args_seq += '*' + arg_dict['name'] + '.get_BEptr()'
                    elif arg_dict['type'].count('*') == 1:
                        args_seq += '(*' + arg_dict['name'] + ')' + '.get_BEptr()'
                    else:
                        raise Exception('funcutils.constrArgsBracket cannot presently deal with arguments of type pointer-to-pointer for wrapper classes.')
                else:
                    args_seq += arg_dict['name']

        args_seq += ', '

    args_seq = args_seq.rstrip(', ')
    args_seq = args_seq.strip()
    args_bracket = '(' + args_seq + ')'

    return args_bracket