vtkWrapClientServer.c 43.3 KB
Newer Older
1
2
/*=========================================================================

3
  Program:   ParaView
4
5
  Module:    vtkWrapClientServer.c

6
7
8
  Copyright (c) Kitware, Inc.
  All rights reserved.
  See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.
9

10
11
12
     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.
13

14
=========================================================================*/
15
#include "vtkWrap.h"
16
#include "vtkParse.h"
17
#include "vtkParseHierarchy.h"
18
#include "vtkParseMain.h"
19
20
21
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
22

23
int numberOfWrappedFunctions = 0;
Kitware Robot's avatar
Kitware Robot committed
24
25
26
FunctionInfo* wrappedFunctions[1000];
extern FunctionInfo* currentFunction;
HierarchyInfo* hierarchyInfo = NULL;
27
28

/* make a guess about whether a class is wrapped */
Kitware Robot's avatar
Kitware Robot committed
29
static int class_is_wrapped(const char* classname)
30
{
Kitware Robot's avatar
Kitware Robot committed
31
  HierarchyEntry* entry;
32
33

  if (hierarchyInfo)
Kitware Robot's avatar
Kitware Robot committed
34
  {
35
36
37
    entry = vtkParseHierarchy_FindEntry(hierarchyInfo, classname);

    if (entry)
Kitware Robot's avatar
Kitware Robot committed
38
    {
39
      /* only allow non-excluded vtkObjects as args */
40
      if (vtkParseHierarchy_GetProperty(entry, "WRAP_EXCLUDE_PYTHON") ||
Kitware Robot's avatar
Kitware Robot committed
41
42
        !vtkParseHierarchy_IsTypeOf(hierarchyInfo, entry, "vtkObjectBase"))
      {
43
44
45
        return 0;
      }
    }
Kitware Robot's avatar
Kitware Robot committed
46
  }
47
48
49

  return 1;
}
50

51
int arg_is_pointer_to_data(unsigned int argType, int count)
52
{
Kitware Robot's avatar
Kitware Robot committed
53
54
55
56
57
58
59
  return (count == 0 && (argType & VTK_PARSE_INDIRECT) == VTK_PARSE_POINTER && /* T*   */
    (argType & VTK_PARSE_BASE_TYPE) != VTK_PARSE_STRING &&                     /* vtkStdString* */
    (argType & VTK_PARSE_BASE_TYPE) != VTK_PARSE_VOID &&                       /* void*    */
    (argType & VTK_PARSE_BASE_TYPE) != VTK_PARSE_CHAR &&                       /* char*    */
    (argType & VTK_PARSE_BASE_TYPE) != VTK_PARSE_BOOL &&                       /* bool*    */
    (argType & VTK_PARSE_BASE_TYPE) != VTK_PARSE_UNKNOWN &&                    /* Foo*  */
    (argType & VTK_PARSE_BASE_TYPE) != VTK_PARSE_VTK_OBJECT /* vtkFoo*  */);
60
}
61

Kitware Robot's avatar
Kitware Robot committed
62
void output_temp(FILE* fp, int i, unsigned int argType, const char* Id, int count)
63
{
64
  /* Store whether this is pointer to data.  */
65
  int isPointerToData = i != MAX_ARGS && arg_is_pointer_to_data(argType, count);
66

67
  /* ignore void */
Kitware Robot's avatar
Kitware Robot committed
68
69
  if (((argType & VTK_PARSE_BASE_TYPE) == VTK_PARSE_VOID) && ((argType & VTK_PARSE_INDIRECT) == 0))
  {
70
    return;
Kitware Robot's avatar
Kitware Robot committed
71
  }
72

73
  /* for const * return types prototype with const */
74
  if ((i == MAX_ARGS) && ((argType & VTK_PARSE_CONST) != 0))
Kitware Robot's avatar
Kitware Robot committed
75
76
77
  {
    fprintf(fp, "    const ");
  }
78
  else
Kitware Robot's avatar
Kitware Robot committed
79
80
81
  {
    fprintf(fp, "    ");
  }
82

83
  /* Handle some objects of known type.  */
Kitware Robot's avatar
Kitware Robot committed
84
85
86
87
88
89
  if (((argType & VTK_PARSE_BASE_TYPE) == VTK_PARSE_VTK_OBJECT) &&
    (((argType & VTK_PARSE_INDIRECT) == VTK_PARSE_POINTER) ||
        ((argType & VTK_PARSE_INDIRECT) == VTK_PARSE_REF)) &&
    strcmp(Id, "vtkClientServerStream") == 0)
  {
    fprintf(fp, "vtkClientServerStream temp%i_inst, *temp%i = &temp%i_inst;\n", i, i, i);
90
    return;
Kitware Robot's avatar
Kitware Robot committed
91
  }
92

93
  /* Start pointer-to-data arguments.  */
Kitware Robot's avatar
Kitware Robot committed
94
95
  if (isPointerToData)
  {
96
    fprintf(fp, "vtkClientServerStreamDataArg<");
Kitware Robot's avatar
Kitware Robot committed
97
  }
98

99
  if (argType & VTK_PARSE_UNSIGNED)
Kitware Robot's avatar
Kitware Robot committed
100
101
102
  {
    fprintf(fp, "unsigned ");
  }
103

104
  switch ((argType & VTK_PARSE_BASE_TYPE) & ~VTK_PARSE_UNSIGNED)
Kitware Robot's avatar
Kitware Robot committed
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
  {
    case VTK_PARSE_FLOAT:
      fprintf(fp, "float  ");
      break;
    case VTK_PARSE_DOUBLE:
      fprintf(fp, "double ");
      break;
    case VTK_PARSE_INT:
      fprintf(fp, "int    ");
      break;
    case VTK_PARSE_SHORT:
      fprintf(fp, "short  ");
      break;
    case VTK_PARSE_LONG:
      fprintf(fp, "long   ");
      break;
    case VTK_PARSE_VOID:
      fprintf(fp, "void   ");
      break;
    case VTK_PARSE_CHAR:
      fprintf(fp, "char   ");
      break;
    case VTK_PARSE_ID_TYPE:
      fprintf(fp, "vtkIdType ");
      break;
    case VTK_PARSE_LONG_LONG:
      fprintf(fp, "long long ");
      break;
    case VTK_PARSE___INT64:
      fprintf(fp, "__int64 ");
      break;
    case VTK_PARSE_SIGNED_CHAR:
      fprintf(fp, "signed char ");
      break;
    case VTK_PARSE_BOOL:
      fprintf(fp, "bool ");
      break;
    case VTK_PARSE_VTK_OBJECT:
      fprintf(fp, "%s ", Id);
      break;
145
    case VTK_PARSE_STRING:
Kitware Robot's avatar
Kitware Robot committed
146
147
148
149
150
151
152
153
154
155
      if (i == MAX_ARGS)
      {
        fprintf(fp, "%s ", Id);
        break;
      }
      else
      {
        fprintf(fp, "char    *");
      }
      break;
156

Kitware Robot's avatar
Kitware Robot committed
157
158
159
    case VTK_PARSE_UNKNOWN:
      return;
  }
160

161
  /* Finish pointer-to-data arguments.  */
Kitware Robot's avatar
Kitware Robot committed
162
163
164
  if (isPointerToData)
  {
    fprintf(fp, "> temp%i(msg, 0, %i);\n", i, i + 2);
165
    return;
Kitware Robot's avatar
Kitware Robot committed
166
  }
167

168
169
  /* handle array arguements */
  if (count > 1)
Kitware Robot's avatar
Kitware Robot committed
170
171
  {
    fprintf(fp, "temp%i[%i];\n", i, count);
172
    return;
Kitware Robot's avatar
Kitware Robot committed
173
  }
174

175
  switch (argType & VTK_PARSE_INDIRECT)
Kitware Robot's avatar
Kitware Robot committed
176
  {
177
178
    case VTK_PARSE_REF:
      if (i == MAX_ARGS)
Kitware Robot's avatar
Kitware Robot committed
179
      {
180
        fprintf(fp, " *"); /* act " &" */
Kitware Robot's avatar
Kitware Robot committed
181
      }
182
      break;
Kitware Robot's avatar
Kitware Robot committed
183
184
185
186
187
188
189
190
191
192
193
194
195
    case VTK_PARSE_POINTER:
      fprintf(fp, " *");
      break;
    case VTK_PARSE_POINTER_REF:
      fprintf(fp, "*&");
      break;
    case VTK_PARSE_POINTER_POINTER:
      fprintf(fp, "**");
      break;
    default:
      fprintf(fp, "  ");
      break;
  }
196

Kitware Robot's avatar
Kitware Robot committed
197
198
  fprintf(fp, "temp%i", i);
  fprintf(fp, ";\n");
199
200
201
}

