Main Page   Compound List   File List   Compound Members   File Members  

Script.h

Go to the documentation of this file.
00001 #pragma once
00002 
00003 extern "C" {
00004 #include "Lua.h"
00005 #include "luadebug.h"
00006 #include "lauxlib.h"
00007 }
00008 
00014 class Script
00015 {
00016 public:
00017     typedef lua_CFunction CFunction;
00018 
00022 
00025     class Object
00026     {
00027     public:
00031         Object(const Object& src) : m_parent(src.m_parent)
00032         {
00033             m_stackIndex = src.m_stackIndex;
00034         }
00035 
00039         const Object& operator=(const Object& src)
00040         {
00041             m_stackIndex = src.m_stackIndex;
00042             return *this;
00043         }
00044 
00045         Script& GetParent() const           {  return m_parent;  }
00046         lua_State* GetState() const         {  return m_parent.m_state;  }
00047 
00048         int GetType() const                 {  return lua_type(GetState(), m_stackIndex);  }
00049 
00050         bool IsNil() const                  {  return m_parent.IsNil(m_stackIndex);  }
00051         bool IsTable() const                {  return m_parent.IsTable(m_stackIndex);  }
00052         bool IsUserData() const             {  return m_parent.IsUserData(m_stackIndex);  }
00053         bool IsCFunction() const            {  return lua_iscfunction(GetState(), m_stackIndex) != 0;  }
00054         bool IsNumber() const               {  return lua_isnumber(GetState(), m_stackIndex) != 0;  }
00055         bool IsString() const               {  return m_parent.IsString(m_stackIndex);  }
00056         bool IsFunction() const             {  return m_parent.IsFunction(m_stackIndex);  }
00057         bool IsNull() const                 {  return m_parent.IsNull(m_stackIndex);  }
00058 
00059         int GetStackIndex() const           {  return m_stackIndex;  }
00060 
00061         int GetInteger() const              {  return (int)lua_tonumber(GetState(), m_stackIndex);  }
00062         float GetNumber() const             {  return (float)lua_tonumber(GetState(), m_stackIndex);  }
00063         const char* GetString() const       {  return lua_tostring(GetState(), m_stackIndex);  }
00064         int StrLen() const                  {  return lua_strlen(GetState(), m_stackIndex);  }
00065         CFunction GetCFunction() const      {  return lua_tocfunction(GetState(), m_stackIndex);  }
00066         void* GetUserData() const           {  return lua_touserdata(GetState(), m_stackIndex);  }
00067         const void* GetPointer() const      {  return lua_topointer(GetState(), m_stackIndex);  }
00068 
00075         Object CreateTable(const char* name)
00076         {
00077             int val;
00078             val = m_parent.GetTop();
00079             lua_newtable(GetState());                           // T
00080             val = m_parent.GetTop();
00081             lua_pushstring(GetState(), name);                   // T name
00082             val = m_parent.GetTop();
00083             lua_pushvalue(GetState(), lua_gettop(GetState()) - 1);  // T name T
00084             val = m_parent.GetTop();
00085             lua_settable(GetState(), m_stackIndex);
00086             val = m_parent.GetTop();
00087 
00088             return Object(m_parent, m_parent.GetTop());
00089         }
00090         
00097         void SetNumber(const char* name, double value)
00098         {
00099             lua_pushstring(GetState(), name);
00100             lua_pushnumber(GetState(), value);
00101             lua_settable(GetState(), m_stackIndex);
00102         }
00103         
00110         void SetString(const char* name, const char* value)
00111         {
00112             lua_pushstring(GetState(), name);
00113             lua_pushstring(GetState(), value);
00114             lua_settable(GetState(), m_stackIndex);
00115         }
00116         
00123         void SetUserData(const char* name, void* data)
00124         {
00125             lua_pushstring(GetState(), name);
00126             lua_pushuserdata(GetState(), data);
00127             lua_settable(GetState(), m_stackIndex);
00128         }
00129         
00130 //      int CallFunction();
00131         int Tag()                               {  return lua_tag(GetState(), m_stackIndex);  }
00132 
00141         Object GetByName(const char* name)
00142         {
00143             lua_pushstring(GetState(), name);
00144             lua_rawget(GetState(), m_stackIndex);
00145             return Object(m_parent, m_parent.GetTop());
00146         }
00147 
00155         Object GetByIndex(int index)
00156         {
00157             lua_rawgeti(GetState(), m_stackIndex, index);
00158             return Object(m_parent, m_parent.GetTop());
00159         }
00160 
00161     protected:
00162         friend class Script;
00163 
00164         Object(Script& parent, int index) :
00165                 m_parent(parent), m_stackIndex(index) { }
00166         Script& m_parent;       
00167         int m_stackIndex;       
00168     };
00169 
00173 
00175     class AutoBlock
00176     {
00177     public:
00178         AutoBlock(Script& script) :
00179             m_script(script)
00180         {
00181             m_stackTop = m_script.GetTop();
00182         }
00183 
00184         AutoBlock(Object& object) :
00185             m_script(object.GetParent())
00186         {
00187             m_stackTop = m_script.GetTop();
00188         }
00189 
00190         ~AutoBlock()
00191         {
00192             m_script.SetTop(m_stackTop);
00193         }
00194 
00195     private:
00196         AutoBlock(const AutoBlock& src);                    // Not implemented
00197         const AutoBlock& operator=(const AutoBlock& src); // Not implemented
00198 
00199         Script& m_script;
00200         int m_stackTop;
00201     };
00202 
00203 
00205     enum { NOREF = LUA_NOREF };
00206     enum { REFNIL = LUA_REFNIL };
00207     enum { ANYTAG = LUA_ANYTAG };
00208 
00209     Script(bool initStandardLibrary = true);
00210     Script(lua_State* state);
00211     ~Script();
00212 
00213     // Basic stack manipulation.
00214     int GetTop()                                {  return lua_gettop(m_state);  }
00215     void SetTop(int index)                      {  lua_settop(m_state, index);  }
00216     void PushValue(int index)                   {  lua_pushvalue(m_state, index);  }
00217     void Remove(int index)                      {  lua_remove(m_state, index);  }
00218     void Insert(int index)                      {  lua_insert(m_state, index);  }
00219     int StackSpace()                            {  return lua_stackspace(m_state);  }
00220 
00221     Object GetObject(int index)                 {  return Object(*this, index);  }
00222     void PushObject(Object object)              {  lua_pushvalue(m_state, object.GetStackIndex());  }
00223 
00224     // access functions (stack -> C)
00225     int Equal(int index1, int index2)           {  return lua_equal(m_state, index1, index2);  }
00226     int LessThan(int index1, int index2)        {  return lua_lessthan(m_state, index1, index2);  }
00227 
00228     // push functions (C -> stack)
00229     void PushBool(bool value)                   {  if (value)  lua_pushnumber(m_state, 1);  else  lua_pushnil(m_state);  }
00230     void PushNil()                              {  lua_pushnil(m_state);  }
00231     void PushNumber(double n)                   {  lua_pushnumber(m_state, n);  }
00232     void PushLString(const char *s, size_t len) {  lua_pushlstring(m_state, s, len);  }
00233     void PushString(const char *s)              {  lua_pushstring(m_state, s);  }
00234     void PushCClosure(lua_CFunction fn, int n)  {  lua_pushcclosure(m_state, fn, n);  }
00235     void PushUserTag(void *u, int tag)          {  lua_pushusertag(m_state, u, tag);  }
00236 
00237     // get functions (Lua -> stack)
00238     Object GetGlobal(const char *name)          {  lua_getglobal(m_state, name);  return Object(*this, GetTop());  }
00239     void GetTable(int index)                    {  lua_gettable(m_state, index);  }
00240     void RawGet(int index)                      {  lua_rawget(m_state, index);  }
00241     void RawGetI(int index, int n)              {  lua_rawgeti(m_state, index, n);  }
00242     Object GetGlobals()                         {  lua_getglobals(m_state);  return Object(*this, GetTop());  }
00243     void GetTagMethod(int tag, const char *event)   {  lua_gettagmethod(m_state, tag, event);  }
00244 
00245     int GetRef(int ref)                         {  return lua_getref(m_state, ref);  }
00246 
00247     Object NewTable()                           {  lua_newtable(m_state);  return Object(*this, GetTop());  }
00248 
00249     // set functions(stack -> Lua)
00250     void SetGlobal(const char *name)            {  lua_setglobal(m_state, name);  }
00251     void SetTable(int index)                    {  lua_settable(m_state, index);  }
00252     void RawSet(int index)                      {  lua_rawset(m_state, index);  }
00253     void RawSetI(int index, int n)              {  lua_rawseti(m_state, index, n);  }
00254     void SetGlobals()                           {  lua_setglobals(m_state);  }
00255     void SetTagMethod(int tag, const char *event)   {  lua_settagmethod(m_state, tag, event);  }
00256     int Ref(int lock)                           {  return lua_ref(m_state, lock);  }
00257 
00258 
00259     // "do" functions(run Lua code)
00260     int Call(int nargs, int nresults)           {  return lua_call(m_state, nargs, nresults);  }
00261     void RawCall(int nargs, int nresults)       {  lua_rawcall(m_state, nargs, nresults);  }
00262     int DoFile(const char *filename)            {  return lua_dofile(m_state, filename);  }
00263     int DoString(const char *str)               {  return lua_dostring(m_state, str);  }
00264     int DoBuffer(const char *buff, size_t size, const char *name)   {  return lua_dobuffer(m_state, buff, size, name);  }
00265 
00266 
00267     // miscellaneous functions
00268     int NewTag()                                {  return lua_newtag(m_state);  }
00269     int CopyTagMethods(int tagto, int tagfrom)  {  return lua_copytagmethods(m_state, tagto, tagfrom);  }
00270     void SetTag(int tag)                        {  lua_settag(m_state, tag);  }
00271 
00272     void Error(const char *s)                   {  lua_error(m_state, s);  }
00273 
00274     void Unref(int ref)                         {  lua_unref(m_state, ref);  }
00275 
00276     int Next(int index)                         {  return lua_next(m_state, index);  }
00277     int GetN(int index)                         {  return lua_getn(m_state, index);  }
00278 
00279     void Concat(int n)                          {  lua_concat(m_state, n);  }
00280 
00281     // Helper function
00282     void Pop()                                  {  lua_pop(m_state, 1);  }
00283     void Pop(int amount)                        {  lua_pop(m_state, amount);  }
00284 
00285     void Register(const char* funcName, lua_CFunction function)  {  lua_register(m_state, funcName, function);  }
00286     void PushUserData(void* u)                  {  lua_pushuserdata(m_state, u);  }
00287     void PushCFunction(lua_CFunction f)         {  lua_pushcclosure(m_state, f, 0);  }
00288     int CloneTag(int t)                         {  lua_copytagmethods(m_state, lua_newtag(m_state), t);  }
00289 
00290     bool IsFunction(int index)                  {  return lua_isfunction(m_state, index);  }
00291     bool IsString(int index)                    {  return lua_isstring(m_state, index) != 0;  }
00292     bool IsTable(int index)                     {  return lua_istable(m_state, index);  }
00293     bool IsUserData(int index)                  {  return lua_isuserdata(m_state, index);  }
00294     bool IsNil(int index)                       {  return lua_isnil(m_state, index);  }
00295     bool IsNull(int index)                      {  return lua_isnull(m_state, index);  }
00296 
00297     
00298     
00299     
00300     
00301     int ConfigGetInteger(const char* section, const char* entry, int defaultValue = 0);
00302     float ConfigGetReal(const char* section, const char* entry, double defaultValue = 0.0);
00303     const char* ConfigGetString(const char* section, const char* entry, const char* defaultValue = "");
00304     void ConfigSetInteger(const char* section, const char* entry, int value);
00305     void ConfigSetReal(const char* section, const char* entry, double value);
00306     void ConfigSetString(const char* section, const char* entry, const char* value);
00307 
00308     void SaveText(const char* filename);
00309 
00310     operator lua_State*()                       {  return m_state;  }
00311     lua_State* GetState() const                 {  return m_state;  }
00312 
00313 public:
00314     friend class Object;
00315 
00316     lua_State* m_state;
00317     bool m_ownState;
00318 };
00319 
00320 
00323 inline Script::Script(lua_State* state)
00324 {
00325     m_state = state;
00326     m_ownState = false;
00327 }
00328 
00329 
00332 inline Script::~Script()
00333 {
00334     // Only close the Lua state if we own it.
00335     if (m_ownState)
00336         lua_close(m_state);
00337 }

Generated at Wed Dec 13 00:55:51 2000 for Script (Lua Wrapper) by doxygen1.2.2 written by Dimitri van Heesch, © 1997-2000