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

LuaState.h

Go to the documentation of this file.
00001 
00002 // This source file is part of the LuaState (Lua Wrapper) source distribution
00003 // and is Copyright 2000-2001 by Joshua C. Jensen (jjensen@workspacewhiz.com).
00004 // The latest version may be obtained from http://www.workspacewhiz.com/.
00005 //
00006 // The code presented in this file may be freely used and modified for all
00007 // non-commercial and commercial purposes.
00009 #pragma once
00010 
00011 #include <stdarg.h>
00012 #include <wchar.h>
00013 #include <assert.h>
00014 
00015 #pragma warning(disable: 4127)
00016 
00017 extern "C" {
00018 #define LUA_PRIVATE
00019 
00020 #include "Lua.h"
00021 #include "luadebug.h"
00022 #include "lauxlib.h"
00023 }
00024 
00025 class LuaStateOutFile;
00026 class LuaState;
00027 
00028 typedef lua_CFunction LuaCFunction;
00029 typedef int (*LuaStateCFunction)(LuaState state);
00030 
00035 struct luaState_reg
00036 {
00037   const lua_char *name;
00038   union
00039   {
00040       LuaStateCFunction luaStateCFunction;
00041       lua_CFunction luaCFunction;
00042   };
00043 };
00044 
00045 typedef luaState_reg LuaFunctionList;
00046 
00050 
00053 class LUA_API LuaObject
00054 {
00055 public:
00059     LuaObject(lua_State* state, int stackIndex) :
00060         m_state(state),
00061         m_stackIndex(stackIndex)
00062     {
00063     }
00064 
00068     LuaObject(LuaState* state, int stackIndex);
00069 
00073     LuaObject(LuaState& state, int stackIndex);
00074 
00078     LuaObject(const LuaObject& src) :
00079         m_state(src.m_state),
00080         m_stackIndex(src.m_stackIndex)
00081     {
00082     }
00083 
00087     const LuaObject& operator=(const LuaObject& src)
00088     {
00089         m_state = src.m_state;
00090         m_stackIndex = src.m_stackIndex;
00091         return *this;
00092     }
00093 
00097     LuaState GetLuaState() const;
00098     lua_State* GetState() const         {  return m_state;  }
00099     operator lua_State*() const         {  return m_state;  }
00100     operator int() const                {  return m_stackIndex;  }
00101 
00102     const char* GetTypeStr() const      {  return lua_type(m_state, m_stackIndex);  }
00103     int GetType() const                 {  return lua_rawtag(m_state, m_stackIndex);  }
00104 
00105     bool IsNil() const                  {  return lua_isnil(m_state, m_stackIndex);  }
00106     bool IsTable() const                {  return lua_istable(m_state, m_stackIndex);  }
00107     bool IsUserData() const             {  return lua_isuserdata(m_state, m_stackIndex);  }
00108     bool IsCFunction() const            {  return lua_iscfunction(m_state, m_stackIndex) != 0;  }
00109     bool IsNumber() const               {  return lua_rawtag(m_state, m_stackIndex) == LUA_TNUMBER;  }
00110     bool IsString() const               {  return lua_isstring(m_state, m_stackIndex) != 0;  }
00111     bool IsUString() const              {  return lua_isustring(m_state, m_stackIndex) != 0;  }
00112     bool IsFunction() const             {  return lua_isfunction(m_state, m_stackIndex);  }
00113     bool IsNull() const                 {  return lua_isnull(m_state, m_stackIndex);  }
00114     bool IsPointer() const              {  return lua_ispointer(m_state, m_stackIndex) != 0;  }
00115 
00116     int GetInteger() const              {  return (int)lua_tonumber(m_state, m_stackIndex);  }
00117     float GetNumber() const             {  return (float)lua_tonumber(m_state, m_stackIndex);  }
00118     const char* GetString() const
00119     {
00120         const char* str = lua_tostring(m_state, m_stackIndex);
00121         assert(str);
00122         return str;
00123     }
00124     const wchar_t* GetUString() const
00125     {
00126         const wchar_t* str = (const wchar_t*)lua_toustring(m_state, m_stackIndex);
00127         assert(str);
00128         return str;
00129     }
00130     int StrLen() const                  {  return lua_strlen(m_state, m_stackIndex);  }
00131     LuaCFunction GetCFunction() const   {  return lua_tocfunction(m_state, m_stackIndex);  }
00132     void* GetUserData() const           {  return lua_touserdata(m_state, m_stackIndex);  }
00133     const void* GetLuaPointer() const   {  return lua_topointer(m_state, m_stackIndex);  }
00134     const void* GetPointer() const      {  return lua_getpointer(m_state, m_stackIndex);  }
00135 
00136     void Push()                         {  lua_pushvalue(m_state, m_stackIndex);  }
00137     void Pop()                          {  lua_remove(m_state, m_stackIndex);  }
00138 
00139     LuaObject GetMethods()              {  lua_getmethods(m_state, m_stackIndex);  return LuaObject(m_state, lua_gettop(m_state));  }
00140     void SetMethods()                   {  lua_setmethods(m_state, m_stackIndex);  }
00141 
00142     void SetTable()                     {  lua_settable(m_state, m_stackIndex);  }
00143     int Tag()                           {  return lua_tag(m_state, m_stackIndex);  }
00144     int GetCount()                      {  return lua_getn(m_state, m_stackIndex);  }
00145 
00146 
00154     LuaObject CreateTable(const char* name, int size = 0)
00155     {
00156         lua_newtablesize(m_state, size);                    // T
00157         lua_pushstring(m_state, name);                      // T name
00158         lua_pushvalue(m_state, lua_gettop(m_state) - 1);    // T name T
00159         lua_settable(m_state, m_stackIndex);
00160 
00161         return LuaObject(m_state, lua_gettop(m_state));
00162     }
00163     
00171     LuaObject CreateTable(int index, int size = 0)
00172     {
00173         lua_newtablesize(m_state, size);                    // T
00174         lua_pushnumber(m_state, index);                     // T name
00175         lua_pushvalue(m_state, lua_gettop(m_state) - 1);    // T name T
00176         lua_settable(m_state, m_stackIndex);
00177 
00178         return LuaObject(m_state, lua_gettop(m_state));
00179     }
00180     
00186     void SetNil(const char* name)
00187     {
00188         lua_pushstring(m_state, name);
00189         lua_pushnil(m_state);
00190         lua_settable(m_state, m_stackIndex);
00191     }
00192     
00198     void SetNil(int index)
00199     {
00200         lua_pushnumber(m_state, index);
00201         lua_pushnil(m_state);
00202         lua_settable(m_state, m_stackIndex);
00203     }
00204     
00211     void SetNumber(const char* name, double value)
00212     {
00213         lua_pushstring(m_state, name);
00214         lua_pushnumber(m_state, value);
00215         lua_settable(m_state, m_stackIndex);
00216     }
00217     
00224     void SetNumber(int index, double value)
00225     {
00226         lua_pushnumber(m_state, index);
00227         lua_pushnumber(m_state, value);
00228         lua_settable(m_state, m_stackIndex);
00229     }
00230     
00237     void SetString(const char* name, const char* value)
00238     {
00239         lua_pushstring(m_state, name);
00240         lua_pushstring(m_state, value);
00241         lua_settable(m_state, m_stackIndex);
00242     }
00243     
00250     void SetString(int index, const char* value)
00251     {
00252         lua_pushnumber(m_state, index);
00253         lua_pushstring(m_state, value);
00254         lua_settable(m_state, m_stackIndex);
00255     }
00256     
00263     void SetString(const char* name, const wchar_t* value)
00264     {
00265         lua_pushstring(m_state, name);
00266         lua_pushustring(m_state, value);
00267         lua_settable(m_state, m_stackIndex);
00268     }
00269     
00276     void SetString(int index, const wchar_t* value)
00277     {
00278         lua_pushnumber(m_state, index);
00279         lua_pushustring(m_state, value);
00280         lua_settable(m_state, m_stackIndex);
00281     }
00282     
00289     void SetUserData(const char* name, void* value)
00290     {
00291         lua_pushstring(m_state, name);
00292         lua_newuserdatabox(m_state, value);
00293         lua_settable(m_state, m_stackIndex);
00294     }
00295 
00302     void SetUserData(int index, void* value)
00303     {
00304         lua_pushnumber(m_state, index);
00305         lua_newuserdatabox(m_state, value);
00306         lua_settable(m_state, m_stackIndex);
00307     }
00308 
00315     void SetPointer(const char* name, const void* value)
00316     {
00317         lua_pushstring(m_state, name);
00318         lua_pushpointer(m_state, value);
00319         lua_settable(m_state, m_stackIndex);
00320     }
00321 
00328     void SetPointer(int index, const void* value)
00329     {
00330         lua_pushnumber(m_state, index);
00331         lua_pushpointer(m_state, value);
00332         lua_settable(m_state, m_stackIndex);
00333     }
00334 
00341     void SetObject(const char* name, LuaObject value)
00342     {
00343         lua_pushstring(m_state, name);
00344         lua_pushvalue(m_state, value);
00345         lua_settable(m_state, m_stackIndex);
00346     }
00347 
00354     void SetObject(int index, LuaObject value)
00355     {
00356         lua_pushnumber(m_state, index);
00357         lua_pushvalue(m_state, value);
00358         lua_settable(m_state, m_stackIndex);
00359     }
00360 
00368     LuaObject GetByName(const char* name)
00369     {
00370         lua_pushstring(m_state, name);
00371         lua_rawget(m_state, m_stackIndex);
00372         return LuaObject(m_state, lua_gettop(m_state));
00373     }
00374 
00382     LuaObject GetByIndex(int index)
00383     {
00384         lua_rawgeti(m_state, m_stackIndex, index);
00385         return LuaObject(m_state, lua_gettop(m_state));
00386     }
00387 
00395     void Register(const char* funcName, lua_CFunction function)
00396     {
00397         lua_pushstring(m_state, funcName);
00398         lua_pushcclosure(m_state, (lua_CFunction)function, 0);
00399         lua_settable(m_state, m_stackIndex);
00400     }
00401 
00409     void Register(const char* funcName, LuaStateCFunction function)
00410     {
00411         lua_pushstring(m_state, funcName);
00412         lua_pushcclosure(m_state, (lua_CFunction)function, 0);
00413         lua_settable(m_state, m_stackIndex);
00414     }
00415 
00423     void Register(const LuaFunctionList* functionList)
00424     {
00425         int i = 0;
00426         while (true)
00427         {
00428             if (!functionList[i].name)
00429                 break;
00430             Register(functionList[i].name, functionList[i].luaStateCFunction);
00431             i++;
00432         }
00433     }
00434 
00441     void Unregister(const char* funcName)
00442     {
00443         lua_pushstring(m_state, funcName);
00444         lua_pushnil(m_state);
00445         lua_settable(m_state, m_stackIndex);
00446     }
00447 
00454     void Unregister(const LuaFunctionList* functionList)
00455     {
00456         int i = 0;
00457         while (true)
00458         {
00459             if (!functionList[i].name)
00460                 break;
00461             Unregister(functionList[i].name);
00462             i++;
00463         }
00464     }
00465 
00466 protected:
00467     friend class LuaState;
00468 
00469     lua_State* m_state;     
00470     int m_stackIndex;       
00471 };
00472 
00473 
00479 class LUA_API LuaState
00480 {
00481 public:
00483     enum { NOREF = LUA_NOREF };
00484     enum { REFNIL = LUA_REFNIL };
00485 
00486     LuaState(lua_State* state);
00487     ~LuaState() {};
00488 
00489     LuaObject GetMethods(int index)             {  lua_getmethods(m_state, index);  return LuaObject(this, lua_gettop(m_state));  }
00490     LuaObject GetDefaultMethods(int type)       {  lua_getdefaultmethods(m_state, type);  return LuaObject(this, lua_gettop(m_state));  }
00491     void SetMethods(int index)                  {  lua_setmethods(m_state, index);  }
00492 
00493     // Basic stack manipulation.
00494     int GetTop()                                {  return lua_gettop(m_state);  }
00495     void SetTop(int index)                      {  lua_settop(m_state, index);  }
00496     void PushValue(int index)                   {  lua_pushvalue(m_state, index);  }
00497     void PushValue(LuaObject object)            {  lua_pushvalue(m_state, object);  }
00498     void RemoveStack(int index)                 {  lua_remove(m_state, index);  }
00499     void InsertStack(int index)                 {  lua_insert(m_state, index);  }
00500     int StackSpace()                            {  return lua_stackspace(m_state);  }
00501 
00502     // access functions (stack -> C)
00503     const lua_char *Type(int index)             {  return lua_type(m_state, index);  }
00504     int IsNumber(int index)                     {  return lua_isnumber(m_state, index);  }
00505     int IsString(int index)                     {  return lua_isstring(m_state, index);  }
00506     int IsUString(int index)                    {  return lua_isustring(m_state, index);  }
00507     int IsCFunction(int index)                  {  return lua_iscfunction(m_state, index);  }
00508     bool IsFunction(int index)                  {  return lua_isfunction(m_state, index);  }
00509     bool IsTable(int index)                     {  return lua_istable(m_state, index);  }
00510     bool IsUserData(int index)                  {  return lua_isuserdata(m_state, index);  }
00511     bool IsNil(int index)                       {  return lua_isnil(m_state, index);  }
00512     bool IsNull(int index)                      {  return lua_isnull(m_state, index);  }
00513     bool IsPointer(int index) const             {  return lua_ispointer(m_state, index) != 0;  }
00514     int Tag(int index)                          {  return lua_tag(m_state, index);  }
00515     int RawTag(int index)                       {  return lua_rawtag(m_state, index);  }
00516 
00517     int Equal(int index1, int index2)           {  return lua_equal(m_state, index1, index2);  }
00518     int LessThan(int index1, int index2)        {  return lua_lessthan(m_state, index1, index2);  }
00519 
00520     lua_Number ToNumber(int index)              {  return lua_tonumber(m_state, index);  }
00521     int ToInteger(int index)                    {  return (int)lua_tonumber(m_state, index);  }
00522     const lua_char* ToString(int index)         {  return lua_tostring(m_state, index);  }
00523     const wchar_t* ToUString(int index)         {  return lua_toustring(m_state, index);  }
00524     size_t StrLen(int index)                    {  return lua_strlen(m_state, index);  }
00525     lua_CFunction ToCFunction(int index)        {  return lua_tocfunction(m_state, index);  }
00526     void *ToUserData(int index)                 {  return lua_touserdata(m_state, index);  }
00527     const void* ToPointer(int index)            {  return lua_getpointer(m_state, index);  }
00528     const void *ToLuaPointer(int index)         {  return lua_topointer(m_state, index);  }
00529 
00530     // push functions (C -> stack)
00531     void PushBool(bool value)                   {  if (value)  lua_pushnumber(m_state, 1);  else  lua_pushnil(m_state);  }
00532     void PushNil()                              {  lua_pushnil(m_state);  }
00533     void PushNumber(double n)                   {  lua_pushnumber(m_state, n);  }
00534     void PushLString(const char *s, size_t len) {  lua_pushlstring(m_state, s, len);  }
00535     void PushString(const char *s)              {  lua_pushstring(m_state, s);  }
00536     void PushLUString(const wchar_t* s, size_t len){  lua_pushlustring(m_state, s, len);  }
00537     void PushUString(const wchar_t* s)          {  lua_pushustring(m_state, s);  }
00538     void PushCClosure(lua_CFunction fn, int n)  {  lua_pushcclosure(m_state, fn, n);  }
00539     void PushCClosure(LuaStateCFunction fn, int n)  {  lua_pushcclosure(m_state, (lua_CFunction)fn, n);  }
00540     void PushPointer(const void* p)             {  lua_pushpointer(m_state, p);  }
00541 
00542     // get functions (Lua -> stack)
00543     LuaObject GetGlobal(const char *name)       {  lua_getglobal(m_state, name);  return LuaObject(*this, GetTop());  }
00544     void GetTable(int index)                    {  lua_gettable(m_state, index);  }
00545     void RawGet(int index)                      {  lua_rawget(m_state, index);  }
00546     void RawGetI(int index, int n)              {  lua_rawgeti(m_state, index, n);  }
00547     LuaObject GetGlobals()                      {  lua_getglobals(m_state);  return LuaObject(*this, GetTop());  }
00548     void GetTagMethod(int tag, const char *event)   {  lua_gettagmethod(m_state, tag, event);  }
00549     LuaObject GetRef(int ref)                   {  lua_getref(m_state, ref);  return LuaObject(*this, GetTop());  }
00550     LuaObject NewTable(int size = 0)            {  lua_newtablesize(m_state, size);  return LuaObject(*this, GetTop());  }
00551     void GetRegistry()                          {  lua_getregistry(m_state);  }
00552     void GetWeakRegistry()                      {  lua_getweakregistry(m_state);  }
00553 
00554     // set functions(stack -> Lua)
00555     void SetGlobal(const char *name)            {  lua_setglobal(m_state, name);  }
00556     void SetTable(int index)                    {  lua_settable(m_state, index);  }
00557     void RawSet(int index)                      {  lua_rawset(m_state, index);  }
00558     void RawSetI(int index, int n)              {  lua_rawseti(m_state, index, n);  }
00559     void SetGlobals()                           {  lua_setglobals(m_state);  }
00560     void SetTagMethod(int tag, const char *event)   {  lua_settagmethod(m_state, tag, event);  }
00561     int Ref(int lock)                           {  return lua_ref(m_state, lock);  }
00562 
00563     // `load' and `do' functions (load and run Lua code)
00564     int Call(int nargs, int nresults)           {  return lua_call(m_state, nargs, nresults);  }
00565     void RawCall(int nargs, int nresults)       {  lua_rawcall(m_state, nargs, nresults);  }
00566     int LoadFile(const lua_char* filename)      {  return lua_loadfile(m_state, filename);  }
00567     int DoFile(const char *filename)            {  return lua_dofile(m_state, filename);  }
00568     int DoString(const char *str)               {  return lua_dostring(m_state, str);  }
00569     int LoadBuffer(const lua_char* buff, size_t size, const lua_char* name)
00570     {
00571         return lua_loadbuffer(m_state, buff, size, name);
00572     }
00573     int DoBuffer(const char *buff, size_t size, const char *name)
00574     {
00575         return lua_dobuffer(m_state, buff, size, name);
00576     }
00577 
00578     // Garbage-collection functions
00579     int GetGCThreshold()                        {  return lua_getgcthreshold(m_state);  }
00580     int GetGCCount()                            {  return lua_getgccount(m_state);  }
00581     void SetGCThreshold(int newthreshold)       {  lua_setgcthreshold(m_state, newthreshold);  }
00582 
00583     // Miscellaneous functions
00584     int NewType(const lua_char *name, int basictype)    {  return lua_newtype(m_state, name, basictype);  }
00585     int CopyTagMethods(int tagto, int tagfrom)  {  return lua_copytagmethods(m_state, tagto, tagfrom);  }
00586     void SetTag(int tag)                        {  lua_settag(m_state, tag);  }
00587 
00588     int Name2Tag(const lua_char* name)          {  return lua_name2tag(m_state, name);  }
00589     const lua_char* Tag2Name(int tag)           {  return lua_tag2name(m_state, tag);  }
00590 
00591     void Error(const lua_char* errorStr)        {  lua_error(m_state, errorStr);  }
00592 
00593     void Unref(int ref)                         {  lua_unref(m_state, ref);  }
00594 
00595     int Next(int index)                         {  return lua_next(m_state, index);  }
00596     int GetN(int index)                         {  return lua_getn(m_state, index);  }
00597 
00598     void Concat(int n)                          {  lua_concat(m_state, n);  }
00599 
00600     void PushUserData(void* u);
00601     void* NewUserData(size_t size)              {  return lua_newuserdata(m_state, size);  }
00602     void NewUserDataBox(void* u)                {  lua_newuserdatabox(m_state, u);  }
00603 
00604     void SetWeakMode(int mode)                  {  lua_setweakmode(m_state, mode);  }
00605     int GetWeakMode(int index)                  {  return lua_getweakmode(m_state, index);  }
00606 
00607 
00608     // Helper functions
00609     void Pop()                                  {  lua_pop(m_state, 1);  }
00610     void Pop(int amount)                        {  lua_pop(m_state, amount);  }
00611 
00612     void Register(const char* funcName, lua_CFunction function)  {  lua_register(m_state, funcName, function);  }
00613     void Register(const char* funcName, LuaStateCFunction function)
00614     {
00615         lua_pushcfunction(m_state, (lua_CFunction)function);
00616         lua_setglobal(m_state, funcName);
00617     }
00618 
00619     void Unregister(const char* funcName)
00620     {
00621         LuaObject globals = GetGlobals();
00622         lua_pushstring(m_state, funcName);
00623         lua_pushnil(m_state);
00624         lua_settable(m_state, globals);
00625     }
00626 
00627     void Register(const LuaFunctionList* functionList)
00628     {
00629         int i = 0;
00630         while (true)
00631         {
00632             if (!functionList[i].name)
00633                 break;
00634             Register(functionList[i].name, functionList[i].luaStateCFunction);
00635             i++;
00636         }
00637     }
00638 
00639 
00640     void Unregister(const LuaFunctionList* functionList)
00641     {
00642         int i = 0;
00643         while (true)
00644         {
00645             if (!functionList[i].name)
00646                 break;
00647             Unregister(functionList[i].name);
00648             i++;
00649         }
00650     }
00651 
00652     void PushCFunction(lua_CFunction f)         {  lua_pushcclosure(m_state, f, 0);  }
00653     void PushCFunction(LuaStateCFunction f)     {  lua_pushcclosure(m_state, (lua_CFunction)f, 0);  }
00654     int CloneTag(int t)                         {  return lua_copytagmethods(m_state, lua_newtag(m_state), t);  }
00655 
00656     void CollectGarbage()                       {  lua_setgcthreshold(m_state, 0);  }   
00657 
00658     void SetNilGCMethod(LuaStateCFunction f)
00659     {  
00660         PushNil();
00661         PushCFunction(f);
00662         SetTagMethod(Tag(-2), "gc");
00663         Pop();
00664     }
00665     
00666     int ConfigGetInteger(const char* section, const char* entry, int defaultValue = 0);
00667     float ConfigGetReal(const char* section, const char* entry, double defaultValue = 0.0);
00668     const char* ConfigGetString(const char* section, const char* entry, const char* defaultValue = "");
00669     void ConfigSetInteger(const char* section, const char* entry, int value);
00670     void ConfigSetReal(const char* section, const char* entry, double value);
00671     void ConfigSetString(const char* section, const char* entry, const char* value);
00672 
00673     void WriteLuaGlobalsFile(const char* filename, bool writeAll = false, bool alphabetical = false,
00674                         bool writeTablePointers = false, unsigned int maxIndentLevel = 0xffffffff);
00675     void WriteLuaFile(const char* filename, const char* name, LuaObject value,
00676                          int indentLevel, bool writeAll = false,
00677                          bool alphabetical = false, bool writeTablePointers = false,
00678                          unsigned int maxIndentLevel = 0xffffffff);
00679     bool WriteLuaObject(LuaStateOutFile& file, const char* name, LuaObject value,
00680                          int indentLevel, bool writeAll = false,
00681                          bool alphabetical = false, bool writeTablePointers = false,
00682                          unsigned int maxIndentLevel = 0xffffffff);
00683     bool WriteLuaObject(FILE* file, const char* name, LuaObject value,
00684                          int indentLevel, bool writeAll = false,
00685                          bool alphabetical = false, bool writeTablePointers = false,
00686                          unsigned int maxIndentLevel = 0xffffffff);
00687 
00688     operator lua_State*()                       {  return m_state;  }
00689 
00690 public:
00691     lua_State* m_state;
00692 
00693 protected:
00694     LuaState() {}
00695 };
00696 
00697 
00703 class LUA_API LuaStateOwner : public LuaState
00704 {
00705 public:
00706     LuaStateOwner(bool initStandardLibrary = true, unsigned long stackSize = 0, bool multithreaded = false);
00707     LuaStateOwner(LuaState& script, bool initStandardLibrary = true, unsigned long stackSize = 0);
00708     ~LuaStateOwner();
00709 
00710 protected:
00711     void Init(bool initStandardLibrary, unsigned long stackSize);
00712 };
00713 
00714 
00718 
00722 class LuaAutoBlock
00723 {
00724 public:
00725     LuaAutoBlock(lua_State* state) :
00726         m_state(state)
00727     {
00728         m_stackTop = lua_gettop(m_state);
00729     }
00730 
00731     LuaAutoBlock(LuaState* state) :
00732         m_state(*state)
00733     {
00734         m_stackTop = lua_gettop(m_state);
00735     }
00736 
00737     LuaAutoBlock(LuaState& state) :
00738         m_state(state)
00739     {
00740         m_stackTop = lua_gettop(m_state);
00741     }
00742 
00743     LuaAutoBlock(LuaObject& object) :
00744         m_state(object)
00745     {
00746         m_stackTop = lua_gettop(m_state);
00747     }
00748 
00749     ~LuaAutoBlock()
00750     {
00751         lua_settop(m_state, m_stackTop);
00752     }
00753 
00754 private:
00755     LuaAutoBlock(const LuaAutoBlock& src);                  // Not implemented
00756     const LuaAutoBlock& operator=(const LuaAutoBlock& src); // Not implemented
00757 
00758     lua_State* m_state;
00759     int m_stackTop;
00760 };
00761 
00762 
00766 inline LuaState::LuaState(lua_State* state)
00767 {
00768     m_state = state;
00769 }
00770 
00771 
00776 inline LuaStateOwner::~LuaStateOwner()
00777 {
00778     lua_close(m_state);
00779 }
00780 
00781 
00785 inline LuaObject::LuaObject(LuaState* state, int stackIndex) :
00786     m_state(*state),
00787     m_stackIndex(stackIndex)
00788 {
00789 }
00790 
00791 
00795 inline LuaObject::LuaObject(LuaState& state, int stackIndex) :
00796     m_state(state),
00797     m_stackIndex(stackIndex)
00798 {
00799 }
00800 
00801     
00804 inline LuaState LuaObject::GetLuaState() const
00805 {
00806     return LuaState(m_state);
00807 }
00808 
00809 
00810 //-----------------------------------------------------------------------------
00811 //-----------------------------------------------------------------------------
00812 // Output file helper class.
00813 //-----------------------------------------------------------------------------
00814 //-----------------------------------------------------------------------------
00820 class LUA_API LuaStateOutFile
00821 {
00822 public:
00823     LuaStateOutFile() : m_file(NULL), m_fileOwner(false) {}
00824     virtual ~LuaStateOutFile()
00825     {
00826         if (m_file  &&  m_fileOwner)
00827             fclose(m_file);
00828     }
00829 
00830     virtual bool Open(const char* fileName)
00831     {
00832         Close();
00833 
00834         m_file = fopen(fileName, "wb");
00835         m_fileOwner = true;
00836 
00837         return true;
00838     }
00839 
00840     virtual void Close()
00841     {
00842         if (m_file  &&  m_fileOwner)
00843             fclose(m_file);
00844     }
00845 
00846     virtual void Print(const char* str, ...)
00847     {
00848         char message[800];
00849         va_list arglist;
00850 
00851         va_start(arglist, str);
00852         vsprintf(message, str, arglist);
00853         va_end(arglist);
00854 
00855         fputs(message, m_file);
00856     }
00857 
00858     bool Assign(FILE* file)
00859     {
00860         m_file = file;
00861         m_fileOwner = false;
00862 
00863         return true;
00864     }
00865     
00866 protected:
00867     FILE* m_file;
00868     bool m_fileOwner;
00869 };
00870 
00871 
00872 

Generated on Wed Nov 7 21:15:16 2001 for LuaState(LuaWrapper) by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001