/* when the cpp file doesn't have enough info use the hint file */
Kitware Robot's avatar
Kitware Robot committed
202
void use_hints(FILE* fp)
203
{
204
  unsigned int rType = currentFunction->ReturnType;
205

206
  /* use the hint */
207
  if ((rType & VTK_PARSE_INDIRECT) == VTK_PARSE_POINTER)
Kitware Robot's avatar
Kitware Robot committed
208
  {
209
    switch (rType & VTK_PARSE_BASE_TYPE)
Kitware Robot's avatar
Kitware Robot committed
210
211
212
213
214
215
    {
      case VTK_PARSE_FLOAT:
      case VTK_PARSE_DOUBLE:
      case VTK_PARSE_INT:
      case VTK_PARSE_SHORT:
      case VTK_PARSE_LONG:
216
      case VTK_PARSE_ID_TYPE:
Kitware Robot's avatar
Kitware Robot committed
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
      case VTK_PARSE_LONG_LONG:
      case VTK_PARSE___INT64:
      case VTK_PARSE_UNSIGNED_CHAR:
      case VTK_PARSE_SIGNED_CHAR:
      case VTK_PARSE_UNSIGNED_INT:
      case VTK_PARSE_UNSIGNED_SHORT:
      case VTK_PARSE_UNSIGNED_LONG:
      case VTK_PARSE_UNSIGNED_ID_TYPE:
      case VTK_PARSE_UNSIGNED_LONG_LONG:
      case VTK_PARSE_UNSIGNED___INT64:
        fprintf(fp,
          "      resultStream.Reset();\n"
          "      resultStream << vtkClientServerStream::Reply << "
          "vtkClientServerStream::InsertArray(temp%i,%i) << vtkClientServerStream::End;\n",
          MAX_ARGS, currentFunction->HintSize);
        break;
233
    }
Kitware Robot's avatar
Kitware Robot committed
234
  }
235
236
}

