Пример #1
0
/* Save a copy of each non-const array arg, so that we can check
 * if they were changed by the method call */
void vtkWrapPython_SaveArgs(FILE *fp, FunctionInfo *currentFunction)
{
  const char *asterisks = "**********";
  ValueInfo *arg;
  int i, j, n, m;
  int noneDone = 1;

  /* do nothing for SetVector macros */
  if (vtkWrap_IsSetVectorMethod(currentFunction))
  {
    return;
  }

  m = vtkWrap_CountWrappedParameters(currentFunction);

  /* save arrays for args that are non-const */
  for (i = 0; i < m; i++)
  {
    arg = currentFunction->Parameters[i];
    n = arg->NumberOfDimensions;
    if (n < 1 && (vtkWrap_IsArray(arg) || vtkWrap_IsPODPointer(arg) ||
                  vtkWrap_IsCharPointer(arg)))
    {
      n = 1;
    }

    if ((vtkWrap_IsArray(arg) || vtkWrap_IsNArray(arg) ||
         vtkWrap_IsPODPointer(arg) || vtkWrap_IsCharPointer(arg)) &&
        (arg->Type & VTK_PARSE_CONST) == 0 &&
        !vtkWrap_IsRef(arg))
    {
      noneDone = 0;

      fprintf(fp,
              "    ap.Save(%.*stemp%d, %.*ssave%d, ",
              (n-1), asterisks, i, (n-1), asterisks, i);

      if (vtkWrap_IsNArray(arg))
      {
        for (j = 0; j < arg->NumberOfDimensions; j++)
        {
          fprintf(fp, "%ssize%d[%d]", (j == 0 ? "" : "*"), i, j);
        }
      }
      else
      {
        fprintf(fp, "size%d", i);
      }

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

  if (!noneDone)
  {
    fprintf(fp,
            "\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);
  }
}
Пример #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");
}
Пример #4
0
/* Write the code to convert one argument with vtkPythonArgs */
void vtkWrapPython_GetSingleArgument(
  FILE *fp, ClassInfo *data, int i, ValueInfo *arg, int static_call)
{
  const char *prefix = "ap.";
  const char *cp;
  char argname[32];
  char pythonname[1024];
  size_t l;
  argname[0] = '\0';

  if (static_call)
    {
    prefix = "vtkPythonArgs::";
    sprintf(argname, "arg%d, ", i);
    }

  if (vtkWrap_IsEnumMember(data, arg))
    {
    fprintf(fp, "%sGetEnumValue(%stemp%d, \"%s.%s\")",
            prefix, argname, i, data->Name, arg->Class);
    }
  else if (arg->IsEnum)
    {
    cp = arg->Class;
    for (l = 0; cp[l] != '\0'; l++)
      {
      if (cp[l] == ':') { break; }
      }
    if (cp[l] == ':' && cp[l+1] == ':')
      {
      fprintf(fp, "%sGetEnumValue(%stemp%d, \"%*.*s.%s\")",
              prefix, argname, i, (int)l, (int)l, cp, &cp[l+2]);
      }
    else
      {
      fprintf(fp, "%sGetEnumValue(%stemp%d, \"%s\")",
              prefix, argname, i, cp);
      }
    }
  else if (vtkWrap_IsPythonObject(arg))
    {
    fprintf(fp, "%s%sGetPythonObject(temp%d)",
            prefix, argname, i);
    }
  else if (vtkWrap_IsVTKObject(arg))
    {
    vtkWrapText_PythonName(arg->Class, pythonname);
    if (strcmp(arg->Class, pythonname) != 0)
      {
      /* use typeid() for templated names */
      fprintf(fp, "%sGetVTKObject(%stemp%d, typeid(%s).name())",
              prefix, argname, i, arg->Class);
      }
    else
      {
      fprintf(fp, "%sGetVTKObject(%stemp%d, \"%s\")",
              prefix, argname, i, pythonname);
      }
    }
  else if (vtkWrap_IsSpecialObject(arg) &&
           !vtkWrap_IsNonConstRef(arg))
    {
    vtkWrapText_PythonName(arg->Class, pythonname);
    fprintf(fp, "%sGetSpecialObject(%stemp%d, pobj%d, \"%s\")",
            prefix, argname, i, i, pythonname);
    }
  else if (vtkWrap_IsSpecialObject(arg) &&
           vtkWrap_IsNonConstRef(arg))
    {
    vtkWrapText_PythonName(arg->Class, pythonname);
    fprintf(fp, "%sGetSpecialObject(%stemp%d, \"%s\")",
            prefix, argname, i, pythonname);
    }
  else if (vtkWrap_IsQtEnum(arg))
    {
    fprintf(fp, "%sGetSIPEnumValue(%stemp%d, \"%s\")",
            prefix, argname, i, arg->Class);
    }
  else if (vtkWrap_IsQtObject(arg))
    {
    fprintf(fp, "%sGetSIPObject(%stemp%d, \"%s\")",
            prefix, argname, i, arg->Class);
    }
  else if (vtkWrap_IsFunction(arg))
    {
    fprintf(fp, "%sGetFunction(%stemp%d)",
            prefix, argname, i);
    }
  else if (vtkWrap_IsVoidPointer(arg))
    {
    fprintf(fp, "%sGetBuffer(%stemp%d, &pbuf%d)",
            prefix, argname, i, i);
    }
  else if (vtkWrap_IsString(arg) ||
           vtkWrap_IsCharPointer(arg))
    {
    fprintf(fp, "%sGetValue(%stemp%d)",
            prefix, argname, i);
    }
  else if (vtkWrap_IsNumeric(arg) &&
           vtkWrap_IsScalar(arg))
    {
    fprintf(fp, "%sGetValue(%stemp%d)",
            prefix, argname, i);
    }
  else if (vtkWrap_IsNArray(arg))
    {
    fprintf(fp, "%sGetNArray(%s%.*stemp%d, %d, size%d)",
            prefix, argname, (int)(arg->NumberOfDimensions-1), "**********",
            i, arg->NumberOfDimensions, i);
    }
  else if (vtkWrap_IsArray(arg))
    {
    fprintf(fp, "%sGetArray(%stemp%d, size%d)",
            prefix, argname, i, i);
    }
  else if (vtkWrap_IsPODPointer(arg))
    {
    fprintf(fp, "%sGetArray(%stemp%d, size%d)",
            prefix, argname, i, i);
    }
}
Пример #5
0
/* Declare all local variables used by the wrapper method */
void vtkWrapPython_DeclareVariables(
  FILE *fp, ClassInfo *data, FunctionInfo *theFunc)
{
  ValueInfo *arg;
  int i, n;

  n = vtkWrap_CountWrappedParameters(theFunc);

  /* temp variables for arg values */
  for (i = 0; i < n; i++)
  {
    arg = theFunc->Parameters[i];

    /* a callable python object for function args */
    if (vtkWrap_IsFunction(arg))
    {
      fprintf(fp,
              "  PyObject *temp%d = nullptr;\n",
              i);
      /* ignore further arguments */
      break;
    }

    /* a PyObject argument will simply be passed through */
    if (vtkWrap_IsPythonObject(arg))
    {
      fprintf(fp,
              "  PyObject *temp%d;\n",
              i);
      continue;
    }

    /* temps for arrays */
    if (vtkWrap_IsArray(arg) || vtkWrap_IsNArray(arg) ||
        vtkWrap_IsPODPointer(arg) ||
        (vtkWrap_IsCharPointer(arg) && !vtkWrap_IsConst(arg)))
    {
      /* for non-const arrays, alloc twice as much space */
      const char *mtwo = "";
      if (!vtkWrap_IsConst(arg) && !vtkWrap_IsSetVectorMethod(theFunc))
      {
        mtwo = "2*";
      }
      if (vtkWrap_IsCharPointer(arg))
      {
        /* prepare for "char *" arg for non-const char pointer */
        fprintf(fp,
              "  size_t size%d = ap.GetStringSize(%d);\n"
              "  vtkPythonArgs::Array<char> store%d(%ssize%d + 1);\n"
              "  char *temp%d = store%d.Data();\n",
              i, i,
              i, mtwo, i,
              i, i);
        if (!vtkWrap_IsRef(arg))
        {
          fprintf(fp,
              "  char *save%d = temp%d + size%d + 1;\n",
              i, i, i);
        }
      }
      else if (arg->CountHint || vtkWrap_IsPODPointer(arg))
      {
        /* prepare for "T *" arg, where T is a plain type */
        fprintf(fp,
              "  size_t size%d = ap.GetArgSize(%d);\n"
              "  vtkPythonArgs::Array<%s> store%d(%ssize%d);\n"
              "  %s *temp%d = store%d.Data();\n",
              i, i,
              vtkWrap_GetTypeName(arg), i, mtwo, i,
              vtkWrap_GetTypeName(arg), i, i);
        if (!vtkWrap_IsConst(arg) &&
            !vtkWrap_IsRef(arg))
        {
          fprintf(fp,
              "  %s *save%d = (size%d == 0 ? nullptr : temp%d + size%d);\n",
              vtkWrap_GetTypeName(arg), i, i, i, i);
        }
      }
      else if (vtkWrap_IsArray(arg) && arg->Value)
      {
        /* prepare for "T a[n] = nullptr" arg (array with default of NULL) */
        fprintf(fp,
              "  size_t size%d = 0;\n"
              "  %s store%d[%s%d];\n"
              "  %s *temp%d = nullptr;\n",
              i,
              vtkWrap_GetTypeName(arg), i, mtwo, arg->Count,
              vtkWrap_GetTypeName(arg), i);
        if (!vtkWrap_IsConst(arg))
        {
          fprintf(fp,
              "  %s *save%d = nullptr;\n",
              vtkWrap_GetTypeName(arg), i);
        }
        fprintf(fp,
              "  if (ap.GetArgSize(%d) > 0)\n"
              "  {\n"
              "    size%d = %d;\n"
              "    temp%d = store%d;\n",
              i,
              i, arg->Count,
              i, i);
        if (!vtkWrap_IsConst(arg))
        {
          fprintf(fp,
              "    save%d = store%d + %d;\n",
              i, i, arg->Count);
        }
        fprintf(fp,
              "  }\n");
      }
      else
      {
        /* prepare for "T a[n]" or "T a[n][m]" array arg */
        vtkWrap_DeclareVariableSize(fp, arg, "size", i);
        vtkWrap_DeclareVariable(fp, data, arg, "temp", i, VTK_WRAP_ARG);

        if (!vtkWrap_IsConst(arg) &&
            !vtkWrap_IsSetVectorMethod(theFunc))
        {
          /* for saving a copy of the array */
          vtkWrap_DeclareVariable(fp, data, arg, "save", i, VTK_WRAP_ARG);
        }
      }
    }
    else if (vtkWrap_IsStdVector(arg))
    {
      fprintf(fp,
        "  %s temp%d(ap.GetArgSize(%d));\n",
        arg->Class, i, i);
    }
    else
    {
      /* make a "temp" variable for any other kind of argument */
      vtkWrap_DeclareVariable(fp, data, arg, "temp", i, VTK_WRAP_ARG);
    }

    /* temps for buffer objects */
    if (vtkWrap_IsVoidPointer(arg) ||
        vtkWrap_IsZeroCopyPointer(arg))
    {
      fprintf(fp,
              "  Py_buffer pbuf%d = VTK_PYBUFFER_INITIALIZER;\n",
              i);
    }

    /* temps for conversion constructed objects, which only occur
     * for special objects */
    if (vtkWrap_IsSpecialObject(arg) &&
        !vtkWrap_IsNonConstRef(arg))
    {
      fprintf(fp,
              "  PyObject *pobj%d = nullptr;\n",
              i);
    }
  }

  if (theFunc->ReturnValue)
  {
    /* the size for a one-dimensional array */
    if (vtkWrap_IsArray(theFunc->ReturnValue) &&
        !theFunc->ReturnValue->CountHint)
    {
      fprintf(fp,
              "  size_t sizer = %d;\n",
              theFunc->ReturnValue->Count);
    }
  }

  /* temp variable for the Python return value */
  fprintf(fp,
          "  PyObject *result = nullptr;\n"
          "\n");
}
Пример #6
0
/* Write back to all the reference arguments and array arguments that
 * were passed, but only write to arrays if the array has changed and
 * the array arg was non-const */
static void vtkWrapPython_WriteBackToArgs(
  FILE *fp, ClassInfo *data, FunctionInfo *currentFunction)
{
  const char *asterisks = "**********";
  ValueInfo *arg;
  int i, j, n, m;

  /* do nothing for SetVector macros */
  if (vtkWrap_IsSetVectorMethod(currentFunction))
  {
    return;
  }

  m = vtkWrap_CountWrappedParameters(currentFunction);

  /* check array value change for args that are non-const */
  for (i = 0; i < m; i++)
  {
    arg = currentFunction->Parameters[i];
    n = arg->NumberOfDimensions;
    if (n < 1 && (vtkWrap_IsArray(arg) || vtkWrap_IsPODPointer(arg) ||
                  (vtkWrap_IsCharPointer(arg) && !vtkWrap_IsConst(arg))))
    {
      n = 1;
    }

    if (vtkWrap_IsNonConstRef(arg) &&
        !vtkWrap_IsStdVector(arg) &&
        !vtkWrap_IsObject(arg))
    {
      fprintf(fp,
              "    if (!ap.ErrorOccurred())\n"
              "    {\n");

      if (vtkWrap_IsArray(arg) ||
          vtkWrap_IsPODPointer(arg))
      {
        fprintf(fp,
              "      ap.SetArgValue(%d, temp%d, ",
              i, i);
        if (arg->CountHint)
        {
          vtkWrapPython_SubstituteCode(fp, data, currentFunction,
                                       arg->CountHint);
        }
        else
        {
          fprintf(fp, "size%d", i);
        }
        fprintf(fp, ");\n");
      }
      else
      {
        fprintf(fp,
              "      ap.SetArgValue(%d, temp%d);\n",
              i, i);
      }
      fprintf(fp,
              "    }\n");
    }
    else if ((vtkWrap_IsArray(arg) || vtkWrap_IsNArray(arg) ||
              vtkWrap_IsPODPointer(arg) || vtkWrap_IsCharPointer(arg)) &&
             !vtkWrap_IsConst(arg) &&
             !vtkWrap_IsSetVectorMethod(currentFunction))
    {
      fprintf(fp,
              "    if (ap.HasChanged(%.*stemp%d, %.*ssave%d, ",
              (n-1), asterisks, i, (n-1), asterisks, i);

      if (vtkWrap_IsNArray(arg))
      {
        for (j = 0; j < arg->NumberOfDimensions; j++)
        {
          fprintf(fp, "%ssize%d[%d]", (j == 0 ? "" : "*"), i, j);
        }
      }
      else
      {
        fprintf(fp, "size%d", i);
      }

      fprintf(fp, ") &&\n"
              "        !ap.ErrorOccurred())\n"
              "    {\n");

      if (vtkWrap_IsNArray(arg))
      {
        fprintf(fp,
                "      ap.SetNArray(%d, %.*stemp%d, %d, size%d);\n",
                i, (n-1), asterisks, i, n, i);
      }
      else
      {
        fprintf(fp,
                "      ap.SetArray(%d, temp%d, size%d);\n",
                i, i, i);
      }

      fprintf(fp,
              "    }\n"
              "\n");
    }
    else if (vtkWrap_IsStdVector(arg) && !vtkWrap_IsConst(arg))
    {
      fprintf(fp,
              "    if (!ap.ErrorOccurred())\n"
              "    {\n"
              "      PyObject *vec = (temp%d.size() == 0 ?\n"
              "        PyTuple_New(0) :\n"
              "        ap.BuildTuple(temp%d.data(), temp%d.size()));\n"
              "      ap.SetContents(%d, vec);\n"
              "      Py_DECREF(vec);\n"
              "    }\n"
              "\n",
              i, i, i, i);
    }
  }
}