예제 #1
0
    def build():
        """生成"""
        # Generate file head.
        cpp_file_path = op.join(Cfg.getcodepath(), 'pyllbc_scripts.h')
        cpp_file = CppFile(cpp_file_path, Cfg.getauthor(), Cfg.getver())
        cpp_file.addincl('pyllbc/common/LibHeader.h')
        cpp_file.addincl('pyllbc/common/ScriptIntegrator.h')

        # Generate constructor/destructor.
        class_name = 'pyllbc_Scripts'
        ctor = CppFun(class_name, rtn='', visit=CppVisit(CppVisit.PUBLIC))
        for root, dirs, files in os.walk(Cfg.getscriptpath()):
            for f in files:
                fpath = op.join(root, f)
                if '.svn' in fpath:
                    continue
                elif op.splitext(fpath)[1].lower() != '.py':
                    continue

                script_cnt = IntegratorBuilder.__transfer_to_cpp_str(fpath)
                ctor.addstmt('_scripts.insert(::std::make_pair("{}", new ::pyllbc_ScriptIntegrator({})));'
                             .format(op.basename(fpath), script_cnt))

        dtor = CppFun('~{}'.format(class_name), rtn='', visit=CppVisit(CppVisit.PUBLIC))
        dtor.addstmt('LLBC_STLHelper::DeleteContainer(_scripts);')

        clazz = CppClass(class_name)
        clazz.addmethod(ctor)
        clazz.addmethod(dtor)

        # Generate integrate method.
        integrate_meth = CppFun('Integrate', rtn='int', visit=CppVisit(CppVisit.PUBLIC))
        integrate_meth.addstmt("for (::std::map<LLBC_String, "
                               "::pyllbc_ScriptIntegrator *>::iterator it = _scripts.begin();")
        integrate_meth.addstmt('     it != _scripts.end();')
        integrate_meth.addstmt('     it++)')
        integrate_meth.addstmt('    if (it->second->Integrate() != LLBC_OK)')
        integrate_meth.addstmt('        return LLBC_FAILED;')
        integrate_meth.addstmt('')
        integrate_meth.addstmt('return LLBC_OK;')
        clazz.addmethod(integrate_meth)

        # Generate data member.
        data_mem = CppData('::std::map<LLBC_String, ::pyllbc_ScriptIntegrator *>',
                           '_scripts',
                           False,
                           CppVisit(CppVisit.PRIVATE))
        clazz.adddata(data_mem)

        cpp_file.addcls(clazz)
        cpp_file.adddef('pyllbc_s_Scripts LLBC_Singleton<{}, LLBC_DummyLock>::Instance()'.format(class_name))

        cpp_file.build()
예제 #2
0
    def build(search_path):
        """构建方法文件"""
        # Create cpp file.
        search_base = op.basename(search_path).capitalize()
        cpp_file_path = op.join(search_path, '_{}Methods.h'.format(search_base))
        cpp_file = CppFile(cpp_file_path, Cfg.getauthor(), Cfg.getver())
        cpp_file.addincl('pyllbc/common/LibHeader.h')

        # Get all methods
        methods = {}
        code_path = Cfg.getcodepath()
        c = re.compile(r'(_[a-zA-Z]+)+\.h')
        for root, dirs, files in os.walk(search_path):
            for f in files:
                fpath = op.join(root, f)
                if '.svn' in fpath:
                    continue
                elif fpath == cpp_file_path:
                    continue

                if c.match(f):
                    file_methods = MethodsBuilder.__parse_file(fpath)
                    if file_methods:
                        incl = fpath[len(op.dirname(code_path)) + 1:]
                        cpp_file.addincl(incl)

                        methods.update(file_methods)

        # Create cpp class.
        cls_name = 'pyllbc_{}Methods'.format(search_base)
        cls = CppClass(cls_name)

        first_data = True
        ctor = CppFun(cls_name, rtn='', visit=CppVisit(CppVisit.PUBLIC))
        for meth_name, meth in methods.iteritems():
            ctor.addstmt('{}.ml_name = {};'.format(meth_name, meth['name']))
            ctor.addstmt('{}.ml_meth = {};'.format(meth_name, meth['meth']))
            ctor.addstmt('{}.ml_flags = {};'.format(meth_name, meth['flags']))
            ctor.addstmt('{}.ml_doc = {};'.format(meth_name, meth['doc']))

            visit = CppVisit(CppVisit.PUBLIC) if first_data else CppVisit(CppVisit.NONE)
            cls.adddata(CppData('::PyMethodDef', meth_name, visit=visit))

            first_data = False

        cls.addmethod(ctor)

        cpp_file.addcls(cls)
        cpp_file.adddef('pyllbc_s_{0}Methods LLBC_Singleton< ::pyllbc_{0}Methods, LLBC_DummyLock>::Instance()'
                        .format(search_base))
        cpp_file.build()