Kitware Robot's avatar
Kitware Robot committed
237
void return_result(FILE* fp)
238
{
239
  unsigned int rType = currentFunction->ReturnType;
Kitware Robot's avatar
Kitware Robot committed
240
  const char* rClass = currentFunction->ReturnClass;
241
242

  switch (rType & VTK_PARSE_BASE_TYPE)
Kitware Robot's avatar
Kitware Robot committed
243
  {
244
245
    case VTK_PARSE_VOID:
      if ((rType & VTK_PARSE_INDIRECT) == 0)
Kitware Robot's avatar
Kitware Robot committed
246
      {
247
        return;
Kitware Robot's avatar
Kitware Robot committed
248
      }
249
      break;
250

Kitware Robot's avatar
Kitware Robot committed
251
252
253
254
255
256
    case VTK_PARSE_FLOAT:
    case VTK_PARSE_CHAR:
    case VTK_PARSE_INT:
    case VTK_PARSE_SHORT:
    case VTK_PARSE_LONG:
    case VTK_PARSE_DOUBLE:
257
    case VTK_PARSE_ID_TYPE:
Kitware Robot's avatar
Kitware Robot committed
258
259
260
261
262
263
264
265
    case VTK_PARSE_LONG_LONG:
    case VTK_PARSE___INT64:
    case VTK_PARSE_SIGNED_CHAR:
    case VTK_PARSE_BOOL:
    case VTK_PARSE_UNSIGNED_CHAR:
    case VTK_PARSE_UNSIGNED_INT:
    case VTK_PARSE_UNSIGNED_SHORT:
    case VTK_PARSE_UNSIGNED_LONG:
266
    case VTK_PARSE_UNSIGNED_ID_TYPE:
Kitware Robot's avatar
Kitware Robot committed
267
268
    case VTK_PARSE_UNSIGNED_LONG_LONG:
    case VTK_PARSE_UNSIGNED___INT64:
269
270
    case VTK_PARSE_STRING:
      if ((rType & VTK_PARSE_INDIRECT) == 0 ||
Kitware Robot's avatar
Kitware Robot committed
271
272
273
274
275
276
277
        (((rType & VTK_PARSE_BASE_TYPE) == VTK_PARSE_CHAR) &&
            ((rType & VTK_PARSE_INDIRECT) == VTK_PARSE_POINTER)))
      {
        fprintf(fp, "      resultStream.Reset();\n"
                    "      resultStream << vtkClientServerStream::Reply << temp%i << "
                    "vtkClientServerStream::End;\n",
          MAX_ARGS);
278
        return;
Kitware Robot's avatar
Kitware Robot committed
279
      }
280
      else if ((rType & VTK_PARSE_INDIRECT) == VTK_PARSE_REF)
Kitware Robot's avatar
Kitware Robot committed
281
282
283
284
285
      {
        fprintf(fp, "      resultStream.Reset();\n"
                    "      resultStream << vtkClientServerStream::Reply << *temp%i << "
                    "vtkClientServerStream::End;\n",
          MAX_ARGS);
286
        return;
Kitware Robot's avatar
Kitware Robot committed
287
      }
288
      else if ((rType & VTK_PARSE_INDIRECT) == VTK_PARSE_POINTER)
Kitware Robot's avatar
Kitware Robot committed
289
      {
290
291
292
293
        /* handle functions returning vectors */
        /* this is done by looking them up in a hint file */
        use_hints(fp);
        return;
Kitware Robot's avatar
Kitware Robot committed
294
      }
295
296
      break;

297
    case VTK_PARSE_VTK_OBJECT:
298
      /* Handle some objects of known type.  */
Kitware Robot's avatar
Kitware Robot committed
299
300
301
302
303
304
      if (strcmp(rClass, "vtkClientServerStream") == 0)
      {
        fprintf(fp, "      resultStream.Reset();\n"
                    "      resultStream << vtkClientServerStream::Reply << *temp%i << "
                    "vtkClientServerStream::End;\n",
          MAX_ARGS);
305
        return;
Kitware Robot's avatar
Kitware Robot committed
306
      }
307
      else if ((rType & VTK_PARSE_INDIRECT) == VTK_PARSE_POINTER)
Kitware Robot's avatar
Kitware Robot committed
308
309
310
311
312
      {
        fprintf(fp, "      resultStream.Reset();\n"
                    "      resultStream << vtkClientServerStream::Reply << (vtkObjectBase *)temp%i "
                    "<< vtkClientServerStream::End;\n",
          MAX_ARGS);
313
        return;
Kitware Robot's avatar
Kitware Robot committed
314
      }
315
      break;
Kitware Robot's avatar
Kitware Robot committed
316
  }
317
318
319

  /* if we get to here, then the type was not recognized */

Kitware Robot's avatar
Kitware Robot committed
320
321
322
323
324
  fprintf(fp, "      resultStream.Reset();\n"
              "      resultStream << vtkClientServerStream::Reply\n"
              "                   << \"unable to return result of type(%#x).\"\n"
              "                   << vtkClientServerStream::End;\n",
    (rType & VTK_PARSE_UNQUALIFIED_TYPE));
325
326
}

