예제 #1
0
        void GenerateCppCode(CodeStringBuilder stbuilder)
        {
#if DEBUG
            _dbug_cpp_count++;
#endif
            //
            //create switch table for C#-interop
            //
            CodeTypeDeclaration orgDecl      = this.OriginalDecl;
            CodeTypeDeclaration implTypeDecl = this.ImplTypeDecl;
            TypePlan            typeTxInfo   = implTypeDecl.Name.Contains("CppToC") ? orgDecl.TypePlan : implTypeDecl.TypePlan;
            //
            FindStaticMethods(orgDecl.TypePlan);
            //



            CppHandleCsMethodRequestCodeGen cppHandlerReqCodeGen = new CppHandleCsMethodRequestCodeGen();
            cppHandlerReqCodeGen.GenerateCppCode(this, orgDecl, implTypeDecl, this.UnderlyingCType, stbuilder);

            string namespaceName = orgDecl.Name + "Ext";
            int    j             = cppHandlerReqCodeGen.callToDotNetMets.Count;


            if (j > 0)
            {
                CodeStringBuilder const_methodNames = new CodeStringBuilder();
                //check if method has duplicate name or not
                //----------
                Dictionary <string, MethodPlan> uniqueNames = new Dictionary <string, MethodPlan>();
                foreach (MethodPlan met in cppHandlerReqCodeGen.callToDotNetMets)
                {
                    MethodPlan existingMet;
                    if (uniqueNames.TryGetValue(met.Name, out existingMet))
                    {
                        string met_capi_name = FindCApiName(met.metDecl);
                        string met_capi_nameOfExistingMet = FindCApiName(existingMet.metDecl);
                        if (met_capi_nameOfExistingMet == null && met_capi_name == null)
                        {
                            throw new NotSupportedException();
                        }
                        //rename both if possible
                        existingMet.HasDuplicatedMethodName = true;
                        if (met_capi_nameOfExistingMet != null)
                        {
                            existingMet.NewOverloadName = met_capi_nameOfExistingMet;
                        }
                        else
                        {
                            existingMet.NewOverloadName = existingMet.Name;
                        }
                        //
                        met.HasDuplicatedMethodName = true;
                        if (met_capi_name != null)
                        {
                            met.NewOverloadName = met_capi_name;
                        }
                        else
                        {
                            met.NewOverloadName = met.Name;
                        }
                    }
                    else
                    {
                        uniqueNames.Add(met.Name, met);
                    }
                }
                //-----------------------
                for (int i = 0; i < j; ++i)
                {
                    MethodPlan met = cppHandlerReqCodeGen.callToDotNetMets[i];
                    if (met.HasDuplicatedMethodName)
                    {
                        const_methodNames.AppendLine("const int " + namespaceName + "_" + met.NewOverloadName + "_" + (i + 1) + "=" + (i + 1) + ";");
                    }
                    else
                    {
                        const_methodNames.AppendLine("const int " + namespaceName + "_" + met.Name + "_" + (i + 1) + "=" + (i + 1) + ";");
                    }
                }



                //--------------
                CppInstanceImplCodeGen instanceImplCodeGen = new CppInstanceImplCodeGen();
                instanceImplCodeGen.GenerateCppImplClass(this,
                                                         typeTxInfo,
                                                         cppHandlerReqCodeGen.callToDotNetMets,
                                                         orgDecl,
                                                         stbuilder);
                cpp_callToDotNetMets = cppHandlerReqCodeGen.callToDotNetMets;
                //-----------------------------------------------------------
                CppToCsMethodArgsClassGen cppMetArgClassGen = new CppToCsMethodArgsClassGen();
                //
                CodeStringBuilder cppArgClassStBuilder = new CodeStringBuilder();
                cppArgClassStBuilder.AppendLine("namespace " + namespaceName + "{");

                for (int i = 0; i < j; ++i)
                {
                    MethodPlan met = cppHandlerReqCodeGen.callToDotNetMets[i];
                    cppMetArgClassGen.GenerateCppMethodArgsClass(met, cppArgClassStBuilder);
                }
                cppArgClassStBuilder.AppendLine("}");
                //----------------------------------------------
                _output._cppHeaderExportFuncAuto.Append(cppArgClassStBuilder.ToString());
                //----------------------------------------------
                //InternalHeaderForExportFunc.h
                CodeStringBuilder internalHeader = _output._cppHeaderInternalForExportFuncAuto;
                internalHeader.AppendLine("namespace " + namespaceName);
                internalHeader.AppendLine("{");
                internalHeader.AppendLine("const int _typeName=" + "CefTypeName_" + orgDecl.Name + ";");
                internalHeader.AppendLine(const_methodNames.ToString());
                internalHeader.AppendLine("}");
                //----------------------------------------------
            }
            if (staticMethods != null)
            {
                CppHandleCsMethodRequestCodeGen cppHandlerReqCodeGen2 = new CppHandleCsMethodRequestCodeGen();
                cppHandlerReqCodeGen2.GenerateCppCodeStatic(staticMethods, orgDecl, implTypeDecl, stbuilder);


                CodeStringBuilder const_methodNames = new CodeStringBuilder();
                //check if method has duplicate name or not

                for (int i = 0; i < j; ++i)
                {
                    MethodPlan met = cppHandlerReqCodeGen.callToDotNetMets[i];
                    const_methodNames.AppendLine("const int " + namespaceName + "_" + met.Name + "_" + (i + 1) + "=" + (i + 1) + ";");
                }

                //create static method for cpp type
                //in this version we don't create an custom impl of the class
                //-----------------------------------------------------------
                CppToCsMethodArgsClassGen cppMetArgClassGen = new CppToCsMethodArgsClassGen();
                //
                CodeStringBuilder cppArgClassStBuilder = new CodeStringBuilder();
                cppArgClassStBuilder.AppendLine("namespace " + namespaceName + "{");

                j = staticMethods.Count;
                for (int i = 0; i < j; ++i)
                {
                    MethodPlan met = staticMethods[i];
                    cppMetArgClassGen.GenerateCppMethodArgsClass(met, cppArgClassStBuilder);
                }
                cppArgClassStBuilder.AppendLine("}");
                //----------------------------------------------
                //generate cpp class
            }
        }
