PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Examples/lua/dual/dual.cpp

#
C++ | 109 lines | 68 code | 16 blank | 25 comment | 1 complexity | 71ecb70f671b0c34c6d972d636616761 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. /*
  2. dual.cpp a test for multiple modules and multiple intrepreters staticly linked together.
  3. Earlier version of lua bindings for SWIG would fail if staticly linked.
  4. What is happening is as follows:
  5. example.i declares a type Foo
  6. examples2.i declares Bar
  7. The first lua state will load example.i
  8. and check to see if types Foo and Bar are registered with it
  9. (Foo should be & Bar should not)
  10. The second lua state will load example2.i
  11. and check to see if types Foo and Bar are registered with it
  12. (Bar should be & Foo should not)
  13. Note: Though both the modules exist and are loaded, they are not linked together,
  14. as they are connected to seperate lua interpreters.
  15. When the third lua state loads both example.i and example2.i,
  16. the two modules are now linked together, and all can now find
  17. both Foo and Bar.
  18. */
  19. #include "swigluarun.h" // the swig runtimes
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. // the 2 libraries which are wrappered via SWIG
  23. extern "C" int luaopen_example(lua_State*L);
  24. extern "C" int luaopen_example2(lua_State*L);
  25. #define DEBUG(X) {printf(X);fflush(stdout);}
  26. #define DEBUG2(X,Y) {printf(X,Y);fflush(stdout);}
  27. #define DEBUG3(X,Y,Z) {printf(X,Y,Z);fflush(stdout);}
  28. void testModule(lua_State *L)
  29. {
  30. swig_type_info *pTypeInfo=0,*pTypeInfo2=0;
  31. swig_module_info *pModule=0;
  32. pModule=SWIG_GetModule(L);
  33. DEBUG2(" SWIG_GetModule() returns %p\n",pModule)
  34. if(pModule==0) return;
  35. pTypeInfo = SWIG_TypeQuery(L,"Foo *");
  36. DEBUG2(" Type (Foo*) is %s\n",pTypeInfo==0?"unknown":"known");
  37. DEBUG3(" Module %p typeinfo(Foo*) %p\n",pModule,pTypeInfo);
  38. pTypeInfo2 = SWIG_TypeQuery(L,"Bar *");
  39. DEBUG2(" Type (Bar*) is %s\n",pTypeInfo2==0?"unknown":"known");
  40. DEBUG3(" Module %p typeinfo(Bar*) %p\n",pModule,pTypeInfo2);
  41. }
  42. int main(int argc,char* argv[])
  43. {
  44. lua_State *L1=0,*L2=0,*L3=0;
  45. printf("This is a test of having two SWIG'ed modules and three lua states\n"
  46. "statically linked together.\n"
  47. "Its mainly to check that all the types are correctly managed\n\n");
  48. DEBUG("creating lua states(L1,L2,L3)");
  49. L1=lua_open();
  50. L2=lua_open();
  51. L3=lua_open();
  52. DEBUG("ok\n\n");
  53. DEBUG("luaopen_example(L1)..");
  54. luaopen_example(L1);
  55. DEBUG("ok\n");
  56. DEBUG("Testing Module L1\n");
  57. DEBUG("This module should know about Foo* but not Bar*\n");
  58. testModule(L1);
  59. DEBUG("End Testing Module L1\n\n");
  60. DEBUG("luaopen_example2(L2)..");
  61. luaopen_example2(L2);
  62. DEBUG("ok\n");
  63. DEBUG("Testing Module L2\n");
  64. DEBUG("This module should know about Bar* but not Foo*\n");
  65. testModule(L2);
  66. DEBUG("End Testing Module L2\n\n");
  67. DEBUG("luaopen_example(L3)..");
  68. luaopen_example(L3);
  69. DEBUG("ok\n");
  70. DEBUG("luaopen_example2(L3)..");
  71. luaopen_example2(L3);
  72. DEBUG("ok\n");
  73. DEBUG("Testing Module L3\n");
  74. DEBUG("This module should know about Foo* and Bar*\n");
  75. testModule(L3);
  76. DEBUG("End Testing Module L3\n\n");
  77. DEBUG("Testing Module L1 again\n");
  78. DEBUG("It now should know about Foo* and Bar*\n");
  79. testModule(L1);
  80. DEBUG("End Testing Module L1 again\n\n");
  81. DEBUG("close all..");
  82. lua_close(L1);
  83. lua_close(L2);
  84. lua_close(L3);
  85. DEBUG("ok, exiting\n");
  86. return 0;
  87. }