Kitware Robot's avatar
Kitware Robot committed
327
void get_args(FILE* fp, int i)
328
{
329
  unsigned int argType = currentFunction->ArgTypes[i];
330
  int argCount = currentFunction->ArgCounts[i];
Kitware Robot's avatar
Kitware Robot committed
331
  const char* argClass = currentFunction->ArgClasses[i];
332
333
  int j;
  int start_arg = 2;
334

335
336
  /* what arg do we start with */
  for (j = 0; j < i; j++)
Kitware Robot's avatar
Kitware Robot committed
337
338
339
  {
    start_arg = start_arg + (currentFunction->ArgCounts[j] ? currentFunction->ArgCounts[j] : 1);
  }
340

341
  /* ignore void */
Kitware Robot's avatar
Kitware Robot committed
342
343
  if (((argType & VTK_PARSE_BASE_TYPE) == VTK_PARSE_VOID) && ((argType & VTK_PARSE_INDIRECT) == 0))
  {
344
    return;
Kitware Robot's avatar
Kitware Robot committed
345
  }
346

347
  switch (argType & VTK_PARSE_BASE_TYPE)
Kitware Robot's avatar
Kitware Robot committed
348
  {
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
    case VTK_PARSE_FLOAT:
    case VTK_PARSE_DOUBLE:
    case VTK_PARSE_INT:
    case VTK_PARSE_SHORT:
    case VTK_PARSE_LONG:
    case VTK_PARSE_ID_TYPE:
    case VTK_PARSE_LONG_LONG:
    case VTK_PARSE___INT64:
    case VTK_PARSE_SIGNED_CHAR:
    case VTK_PARSE_BOOL:
    case VTK_PARSE_CHAR:
    case VTK_PARSE_UNSIGNED_CHAR:
    case VTK_PARSE_UNSIGNED_INT:
    case VTK_PARSE_UNSIGNED_SHORT:
    case VTK_PARSE_UNSIGNED_LONG:
    case VTK_PARSE_UNSIGNED_ID_TYPE:
    case VTK_PARSE_UNSIGNED_LONG_LONG:
    case VTK_PARSE_UNSIGNED___INT64:
    case VTK_PARSE_STRING:
      if (((argType & VTK_PARSE_INDIRECT) == 0) ||
Kitware Robot's avatar
Kitware Robot committed
369
370
371
372
373
374
375
376
377
378
        ((argType & VTK_PARSE_INDIRECT) == VTK_PARSE_REF) ||
        (((argType & VTK_PARSE_BASE_TYPE) == VTK_PARSE_CHAR) &&
            ((argType & VTK_PARSE_INDIRECT) == VTK_PARSE_POINTER)))
      {
        fprintf(fp, "msg.GetArgument(0, %i, &temp%i)", i + 2, i);
      }
      else if (((argType & VTK_PARSE_INDIRECT) == VTK_PARSE_POINTER) && (argCount > 1))
      {
        fprintf(fp, "msg.GetArgument(0, %i, temp%i, %i)", i + 2, i, argCount);
      }
379
      else if (((argType & VTK_PARSE_INDIRECT) == VTK_PARSE_POINTER) &&
Kitware Robot's avatar
Kitware Robot committed
380
381
        (arg_is_pointer_to_data(argType, argCount)))
      {
382
383
384
        /* Pointer-to-data arguments are handled by an object
           convertible to bool.  */
        fprintf(fp, "temp%i", i);
Kitware Robot's avatar
Kitware Robot committed
385
      }
386

Kitware Robot's avatar
Kitware Robot committed
387
      break;
388
389

    case VTK_PARSE_VTK_OBJECT:
390
      /* Handle some objects of known type.  */
391
      if ((((argType & VTK_PARSE_INDIRECT) == VTK_PARSE_REF) ||
Kitware Robot's avatar
Kitware Robot committed
392
393
394
395
396
            ((argType & VTK_PARSE_INDIRECT) == VTK_PARSE_POINTER)) &&
        strcmp(argClass, "vtkClientServerStream") == 0)
      {
        fprintf(fp, "msg.GetArgument(0, %i, temp%i)", i + 2, i);
      }
397
      else if ((argType & VTK_PARSE_INDIRECT) == VTK_PARSE_POINTER)
Kitware Robot's avatar
Kitware Robot committed
398
399
400
401
      {
        fprintf(fp, "vtkClientServerStreamGetArgumentObject(msg, 0, %i, &temp%i, \"%s\")", i + 2, i,
          argClass);
      }
402
      break;
403

404
405
406
    case VTK_PARSE_VOID:
    case VTK_PARSE_UNKNOWN:
      break;
Kitware Robot's avatar
Kitware Robot committed
407
  }
408
409
}

410
/* declare these so they can be used in outputFunction */
Kitware Robot's avatar
Kitware Robot committed
411
412
int managableArguments(FunctionInfo* curFunction);
int notWrappable(FunctionInfo* curFunction);
413

