00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00015
00016 #ifndef header_lua_lib
00017 #define header_lua_lib
00018
00019 extern "C" {
00020 #define LUA_COMPAT2_5 // to include old Lua macros
00021 #include <lua.h>
00022 #include <lualib.h>
00023 }
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class CL_Lua;
00036 class CL_LuaValue;
00037 class CL_LuaObject;
00038 class CL_LuaValue
00039
00040
00041
00042
00043
00044
00045
00046 {
00047
00048
00049
00050 CL_LuaValue (const CL_LuaObject&);
00051
00052
00053
00054 CL_LuaValue& operator= (const CL_LuaObject&);
00055
00056
00057 protected:
00058 enum {
00059 LUA_PP_USERDATA=0,
00060 LUA_PP_NIL,
00061 LUA_PP_NUMBER,
00062 LUA_PP_STRING,
00063 LUA_PP_CFUNCTION,
00064 LUA_PP_REFERENCE
00065 };
00066
00067 int d_type;
00068 union {
00069 float d_num;
00070 char* d_str;
00071 const char* d_cstr;
00072 lua_CFunction d_fnc;
00073 void* d_ptr;
00074 const void* d_cptr;
00075 lua_Object d_lo;
00076 int d_ref;
00077 };
00078
00079
00080 CL_LuaValue (int ref, int mark) : d_type(mark), d_ref(ref) {}
00081
00082 public:
00083
00084
00085
00086
00087
00088
00089 CL_LuaValue (void) : d_type(LUA_PP_NIL) {}
00090
00091
00092 CL_LuaValue (int v) : d_type(LUA_PP_NUMBER), d_num(v) {}
00093
00094
00095 CL_LuaValue (float v) : d_type(LUA_PP_NUMBER), d_num(v) {}
00096
00097
00098 CL_LuaValue (double v) : d_type(LUA_PP_NUMBER), d_num(v) {}
00099
00100
00101 CL_LuaValue (char* v) : d_type(LUA_PP_STRING), d_str(v) {}
00102
00103
00104 CL_LuaValue (const char* v) : d_type(LUA_PP_STRING), d_cstr(v) {}
00105
00106
00107 CL_LuaValue (lua_CFunction v) : d_type(LUA_PP_CFUNCTION),d_fnc(v) {}
00108
00109
00110 CL_LuaValue (void* v, int tag=LUA_PP_USERDATA) : d_type(tag), d_ptr(v) {}
00111
00112
00113 CL_LuaValue (const void* v, int tag=LUA_PP_USERDATA) : d_type(tag), d_cptr(v) {}
00114
00115
00116 CL_LuaValue (const CL_LuaValue& v) { *this = v; }
00117
00118
00119 CL_LuaValue& operator= (const CL_LuaValue& v)
00120 {
00121 d_type = v.d_type;
00122 d_num = v.d_num;
00123 return *this;
00124 }
00125
00126 virtual ~CL_LuaValue (void) {}
00127
00128
00129 virtual void push (void) const
00130 {
00131 switch (d_type)
00132 {
00133 case LUA_PP_NIL: lua_pushnil(); break;
00134 case LUA_PP_NUMBER: lua_pushnumber(d_num); break;
00135 case LUA_PP_STRING: lua_pushstring((char*)d_str); break;
00136 case LUA_PP_CFUNCTION: lua_pushcfunction(d_fnc); break;
00137 default: lua_pushusertag((void*)d_ptr,d_type); break;
00138 }
00139 }
00140
00141
00142 void get (char *varname)
00143 {
00144 lua_Object obj(lua_getglobal(varname));
00145 switch (d_type)
00146 {
00147 case LUA_PP_NIL: lua_pop(); break;
00148 case LUA_PP_NUMBER: d_num=lua_getnumber(obj); break;
00149 case LUA_PP_STRING: d_str=lua_getstring(obj); break;
00150 case LUA_PP_CFUNCTION: d_fnc=lua_getcfunction(obj); break;
00151 default: d_ptr=lua_getuserdata(obj); break;
00152 }
00153 }
00154 void store (char* varname)
00155 {
00156 push();
00157 lua_storeglobal(varname);
00158 }
00159
00160
00161
00162
00163
00164
00165 virtual int isNil(void) const { return type()==LUA_PP_NIL; }
00166
00167
00168 virtual int isNumber(void) const { return type()==LUA_PP_NUMBER; }
00169
00170
00171 virtual int isString(void) const { return type()==LUA_PP_STRING; }
00172
00173
00174 virtual int isTable(void) const { return 0; }
00175
00176
00177 virtual int isFunction(void) const { return type()==LUA_PP_CFUNCTION; }
00178
00179
00180 virtual int isUserData(void) const { return type()>=LUA_PP_USERDATA; }
00181
00182
00183 virtual int type(void) const { return d_type; }
00184
00185
00186
00187
00188
00189 virtual operator float() const
00190 { return d_type==LUA_PP_NUMBER ? d_num : 0; }
00191
00192 virtual operator char*() const
00193 { return d_type==LUA_PP_STRING ? (char*)d_str : 0; }
00194
00195
00196 virtual operator const char*() const
00197 { return d_type==LUA_PP_STRING ? d_str : 0; }
00198
00199
00200 virtual operator void*() const
00201 { return d_type>=LUA_PP_USERDATA ? (void*)d_ptr : 0; }
00202
00203
00204 virtual operator const void*() const
00205 { return d_type>=LUA_PP_USERDATA ? d_ptr : 0; }
00206
00207 };
00208
00209
00210 class CL_LuaObject : public CL_LuaValue
00211
00212
00213 {
00214
00215 lua_Object d_table;
00216
00217
00218 CL_LuaObject (lua_Object lo, const CL_LuaValue& v)
00219 : CL_LuaValue(v), d_table(lo) {}
00220
00221 protected:
00222
00223 friend class CL_Lua;
00224
00225 CL_LuaObject (lua_Object lo)
00226 : d_table(LUA_NOOBJECT) { d_lo = lo; }
00227
00228
00229 CL_LuaObject (int ref, int mark)
00230 : CL_LuaValue(ref,mark), d_table(LUA_NOOBJECT) {}
00231
00232
00233 public:
00234
00235
00236 CL_LuaObject (void)
00237 : CL_LuaValue(-1,LUA_PP_REFERENCE), d_table(LUA_NOOBJECT) {}
00238
00239
00240
00241
00242
00243 CL_LuaObject (const CL_LuaObject& org) { *this = org; }
00244
00245 virtual ~CL_LuaObject (void) {}
00246
00247 CL_LuaObject& operator= (const CL_LuaObject& value)
00248 {
00249 d_table = value.d_table;
00250 CL_LuaValue& base1 = *this;
00251 const CL_LuaValue& base2 = value;
00252 base1 = base2;
00253 return *this;
00254 }
00255
00256
00257 CL_LuaObject& operator= (const CL_LuaValue& value)
00258 {
00259 if (d_table != LUA_NOOBJECT)
00260 {
00261 lua_beginblock();
00262 lua_pushobject(d_table);
00263 CL_LuaValue::push();
00264 value.push();
00265 lua_storesubscript();
00266 lua_endblock();
00267 }
00268 else
00269 {
00270 lua_error("lua++: cannot assign a CL_LuaValue to a CL_LuaObject.");
00271 }
00272 return *this;
00273 }
00274
00275
00276 virtual lua_Object getobject (void) const
00277 {
00278 if (d_table == LUA_NOOBJECT)
00279 {
00280 if (d_type == LUA_PP_REFERENCE) return lua_getref(d_ref);
00281 else return d_lo;
00282 }
00283 else
00284 {
00285 lua_pushobject(d_table);
00286 CL_LuaValue::push();
00287 return lua_getsubscript();
00288 }
00289 }
00290
00291
00292
00293 void push (void) const
00294 {
00295 if (d_table == LUA_NOOBJECT)
00296 {
00297 if (d_type == LUA_PP_REFERENCE) lua_pushref(d_ref);
00298 else lua_pushobject(d_lo);
00299 }
00300 else
00301 {
00302 lua_beginblock();
00303 lua_pushobject(d_table);
00304 CL_LuaValue::push();
00305 lua_Object lo = lua_getsubscript();
00306 lua_endblock();
00307 lua_pushobject(lo);
00308 }
00309 }
00310
00311
00312 int isNil (void) const
00313 {
00314 lua_beginblock();
00315 int result = lua_isnil(getobject());
00316 lua_endblock();
00317 return result;
00318 }
00319
00320 int isNumber (void) const
00321 {
00322 lua_beginblock();
00323 int result = lua_isnumber(getobject());
00324 lua_endblock();
00325 return result;
00326 }
00327
00328 int isString (void) const
00329 {
00330 lua_beginblock();
00331 int result = lua_isstring(getobject());
00332 lua_endblock();
00333 return result;
00334 }
00335
00336 int isFunction (void) const
00337 {
00338 lua_beginblock();
00339 int result = lua_isfunction(getobject());
00340 lua_endblock();
00341 return result;
00342 }
00343
00344 int isUserData (void) const
00345 {
00346 lua_beginblock();
00347 int result = lua_isuserdata(getobject());
00348 lua_endblock();
00349 return result;
00350 }
00351
00352 int isTable (void) const
00353 {
00354 lua_beginblock();
00355 int result = lua_istable(getobject());
00356 lua_endblock();
00357 return result;
00358 }
00359
00360 int type (void) const
00361 {
00362 lua_beginblock();
00363 int result = lua_type(getobject());
00364 lua_endblock();
00365 return result;
00366 }
00367
00368 operator float() const
00369 {
00370 lua_beginblock();
00371 lua_Object lo = getobject();
00372 float res = lua_getnumber(lo);
00373 lua_endblock();
00374 return res;
00375 }
00376
00377
00378 operator char*() const
00379 {
00380 lua_beginblock();
00381 lua_Object lo = getobject();
00382 char* res = lua_getstring(lo);
00383 lua_endblock();
00384 return res;
00385 }
00386
00387
00388 operator const char*() const
00389 {
00390 lua_beginblock();
00391 lua_Object lo = getobject();
00392 const char* res = lua_getstring(lo);
00393 lua_endblock();
00394 return res;
00395 }
00396
00397
00398 operator void*() const
00399 {
00400 lua_beginblock();
00401 lua_Object lo = getobject();
00402 void* res = lua_getuserdata(lo);
00403 lua_endblock();
00404 return res;
00405 }
00406
00407
00408 operator const void*() const
00409 {
00410 lua_beginblock();
00411 lua_Object lo = getobject();
00412 const void* res = lua_getuserdata(lo);
00413 lua_endblock();
00414 return res;
00415 }
00416
00417
00418
00419
00420 CL_LuaObject operator[] (const CL_LuaValue& index)
00421 {
00422 return CL_LuaObject(getobject(),index);
00423 }
00424
00425
00426
00427
00428
00429 CL_LuaObject operator[] (int index)
00430 {
00431 return CL_LuaObject(getobject(),CL_LuaValue(index));
00432 }
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443 int operator() (void) const
00444 {
00445 lua_Object lo = getobject();
00446 return lua_callfunction(lo);
00447 }
00448
00449
00450 int operator() (const CL_LuaValue& arg1)
00451 {
00452 lua_Object lo = getobject();
00453 arg1.push();
00454 return lua_callfunction(lo);
00455 }
00456
00457
00458 int operator() (const CL_LuaValue& arg1, const CL_LuaValue& arg2)
00459 {
00460 lua_Object lo = getobject();
00461 arg1.push(); arg2.push();
00462 return lua_callfunction(lo);
00463 }
00464
00465
00466 int operator() (const CL_LuaValue& arg1,
00467 const CL_LuaValue& arg2,
00468 const CL_LuaValue& arg3)
00469 {
00470 lua_Object lo = getobject();
00471 arg1.push(); arg2.push(); arg3.push();
00472 return lua_callfunction(lo);
00473 }
00474
00475
00476 int operator() (const CL_LuaValue& arg1,
00477 const CL_LuaValue& arg2,
00478 const CL_LuaValue& arg3,
00479 const CL_LuaValue& arg4)
00480 {
00481 lua_Object lo = getobject();
00482 arg1.push(); arg2.push(); arg3.push(); arg4.push();
00483 return lua_callfunction(lo);
00484 }
00485
00486
00487 int operator() (const CL_LuaValue& arg1,
00488 const CL_LuaValue& arg2,
00489 const CL_LuaValue& arg3,
00490 const CL_LuaValue& arg4,
00491 const CL_LuaValue& arg5)
00492 {
00493 lua_Object lo = getobject();
00494 arg1.push(); arg2.push(); arg3.push(); arg4.push(); arg5.push();
00495 return lua_callfunction(lo);
00496 }
00497
00498 };
00499
00500
00501 class CL_LuaTable : public CL_LuaObject
00502
00503
00504 {
00505 public:
00506 CL_LuaTable (void)
00507 : CL_LuaObject(lua_createtable()) {}
00508
00509
00510
00511 ~CL_LuaTable (void) {}
00512 };
00513
00514
00515
00516 class CL_LuaReference : public CL_LuaObject
00517
00518
00519
00520
00521 {
00522 public:
00523 CL_LuaReference (void)
00524 : CL_LuaObject() {}
00525
00526
00527 CL_LuaReference (const CL_LuaObject& obj, int lock=0)
00528 : CL_LuaObject()
00529 {
00530 ref(obj,lock);
00531 }
00532
00533
00534
00535
00536
00538
00539 void ref (const CL_LuaObject& obj, int lock=0)
00540 {
00541 lua_beginblock();
00542 obj.push();
00543 d_ref = lua_ref(lock);
00544 lua_endblock();
00545 }
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556 void unref (void)
00557 {
00558 if (d_type == LUA_PP_REFERENCE)
00559 lua_unref(d_ref);
00560 d_ref = -1;
00561 }
00562
00563 };
00564
00565
00566 class CL_Lua
00567
00568
00569
00570 {
00571 public:
00572 static int dofile (char *filename)
00573 { return lua_dofile(filename); }
00574
00577
00578 static int dostring (char *string)
00579 { return lua_dostring(string); }
00580
00583
00584 static void error (char* message)
00585 { lua_error(message); }
00586
00588
00589 static CL_LuaObject getparam (int number)
00590 { return CL_LuaObject(lua_getparam(number)); }
00591
00594
00595 static CL_LuaObject getresult (int number)
00596 { return CL_LuaObject(lua_getresult(number)); }
00597
00600
00601 static CL_LuaObject getglobal (char* var)
00602 {
00603 return CL_LuaObject(lua_getglobal(var));
00604 }
00605
00606
00609
00610 static CL_LuaObject setfallback (char* name, lua_CFunction fallback)
00611 { return CL_LuaObject(lua_setfallback(name,fallback)); }
00612
00616
00617 static void record (char* name, lua_CFunction func)
00618 {
00619 lua_beginblock();
00620 lua_register(name,func);
00621 lua_endblock();
00622 }
00623
00626
00627 static CL_LuaObject createObject (lua_Object lo)
00628 { return CL_LuaObject(lo); }
00629
00632
00633 static void close(){lua_close();}
00634 static void iolibopen(void) { lua_iolibopen(); }
00635
00636
00637 static void strlibopen(void) { lua_strlibopen(); }
00638
00639
00640 static void mathlibopen(void) { lua_mathlibopen(); }
00641
00642
00643 static void dblibopen(void) { lua_dblibopen(); }
00644
00645 };
00646
00647 #endif
00648
00649
00650
00651
00652
00653
00654
00655