File: vtkWrapPythonConstant.c

package info (click to toggle)
vtk9 9.5.2%2Bdfsg3-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 205,916 kB
  • sloc: cpp: 2,336,565; ansic: 327,116; python: 111,200; yacc: 4,104; java: 3,977; sh: 3,032; xml: 2,771; perl: 2,189; lex: 1,787; makefile: 178; javascript: 165; objc: 153; tcl: 59
file content (323 lines) | stat: -rw-r--r-- 9,214 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause

#include "vtkWrapPythonConstant.h"
#include "vtkWrap.h"
#include "vtkWrapText.h"

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* -------------------------------------------------------------------- */
/* The scope, attrib, and valstring parameters are optional and can be
   set to NULL.

   The "scope" is a namespace to use for enum constants, it is ignored
   if null.

   The "pythonscope" is the same namespace, but with any template
   parameters mangled so that it can be used as a python identifier.

   The "attrib" is the attribute to set in the dictionary, if null then
   val->Name is used as the attribute name.

   The "attribval" is the value to set the attribute to, if null then
   val->Value is used.
*/
void vtkWrapPython_AddConstantHelper(FILE* fp, const char* indent, const char* dictvar,
  const char* objvar, const char* scope, const char* pythonscope, const char* attrib,
  const char* attribval, ValueInfo* val)
{
  unsigned int valtype;
  const char* valname;
  const char* valstring;
  int objcreated = 0;

  valtype = (val->Type & VTK_PARSE_UNQUALIFIED_TYPE);
  valname = val->Name;
  valstring = attribval;
  if (valstring == 0)
  {
    valstring = val->Value;
  }

  if (valtype == 0 && (valstring == NULL || valstring[0] == '\0'))
  {
    valtype = VTK_PARSE_VOID;
  }
  else if (strcmp(valstring, "nullptr") == 0)
  {
    valtype = VTK_PARSE_VOID;
  }

  if (valtype == 0 || val->Name == NULL)
  {
    return;
  }

  if (val->IsEnum)
  {
    if (val->Class && val->Class[0] != '\0' && strcmp(val->Class, "int") != 0)
    {
      fprintf(fp, "%s%s = Py%s%s%s_FromEnum(%s%s%s);\n", indent, objvar,
        (pythonscope ? pythonscope : ""), (pythonscope ? "_" : ""), val->Class,
        ((pythonscope && !attribval) ? pythonscope : ""), ((pythonscope && !attribval) ? "::" : ""),
        (!attribval ? valname : attribval));
      objcreated = 1;
    }
    else
    {
      fprintf(fp, "%s%s = PyLong_FromLong(%s%s%s);\n", indent, objvar,
        ((scope && !attribval) ? scope : ""), ((scope && !attribval) ? "::" : ""),
        (!attribval ? valname : attribval));
      objcreated = 1;
    }
  }
  else
    switch (valtype)
    {
      case VTK_PARSE_VOID:
        fprintf(fp,
          "%sPy_INCREF(Py_None);\n"
          "%s%s = Py_None;\n",
          indent, indent, objvar);
        objcreated = 1;
        break;

      case VTK_PARSE_CHAR_PTR:
        fprintf(fp, "%s%s = PyUnicode_FromString(%s);\n", indent, objvar, valstring);
        objcreated = 1;
        break;

      case VTK_PARSE_FLOAT:
      case VTK_PARSE_DOUBLE:
        fprintf(fp, "%s%s = PyFloat_FromDouble(%s);\n", indent, objvar, valstring);
        objcreated = 1;
        break;

      case VTK_PARSE_LONG:
      case VTK_PARSE_INT:
      case VTK_PARSE_SHORT:
      case VTK_PARSE_UNSIGNED_SHORT:
      case VTK_PARSE_CHAR:
      case VTK_PARSE_SIGNED_CHAR:
      case VTK_PARSE_UNSIGNED_CHAR:
        fprintf(fp, "%s%s = PyLong_FromLong(%s);\n", indent, objvar, valstring);
        objcreated = 1;
        break;

      case VTK_PARSE_UNSIGNED_INT:
        fprintf(fp, "%s%s = PyLong_FromUnsignedLong(%s);\n", indent, objvar, valstring);
        objcreated = 1;
        break;

      case VTK_PARSE_UNSIGNED_LONG:
        fprintf(fp, "%s%s = PyLong_FromUnsignedLong(%s);\n", indent, objvar, valstring);
        objcreated = 1;
        break;

      case VTK_PARSE_LONG_LONG:
        fprintf(fp, "%s%s = PyLong_FromLongLong(%s);\n", indent, objvar, valstring);
        objcreated = 1;
        break;

      case VTK_PARSE_UNSIGNED_LONG_LONG:
        fprintf(fp, "%s%s = PyLong_FromUnsignedLongLong(%s);\n", indent, objvar, valstring);
        objcreated = 1;
        break;

      case VTK_PARSE_BOOL:
        fprintf(fp, "%s%s = PyBool_FromLong((long)(%s));\n", indent, objvar, valstring);
        objcreated = 1;
        break;
    }

  if (objcreated)
  {
    fprintf(fp,
      "%sif (%s)\n"
      "%s{\n"
      "%s  PyDict_SetItemString(%s, %s%s%s%s, %s);\n"
      "%s  Py_DECREF(%s);\n"
      "%s}\n",
      indent, objvar, indent, indent, dictvar, (attrib ? "" : "\""), (attrib ? attrib : valname),
      (attrib || !vtkWrapText_IsPythonKeyword(valname) ? "" : "_"), (attrib ? "" : "\""), objvar,
      indent, objvar, indent);
  }
}

