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 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkWrapPythonType.c
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
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.
=========================================================================*/
#include "vtkWrapPythonType.h"
#include "vtkWrapPythonClass.h"
#include "vtkWrapPythonConstant.h"
#include "vtkWrapPythonEnum.h"
#include "vtkWrapPythonMethod.h"
#include "vtkWrapPythonMethodDef.h"
#include "vtkWrapPythonTemplate.h"
#include "vtkWrap.h"
#include "vtkWrapText.h"
#include "vtkParseExtras.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
/* -------------------------------------------------------------------- */
/* A struct for special types to store info about the type, it is fairly
* small because not many operators or special features are wrapped */
typedef struct _SpecialTypeInfo
{
int has_print; /* there is "<<" stream operator */
int has_compare; /* there are comparison operators e.g. "<" */
int has_sequence; /* the [] operator takes a single integer */
} SpecialTypeInfo;
/* -------------------------------------------------------------------- */
/* The following functions are for generating code for special types,
* i.e. types that are not derived from vtkObjectBase */
/* -------------------------------------------------------------------- */
/* -------------------------------------------------------------------- */
/* generate function for printing a special object */
static void vtkWrapPython_NewDeleteProtocol(
FILE *fp, const char *classname, ClassInfo *data)
{
const char *constructor;
size_t n, m;
/* remove namespaces and template parameters from the
* class name to get the constructor name */
constructor = data->Name;
m = vtkParse_UnscopedNameLength(constructor);
while (constructor[m] == ':' && constructor[m+1] == ':')
{
constructor += m + 2;
m = vtkParse_UnscopedNameLength(constructor);
}
for (n = 0; n < m; n++)
{
if (constructor[n] == '<')
{
break;
}
}
/* the "new" method */
if (data->IsAbstract)
{
fprintf(fp,
"static PyObject *\n"
"Py%s_New(PyTypeObject *, PyObject *, PyObject *)\n"
"{\n"
" PyErr_SetString(PyExc_TypeError,\n"
" \"this abstract class cannot be instantiated\");\n"
"\n"
" return NULL;\n"
"}\n"
"\n",
classname);
}
else
{
fprintf(fp,
"static PyObject *\n"
"Py%s_New(PyTypeObject *, PyObject *args, PyObject *kwds)\n"
"{\n"
" if (kwds && PyDict_Size(kwds))\n"
" {\n"
" PyErr_SetString(PyExc_TypeError,\n"
" \"this function takes no keyword arguments\");\n"
" return NULL;\n"
" }\n"
"\n"
" return Py%s_%*.*s(NULL, args);\n"
"}\n"
"\n",
classname, classname, (int)n, (int)n, constructor);
}
/* the delete method */
fprintf(fp,
"static void Py%s_Delete(PyObject *self)\n"
"{\n"
" PyVTKSpecialObject *obj = (PyVTKSpecialObject *)self;\n"
" delete static_cast<%s *>(obj->vtk_ptr);\n"
" PyObject_Del(self);\n"
"}\n"
"\n",
classname, data->Name);
}
/* -------------------------------------------------------------------- */
/* generate function for printing a special object */
static void vtkWrapPython_PrintProtocol(
FILE *fp, const char *classname, ClassInfo *data,
FileInfo *finfo, SpecialTypeInfo *info)
{
int i;
FunctionInfo *func;
/* look in the file for "operator<<" for printing */
for (i = 0; i < finfo->Contents->NumberOfFunctions; i++)
{
func = finfo->Contents->Functions[i];
if (func->Name && func->IsOperator &&
strcmp(func->Name, "operator<<") == 0)
{
if (func->NumberOfParameters == 2 &&
(func->Parameters[0]->Type & VTK_PARSE_UNQUALIFIED_TYPE) ==
VTK_PARSE_OSTREAM_REF &&
(func->Parameters[1]->Type & VTK_PARSE_BASE_TYPE) ==
VTK_PARSE_OBJECT &&
(func->Parameters[1]->Type & VTK_PARSE_POINTER_MASK) == 0 &&
!vtkWrap_IsNonConstRef(func->Parameters[1]) &&
strcmp(func->Parameters[1]->Class, data->Name) == 0)
{
info->has_print = 1;
}
}
}
/* the str function */
if (info->has_print)
{
fprintf(fp,
"static PyObject *Py%s_String(PyObject *self)\n"
"{\n"
" PyVTKSpecialObject *obj = (PyVTKSpecialObject *)self;\n"
" std::ostringstream os;\n"
" if (obj->vtk_ptr)\n"
" {\n"
" os << *static_cast<const %s *>(obj->vtk_ptr);\n"
" }\n"
" const std::string &s = os.str();\n"
" return PyString_FromStringAndSize(s.data(), s.size());\n"
"}\n"
"\n",
classname, data->Name);
}
}
/* -------------------------------------------------------------------- */
/* generate function for comparing special objects */
static void vtkWrapPython_RichCompareProtocol(
FILE *fp, const char *classname, ClassInfo *data,
FileInfo *finfo, SpecialTypeInfo *info)
{
static const char *compare_consts[6] = {
"Py_LT", "Py_LE", "Py_EQ", "Py_NE", "Py_GT", "Py_GE" };
static const char *compare_tokens[6] = {
"<", "<=", "==", "!=", ">", ">=" };
int compare_ops = 0;
int i, n;
FunctionInfo *func;
/* look for comparison operator methods */
n = data->NumberOfFunctions + finfo->Contents->NumberOfFunctions;
for (i = 0; i < n; i++)
{
if (i < data->NumberOfFunctions)
{
/* member function */
func = data->Functions[i];
if (func->NumberOfParameters != 1 ||
(func->Parameters[0]->Type & VTK_PARSE_BASE_TYPE) !=
VTK_PARSE_OBJECT ||
(func->Parameters[0]->Type & VTK_PARSE_POINTER_MASK) != 0 ||
strcmp(func->Parameters[0]->Class, data->Name) != 0)
{
continue;
}
}
else
{
/* non-member function: both args must be of our type */
func = finfo->Contents->Functions[i - data->NumberOfFunctions];
if (func->NumberOfParameters != 2 ||
(func->Parameters[0]->Type & VTK_PARSE_BASE_TYPE) !=
VTK_PARSE_OBJECT ||
(func->Parameters[0]->Type & VTK_PARSE_POINTER_MASK) != 0 ||
strcmp(func->Parameters[0]->Class, data->Name) != 0 ||
(func->Parameters[1]->Type & VTK_PARSE_BASE_TYPE) !=
VTK_PARSE_OBJECT ||
(func->Parameters[1]->Type & VTK_PARSE_POINTER_MASK) != 0 ||
strcmp(func->Parameters[1]->Class, data->Name) != 0)
{
continue;
}
}
if (func->IsOperator && func->Name != NULL)
{
if (strcmp(func->Name, "operator<") == 0)
{
compare_ops = (compare_ops | (1 << 0));
}
else if (strcmp(func->Name, "operator<=") == 0)
{
compare_ops = (compare_ops | (1 << 1));
}
else if (strcmp(func->Name, "operator==") == 0)
{
compare_ops = (compare_ops | (1 << 2));
}
else if (strcmp(func->Name, "operator!=") == 0)
{
compare_ops = (compare_ops | (1 << 3));
}
else if (strcmp(func->Name, "operator>") == 0)
{
compare_ops = (compare_ops | (1 << 4));
}
else if (strcmp(func->Name, "operator>=") == 0)
{
compare_ops = (compare_ops | (1 << 5));
}
}
}
/* the compare function */
if (compare_ops != 0)
{
info->has_compare = 1;
fprintf(fp,
"static int Py%s_CheckExact(PyObject *ob);\n\n",
classname);
fprintf(fp,
"static PyObject *Py%s_RichCompare(\n"
" PyObject *o1, PyObject *o2, int opid)\n"
"{\n"
" PyObject *n1 = NULL;\n"
" PyObject *n2 = NULL;\n"
" const %s *so1 = NULL;\n"
" const %s *so2 = NULL;\n"
" int result = -1;\n"
"\n",
classname, data->Name, data->Name);
for (i = 1; i <= 2; i++)
{
/* use GetPointerFromSpecialObject to do type conversion, but
* at least one of the args will already be the correct type */
fprintf(fp,
" if (Py%s_CheckExact(o%d))\n"
" {\n"
" PyVTKSpecialObject *s%d = (PyVTKSpecialObject *)o%d;\n"
" so%d = static_cast<const %s *>(s%d->vtk_ptr);\n"
" }\n"
" else\n"
" {\n"
" so%d = static_cast<const %s *>(\n"
" vtkPythonUtil::GetPointerFromSpecialObject(\n"
" o%d, \"%s\", &n%d));\n"
" if (so%d == NULL)\n"
" {\n"
" PyErr_Clear();\n"
" Py_INCREF(Py_NotImplemented);\n"
" return Py_NotImplemented;\n"
" }\n"
" }\n"
"\n",
classname, i, i, i, i, data->Name, i, i, data->Name,
i, classname, i, i);
}
/* the switch statement for all possible compare ops */
fprintf(fp,
" switch (opid)\n"
" {\n");
for (i = 0; i < 6; i++)
{
if ( ((compare_ops >> i) & 1) != 0 )
{
fprintf(fp,
" case %s:\n"
" result = ((*so1) %s (*so2));\n"
" break;\n",
compare_consts[i], compare_tokens[i]);
}
else
{
fprintf(fp,
" case %s:\n"
" break;\n",
compare_consts[i]);
}
}
fprintf(fp,
" }\n"
"\n");
/* delete temporary objects, there will be at most one */
fprintf(fp,
" if (n1)\n"
" {\n"
" Py_DECREF(n1);\n"
" }\n"
" else if (n2)\n"
" {\n"
" Py_DECREF(n2);\n"
" }\n"
"\n");
/* return the result */
fprintf(fp,
" if (result == -1)\n"
" {\n"
" PyErr_SetString(PyExc_TypeError, \"operation not available\");\n"
" return NULL;\n"
" }\n"
"\n"
" // avoids aliasing issues with Py_INCREF(Py_False)\n"
" return PyBool_FromLong((long)result);\n"
"}\n"
"\n");
}
}
/* -------------------------------------------------------------------- */
/* generate functions for indexing into special objects */
static void vtkWrapPython_SequenceProtocol(
FILE *fp, const char *classname, ClassInfo *data,
HierarchyInfo *hinfo, SpecialTypeInfo *info)
{
int i;
FunctionInfo *func;
FunctionInfo *getItemFunc = 0;
FunctionInfo *setItemFunc = 0;
/* look for [] operator */
for (i = 0; i < data->NumberOfFunctions; i++)
{
func = data->Functions[i];
if (func->Name && func->IsOperator &&
strcmp(func->Name, "operator[]") == 0 &&
vtkWrapPython_MethodCheck(data, func, hinfo))
{
if (func->NumberOfParameters == 1 && func->ReturnValue &&
vtkWrap_IsInteger(func->Parameters[0]))
{
if (!setItemFunc && vtkWrap_IsNonConstRef(func->ReturnValue))
{
setItemFunc = func;
}
if (!getItemFunc || (func->IsConst && !getItemFunc->IsConst))
{
getItemFunc = func;
}
}
}
}
if (getItemFunc && getItemFunc->SizeHint)
{
info->has_sequence = 1;
fprintf(fp,
"Py_ssize_t Py%s_SequenceSize(PyObject *self)\n"
"{\n"
" void *vp = vtkPythonArgs::GetSelfSpecialPointer(self);\n"
" %s *op = static_cast<%s *>(vp);\n"
"\n"
" return static_cast<Py_ssize_t>(op->%s);\n"
"}\n\n",
classname, data->Name, data->Name, getItemFunc->SizeHint);
fprintf(fp,
"PyObject *Py%s_SequenceItem(PyObject *self, Py_ssize_t i)\n"
"{\n"
" void *vp = vtkPythonArgs::GetSelfSpecialPointer(self);\n"
" %s *op = static_cast<%s *>(vp);\n"
"\n",
classname, data->Name, data->Name);
vtkWrapPython_DeclareVariables(fp, data, getItemFunc);
fprintf(fp,
" temp0 = static_cast<%s>(i);\n"
"\n"
" if (temp0 < 0 || temp0 >= op->%s)\n"
" {\n"
" PyErr_SetString(PyExc_IndexError, \"index out of range\");\n"
" }\n"
" else\n"
" {\n",
vtkWrap_GetTypeName(getItemFunc->Parameters[0]),
getItemFunc->SizeHint);
fprintf(fp, " ");
vtkWrap_DeclareVariable(fp, data, getItemFunc->ReturnValue,
"tempr", -1, VTK_WRAP_RETURN | VTK_WRAP_NOSEMI);
fprintf(fp, " = %s(*op)[temp0];\n"
"\n",
(vtkWrap_IsRef(getItemFunc->ReturnValue) ? "&" : ""));
vtkWrapPython_ReturnValue(fp, data, getItemFunc->ReturnValue, 1);
fprintf(fp,
" }\n"
"\n"
" return result;\n"
"}\n\n");
if (setItemFunc)
{
fprintf(fp,
"int Py%s_SequenceSetItem(\n"
" PyObject *self, Py_ssize_t i, PyObject *arg1)\n"
"{\n"
" void *vp = vtkPythonArgs::GetSelfSpecialPointer(self);\n"
" %s *op = static_cast<%s *>(vp);\n"
"\n",
classname, data->Name, data->Name);
vtkWrap_DeclareVariable(fp, data, setItemFunc->Parameters[0],
"temp", 0, VTK_WRAP_ARG);
vtkWrap_DeclareVariable(fp, data, setItemFunc->ReturnValue,
"temp", 1, VTK_WRAP_ARG);
fprintf(fp,
" int result = -1;\n"
"\n"
" temp0 = static_cast<%s>(i);\n"
"\n"
" if (temp0 < 0 || temp0 >= op->%s)\n"
" {\n"
" PyErr_SetString(PyExc_IndexError, \"index out of range\");\n"
" }\n"
" else if (",
vtkWrap_GetTypeName(setItemFunc->Parameters[0]),
getItemFunc->SizeHint);
vtkWrapPython_GetSingleArgument(
fp, data, 1, setItemFunc->ReturnValue, 1);
fprintf(fp,")\n"
" {\n"
" (*op)[temp0] = %stemp1;\n"
"\n",
((vtkWrap_IsRef(getItemFunc->ReturnValue) &&
vtkWrap_IsObject(getItemFunc->ReturnValue)) ? "*" : ""));
fprintf(fp,
" if (PyErr_Occurred() == NULL)\n"
" {\n"
" result = 0;\n"
" }\n"
" }\n"
"\n"
" return result;\n"
"}\n\n");
}
fprintf(fp,
"static PySequenceMethods Py%s_AsSequence = {\n"
" Py%s_SequenceSize, // sq_length\n"
" 0, // sq_concat\n"
" 0, // sq_repeat\n"
" Py%s_SequenceItem, // sq_item\n"
" 0, // sq_slice\n",
classname, classname, classname);
if (setItemFunc)
{
fprintf(fp,
" Py%s_SequenceSetItem, // sq_ass_item\n",
classname);
}
else
{
fprintf(fp,
" 0, // sq_ass_item\n");
}
fprintf(fp,
" 0, // sq_ass_slice\n"
" 0, // sq_contains\n"
" 0, // sq_inplace_concat\n"
" 0, // sq_inplace_repeat\n"
"};\n\n");
}
}
/* -------------------------------------------------------------------- */
/* generate function for hashing special objects */
static void vtkWrapPython_HashProtocol(
FILE *fp, const char *classname, ClassInfo *data)
{
/* the hash function, defined only for specific types */
fprintf(fp,
"static Py_hash_t Py%s_Hash(PyObject *self)\n",
classname);
if (strcmp(data->Name, "vtkTimeStamp") == 0)
{
/* hash for vtkTimeStamp is just the timestamp itself */
fprintf(fp,
"{\n"
" PyVTKSpecialObject *obj = (PyVTKSpecialObject *)self;\n"
" const vtkTimeStamp *op = static_cast<const vtkTimeStamp *>(obj->vtk_ptr);\n"
" vtkMTimeType mtime = *op;\n"
" long h = (long)mtime;\n"
" if (h != -1) { return h; }\n"
" return -2;\n"
"}\n"
"\n");
}
else if (strcmp(data->Name, "vtkVariant") == 0)
{
/* hash for vtkVariant is cached to avoid recomputation, this is
* safe because vtkVariant is an immutable object, and is necessary
* because computing the hash for vtkVariant is very expensive */
fprintf(fp,
"{\n"
" PyVTKSpecialObject *obj = (PyVTKSpecialObject *)self;\n"
" const vtkVariant *op = static_cast<const vtkVariant *>(obj->vtk_ptr);\n"
" long h = obj->vtk_hash;\n"
" if (h != -1)\n"
" {\n"
" return h;\n"
" }\n"
" h = vtkPythonUtil::VariantHash(op);\n"
" obj->vtk_hash = h;\n"
" return h;\n"
"}\n"
"\n");
}
else
{
/* if hash is not implemented, raise an exception */
fprintf(fp,
"{\n"
"#if PY_VERSION_HEX >= 0x020600B2\n"
" return PyObject_HashNotImplemented(self);\n"
"#else\n"
" char text[256];\n"
" sprintf(text, \"unhashable type: \'%%s\'\", Py_TYPE(self)->tp_name);\n"
" PyErr_SetString(PyExc_TypeError, text);\n"
" return -1;\n"
"#endif\n"
"}\n"
"\n");
}
}
/* -------------------------------------------------------------------- */
/* generate extra functions for a special object */
static void vtkWrapPython_SpecialTypeProtocols(
FILE *fp, const char *classname, ClassInfo *data,
FileInfo *finfo, HierarchyInfo *hinfo, SpecialTypeInfo *info)
{
/* clear all info about the type */
info->has_print = 0;
info->has_compare = 0;
info->has_sequence = 0;
vtkWrapPython_NewDeleteProtocol(fp, classname, data);
vtkWrapPython_PrintProtocol(fp, classname, data, finfo, info);
vtkWrapPython_RichCompareProtocol(fp, classname, data, finfo, info);
vtkWrapPython_SequenceProtocol(fp, classname, data, hinfo, info);
vtkWrapPython_HashProtocol(fp, classname, data);
}
/* -------------------------------------------------------------------- */
/* For classes that aren't derived from vtkObjectBase, check to see if
* they are wrappable */
int vtkWrapPython_IsSpecialTypeWrappable(ClassInfo *data)
{
/* no templated types */
if (data->Template)
{
return 0;
}
/* restrict wrapping to classes that have a "vtk" prefix */
if (strncmp(data->Name, "vtk", 3) != 0)
{
return 0;
}
/* require public destructor and copy contructor */
if (!vtkWrap_HasPublicDestructor(data) ||
!vtkWrap_HasPublicCopyConstructor(data))
{
return 0;
}
return 1;
}
/* -------------------------------------------------------------------- */
/* write out a special type object */
void vtkWrapPython_GenerateSpecialType(
FILE *fp, const char *module, const char *classname,
ClassInfo *data, FileInfo *finfo, HierarchyInfo *hinfo)
{
char supername[1024];
const char *name;
SpecialTypeInfo info;
const char *constructor;
size_t n, m;
int i;
int has_constants = 0;
int has_superclass = 0;
int is_external = 0;
/* remove namespaces and template parameters from the
* class name to get the constructor name */
constructor = data->Name;
m = vtkParse_UnscopedNameLength(constructor);
while (constructor[m] == ':' && constructor[m+1] == ':')
{
constructor += m + 2;
m = vtkParse_UnscopedNameLength(constructor);
}
for (n = 0; n < m; n++)
{
if (constructor[n] == '<')
{
break;
}
}
/* get the superclass */
has_superclass = vtkWrapPython_HasWrappedSuperClass(
hinfo, data->Name, &is_external);
if (has_superclass)
{
name = vtkWrapPython_GetSuperClass(data, hinfo);
vtkWrapText_PythonName(name, supername);
}
/* generate all constructor methods */
if (!data->IsAbstract)
{
vtkWrapPython_GenerateMethods(fp, classname, data, finfo, hinfo, 0, 1);
}
/* generate all functions and protocols needed for the type */
vtkWrapPython_SpecialTypeProtocols(
fp, classname, data, finfo, hinfo, &info);
/* Generate the TypeObject */
fprintf(fp,
"static PyTypeObject Py%s_Type = {\n"
" PyVarObject_HEAD_INIT(&PyType_Type, 0)\n"
" \"%sPython.%s\", // tp_name\n"
" sizeof(PyVTKSpecialObject), // tp_basicsize\n"
" 0, // tp_itemsize\n"
" Py%s_Delete, // tp_dealloc\n"
" 0, // tp_print\n"
" 0, // tp_getattr\n"
" 0, // tp_setattr\n"
" 0, // tp_compare\n"
" PyVTKSpecialObject_Repr, // tp_repr\n",
classname, module, classname, classname);
fprintf(fp,
" 0, // tp_as_number\n");
if (info.has_sequence)
{
fprintf(fp,
" &Py%s_AsSequence, // tp_as_sequence\n",
classname);
}
else
{
fprintf(fp,
" 0, // tp_as_sequence\n");
}
fprintf(fp,
" 0, // tp_as_mapping\n"
" Py%s_Hash, // tp_hash\n"
" 0, // tp_call\n",
classname);
if (info.has_print)
{
fprintf(fp,
" Py%s_String, // tp_str\n",
classname);
}
else if (info.has_sequence)
{
fprintf(fp,
" PyVTKSpecialObject_SequenceString, // tp_str\n");
}
else
{
fprintf(fp,
" 0, // tp_str\n");
}
fprintf(fp,
" PyObject_GenericGetAttr, // tp_getattro\n"
" 0, // tp_setattro\n"
" 0, // tp_as_buffer\n"
" Py_TPFLAGS_DEFAULT, // tp_flags\n"
" 0, // tp_doc\n"
" 0, // tp_traverse\n"
" 0, // tp_clear\n");
if (info.has_compare)
{
fprintf(fp,
" Py%s_RichCompare, // tp_richcompare\n",
classname);
}
else
{
fprintf(fp,
" 0, // tp_richcompare\n");
}
fprintf(fp,
" 0, // tp_weaklistoffset\n"
" 0, // tp_iter\n"
" 0, // tp_iternext\n"
" 0, // tp_methods\n"
" 0, // tp_members\n"
" 0, // tp_getset\n"
" 0, // tp_base\n"
" 0, // tp_dict\n"
" 0, // tp_descr_get\n"
" 0, // tp_descr_set\n"
" 0, // tp_dictoffset\n"
" 0, // tp_init\n"
" 0, // tp_alloc\n"
" Py%s_New, // tp_new\n"
" PyObject_Del, // tp_free\n"
" 0, // tp_is_gc\n",
classname);
/* fields set by python itself */
fprintf(fp,
" 0, // tp_bases\n"
" 0, // tp_mro\n"
" 0, // tp_cache\n"
" 0, // tp_subclasses\n"
" 0, // tp_weaklist\n");
/* internal struct members */
fprintf(fp,
" VTK_WRAP_PYTHON_SUPPRESS_UNINITIALIZED\n"
"};\n"
"\n");
/* need a check function for some protocols */
if (info.has_compare)
{
fprintf(fp,
"static int Py%s_CheckExact(PyObject *ob)\n"
"{\n"
" return (Py_TYPE(ob) == &Py%s_Type);\n"
"}\n\n",
classname, classname);
}
/* generate the copy constructor helper function */
if (!data->IsAbstract)
{
fprintf(fp,
"static void *Py%s_CCopy(const void *obj)\n"
"{\n"
" if (obj)\n"
" {\n"
" return new %s(*static_cast<const %s*>(obj));\n"
" }\n"
" return 0;\n"
"}\n"
"\n",
classname, data->Name, data->Name);
}
/* export New method for use by subclasses */
fprintf(fp,
"extern \"C\" { %s PyObject *Py%s_TypeNew(); }\n\n",
"VTK_ABI_EXPORT", classname);
/* import New method of the superclass */
if (has_superclass)
{
fprintf(fp,
"#ifndef DECLARED_Py%s_TypeNew\n"
"extern \"C\" { PyObject *Py%s_TypeNew(); }\n"
"#define DECLARED_Py%s_TypeNew\n"
"#endif\n",
supername, supername, supername);
}
/* the method for adding the VTK extras to the type,
* the unused "const char *" arg is the module name */
fprintf(fp,
"PyObject *Py%s_TypeNew()\n"
"{\n",
classname);
if (data->IsAbstract)
{
fprintf(fp,
" PyVTKSpecialType_Add(\n"
" &Py%s_Type,\n"
" Py%s_Methods,\n"
" NULL,\n"
" Py%s_Doc(), NULL);\n"
"\n",
classname, classname, classname);
}
else
{
fprintf(fp,
" PyVTKSpecialType_Add(\n"
" &Py%s_Type,\n"
" Py%s_Methods,\n"
" Py%s_%*.*s_Methods,\n"
" Py%s_Doc(), &Py%s_CCopy);\n"
"\n",
classname, classname,
classname, (int)n, (int)n, constructor,
classname, classname);
}
fprintf(fp,
" PyTypeObject *pytype = &Py%s_Type;\n\n",
classname);
/* if type is already ready, then return */
fprintf(fp,
" if ((pytype->tp_flags & Py_TPFLAGS_READY) != 0)\n"
" {\n"
" return (PyObject *)pytype;\n"
" }\n\n");
/* call the superclass New (initialize in dependency order) */
if (has_superclass)
{
fprintf(fp,
" pytype->tp_base = (PyTypeObject *)Py%s_TypeNew();\n\n",
supername);
}
/* check whether the class has any constants as members */
for (i = 0; i < data->NumberOfConstants; i++)
{
if (data->Constants[i]->Access == VTK_ACCESS_PUBLIC)
{
has_constants = 1;
}
}
if (has_constants)
{
fprintf(fp,
" PyObject *d = pytype->tp_dict;\n"
" PyObject *o;\n\n");
/* add any enum types defined in the class to its dict */
vtkWrapPython_AddPublicEnumTypes(fp, " ", "d", "o", data);
/* add any constants defined in the class to its dict */
vtkWrapPython_AddPublicConstants(fp, " ", "d", "o", data);
}
fprintf(fp,
" PyType_Ready(pytype);\n"
" return (PyObject *)pytype;\n"
"}\n\n");
}
|