~ubuntu-branches/ubuntu/maverick/swig1.3/maverick

« back to all changes in this revision

Viewing changes to Examples/lua/embed3/embed3.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michael Vogt
  • Date: 2008-11-10 16:29:56 UTC
  • mfrom: (1.2.8 upstream) (2.1.3 lenny)
  • Revision ID: james.westby@ubuntu.com-20081110162956-xue6itkuqhbza87s
Tags: 1.3.36-1ubuntu1
* Merge from debian unstable, remaining changes:
  - Drop pike and libchicken-dev from the build-depends 
    (both are universe)
  - Use python2.5 instead of python2.4.
  - use php5
  - Clean Runtime/ as well.
  - debian/Rules (clean): Remove Lib/ocaml/swigp4.ml.
  - drop "--without-mzscheme", we don't have it in our build-depends

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* embed3.cpp A C++ embeded interpreter
 
2
 
 
3
This will register a C++ class with Lua, and then call a Lua function
 
4
passing C++ objects to this function.
 
5
 
 
6
*/
 
7
 
 
8
/* Deal with Microsoft's attempt at deprecating C standard runtime functions */
 
9
#if !defined(SWIG_NO_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER) && !defined(_CRT_SECURE_NO_DEPRECATE)
 
10
# define _CRT_SECURE_NO_DEPRECATE
 
11
#endif
 
12
 
 
13
/* Deal with Microsoft's attempt at deprecating methods in the standard C++ library */
 
14
#if !defined(SWIG_NO_SCL_SECURE_NO_DEPRECATE) && defined(_MSC_VER) && !defined(_SCL_SECURE_NO_DEPRECATE)
 
15
# define _SCL_SECURE_NO_DEPRECATE
 
16
#endif
 
17
 
 
18
 
 
19
#include <stdlib.h>
 
20
#include <stdio.h>
 
21
#include <string.h>
 
22
#include <stdarg.h>
 
23
 
 
24
extern "C" {
 
25
#include <lua.h>
 
26
#include <lauxlib.h>
 
27
#include <lualib.h>
 
28
}
 
29
 
 
30
#include "swigluarun.h"   // the SWIG external runtime
 
31
 
 
32
/* the SWIG wrappered library */
 
33
extern "C" int luaopen_example(lua_State*L);
 
34
 
 
35
// the code itself
 
36
#include "example.h"
 
37
 
 
38
// this code pushes a C++ pointer as well as the SWIG type onto the Lua stack
 
39
bool push_pointer(lua_State*L, void* ptr, const char* type_name, int owned = 0) {
 
40
  // task 1: get the object 'type' which is registered with SWIG
 
41
  // you need to call SWIG_TypeQuery() with the class name
 
42
  // (normally, just look in the wrapper file to get this)
 
43
  swig_type_info * pTypeInfo = SWIG_TypeQuery(L, type_name);
 
44
  if (pTypeInfo == 0)
 
45
    return false;   // error
 
46
  // task 2: push the pointer to the Lua stack
 
47
  // this requires a pointer & the type
 
48
  // the last param specifies if Lua is responsible for deleting the object
 
49
  SWIG_NewPointerObj(L, ptr, pTypeInfo, owned);
 
50
  return true;
 
51
}
 
52
 
 
53
/* This is an example of how to call the Lua function
 
54
    void onEvent(Event e) 
 
55
  its very tedious, but gives you an idea of the issues involed.
 
56
*/
 
