PageRenderTime 25ms CodeModel.GetById 2ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 0ms

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

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