swiginit.swg 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /* -----------------------------------------------------------------------------
  2. * Type initialization:
  3. * This problem is tough by the requirement that no dynamic
  4. * memory is used. Also, since swig_type_info structures store pointers to
  5. * swig_cast_info structures and swig_cast_info structures store pointers back
  6. * to swig_type_info structures, we need some lookup code at initialization.
  7. * The idea is that swig generates all the structures that are needed.
  8. * The runtime then collects these partially filled structures.
  9. * The SWIG_InitializeModule function takes these initial arrays out of
  10. * swig_module, and does all the lookup, filling in the swig_module.types
  11. * array with the correct data and linking the correct swig_cast_info
  12. * structures together.
  13. *
  14. * The generated swig_type_info structures are assigned statically to an initial
  15. * array. We just loop through that array, and handle each type individually.
  16. * First we lookup if this type has been already loaded, and if so, use the
  17. * loaded structure instead of the generated one. Then we have to fill in the
  18. * cast linked list. The cast data is initially stored in something like a
  19. * two-dimensional array. Each row corresponds to a type (there are the same
  20. * number of rows as there are in the swig_type_initial array). Each entry in
  21. * a column is one of the swig_cast_info structures for that type.
  22. * The cast_initial array is actually an array of arrays, because each row has
  23. * a variable number of columns. So to actually build the cast linked list,
  24. * we find the array of casts associated with the type, and loop through it
  25. * adding the casts to the list. The one last trick we need to do is making
  26. * sure the type pointer in the swig_cast_info struct is correct.
  27. *
  28. * First off, we lookup the cast->type name to see if it is already loaded.
  29. * There are three cases to handle:
  30. * 1) If the cast->type has already been loaded AND the type we are adding
  31. * casting info to has not been loaded (it is in this module), THEN we
  32. * replace the cast->type pointer with the type pointer that has already
  33. * been loaded.
  34. * 2) If BOTH types (the one we are adding casting info to, and the
  35. * cast->type) are loaded, THEN the cast info has already been loaded by
  36. * the previous module so we just ignore it.
  37. * 3) Finally, if cast->type has not already been loaded, then we add that
  38. * swig_cast_info to the linked list (because the cast->type) pointer will
  39. * be correct.
  40. * ----------------------------------------------------------------------------- */
  41. #ifdef __cplusplus
  42. extern "C" {
  43. #if 0
  44. } /* c-mode */
  45. #endif
  46. #endif
  47. #if 0
  48. #define SWIGRUNTIME_DEBUG
  49. #endif
  50. SWIGRUNTIME void
  51. SWIG_InitializeModule(void *clientdata) {
  52. size_t i;
  53. swig_module_info *module_head, *iter;
  54. int init;
  55. /* check to see if the circular list has been setup, if not, set it up */
  56. if (swig_module.next==0) {
  57. /* Initialize the swig_module */
  58. swig_module.type_initial = swig_type_initial;
  59. swig_module.cast_initial = swig_cast_initial;
  60. swig_module.next = &swig_module;
  61. init = 1;
  62. } else {
  63. init = 0;
  64. }
  65. /* Try and load any already created modules */
  66. module_head = SWIG_GetModule(clientdata);
  67. if (!module_head) {
  68. /* This is the first module loaded for this interpreter */
  69. /* so set the swig module into the interpreter */
  70. SWIG_SetModule(clientdata, &swig_module);
  71. } else {
  72. /* the interpreter has loaded a SWIG module, but has it loaded this one? */
  73. iter=module_head;
  74. do {
  75. if (iter==&swig_module) {
  76. /* Our module is already in the list, so there's nothing more to do. */
  77. return;
  78. }
  79. iter=iter->next;
  80. } while (iter!= module_head);
  81. /* otherwise we must add our module into the list */
  82. swig_module.next = module_head->next;
  83. module_head->next = &swig_module;
  84. }
  85. /* When multiple interpreters are used, a module could have already been initialized in
  86. a different interpreter, but not yet have a pointer in this interpreter.
  87. In this case, we do not want to continue adding types... everything should be
  88. set up already */
  89. if (init == 0) return;
  90. /* Now work on filling in swig_module.types */
  91. #ifdef SWIGRUNTIME_DEBUG
  92. printf("SWIG_InitializeModule: size %d\n", swig_module.size);
  93. #endif
  94. for (i = 0; i < swig_module.size; ++i) {
  95. swig_type_info *type = 0;
  96. swig_type_info *ret;
  97. swig_cast_info *cast;
  98. #ifdef SWIGRUNTIME_DEBUG
  99. printf("SWIG_InitializeModule: type %d %s\n", i, swig_module.type_initial[i]->name);
  100. #endif
  101. /* if there is another module already loaded */
  102. if (swig_module.next != &swig_module) {
  103. type = SWIG_MangledTypeQueryModule(swig_module.next, &swig_module, swig_module.type_initial[i]->name);
  104. }
  105. if (type) {
  106. /* Overwrite clientdata field */
  107. #ifdef SWIGRUNTIME_DEBUG
  108. printf("SWIG_InitializeModule: found type %s\n", type->name);
  109. #endif
  110. if (swig_module.type_initial[i]->clientdata) {
  111. type->clientdata = swig_module.type_initial[i]->clientdata;
  112. #ifdef SWIGRUNTIME_DEBUG
  113. printf("SWIG_InitializeModule: found and overwrite type %s \n", type->name);
  114. #endif
  115. }
  116. } else {
  117. type = swig_module.type_initial[i];
  118. }
  119. /* Insert casting types */
  120. cast = swig_module.cast_initial[i];
  121. while (cast->type) {
  122. /* Don't need to add information already in the list */
  123. ret = 0;
  124. #ifdef SWIGRUNTIME_DEBUG
  125. printf("SWIG_InitializeModule: look cast %s\n", cast->type->name);
  126. #endif
  127. if (swig_module.next != &swig_module) {
  128. ret = SWIG_MangledTypeQueryModule(swig_module.next, &swig_module, cast->type->name);
  129. #ifdef SWIGRUNTIME_DEBUG
  130. if (ret) printf("SWIG_InitializeModule: found cast %s\n", ret->name);
  131. #endif
  132. }
  133. if (ret) {
  134. if (type == swig_module.type_initial[i]) {
  135. #ifdef SWIGRUNTIME_DEBUG
  136. printf("SWIG_InitializeModule: skip old type %s\n", ret->name);
  137. #endif
  138. cast->type = ret;
  139. ret = 0;
  140. } else {
  141. /* Check for casting already in the list */
  142. swig_cast_info *ocast = SWIG_TypeCheck(ret->name, type);
  143. #ifdef SWIGRUNTIME_DEBUG
  144. if (ocast) printf("SWIG_InitializeModule: skip old cast %s\n", ret->name);
  145. #endif
  146. if (!ocast) ret = 0;
  147. }
  148. }
  149. if (!ret) {
  150. #ifdef SWIGRUNTIME_DEBUG
  151. printf("SWIG_InitializeModule: adding cast %s\n", cast->type->name);
  152. #endif
  153. if (type->cast) {
  154. type->cast->prev = cast;
  155. cast->next = type->cast;
  156. }
  157. type->cast = cast;
  158. }
  159. cast++;
  160. }
  161. /* Set entry in modules->types array equal to the type */
  162. swig_module.types[i] = type;
  163. }
  164. swig_module.types[i] = 0;
  165. #ifdef SWIGRUNTIME_DEBUG
  166. printf("**** SWIG_InitializeModule: Cast List ******\n");
  167. for (i = 0; i < swig_module.size; ++i) {
  168. int j = 0;
  169. swig_cast_info *cast = swig_module.cast_initial[i];
  170. printf("SWIG_InitializeModule: type %d %s\n", i, swig_module.type_initial[i]->name);
  171. while (cast->type) {
  172. printf("SWIG_InitializeModule: cast type %s\n", cast->type->name);
  173. cast++;
  174. ++j;
  175. }
  176. printf("---- Total casts: %d\n",j);
  177. }
  178. printf("**** SWIG_InitializeModule: Cast List ******\n");
  179. #endif
  180. }
  181. /* This function will propagate the clientdata field of type to
  182. * any new swig_type_info structures that have been added into the list
  183. * of equivalent types. It is like calling
  184. * SWIG_TypeClientData(type, clientdata) a second time.
  185. */
  186. SWIGRUNTIME void
  187. SWIG_PropagateClientData(void) {
  188. size_t i;
  189. swig_cast_info *equiv;
  190. static int init_run = 0;
  191. if (init_run) return;
  192. init_run = 1;
  193. for (i = 0; i < swig_module.size; i++) {
  194. if (swig_module.types[i]->clientdata) {
  195. equiv = swig_module.types[i]->cast;
  196. while (equiv) {
  197. if (!equiv->converter) {
  198. if (equiv->type && !equiv->type->clientdata)
  199. SWIG_TypeClientData(equiv->type, swig_module.types[i]->clientdata);
  200. }
  201. equiv = equiv->next;
  202. }
  203. }
  204. }
  205. }
  206. #ifdef __cplusplus
  207. #if 0
  208. { /* c-mode */
  209. #endif
  210. }
  211. #endif