예제 #2
0
        void GenerateCppCode(CodeStringBuilder stbuilder)
        {
#if DEBUG
            _dbug_cpp_count++;
#endif
            //
            //create switch table for C#-interop
            //
            CodeTypeDeclaration orgDecl      = this.OriginalDecl;
            CodeTypeDeclaration implTypeDecl = this.ImplTypeDecl;
            _typePlan = orgDecl.TypePlan;
            //-----------------------------------------------------------------------
            List <MethodPlan> onEventMethods = new List <MethodPlan>();

            int j      = _typePlan.methods.Count;
            int maxPar = 0;
            for (int i = 0; i < j; ++i)
            {
                MethodPlan metTx = _typePlan.methods[i];
                if (metTx.metDecl.IsVirtual)
                {
                    //this method need a callback to .net side (.net-side event listener)
                    if (metTx.metDecl.Name.StartsWith("On"))
                    {
                        onEventMethods.Add(metTx);
                    }
                }
                metTx.CppMethodSwitchCaseName = orgDecl.Name + "_" + metTx.Name + "_" + (i + 1);
                if (metTx.pars.Count > maxPar)
                {
                    maxPar = metTx.pars.Count;
                }
            }

            MaxMethodParCount = maxPar;
            //----------
            CppHandleCsMethodRequestCodeGen cppHandleCsMetCodeGen2 = new CppHandleCsMethodRequestCodeGen();
            cppHandleCsMetCodeGen2.PrepareInstanceCallFromCppToCsMethodName(
                this,
                orgDecl,
                ImplTypeDecl,
                this.UnderlyingCType);
            //
            cppHandleCsMetCodeGen2.GenerateCppCode(
                this,
                orgDecl,
                ImplTypeDecl,
                this.UnderlyingCType,
                stbuilder
                );

            //----------
            if (onEventMethods.Count > 0)
            {
                //the callback need some method to call to C# side
                var eventListenerCodeGen = new CppEventListenerInstanceImplCodeGen();
                eventListenerCodeGen.GenerateCppImplClass(
                    this,
                    _typePlan,
                    orgDecl,
                    onEventMethods,
                    stbuilder);
            }
        }