Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/script_engine/src/lib/script_engine/account.cc @ 7975

Last change on this file since 7975 was 7975, checked in by snellen, 18 years ago

works finally

File size: 2.0 KB
Line 
1    #include "luaincl.h"
2    #include <iostream>
3
4    #include "lunar.h"
5
6    class Account {
7      lua_Number m_balance;
8    public:
9      static const char className[];
10      static Lunar<Account>::RegType methods[];
11
12      Account(lua_State *L)      { m_balance = luaL_checknumber(L, 1); }
13      int deposit (lua_State *L) { m_balance += luaL_checknumber(L, 1); return 0; }
14      int withdraw(lua_State *L) { m_balance -= luaL_checknumber(L, 1); return 0; }
15      int balance (lua_State *L) { lua_pushnumber(L, m_balance); return 1; }
16      ~Account() { printf("deleted Account (%p)\n", this); }
17    };
18
19    const char Account::className[] = "Account";
20
21
22    #define method(class, name) {#name, &class::name}
23
24    Lunar<Account>::RegType Account::methods[] = {
25      method(Account, deposit),
26      method(Account, withdraw),
27      method(Account, balance),
28      {0,0}
29    };
30
31
32    class Object {
33      public:
34        static const char className[];
35        static Lunar<Object>::RegType methods[];
36
37        Object(lua_State* L) {callCount = 0; }
38        ~Object() { printf("deleted Object (%p)\n", this); }
39
40        int printName(lua_State* L)
41        {
42          printf("Hi i'm object %p ! This is the %i. call.\n",this,callCount);
43          callCount ++;
44          return 0;
45        }
46
47
48      private:
49        int callCount;
50
51    };
52
53    const char Object::className[] = "Object";
54
55    Lunar<Object>::RegType Object::methods[] = {
56      method(Object, printName),
57      {0,0}
58    };
59
60    int main(int argc, char *argv[])
61    {
62      lua_State *L = lua_open();
63
64      luaopen_base(L);
65      luaopen_table(L);
66      luaopen_io(L);
67      luaopen_string(L);
68      luaopen_math(L);
69      luaopen_debug(L);
70
71      Lunar<Account>::Register(L);
72      Lunar<Object>::Register(L);
73
74      Object* obj= new Object(L);
75      Lunar<Object>::insertObject(L,obj,"Obj",false);
76      //delete obj;
77
78      if(argc>1) lua_dofile(L, argv[1]);
79
80      lua_setgcthreshold(L, 0);  // collected garbage
81      lua_close(L);
82      return 0;
83    }
84
Note: See TracBrowser for help on using the repository browser.