Exemplo n.º 1
0
/* generate the code that calls the C++ method */
static void vtkWrapPython_GenerateMethodCall(
  FILE *fp, FunctionInfo *currentFunction, ClassInfo *data,
  HierarchyInfo *hinfo, int is_vtkobject)
{
  char methodname[256];
  ValueInfo *arg;
  int totalArgs;
  int is_constructor;
  int i, k, n;

  totalArgs = vtkWrap_CountWrappedParameters(currentFunction);

  is_constructor = vtkWrap_IsConstructor(data, currentFunction);

  /* for vtkobjects, do a bound call and an unbound call */
  n = 1;
  if (is_vtkobject &&
      !currentFunction->IsStatic &&
      !currentFunction->IsPureVirtual &&
      !is_constructor)
    {
    n = 2;
    }

  if (!is_constructor &&
      !vtkWrap_IsVoid(currentFunction->ReturnValue))
    {
    /* temp variable for C++-type return value */
    fprintf(fp, "  ");
    vtkWrap_DeclareVariable(fp, data, currentFunction->ReturnValue,
      "tempr", -1, VTK_WRAP_RETURN | VTK_WRAP_NOSEMI);
    fprintf(fp, " =");
    }

  /* handle both bound and unbound calls */
  if (n == 2)
    {
    if (!is_constructor &&
        !vtkWrap_IsVoid(currentFunction->ReturnValue))
      {
      fprintf(fp, " (ap.IsBound() ?\n"
             "     ");
      }
    else
      {
      fprintf(fp,
              "    if (ap.IsBound())\n"
              "      {\n"
              "  ");
      }
    }

  /* print the code that calls the method */
  for (k = 0; k < n; k++)
    {
    if (k == 1)
      {
      /* unbound method call */
      sprintf(methodname, "op->%s::%s",
              data->Name, currentFunction->Name);
      }
    else if (currentFunction->IsStatic)
      {
      /* static method call */
      sprintf(methodname, "%s::%s",
              data->Name, currentFunction->Name);
      }
    else if (is_constructor)
      {
      /* constructor call */
      sprintf(methodname, "new %s", currentFunction->Name);
      }
    else
      {
      /* standard bound method call */
      sprintf(methodname, "op->%s", currentFunction->Name);
      }

    if (is_constructor)
      {
      fprintf(fp,
              "    %s *op = new %s(",
              data->Name, data->Name);
      }
    else if (vtkWrap_IsVoid(currentFunction->ReturnValue))
      {
      fprintf(fp,
              "    %s(",
              methodname);
      }
    else if (vtkWrap_IsRef(currentFunction->ReturnValue))
      {
      fprintf(fp,
              " &%s(",
              methodname);
      }
    else
      {
      fprintf(fp,
              " %s(",
              methodname);
      }

    /* print all the arguments in the call */
    for (i = 0; i < totalArgs; i++)
      {
      arg = currentFunction->Parameters[i];

      if (vtkWrap_IsFunction(arg))
        {
        fprintf(fp,"\n"
                "        (temp%d == Py_None ? NULL : vtkPythonVoidFunc),\n"
                "        (temp%d == Py_None ? NULL : temp%d));\n",
                i, i, i);
        fprintf(fp,
                "      if (temp%d != Py_None)\n"
                "        {\n"
                "        Py_INCREF(temp%d);\n"
                "        }\n"
                "      %sArgDelete(\n"
                "        (temp%d == Py_None ? NULL : vtkPythonVoidFuncArgDelete)",
                i, i, methodname, i);
        break;
        }

      if (i)
        {
        fprintf(fp,", ");
        }

      if ((vtkWrap_IsSpecialObject(arg) ||
           vtkWrap_IsQtObject(arg)) &&
          !vtkWrap_IsPointer(arg))
        {
        fprintf(fp, "*temp%i", i);
        }
      else
        {
        fprintf(fp, "temp%i", i);
        }
      }
    fprintf(fp, ")");

    /* handle ternary operator for ap.IsBound() */
    if (n == 2)
      {
      if (!is_constructor &&
          !vtkWrap_IsVoid(currentFunction->ReturnValue))
        {
        fprintf(fp, (k == 0 ? " :\n     " : ");\n"));
        }
      else if (k == 0)
        {
        fprintf(fp, ";\n"
                "      }\n"
                "    else\n"
                "      {\n"
                "  ");
        }
      else
        {
        fprintf(fp, ";\n"
                "      }\n");
        }
      }
    else
      {
      fprintf(fp, ";\n");
      }
    }

  if (is_constructor)
    {
    /* initialize tuples created with default constructor */
    if (currentFunction->NumberOfParameters == 0 && hinfo)
      {
      n = vtkWrap_GetTupleSize(data, hinfo);
      for (i = 0; i < n; i++)
        {
        fprintf(fp,
                "    (*op)[%d] = 0;\n",
                i);
        }
      }
    }

  fprintf(fp,
          "\n");
}
Exemplo n.º 2
0
static void vtkWrapText_PythonTypeSignature(
  struct vtkWPString *result, const char *braces[2], ValueInfo *arg)
{
  char text[256];
  const char *dimension;
  const char *classname = "";

