Beispiel #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");
  }
}
Beispiel #2
0
const char *vtkWrapText_PythonSignature(
  FunctionInfo *currentFunction)
{
  /* string is intentionally not freed until the program exits */
  static struct vtkWPString staticString = { NULL, 0, 0 };
  struct vtkWPString *result;
  ValueInfo *arg, *ret;
  const char *parens[2] = { "(", ")" };
  const char *braces[2] = { "[", "]" };
  const char **delims;
  int i, n;

  n = vtkWrap_CountWrappedParameters(currentFunction);

  result = &staticString;
  result->len = 0;

  /* print out the name of the method */
  vtkWPString_Append(result, "V.");
  vtkWPString_Append(result, currentFunction->Name);

  /* print the arg list */
  vtkWPString_Append(result, "(");

  for (i = 0; i < n; i++)
  {
    arg = currentFunction->Parameters[i];

    if (i != 0)
    {
      vtkWPString_Append(result, ", ");
    }

    delims = parens;
    if (!vtkWrap_IsConst(arg) &&
        !vtkWrap_IsSetVectorMethod(currentFunction))
    {
      delims = braces;
    }

    vtkWrapText_PythonTypeSignature(result, delims, arg);
  }

  vtkWPString_Append(result, ")");

  /* if this is a void method, we are finished */
  /* otherwise, print "->" and the return type */
  ret = currentFunction->ReturnValue;
  if (ret && (ret->Type & VTK_PARSE_UNQUALIFIED_TYPE) != VTK_PARSE_VOID)
  {
    vtkWPString_Append(result, " -> ");

    vtkWrapText_PythonTypeSignature(result, parens, ret);
  }

  if (currentFunction->Signature)
  {
    vtkWPString_Append(result, "\nC++: ");
    vtkWPString_Append(result, currentFunction->Signature);
  }

  return result->str;
}
Beispiel #3
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, 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)))
      {
      n = 1;
      }

    if (vtkWrap_IsNonConstRef(arg) &&
        !vtkWrap_IsObject(arg))
      {
      fprintf(fp,
              "    if (!ap.ErrorOccurred())\n"
              "      {\n"
              "      ap.SetArgValue(%d, temp%d);\n"
              "      }\n",
              i, i);
      }

    else if ((vtkWrap_IsArray(arg) || vtkWrap_IsNArray(arg) ||
              vtkWrap_IsPODPointer(arg)) &&
             !vtkWrap_IsConst(arg) &&
             !vtkWrap_IsSetVectorMethod(currentFunction))
      {
      fprintf(fp,
              "    if (ap.ArrayHasChanged(%.*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");
      }
    }
}
Beispiel #4
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 = NULL;\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))
      {
      /* for non-const arrays, alloc twice as much space */
      const char *mtwo = "";
      if (!vtkWrap_IsConst(arg) && !vtkWrap_IsSetVectorMethod(theFunc))
        {
        mtwo = "2*";
        }
      if (arg->CountHint || vtkWrap_IsPODPointer(arg))
        {
        /* prepare for "T *" arg, where T is a plain type */
        fprintf(fp,
              "  int 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))
          {
          fprintf(fp,
              "  %s *save%d = (size%d == 0 ? NULL : temp%d + size%d);\n",
              vtkWrap_GetTypeName(arg), i, i, i, i);
          }
        }
      else if (vtkWrap_IsArray(arg) && arg->Value)
        {
        /* prepare for "T a[n] = NULL" arg (array whose default is NULL) */
        fprintf(fp,
              "  int size%d = 0;\n"
              "  %s store%d[%s%d];\n"
              "  %s *temp%d = NULL;\n",
              i,
              vtkWrap_GetTypeName(arg), i, mtwo, arg->Count,
              vtkWrap_GetTypeName(arg), i);
        if (!vtkWrap_IsConst(arg))
          {
          fprintf(fp,
              "  %s *save%d = NULL;\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
      {
      /* 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))
      {
      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 = NULL;\n",
              i);
      }
    }

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

  /* temp variable for the Python return value */
  fprintf(fp,
          "  PyObject *result = NULL;\n"
          "\n");
}
/* Declare all local variables used by the wrapper method */
void vtkWrapPython_DeclareVariables(
  FILE *fp, ClassInfo *data, FunctionInfo *theFunc)
{
  ValueInfo *arg;
  int i, n;
  int storageSize;

  n = vtkWrap_CountWrappedParameters(theFunc);

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

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

    /* make a "temp" variable for the argument */
    vtkWrap_DeclareVariable(fp, data, arg, "temp", i, VTK_WRAP_ARG);

    /* temps for buffer objects */
    if (vtkWrap_IsVoidPointer(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 = NULL;\n",
              i);
      }

    /* temps for arrays */
    if (vtkWrap_IsArray(arg) || vtkWrap_IsNArray(arg) ||
        vtkWrap_IsPODPointer(arg))
      {
      storageSize = 4;
      if (!vtkWrap_IsConst(arg) &&
          !vtkWrap_IsSetVectorMethod(theFunc))
        {
        /* for saving a copy of the array */
        vtkWrap_DeclareVariable(fp, data, arg, "save", i, VTK_WRAP_ARG);
        storageSize *= 2;
        }
      if (arg->CountHint || vtkWrap_IsPODPointer(arg))
        {
        fprintf(fp,
                "  %s small%d[%d];\n",
                vtkWrap_GetTypeName(arg), i, storageSize);
        }
      /* write an int array containing the dimensions */
      vtkWrap_DeclareVariableSize(fp, arg, "size", i);
      }
    }

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

  /* temp variable for the Python return value */
  fprintf(fp,
          "  PyObject *result = NULL;\n"
          "\n");
}
/* Get the size for vtkDataArray Tuple arguments */
static void vtkWrapPython_GetSizesForArrays(
  FILE *fp, FunctionInfo *theFunc, int is_vtkobject)
{
  int i, j, n;
  const char *indentation = "";
  const char *mtwo;
  ValueInfo *arg;

  n = vtkWrap_CountWrappedParameters(theFunc);

  j = ((is_vtkobject && !theFunc->IsStatic) ? 1 : 0);
  for (i = 0; i < n; i++)
    {
    arg = theFunc->Parameters[i];

    if (arg->CountHint || vtkWrap_IsPODPointer(arg))
      {
      if (j == 1)
        {
        fprintf(fp,
                "  if (op)\n"
                "    {\n");
        indentation = "  ";
        }
      j += 2;
      if (arg->CountHint)
        {
        fprintf(fp,
              "%s  size%d = op->%s;\n",
              indentation, i, arg->CountHint);
        }
      else
        {
        fprintf(fp,
              "%s  size%d = ap.GetArgSize(%d);\n",
              indentation, i, i);
        }

      /* for non-const arrays, alloc twice as much space */
      mtwo = "";
      if (!vtkWrap_IsConst(arg) && !vtkWrap_IsSetVectorMethod(theFunc))
        {
        mtwo = "2*";
        }

      fprintf(fp,
              "%s  temp%d = small%d;\n"
              "%s  if (size%d > 4)\n"
              "%s    {\n"
              "%s    temp%d = new %s[%ssize%d];\n"
              "%s    }\n",
              indentation, i, i,
              indentation, i,
              indentation,
              indentation, i, vtkWrap_GetTypeName(arg), mtwo, i,
              indentation);

      if (*mtwo)
        {
        fprintf(fp,
              "%s  save%d = &temp%d[size%d];\n",
              indentation, i, i, i);
        }
      }
    }
  if (j > 1)
    {
    if ((j & 1) != 0)
      {
      fprintf(fp,
                  "    }\n");
      }
    fprintf(fp,
            "\n");
    }
}