Kitware Robot's avatar
Kitware Robot committed
414
void outputFunction(FILE* fp, ClassInfo* data)
415
416
{
  int i;
417

418
  if (notWrappable(currentFunction))
Kitware Robot's avatar
Kitware Robot committed
419
  {
420
    return;
Kitware Robot's avatar
Kitware Robot committed
421
  }
422

423
  /* if the args are OK and it is not a constructor or destructor */
Kitware Robot's avatar
Kitware Robot committed
424
425
426
427
  if (managableArguments(currentFunction) && strcmp(data->Name, currentFunction->Name) &&
    strcmp(data->Name, currentFunction->Name + 1))
  {
    if (currentFunction->IsLegacy)
428
    {
Kitware Robot's avatar
Kitware Robot committed
429
430
431
432
      fprintf(fp, "#if !defined(VTK_LEGACY_REMOVE)\n");
    }
    fprintf(fp, "  if (!strcmp(\"%s\",method) && msg.GetNumberOfArguments(0) == %i)\n",
      currentFunction->Name, currentFunction->NumberOfArguments + 2);
433
    fprintf(fp, "    {\n");
434

435
436
    /* process the args */
    for (i = 0; i < currentFunction->NumberOfArguments; i++)
Kitware Robot's avatar
Kitware Robot committed
437
438
439
440
441
    {
      output_temp(fp, i, currentFunction->ArgTypes[i], currentFunction->ArgClasses[i],
        currentFunction->ArgCounts[i]);
    }
    output_temp(fp, MAX_ARGS, currentFunction->ReturnType, currentFunction->ReturnClass, 0);
442

Kitware Robot's avatar
Kitware Robot committed
443
444
    if (currentFunction->NumberOfArguments > 0)
    {
445
446
447
      const char* amps = "    if(";
      /* now get the required args from the stack */
      for (i = 0; i < currentFunction->NumberOfArguments; i++)
Kitware Robot's avatar
Kitware Robot committed
448
      {
449
450
        fprintf(fp, "%s", amps);
        amps = " &&\n      ";
Kitware Robot's avatar
Kitware Robot committed
451
        get_args(fp, i);
452
      }
Kitware Robot's avatar
Kitware Robot committed
453
454
      fprintf(fp, ")\n");
    }
455
    fprintf(fp, "      {\n");
456

Kitware Robot's avatar
Kitware Robot committed
457
458
459
460
461
462
463
464
    if ((currentFunction->ReturnType & VTK_PARSE_UNQUALIFIED_TYPE) == VTK_PARSE_VOID)
    {
      fprintf(fp, "      op->%s(", currentFunction->Name);
    }
    else if ((currentFunction->ReturnType & VTK_PARSE_INDIRECT) == VTK_PARSE_REF)
    {
      fprintf(fp, "      temp%i = &(op)->%s(", MAX_ARGS, currentFunction->Name);
    }
465
    else
Kitware Robot's avatar
Kitware Robot committed
466
467
468
    {
      fprintf(fp, "      temp%i = (op)->%s(", MAX_ARGS, currentFunction->Name);
    }
469

470
    for (i = 0; i < currentFunction->NumberOfArguments; i++)
Kitware Robot's avatar
Kitware Robot committed
471
    {
472
      if (i)
Kitware Robot's avatar
Kitware Robot committed
473
474
475
476
477
478
479
480
      {
        fprintf(fp, ",");
      }
      if (((currentFunction->ArgTypes[i] & VTK_PARSE_INDIRECT) == VTK_PARSE_REF) &&
        ((currentFunction->ArgTypes[i] & VTK_PARSE_BASE_TYPE) == VTK_PARSE_VTK_OBJECT))
      {
        fprintf(fp, "*(temp%i)", i);
      }
481
      else if ((((currentFunction->ArgTypes[i] & VTK_PARSE_INDIRECT) == 0) ||
Kitware Robot's avatar
Kitware Robot committed
482
483
484
                 ((currentFunction->ArgTypes[i] & VTK_PARSE_INDIRECT) == VTK_PARSE_REF)) &&
        ((currentFunction->ArgTypes[i] & VTK_PARSE_BASE_TYPE) == VTK_PARSE_STRING))
      {
485
        /* explicit construction avoids possible ambiguity */
Kitware Robot's avatar
Kitware Robot committed
486
        fprintf(fp, "vtkStdString(temp%i)", i);
487
      }
Kitware Robot's avatar
Kitware Robot committed
488
      else
489
      {
Kitware Robot's avatar
Kitware Robot committed
490
        fprintf(fp, "temp%i", i);
491
      }
Kitware Robot's avatar
Kitware Robot committed
492
493
494
495
496
497
498
499
500
501
    }
    fprintf(fp, ");\n");
    return_result(fp);
    fprintf(fp, "      return 1;\n");
    fprintf(fp, "      }\n");
    fprintf(fp, "    }\n");
    if (currentFunction->IsLegacy)
    {
      fprintf(fp, "#endif\n");
    }
502

503
504
    wrappedFunctions[numberOfWrappedFunctions] = currentFunction;
    numberOfWrappedFunctions++;
Kitware Robot's avatar
Kitware Robot committed
505
  }
506
507

#if 0
508
  if (!strcmp("vtkObject",data->Name))
509
    {
510
511
    fprintf(fp,"  if (!strcmp(\"AddProgressObserver\",method) && msg.NumberOfArguments == 3 &&\n");
    fprintf(fp,"      msg.ArgumentTypes[2] == vtkClietnServerStream::string_value)\n");
512
513
514
515
516
517
518
519
520
    fprintf(fp,"    {\n");
    fprintf(fp,"    vtkClientServerProgressObserver *apo = vtkClientServerProgressObserver::New();\n");
    fprintf(fp,"    vtkObject* obj = arlu->GetObjectFromMessage(msg, 0, 1);\n");
    fprintf(fp,"    apo->SetFilterID(arlu->GetIDFromObject(obj));\n");
    fprintf(fp,"    apo->SetClientServerUtil(arlu);\n");
    fprintf(fp,"    char *temp0 = vtkClientServerInterpreter::GetString(msg,2);\n");
    fprintf(fp,"    op->AddObserver(temp0,apo);\n");
    fprintf(fp,"    apo->Delete();\n");
    fprintf(fp,"    delete [] temp0;\n");
521
    fprintf(fp,"    return 1;\n");
522
523
524
525
526
    fprintf(fp,"    }\n");
    }
#endif
}

527
528
529
530
531
532
533
534
//--------------------------------------------------------------------------nix
/*
 * This structure is used internally to sort+collect individual functions.
 * Polymorphed functions will be combined and can be handeled together.
 *
 */
typedef struct _UniqueFunctionInfo
{
Kitware Robot's avatar
Kitware Robot committed
535
  const char* Name;
536
  int TotalPolymorphTypes;
Kitware Robot's avatar
Kitware Robot committed
537
  FunctionInfo* Function[20];
538
539
540
541
542
543
544
545
} UniqueFunctionInfo;

//--------------------------------------------------------------------------nix
/*
 * This structure is used to collect and hold class information. It is a
 * modified version of FileInfo
 *
 */
546
typedef struct _NewClassInfo
547
{
Kitware Robot's avatar
Kitware Robot committed
548
549
550
551
552
553
554
555
556
  int HasDelete;
  int IsAbstract;
  int IsConcrete;
  const char* ClassName;
  const char* FileName;
  const char* OutputFileName;
  const char* SuperClasses[10];
  int NumberOfSuperClasses;
  int NumberOfFunctions;
557
  UniqueFunctionInfo Functions[1000];
Kitware Robot's avatar
Kitware Robot committed
558
559
560
561
  const char* NameComment;
  const char* Description;
  const char* Caveats;
  const char* SeeAlso;
562
} NewClassInfo;
563

564
565
566
//--------------------------------------------------------------------------nix
/*
 * notWrappable returns true if the current-function is not wrappable.
567
 *
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
568
 * @param curFunction function-info being worked on
569
 *
570
571
 * @return true if the function is not wrappable
 */
Kitware Robot's avatar
Kitware Robot committed
572
int notWrappable(FunctionInfo* curFunction)
573
{
Kitware Robot's avatar
Kitware Robot committed
574
  return (curFunction->IsOperator || curFunction->ArrayFailure || !curFunction->IsPublic ||
575
    !curFunction->Name || curFunction->Template);
576
577
578
579
580
581
}

//--------------------------------------------------------------------------nix
/*
 * managableArguments check if the functions arguments are in a form
 * which can easily be used for automatic wrapper generation.
582
 *
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
583
 * @param curFunction the function beign worked on
584
 *
585
586
 * @return true if the arguments are okay for code generation
 */