  if (vtkWrap_IsVoid(arg))
  {
    classname = "void";
  }
  else if (vtkWrap_IsFunction(arg))
  {
    classname = "function";
  }
  else if (vtkWrap_IsString(arg) || vtkWrap_IsCharPointer(arg))
  {
    classname = "string";
    if ((arg->Type & VTK_PARSE_BASE_TYPE) == VTK_PARSE_UNICODE_STRING)
    {
      classname = "unicode";
    }
  }
  else if (vtkWrap_IsChar(arg))
  {
    classname = "char";
  }
  else if (vtkWrap_IsBool(arg))
  {
    classname = "bool";
  }
  else if (vtkWrap_IsRealNumber(arg))
  {
    classname = "float";
  }
  else if (vtkWrap_IsInteger(arg))
  {
    classname = "int";
  }
  else
  {
    vtkWrapText_PythonName(arg->Class, text);
    classname = text;
  }

  if ((vtkWrap_IsArray(arg) && arg->CountHint) ||
      vtkWrap_IsPODPointer(arg))
  {
    vtkWPString_Append(result, braces[0]);
    vtkWPString_Append(result, classname);
    vtkWPString_Append(result, ", ...");
    vtkWPString_Append(result, braces[1]);
  }
  else if (vtkWrap_IsArray(arg))
  {
    sprintf(text, "%d", arg->Count);
    dimension = text;
    vtkWrapText_PythonArraySignature(result, classname, braces,
      1, &dimension);
  }
  else if (vtkWrap_IsNArray(arg))
  {
    vtkWrapText_PythonArraySignature(result, classname, braces,
      arg->NumberOfDimensions, arg->Dimensions);
  }
  else
  {
    vtkWPString_Append(result, classname);
  }
}
Exemplo n.º 3
0
/* Convert values into python object and return them within python */
void vtkWrapPython_ReturnValue(
  FILE *fp, ClassInfo *data, ValueInfo *val, int static_call)
{
  char pythonname[1024];
  const char *deref = "";
  const char *prefix = "ap.";

  if (static_call)
    {
    prefix = "vtkPythonArgs::";

    fprintf(fp,
            "    if (PyErr_Occurred() == NULL)\n"
            "      {\n");
    }
  else
    {
    fprintf(fp,
            "    if (!ap.ErrorOccurred())\n"
            "      {\n");
    }

  if (val && vtkWrap_IsRef(val))
    {
    deref = "*";
    }

  if (vtkWrap_IsVoid(val))
    {
    fprintf(fp,
            "      result = %sBuildNone();\n",
            prefix);
    }
  else if (vtkWrap_IsEnumMember(data, val))
    {
    fprintf(fp,
            "      result = Py%s_%s_FromEnum(tempr);\n",
            data->Name, val->Class);
    }
  else if (vtkWrap_IsPythonObject(val))
    {
    fprintf(fp,
            "      result = tempr;\n");
    }
  else if (vtkWrap_IsVTKObject(val))
    {
    fprintf(fp,
            "      result = %sBuildVTKObject(tempr);\n",
            prefix);

    if (vtkWrap_IsNewInstance(val))
      {
      fprintf(fp,
            "      if (result && PyVTKObject_Check(result))\n"
            "        {\n"
            "        PyVTKObject_GetObject(result)->UnRegister(0);\n"
            "        PyVTKObject_SetFlag(result, VTK_PYTHON_IGNORE_UNREGISTER, 1);\n"
            "        }\n");
      }
    }
  else if (vtkWrap_IsSpecialObject(val) &&
           vtkWrap_IsRef(val))
    {
    vtkWrapText_PythonName(val->Class, pythonname);
    fprintf(fp,
            "      result = %sBuildSpecialObject(tempr, \"%s\");\n",
            prefix, pythonname);
    }
  else if (vtkWrap_IsSpecialObject(val) &&
           !vtkWrap_IsRef(val))
    {
    vtkWrapText_PythonName(val->Class, pythonname);
    fprintf(fp,
            "      result = %sBuildSpecialObject(&tempr, \"%s\");\n",
            prefix, pythonname);
    }
  else if (vtkWrap_IsQtObject(val) &&
           (vtkWrap_IsRef(val) || vtkWrap_IsPointer(val)))
    {
    fprintf(fp,
            "      result = %sBuildSIPObject(tempr, \"%s\", false);\n",
            prefix, val->Class);
    }
  else if (vtkWrap_IsQtObject(val) &&
           !vtkWrap_IsRef(val) && !vtkWrap_IsPointer(val))
    {
    fprintf(fp,
            "      result = %sBuildSIPObject(new %s(tempr), \"%s\", false);\n",
            prefix, val->Class, val->Class);
    }
  else if (vtkWrap_IsQtEnum(val))
    {
    fprintf(fp,
            "      result = %sBuildSIPEnumValue(tempr, \"%s\");\n",
            prefix, val->Class);
    }
  else if (vtkWrap_IsCharPointer(val))
    {
    fprintf(fp,
            "      result = %sBuildValue(tempr);\n",
            prefix);
    }
  else if (vtkWrap_IsVoidPointer(val))
    {
    fprintf(fp,
            "      result = %sBuildValue(tempr);\n",
            prefix);
    }
  else if (vtkWrap_IsChar(val) && vtkWrap_IsArray(val))
    {
    fprintf(fp,
            "      result = %sBuildBytes(tempr, sizer);\n",
            prefix);
    }
  else if (vtkWrap_IsArray(val))
    {
    fprintf(fp,
            "      result = %sBuildTuple(tempr, sizer);\n",
            prefix);
    }
  else
    {
    fprintf(fp,
            "      result = %sBuildValue(%stempr);\n",
            prefix, deref);
    }

  fprintf(fp,
          "      }\n");
}