57
int call_onEvent(lua_State *L, Event e) {
 
58
  int top;
 
59
  /* ok, here we go:
 
60
  push a, push b, call 'add' check & return res
 
61
  */
 
62
  top = lua_gettop(L);  /* for later */
 
63
  lua_pushstring(L, "onEvent");                                  /* function name */
 
64
  lua_gettable(L, LUA_GLOBALSINDEX);               /* function to be called */
 
65
  if (!lua_isfunction(L, -1)) {
 
66
    printf("[C++] error: cannot find function 'OnEvent'\n");
 
67
    lua_settop(L, top);  // reset
 
68
    return 0;
 
69
  }
 
70
  // push the event object
 
71
  push_pointer(L, &e, "Event *", 0);
 
72
  if (lua_pcall(L, 1, 0, 0) != 0)  /* call function with 1 arguments and no result */
 
73
  {
 
74
    printf("[C++] error running function `OnEvent': %s\n", lua_tostring(L, -1));
 
75
    lua_settop(L, top);  // reset
 
76
    return 0;
 
77
  }
 
78
  lua_settop(L, top);  /* reset stack */
 
79
  return 1;   // ok
 
80
}
 
81
 
 
82
 
 
83
int main(int argc, char* argv[]) {
 
84
  printf("[C++] Welcome to the simple embedded Lua example v3\n");
 
85
  printf("[C++] We are in C++\n");
 
86
  printf("[C++] opening a Lua state & loading the libraries\n");
 
87
  lua_State *L = lua_open();
 
88
  luaopen_base(L);
 
89
  luaopen_string(L);
 
90
  luaopen_math(L);
 
91
  printf("[C++] now loading the SWIG wrappered library\n");
 
92
  luaopen_example(L);
 
93
  printf("[C++] all looks ok\n");
 
94
  printf("\n");
 
95
  printf("[C++] lets create an Engine and pass a pointer to Lua\n");
 
96
  Engine engine;
 
97
  /* this code will pass a pointer into lua, but C++ still owns the object
 
98
  this is a little tedious, to do, but lets do it
 
99
  we need to pass the pointer (obviously), the type name 
 
100
  and a flag which states if Lua should delete the pointer once its finished with it
 
101
  The type name is a class name string which is registered with SWIG
 
102
  (normally, just look in the wrapper file to get this)
 
103
  in this case we don't want Lua to delete the pointer so the ownership flag is 0
 
104
  */
 
105
  push_pointer(L,&engine,"Engine *",0);
 
106
  lua_setglobal(L, "pEngine");  // set as global variable
 
107
 
 
108
  printf("[C++] now lets load the file 'runme.lua'\n");
 
109
  printf("[C++] any lua code in this file will be executed\n");
 
110
  if (luaL_loadfile(L, "runme.lua") || lua_pcall(L, 0, 0, 0)) {
 
111
    printf("[C++] ERROR: cannot run lua file: %s", lua_tostring(L, -1));
 
112
    exit(3);
 
113
  }
 
114
  printf("[C++] We are now back in C++, all looks ok\n");
 
115
  printf("\n");
 
116
 
 
117
  printf("[C++] Lets call the Lua function onEvent(e)\n");
 
118
  printf("[C++] We will give it different events, as we wish\n");
 
119
  printf("[C++] Starting with STARTUP\n");
 
120
  Event ev;
 
121
  ev.mType = Event::STARTUP;
 
122
  call_onEvent(L, ev);
 
123
  printf("[C++] ok\n");
 
124
  printf("[C++] now we will try MOUSEPRESS,KEYPRESS,MOUSEPRESS\n");
 
125
  ev.mType = Event::MOUSEPRESS;
 
126
  call_onEvent(L, ev);
 
127
  ev.mType = Event::KEYPRESS;
 
128
  call_onEvent(L, ev);
 
129
  ev.mType = Event::MOUSEPRESS;
 
130
  call_onEvent(L, ev);
 
131
  printf("[C++] ok\n");
 
132
  printf("[C++] Finally we will SHUTDOWN\n");
 
133
  ev.mType = Event::SHUTDOWN;
 
134
  call_onEvent(L, ev);
 
135
  printf("[C++] ok\n");
 
136
 
 
137
  printf("\n");
 
138
  printf("[C++] all finished, closing the lua state\n");
 
139
  lua_close(L);
 
140
  return 0;
 
141
}