Kitware Robot's avatar
Kitware Robot committed
587
int managableArguments(FunctionInfo* curFunction)
588
{
Kitware Robot's avatar
Kitware Robot committed
589
590
591
592
593
594
  static unsigned int supported_types[] = { VTK_PARSE_VOID, VTK_PARSE_BOOL, VTK_PARSE_FLOAT,
    VTK_PARSE_DOUBLE, VTK_PARSE_CHAR, VTK_PARSE_UNSIGNED_CHAR, VTK_PARSE_SIGNED_CHAR, VTK_PARSE_INT,
    VTK_PARSE_UNSIGNED_INT, VTK_PARSE_SHORT, VTK_PARSE_UNSIGNED_SHORT, VTK_PARSE_LONG,
    VTK_PARSE_UNSIGNED_LONG, VTK_PARSE_ID_TYPE, VTK_PARSE_UNSIGNED_ID_TYPE, VTK_PARSE_LONG_LONG,
    VTK_PARSE_UNSIGNED_LONG_LONG, VTK_PARSE___INT64, VTK_PARSE_UNSIGNED___INT64,
    VTK_PARSE_VTK_OBJECT, VTK_PARSE_STRING, 0 };
595
596

  int i, j;
597
  int args_ok = 1;
598
599
600
  unsigned int returnType = 0;
  unsigned int argType = 0;
  unsigned int baseType = 0;
601

602
  /* check to see if we can handle the args */
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
603
  for (i = 0; i < curFunction->NumberOfArguments; i++)
Kitware Robot's avatar
Kitware Robot committed
604
605
606
  {
    int isPointerToData =
      arg_is_pointer_to_data(curFunction->ArgTypes[i], curFunction->ArgCounts[i]);
607
608
609
610
    argType = (curFunction->ArgTypes[i] & VTK_PARSE_UNQUALIFIED_TYPE);
    baseType = (argType & VTK_PARSE_BASE_TYPE);

    if (curFunction->ArgTypes[i] != VTK_PARSE_FUNCTION)
Kitware Robot's avatar
Kitware Robot committed
611
    {
612
      for (j = 0; supported_types[j] != 0; j++)
Kitware Robot's avatar
Kitware Robot committed
613
614
      {
        if (baseType == supported_types[j])
615
        {
Kitware Robot's avatar
Kitware Robot committed
616
          break;
617
        }
Kitware Robot's avatar
Kitware Robot committed
618
      }
619
      if (supported_types[j] == 0)
Kitware Robot's avatar
Kitware Robot committed
620
      {
621
622
        args_ok = 0;
      }
Kitware Robot's avatar
Kitware Robot committed
623
    }
624

625
    /* if its a pointer arg make sure we have the ArgCount */
Kitware Robot's avatar
Kitware Robot committed
626
627
628
629
630
631
    if (((argType & VTK_PARSE_INDIRECT) == VTK_PARSE_POINTER) && !isPointerToData &&
      ((argType & VTK_PARSE_BASE_TYPE) != VTK_PARSE_CHAR) &&
      ((argType & VTK_PARSE_BASE_TYPE) != VTK_PARSE_VTK_OBJECT))
    {
      if (curFunction->NumberOfArguments > 1 || curFunction->ArgCounts[i] == 0 ||
        ((argType & VTK_PARSE_BASE_TYPE) == VTK_PARSE_STRING))
632
633
634
      {
        args_ok = 0;
      }
Kitware Robot's avatar
Kitware Robot committed
635
    }
636
637

    /* if it has a reference arg, don't wrap it */
638
    if ((argType & VTK_PARSE_INDIRECT) == VTK_PARSE_REF)
Kitware Robot's avatar
Kitware Robot committed
639
    {
640
641
      /* make exception for "vtkClientServerStream&" */
      if (((argType & VTK_PARSE_BASE_TYPE) != VTK_PARSE_VTK_OBJECT) ||
Kitware Robot's avatar
Kitware Robot committed
642
643
        strcmp(curFunction->ArgClasses[i], "vtkClientServerStream"))
      {
644
645
        /* also make exception for "const vtkStdString&" */
        if ((argType & VTK_PARSE_BASE_TYPE) != VTK_PARSE_STRING ||
Kitware Robot's avatar
Kitware Robot committed
646
647
          (curFunction->ArgTypes[i] & VTK_PARSE_CONST) == 0)
        {
648
649
650
          args_ok = 0;
        }
      }
Kitware Robot's avatar
Kitware Robot committed
651
    }
652

653
    /* if it is a vtk object that isn't a pointer or ref, don't wrap it */
654
    if (((argType & VTK_PARSE_INDIRECT) == 0) &&
Kitware Robot's avatar
Kitware Robot committed
655
656
      ((argType & VTK_PARSE_BASE_TYPE) == VTK_PARSE_VTK_OBJECT))
    {
657
      args_ok = 0;
Kitware Robot's avatar
Kitware Robot committed
658
    }
659

660
661
    /* if arg is a vtk object, make sure it is a wrapped object */
    if ((argType & VTK_PARSE_BASE_TYPE) == VTK_PARSE_VTK_OBJECT &&
Kitware Robot's avatar
Kitware Robot committed
662
663
      !class_is_wrapped(curFunction->ArgClasses[i]))
    {
664
      args_ok = 0;
Kitware Robot's avatar
Kitware Robot committed
665
    }
666

667
668
    /* if it is "**" or "*&" then don't wrap it */
    if (((argType & VTK_PARSE_INDIRECT) != 0) &&
Kitware Robot's avatar
Kitware Robot committed
669
670
671
      ((argType & VTK_PARSE_INDIRECT) != VTK_PARSE_POINTER) &&
      ((argType & VTK_PARSE_INDIRECT) != VTK_PARSE_REF))
    {
672
      args_ok = 0;
Kitware Robot's avatar
Kitware Robot committed
673
    }
674

675
    /* if it is an array of chars, then don't wrap it */
Kitware Robot's avatar
Kitware Robot committed
676
677
    if ((argType & VTK_PARSE_BASE_TYPE) == VTK_PARSE_CHAR && curFunction->ArgCounts[i] != 0)
    {
678
      args_ok = 0;
Kitware Robot's avatar
Kitware Robot committed
679
    }
680

Kitware Robot's avatar
Kitware Robot committed
681
682
683
684
685
    if ((argType & VTK_PARSE_UNSIGNED) && (argType != VTK_PARSE_UNSIGNED_CHAR) &&
      (argType != VTK_PARSE_UNSIGNED_INT) && (argType != VTK_PARSE_UNSIGNED_SHORT) &&
      (argType != VTK_PARSE_UNSIGNED_LONG) && (argType != VTK_PARSE_UNSIGNED_ID_TYPE) &&
      !isPointerToData)
    {
686
687
      args_ok = 0;
    }
Kitware Robot's avatar
Kitware Robot committed
688
  }
689
690
691
692
693
694

  /* check the return type */
  returnType = (curFunction->ReturnType & VTK_PARSE_UNQUALIFIED_TYPE);
  baseType = (returnType & VTK_PARSE_BASE_TYPE);

  for (j = 0; supported_types[j] != 0; j++)
Kitware Robot's avatar
Kitware Robot committed
695
696
  {
    if (baseType == supported_types[j])
697
    {
Kitware Robot's avatar
Kitware Robot committed
698
      break;
699
    }
Kitware Robot's avatar
Kitware Robot committed
700
  }
701
  if (supported_types[j] == 0)
Kitware Robot's avatar
Kitware Robot committed
702
  {
703
    args_ok = 0;
Kitware Robot's avatar
Kitware Robot committed
704
  }
705
706

  /* if it is a reference, then don't wrap it */
707
  if ((returnType & VTK_PARSE_INDIRECT) == VTK_PARSE_REF)
Kitware Robot's avatar
Kitware Robot committed
708
  {
709
710
    /* make exception for "vtkClientServerStream&" */
    if (((returnType & VTK_PARSE_BASE_TYPE) != VTK_PARSE_VTK_OBJECT) ||
Kitware Robot's avatar
Kitware Robot committed
711
712
      strcmp(curFunction->ReturnClass, "vtkClientServerStream"))
    {
713
      /* also make exception for "vtkStdString" */
714
      if ((returnType & VTK_PARSE_BASE_TYPE) != VTK_PARSE_STRING)
Kitware Robot's avatar
Kitware Robot committed
715
      {
716
717
718
        args_ok = 0;
      }
    }
Kitware Robot's avatar
Kitware Robot committed
719
  }
720
721
722

  /* if it is a vtk object that isn't a pointer, don't wrap it */
  if (((returnType & VTK_PARSE_INDIRECT) == 0) &&
Kitware Robot's avatar
Kitware Robot committed
723
724
    ((returnType & VTK_PARSE_BASE_TYPE) == VTK_PARSE_VTK_OBJECT))
  {
725
    args_ok = 0;
Kitware Robot's avatar
Kitware Robot committed
726
  }
727

728
729
  /* if arg is a vtk object, make sure it is a wrapped object */
  if ((returnType & VTK_PARSE_BASE_TYPE) == VTK_PARSE_VTK_OBJECT &&
Kitware Robot's avatar
Kitware Robot committed
730
731
    !class_is_wrapped(curFunction->ReturnClass))
  {
732
    args_ok = 0;
Kitware Robot's avatar
Kitware Robot committed
733
  }
734

735
736
  /* if it is "**" or "*&" then don't wrap it */
  if (((returnType & VTK_PARSE_INDIRECT) != 0) &&
Kitware Robot's avatar
Kitware Robot committed
737
738
739
    ((returnType & VTK_PARSE_INDIRECT) != VTK_PARSE_POINTER) &&
    ((returnType & VTK_PARSE_INDIRECT) != VTK_PARSE_REF))
  {
740
    args_ok = 0;
Kitware Robot's avatar
Kitware Robot committed
741
  }
742

743
  /* cannot wrap function pointers */
Kitware Robot's avatar
Kitware Robot committed
744
745
  if (curFunction->NumberOfArguments && (curFunction->ArgTypes[0] == VTK_PARSE_FUNCTION))
  {
746
    args_ok = 0;
Kitware Robot's avatar
Kitware Robot committed
747
  }
748
749

  /* we can't handle void * return types */
750
  if (((returnType & VTK_PARSE_BASE_TYPE) == VTK_PARSE_VOID) &&
Kitware Robot's avatar
Kitware Robot committed
751
752
    ((returnType & VTK_PARSE_INDIRECT) != 0))
  {
753
    args_ok = 0;
Kitware Robot's avatar
Kitware Robot committed
754
  }
755
756

  /* watch out for functions that dont have enough info */
757
  if ((returnType & VTK_PARSE_INDIRECT) == VTK_PARSE_POINTER)
Kitware Robot's avatar
Kitware Robot committed
758
  {
759
    switch (returnType & VTK_PARSE_BASE_TYPE)
Kitware Robot's avatar
Kitware Robot committed
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
    {
      case VTK_PARSE_FLOAT:
      case VTK_PARSE_DOUBLE:
      case VTK_PARSE_INT:
      case VTK_PARSE_SHORT:
      case VTK_PARSE_LONG:
      case VTK_PARSE_ID_TYPE:
      case VTK_PARSE_LONG_LONG:
      case VTK_PARSE___INT64:
      case VTK_PARSE_SIGNED_CHAR:
      case VTK_PARSE_UNSIGNED_CHAR:
      case VTK_PARSE_UNSIGNED_INT:
      case VTK_PARSE_UNSIGNED_SHORT:
      case VTK_PARSE_UNSIGNED_LONG:
      case VTK_PARSE_UNSIGNED_ID_TYPE:
      case VTK_PARSE_UNSIGNED_LONG_LONG:
      case VTK_PARSE_UNSIGNED___INT64:
777
778
779
780
781
782
783
784
785
786
        args_ok = curFunction->HaveHint;
        break;

      case VTK_PARSE_CHAR:
      case VTK_PARSE_VTK_OBJECT:
        break;

      default:
        args_ok = 0;
        break;
787
    }
Kitware Robot's avatar
Kitware Robot committed
788
  }
789

790
791
  return args_ok;
}
792

