Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

lua.h

Go to the documentation of this file.
00001 /*
00002         
00003          ------------------------------------------------------------------------
00004          ClanLib, the platform independent game SDK.
00005 
00006          This library is distributed under the GNU LIBRARY GENERAL PUBLIC LICENSE
00007          version 2. See COPYING for details.
00008 
00009          For a total list of contributers see CREDITS.
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 /* These c++ classes were created by Waldemer Celes. Additions and changes were made
00026 *  to adapt it to ClanLib.
00027 */
00028 
00029 //* Lua++: Lua API for C++
00030 // These classes implement an API for accessing Lua from C++ code.
00031 // It is based on the Lua 3.0a API for C programs.
00032 // Developed by Waldemar Celes.
00033 // May 1997 - version 1.1 (beta)
00034 
00035 class CL_Lua;
00036 class CL_LuaValue;
00037 class CL_LuaObject;
00038 class CL_LuaValue
00039 //: Lua value
00040 // Represents a value that can be passed to Lua.
00041 // When calling Lua functions or indexing Lua tables,
00042 // objects of this class are expected.
00043 // However, they are implicitly constructed from C++ primitive types.
00044 // Thus, in general,
00045 // there is no need to directly create objects of this class.
00046 {
00047                         //* Copying from LuaObject
00048                         // To convert a LuaValue from a LuaObject
00049                         // is _not_ allowed.
00050         CL_LuaValue (const CL_LuaObject&);  // not implemented
00051 
00052                         //* Assigning a LuaObject
00053                         // Assigning a LuaObject is _not_ allowed.
00054         CL_LuaValue& operator= (const CL_LuaObject&); // not implemented
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;   // used by derived class
00076                 int           d_ref;  // used by derived class
00077         };
00078 
00079                         // Protected constructor to create references
00080         CL_LuaValue (int ref, int mark) : d_type(mark), d_ref(ref) {}
00081 
00082 public:
00083  //* CREATORS
00084  // The following constructors are available,
00085  // allowing automatic convertion from
00086  // C++ primitive types.
00087 
00088 
00089         CL_LuaValue (void) : d_type(LUA_PP_NIL) {}
00090                         //: Create a 'nil' object
00091 
00092         CL_LuaValue (int v) : d_type(LUA_PP_NUMBER),   d_num(v)  {}
00093                         //: Create a 'number' from an 'int'
00094 
00095         CL_LuaValue (float v) : d_type(LUA_PP_NUMBER),   d_num(v)  {}
00096                         //: Create a 'number' from a 'float'
00097 
00098         CL_LuaValue (double v) : d_type(LUA_PP_NUMBER),   d_num(v)  {}
00099                         //: Create a 'number' from a 'double'
00100 
00101         CL_LuaValue (char* v) : d_type(LUA_PP_STRING),   d_str(v)  {}
00102                         //: Create a 'string' from a 'char*'
00103 
00104         CL_LuaValue (const char* v) : d_type(LUA_PP_STRING),   d_cstr(v) {}
00105                         //: Create a 'string' from a 'const char*'
00106 
00107         CL_LuaValue (lua_CFunction v) : d_type(LUA_PP_CFUNCTION),d_fnc(v)  {}
00108                         //: Create a 'cfunction' from a 'lua_CFunction}
00109 
00110         CL_LuaValue (void* v, int tag=LUA_PP_USERDATA) : d_type(tag), d_ptr(v)  {}
00111                         //: Create a 'userdata' from a 'void*'
00112 
00113         CL_LuaValue (const void* v, int tag=LUA_PP_USERDATA) : d_type(tag), d_cptr(v) {}
00114                         //: Create a 'userdata' from a 'const void*'
00115 
00116         CL_LuaValue (const CL_LuaValue& v)  { *this = v; }
00117                         //: Copy and assignment 
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  //* MANIPULATORS
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                         //: Push
00141                         // Pushs the value onto Lua stack.
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                         //: Store
00160                         // Stores the value in a given Lua global variable.
00161 
00162  //* ACCESSORS
00163 
00164 
00165         virtual int isNil(void) const { return type()==LUA_PP_NIL; }
00166                         //: Query if 'nil'      
00167 
00168         virtual int isNumber(void) const { return type()==LUA_PP_NUMBER; }
00169                         //: Query if 'number'
00170 
00171         virtual int isString(void) const { return type()==LUA_PP_STRING; }
00172                         //: Query if 'string'   
00173 
00174         virtual int isTable(void) const { return 0; } // never happens
00175                         //: Query if 'table'
00176 
00177         virtual int isFunction(void) const { return type()==LUA_PP_CFUNCTION; }
00178                         //: Query if 'function'
00179 
00180         virtual int isUserData(void) const { return type()>=LUA_PP_USERDATA; }
00181                         //: Query if 'userdata'
00182 
00183         virtual int type(void) const { return d_type; }
00184                         //: Return the corresponding Lua type
00185  //* CONVERSORS
00186  // Allow conversion to C++ primitive types.
00187 
00188 
00189         virtual operator float() const
00190                 { return d_type==LUA_PP_NUMBER ? d_num : 0; }
00191                         //: Convert to 'float'
00192         virtual operator char*() const
00193                 { return d_type==LUA_PP_STRING ? (char*)d_str : 0; }
00194                         //: Convert to {char*}
00195 
00196         virtual operator const char*() const
00197                 { return d_type==LUA_PP_STRING ? d_str : 0; }
00198                         //: Convert to {const char*}
00199 
00200         virtual operator void*() const
00201                 { return d_type>=LUA_PP_USERDATA ? (void*)d_ptr : 0; }
00202                         //: Convert to {void*}          
00203 
00204         virtual operator const void*() const
00205                 { return d_type>=LUA_PP_USERDATA ? d_ptr : 0; }
00206                         //: Convert to {const void*}
00207 };
00208 
00209 
00210 class CL_LuaObject : public CL_LuaValue
00211 //: Lua object
00212 // Represents a general Lua object, that is, an object stored at Lua stack.
00213 {
00214 
00215         lua_Object d_table;    // used for subsructor to create subscript
00216 
00217                         // Private constructor to create subscripts
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 //: Protected constructor to create objects from the stack values
00228         
00229         CL_LuaObject (int ref, int mark)
00230                 : CL_LuaValue(ref,mark), d_table(LUA_NOOBJECT) {}
00231 //: Protected constructor to create references
00232 
00233 public:
00234  //* CREATORS
00235 
00236         CL_LuaObject (void)
00237                 : CL_LuaValue(-1,LUA_PP_REFERENCE), d_table(LUA_NOOBJECT) {}
00238 //: Create a 'nil' object (default)
00239 // After creating a 'nil' object,
00240 // we use the Lua class methods to
00241 // access values from the Lua stack     
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 //: operator=
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);     // push table
00263                         CL_LuaValue::push();            // push index
00264                         value.push();                // push value
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 //: operator=
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 //: Get the corresponding Lua object
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 //: Base virtual functions
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 //: Convert lua value to ordinary C types
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 //: Convert lua value to ordinary C types
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 //: Convert lua value to ordinary C types
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 //: Convert lua value to ordinary C types
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 //: Convert lua value to ordinary C types
00417 
00418  // TABLES
00419 
00420         CL_LuaObject operator[] (const CL_LuaValue& index)
00421         {
00422                 return CL_LuaObject(getobject(),index);
00423         }
00424                         //: Subscription
00425                         // If the object is a Lua table,
00426                         // the {operator[]} can be used to indexed the Lua
00427                         // corresponding value.
00428 
00429         CL_LuaObject operator[] (int index)
00430         {
00431                 return CL_LuaObject(getobject(),CL_LuaValue(index));
00432         }
00433                         //: Subscription
00434                         // If the object is a Lua table,
00435                         // the {operator[]} can be used to indexed the Lua
00436                         // corresponding value. 
00437 
00438  // FUNCTIONS
00439  // If the object is a Lua function,
00440  // the operator () can be used to call it.
00441 
00442 
00443         int operator() (void) const
00444         {
00445                 lua_Object lo = getobject();
00446                 return lua_callfunction(lo);
00447         }
00448                         //: Call function with no argument
00449 
00450         int operator() (const CL_LuaValue& arg1)
00451         {
00452                 lua_Object lo = getobject();
00453                 arg1.push();
00454                 return lua_callfunction(lo);
00455         }
00456                         //: Call function with 1 argument
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                         //: Call function with 2 arguments
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                         //: Call function with 3 arguments
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                         //: Call function with 4 arguments
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                         //: Call function with 5 arguments
00498 };
00499 
00500 
00501 class CL_LuaTable : public CL_LuaObject
00502 //: Lua table
00503 // Allows a Lua table to be explicitly created from C++ code.
00504 {
00505 public:
00506         CL_LuaTable (void)
00507                 : CL_LuaObject(lua_createtable()) {}
00508 //: Creator
00509 // An initialy empty Lua table is created.
00510 
00511         ~CL_LuaTable (void) {}
00512 };
00513 
00514 
00515 
00516 class CL_LuaReference : public CL_LuaObject
00517 //: Lua reference
00518 // If the user needs to store references to LuaObject,
00519 // this reference must be explicitly created, creating objects of
00520 // this class.
00521 {
00522 public:
00523         CL_LuaReference (void)
00524         : CL_LuaObject() {}
00525 //: Create a reference to a 'nil' object (default)
00526 
00527         CL_LuaReference (const CL_LuaObject& obj, int lock=0)
00528         :  CL_LuaObject()
00529         {
00530                 ref(obj,lock);
00531         }
00532 //: Create a reference to the given Lua object
00533 // An optinal parameter specifies if the
00534 // reference is not to be garbage collected by Lua.
00535 // The default, {lock=0}, means the reference becomes
00536 // invalid if Lua collects the referenced object.
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 //: Replace the reference
00547 // This method allows the user to replace the
00548 // referenced object; it is useful when the object
00549 // is created with the default constructor.
00550 // _Note_: if there is already a reference to a
00551 // non-'nil' object, this reference, if not used
00552 // by other LuaReference objets, should be
00553 // eliminated (using the 'unref' method) before
00554 // calling this method.
00555 
00556         void unref (void)
00557         {
00558                 if (d_type == LUA_PP_REFERENCE)
00559                         lua_unref(d_ref);
00560                 d_ref = -1;
00561         }
00562 //: Unreference an object
00563 };
00564 
00565 
00566 class CL_Lua
00567 //: CL_Lua class
00568 // Binds global functions of Lua API.
00569 // Refer to Lua manual for details.
00570 {
00571 public:
00572         static int dofile (char *filename)
00573                 { return lua_dofile(filename); }
00574 //: Execute a Lua chunk stored in a file
00577 
00578         static int dostring (char *string)
00579                 { return lua_dostring(string); }
00580 //: Execute a Lua chunk stored in a string
00583 
00584         static void error (char* message)
00585                 { lua_error(message); }
00586 //: Generate a Lua error, with the given message
00588 
00589         static CL_LuaObject getparam (int number)
00590                 { return CL_LuaObject(lua_getparam(number)); }
00591 //: Get a parameter from Lua stack
00594 
00595         static CL_LuaObject getresult (int number)
00596                 { return CL_LuaObject(lua_getresult(number)); }
00597 //: Get a result from Lua stack
00600 
00601         static CL_LuaObject getglobal (char* var)
00602                 {
00603                         return CL_LuaObject(lua_getglobal(var));
00604                 }
00605                 
00606 //: Get a Lua global variable
00609 
00610         static CL_LuaObject setfallback (char* name, lua_CFunction fallback)
00611                 { return CL_LuaObject(lua_setfallback(name,fallback)); }
00612 //: Set Lua fallback            
00616 
00617         static void record (char* name, lua_CFunction func)
00618         {
00619                 lua_beginblock();
00620                 lua_register(name,func);
00621                 lua_endblock();
00622         }
00623 //: Register a C++ function to be called from Lua
00626 
00627         static CL_LuaObject createObject (lua_Object lo)
00628                 { return CL_LuaObject(lo); }
00629 //: Create a LuaObject from a lua_Object
00632         static void open(){lua_open();}
00633         static void close(){lua_close();}
00634         static void iolibopen(void) { lua_iolibopen(); }
00635 //: Open the IO library
00636 
00637         static void strlibopen(void) { lua_strlibopen(); }
00638 //: Open the string library
00639 
00640         static void mathlibopen(void) { lua_mathlibopen(); }
00641 //: Open the math library
00642 
00643         static void dblibopen(void) { lua_dblibopen(); }
00644 //: Open the db library
00645 };
00646 
00647 #endif
00648 
00649 
00650 
00651 
00652 
00653 
00654 
00655 

Generated at Wed Apr 4 19:54:01 2001 for ClanLib by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001