/* -------------------------------------------------------------------- */
/* Add all constants defined in the namespace to the module */

void vtkWrapPython_AddPublicConstants(
  FILE* fp, const char* indent, const char* dictvar, const char* objvar, NamespaceInfo* data)
{
  char text[1024];
  const char* nextindent = "        ";
  ValueInfo* val;
  ValueInfo* firstval;
  const char* scope;
  const char* pythonscope = 0;
  int scopeType, scopeValue;
  unsigned int valtype;
  const char* typeName;
  const char* tname;
  int j = 0;
  int count, k, i;
  size_t l, m;

  /* get the next indent to use */
  l = strlen(indent);
  m = strlen(nextindent);
  if (m > l + 2)
  {
    nextindent += m - l - 2;
  }

  /* get the name of the namespace, or NULL if global */
  scope = data->Name;
  if (scope)
  {
    if (scope[0] == '\0')
    {
      scope = 0;
    }
    else
    {
      /* convert C++ class names to a python-friendly format */
      vtkWrapText_PythonName(scope, text);
      pythonscope = text;
    }
  }

  /* go through the constants, collecting them by type */
  while (j < data->NumberOfConstants)
  {
    val = data->Constants[j];
    if (val->Access != VTK_ACCESS_PUBLIC)
    {
      j++;
      continue;
    }

    /* write a single constant if not numerical */
    if (j + 1 == data->NumberOfConstants || val->Type != data->Constants[j + 1]->Type ||
      !vtkWrap_IsScalar(val) || (!val->IsEnum && !vtkWrap_IsNumeric(val)))
    {
      vtkWrapPython_AddConstant(fp, indent, dictvar, objvar, scope, val);
      j++;
      continue;
    }

    /* get important information about the value */
    valtype = val->Type;
    typeName = (val->IsEnum ? val->Class : vtkWrap_GetTypeName(val));
    scopeType = (scope && val->IsEnum && strcmp(typeName, "int") != 0);
    scopeValue = (scope && val->IsEnum);

    /* count a series of constants of the same type */
    firstval = val;
    count = 0;
    for (k = j; k < data->NumberOfConstants; k++)
    {
      val = data->Constants[k];
      if (val->Access == VTK_ACCESS_PUBLIC)
      {
        tname = (val->IsEnum ? val->Class : vtkWrap_GetTypeName(val));
        if (val->Type != valtype || strcmp(tname, typeName) != 0)
        {
          break;
        }
        count++;
      }
    }

    /* if no constants to generate, then continue */
    if (count == 0)
    {
      j = k;
      continue;
    }

    if (scopeType)
    {
      int found = 0;

      /* check to make sure that the enum type is wrapped */
      for (i = 0; i < data->NumberOfEnums && !found; i++)
      {
        const EnumInfo* info = data->Enums[i];
        found = (info->IsExcluded && info->Name && strcmp(typeName, info->Name) == 0);
      }
      if (found)
      {
        j = k;
        continue;
      }

      /* check to make sure there won't be a name conflict between an
         enum type and some other class member, it happens specifically
         for vtkImplicitBoolean which has a variable and enum type both
         with the name OperationType */
      for (i = 0; i < data->NumberOfVariables && !found; i++)
      {
        found = (strcmp(data->Variables[i]->Name, typeName) == 0);
      }
      if (found)
      {
        valtype = VTK_PARSE_INT;
        typeName = "int";
        scopeType = 0;
      }
    }

    /* generate the code */
    fprintf(fp,
      "%sfor (int c = 0; c < %d; c++)\n"
      "%s{\n",
      indent, count, indent);

    if (scopeType)
    {
      fprintf(fp, "%s  typedef %s::%s cxx_enum_type;\n\n", indent, scope, typeName);
    }

    fprintf(fp,
      "%s  static const struct { const char *name; %s value; }\n"
      "%s    constants[%d] = {\n",
      indent, (scopeType ? "cxx_enum_type" : typeName), indent, count);

    while (j < k)
    {
      val = data->Constants[j++];
      if (val->Access == VTK_ACCESS_PUBLIC)
      {
        fprintf(fp, "%s      { \"%s%s\", %s%s%s },%s\n", indent, val->Name,
          (vtkWrapText_IsPythonKeyword(val->Name) ? "_" : ""), (scopeValue ? scope : ""),
          (scopeValue ? "::" : ""), (val->IsEnum ? val->Name : val->Value),
          ((val->Attributes & VTK_PARSE_DEPRECATED) ? " /* deprecated */" : ""));
      }
    }

    fprintf(fp,
      "%s    };\n"
      "\n",
      indent);

    vtkWrapPython_AddConstantHelper(fp, nextindent, dictvar, objvar, scope, pythonscope,
      "constants[c].name", "constants[c].value", firstval);

    fprintf(fp, "%s}\n\n", indent);
  }
}

/* -------------------------------------------------------------------- */
/* This method adds one constant defined in the file to the module */

void vtkWrapPython_AddConstant(FILE* fp, const char* indent, const char* dictvar,
  const char* objvar, const char* scope, ValueInfo* val)
{
  vtkWrapPython_AddConstantHelper(fp, indent, dictvar, objvar, scope, scope, NULL, NULL, val);
}