793
//--------------------------------------------------------------------------nix
794
795
796
/*
 * funCmp is used to compare the function names of two FunInfo data.
 * This is used as a utility to the sort function.
797
 *
798
799
 * @param fun1 first function data which is compared
 * @param fun2 second function data which is compared
800
 *
801
 * @return values returned by strcmp
802
 */
803

Kitware Robot's avatar
Kitware Robot committed
804
int funCmp(const void* fun1, const void* fun2)
805
{
Kitware Robot's avatar
Kitware Robot committed
806
807
808
  FunctionInfo* a = (FunctionInfo*)fun1;
  FunctionInfo* b = (FunctionInfo*)fun2;
  return strcmp(a->Name, b->Name);
809
810
811
}

//--------------------------------------------------------------------------nix
812
/*
813
 * copy copies data from the source array to the destination
814
 *
815
816
817
 * @param from the source array from which we want to copy data
 * @param fromSize the size of the source array
 * @param to the destination to which we wanto to copy the data
818
 *
819
 * @return the size of the destination array
820
 */
821
822
/*
int copy(FunctionInfo* from, int fromSize, FunctionInfo* to)
823
{
824
825
  int i;
  for (i=0 ; i<fromSize;i++)
826
    {
827
    to[i] =  from[i];
828
    }
829
  return i;
830
}
831
*/
832

