コード例 #1
0
ファイル: CefHandlerTx.cs プロジェクト: 2php/kneadium
        void GenerateCppImplMethodForNs(MethodPlan met, CodeStringBuilder stbuilder)
        {
            CodeMethodDeclaration metDecl = met.metDecl;

            stbuilder.AppendLine("//CefHandlerTx::GenerateCppImplMethodForNs ," + (++codeGenNum));
            stbuilder.AppendLine("//gen! " + metDecl.ToString());
            //---------------------------

            //temp
            if (metDecl.ReturnType.ToString() == "FilterStatus")
            {
                stbuilder.Append(metDecl.ReturnType.ResolvedType + " " + metDecl.Name + "(");
            }
            else if (metDecl.ReturnType.ToString() == "ReturnValue")
            {
                string ownerName = metDecl.OwnerTypeDecl.Name;
                stbuilder.Append(ownerName + "::" + metDecl.ReturnType + " " + metDecl.Name + "(");
            }
            else
            {
                stbuilder.Append(metDecl.ReturnType + " " + metDecl.Name + "(");
            }


            List <CodeMethodParameter> pars = metDecl.Parameters;
            int j = pars.Count;
            //transfer data slot
            bool hasSomeOutVars = false;

            CppTempParameterSlot[] dataTransferSlots = new CppTempParameterSlot[j];
            for (int i = 0; i < j; ++i)
            {
                CppTempParameterSlot transferSlot = new CppTempParameterSlot();
                CodeMethodParameter  par          = pars[i];
                transferSlot.par     = pars[i];
                dataTransferSlots[i] = transferSlot;
                //
                //
                string parTypeName = par.ParameterType.ToString();
                if (parTypeName == "CefRefPtr<CefV8Value>&")
                {
                    if (!par.IsConstPar)
                    {
                        transferSlot.newVarName  = "temp_" + i;
                        transferSlot.preStmt     = "auto " + transferSlot.newVarName + "=" + "CefV8ValueCToCpp::Unwrap(" + transferSlot.par.ParameterName + ");";
                        transferSlot.postStmt    = transferSlot.par.ParameterName + "= CefV8ValueCToCpp::Wrap(" + transferSlot.newVarName + ");";
                        transferSlot.isCppOutVar = true;
                        hasSomeOutVars           = true;
                    }
                }
                else if (parTypeName == "CefString&")
                {
                    if (!par.IsConstPar)
                    {
                        transferSlot.newVarName  = "temp_" + i;
                        transferSlot.preStmt     = "auto " + transferSlot.newVarName + "=" + transferSlot.par.ParameterName + ";";
                        transferSlot.postStmt    = transferSlot.par.ParameterName + "=" + transferSlot.newVarName + ";";
                        transferSlot.isCppOutVar = true;
                        hasSomeOutVars           = true;
                    }
                }
            }


            //first par is managed callback
            stbuilder.Append("managed_callback mcallback");
            for (int i = 0; i < j; ++i)
            {
                stbuilder.Append(",");
                CodeMethodParameter par = pars[i];
                if (par.IsConstPar)
                {
                    stbuilder.Append("const ");
                }
                //parameter type
                stbuilder.Append(par.ParameterType.ResolvedType.FullName + " ");
                stbuilder.Append(par.ParameterName);
            }
            stbuilder.AppendLine("){");
            //-----------

            for (int i = 0; i < j; ++i)
            {
                MethodParameter parTx = met.pars[i];
                parTx.ClearExtractCode();
                PrepareDataFromNativeToCs(parTx, "&vargs[" + (i + 1) + "]", parTx.Name, true);
            }
            //method body

            //-----------------------------
            //use native C data slot
            stbuilder.AppendLine("if(mcallback){");

            //'out' vars
            if (hasSomeOutVars)
            {
                //temp,
                for (int i = 0; i < j; ++i)
                {
                    CppTempParameterSlot transferSlot = dataTransferSlots[i];
                    if (transferSlot.isCppOutVar)
                    {
                        stbuilder.AppendLine(transferSlot.preStmt);
                    }
                }
            }
            //-----------------------------
            //
            string metArgsClassName = metDecl.Name + "Args";

            stbuilder.Append(metArgsClassName + " args1");
            //with ctors
            if (j == 0)
            {
                stbuilder.AppendLine(";");
            }
            else
            {
                stbuilder.Append("(");
                for (int i = 0; i < j; ++i)
                {
                    if (i > 0)
                    {
                        stbuilder.Append(",");
                    }
                    //
                    MethodParameter      par          = met.pars[i];
                    CppTempParameterSlot transferSlot = dataTransferSlots[i];
                    if (transferSlot.isCppOutVar)
                    {
                        stbuilder.Append("&" + transferSlot.newVarName);
                    }
                    else
                    {
                        //temp
                        string parType = par.TypeSymbol.ToString();
                        if (parType.EndsWith("&"))
                        {
                            stbuilder.Append("&");
                        }
                        stbuilder.Append(par.Name);
                    }
                }
                stbuilder.AppendLine(");");
            }
            stbuilder.AppendLine("mcallback( (_typeName << 16) | " + met.CppMethodSwitchCaseName + ",&args1.arg);");


            //------
            //'out' vars
            if (hasSomeOutVars)
            {
                //temp,
                for (int i = 0; i < j; ++i)
                {
                    CppTempParameterSlot transferSlot = dataTransferSlots[i];
                    if (transferSlot.isCppOutVar)
                    {
                        stbuilder.AppendLine(transferSlot.postStmt);
                    }
                }
            }

            //temp fix, arg extract code
            if (!met.ReturnPlan.IsVoid)
            {
                stbuilder.AppendLine("return args1.arg.myext_ret_value;");
            }
            stbuilder.AppendLine("}");


            //-------------------
            //default return if no managed callback
            if (!met.ReturnPlan.IsVoid)
            {
                string retTypeName = metDecl.ReturnType.ToString();
                if (retTypeName.StartsWith("CefRefPtr<"))
                {
                    stbuilder.Append("return nullptr;");
                }
                else
                {
                    switch (metDecl.ReturnType.ToString())
                    {
                    case "bool":
                        stbuilder.Append("return false;");
                        break;

                    case "FilterStatus":     //TODO: revisit here
                        stbuilder.Append("return (FilterStatus)0;");
                        break;

                    case "ReturnValue":
                    {
                        string ownerName = metDecl.OwnerTypeDecl.Name;
                        stbuilder.Append("return (" + ownerName + "::ReturnValue)0;");
                    }
                    break;

                    case "CefSize":
                        stbuilder.Append("return CefSize();");
                        break;

                    case "size_t":
                        stbuilder.Append("return 0;");
                        break;

                    case "int":
                        stbuilder.Append("return 0;");
                        break;

                    case "int64":
                        stbuilder.Append("return 0;");
                        break;

                    default:
                        throw new NotSupportedException();
                    }
                }
            }

            stbuilder.AppendLine("}"); //method
        }