833
//--------------------------------------------------------------------------nix
834
/*
835
836
837
 * extractWrappable copies data from the source array to the destination
 * array and removes unwrappable functions whenever encountered.
 * It returns the count of the destination array.
838
 *
839
840
841
 * @param from the source array from which we want to extract data
 * @param fromSize the size of the source array
 * @param to the destination to which we wanto to copy extracted data
842
 *
843
 * @return the size of the destination array (this may not be the size of the source)
844
 */
Kitware Robot's avatar
Kitware Robot committed
845
int extractWrappable(FunctionInfo* from[], int fromSize, FunctionInfo* to[], const char* ClassName)
846
{
Kitware Robot's avatar
Kitware Robot committed
847
848
849
  int i, j;
  for (i = 0, j = 0; i < fromSize; i++)
  {
850
851
852
    /* if the function is wrappable and
       args are OK and
       it is not a constructor or destructor */
Kitware Robot's avatar
Kitware Robot committed
853
854
855
    if (!notWrappable(from[i]) && managableArguments(from[i]) && strcmp(ClassName, from[i]->Name) &&
      strcmp(ClassName, from[i]->Name + 1))
    {
856
      to[j] = from[i];
857
      ++j;
858
    }
Kitware Robot's avatar
Kitware Robot committed
859
  }
860
861
  return j;
}
862

863
864
865
866
//--------------------------------------------------------------------------nix
/*
 * collectUniqueFunctionInfo collects unique function. Polymorphed functions
 * are grouped in the UniqueFunctionInfo structure.
867
 *
868
869
870
 * @param src source data array with all applicable function info
 * @param srcSize the size of the array holding this info
 * @param dest the destnation array to store the extracted info
871
 *
872
873
 * @return the total number of elements in the dest array
 */
Kitware Robot's avatar
Kitware Robot committed
874
int collectUniqueFunctionInfo(FunctionInfo* src[], int srcSize, UniqueFunctionInfo dest[])
875
{
Kitware Robot's avatar
Kitware Robot committed
876
  int i, j, k;
877

Kitware Robot's avatar
Kitware Robot committed
878
879
  for (i = 0; i < srcSize; i++)
  {
880
    dest[i].Name = src[i]->Name;
881
    dest[i].TotalPolymorphTypes = 1;
882
    dest[i].Function[0] = src[i];
Kitware Robot's avatar
Kitware Robot committed
883
884
885
    for (j = i + 1; j < srcSize; j++)
    {
      if (strcmp(dest[i].Name, src[j]->Name) == 0)
886
      {
Kitware Robot's avatar
Kitware Robot committed
887
888
        // printf("%d [%d]> dest = %s :: src = %s\n",i,srcSize, dest[i].Name,src[j].Name);
        // printf("    > [SAME]\n");
889
        dest[i].Function[dest[i].TotalPolymorphTypes] = src[j];
890
891
        dest[i].TotalPolymorphTypes++;

Kitware Robot's avatar
Kitware Robot committed
892
893
894
895
896
        for (k = j; k < srcSize - 1; k++)
        {
          // printf("remaining = %s\n",src[k]->Name);
          src[k] = src[k + 1];
        }
897
898
        j--;
        srcSize--;
899
      }
900
    }
Kitware Robot's avatar
Kitware Robot committed
901
  }
902

903
  return srcSize;
904
905
906
907
908
}

//--------------------------------------------------------------------------nix
/*
 * This is a format converter function which converts from traditional
909
 * FileInfo format to NewClassInfo format. The NewClassInfo format combines
910
 * polymorphed functions into UniqueFunctionInfo internal format.
911
 *
912
913
914
 * @param data hold the collected file data form the parser
 * @param classData the form into which we want to convert
 */
Kitware Robot's avatar
Kitware Robot committed
915
void getClassInfo(FileInfo* fileInfo, ClassInfo* data, NewClassInfo* classData)
916
{
917
  int i;
Kitware Robot's avatar
Kitware Robot committed
918
  int TotalUniqueFunctions = 0;