Pushok

Lua Stack Module

May 2nd, 2011
732
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.61 KB | None | 0 0
  1. #include "windows.h"
  2.  
  3. extern "C" {
  4. #include "math.h"
  5. #include "string.h"
  6. #include "stdio.h"
  7. #include "stdlib.h"
  8. #include "lua/lua.h"
  9. #include "lua/lauxlib.h"
  10. #include "lua/lualib.h"
  11. }
  12.  
  13. template<typename T> class CStack {
  14. public:
  15.     static enum {CStack_StackEmpty, CStack_BadMemory};
  16.  
  17.     unsigned int slots()
  18.     {
  19.         return StackSize;
  20.     }
  21.  
  22.     unsigned int size()
  23.     {
  24.         return StackIter;
  25.     }
  26.  
  27.     bool empty()
  28.     {
  29.         return StackIter == 0;
  30.     }
  31.  
  32.     T pop()
  33.     {
  34.         if(StackIter == 0) throw (int)CStack_StackEmpty;
  35.  
  36.         return _Stack[--StackIter];
  37.     }
  38.  
  39.     void push(T j)
  40.     {
  41.         if(StackSize >= StackIter && _Stack != 0)
  42.         {
  43.             T* _newstack = (T*)malloc(sizeof(T) * (StackSize + 1));
  44.  
  45.             if(_newstack == NULL)
  46.             {
  47.                 free(_newstack);
  48.                 this->~CStack();
  49.                 throw (int)CStack_BadMemory;
  50.             }
  51.  
  52.             for(int i=0;i<StackSize;i++)
  53.                 _newstack[i] = _Stack[i];
  54.  
  55.             free(_Stack);
  56.  
  57.             _Stack = _newstack;
  58.         }
  59.         else if (_Stack == 0)
  60.         {
  61.             _Stack = (T*)malloc(sizeof(T) * (StackSize == 0 ? 1 : StackSize));
  62.             if(_Stack == 0)
  63.             {
  64.                 this->~CStack();
  65.                 throw (int)CStack_BadMemory;
  66.             }
  67.         }
  68.  
  69.         _Stack[StackSize++] = j;
  70.  
  71.         StackIter = StackSize;
  72.  
  73.     }
  74.  
  75.     void clear()
  76.     {
  77.         StackIter = 0;
  78.     }
  79.  
  80.     CStack()
  81.     {
  82.         StackIter = StackSize = 0;
  83.         _Stack = NULL;
  84.     }
  85.  
  86.     ~CStack()
  87.     {
  88.         free(_Stack);
  89.         StackIter = StackSize = 0;
  90.     }
  91.  
  92. private:
  93.     unsigned int StackSize, StackIter;
  94.     T* _Stack;
  95. };
  96.  
  97. CStack<LUA_NUMBER> *LuaStack = NULL;
  98.  
  99. extern "C" {
  100.     int __declspec(dllexport) luaopen_stack (lua_State *L);
  101. }
  102.  
  103. static int luaStack_Create(lua_State *Lua)
  104. {
  105.     if(LuaStack != NULL)
  106.     {
  107.         luaL_error(Lua, "Stack already created!");
  108.         return 0;
  109.     }
  110.  
  111.     LuaStack = new CStack<LUA_NUMBER>();
  112.     return 0;
  113. }
  114.  
  115. static int luaStack_Close(lua_State *Lua)
  116. {
  117.     if(LuaStack == NULL)
  118.     {
  119.         luaL_error(Lua, "Stack is not created!");
  120.         return 0;
  121.     }
  122.  
  123.     delete LuaStack;
  124.     LuaStack = NULL;
  125.     return 0;
  126. }
  127.  
  128. static int luaStack_Push(lua_State *Lua)
  129. {
  130.     if(LuaStack == NULL)
  131.     {
  132.         luaL_error(Lua, "Stack is not created!");
  133.         return 0;
  134.     }
  135.  
  136.     LuaStack->push(lua_tonumber(Lua, 1));
  137.     return 0;
  138. }
  139.  
  140. static int luaStack_Pop(lua_State *Lua)
  141. {
  142.     if(LuaStack == NULL)
  143.     {
  144.         luaL_error(Lua, "Stack is not created!");
  145.         return 0;
  146.     }
  147.  
  148.     try
  149.     {
  150.         lua_pushnumber(Lua, LuaStack->pop());
  151.     }
  152.     catch (int e)
  153.     {
  154.         switch(e)
  155.         {
  156.         case static_cast<int>(CStack<LUA_NUMBER>::CStack_StackEmpty):
  157.             lua_pushstring(Lua, "Stack is empty!");
  158.             break;
  159.         }
  160.     }
  161.     return 1;
  162. }
  163.  
  164. static int luaStack_Clear(lua_State *Lua)
  165. {
  166.     if(LuaStack == NULL)
  167.     {
  168.         luaL_error(Lua, "Stack is not created!");
  169.         return 0;
  170.     }
  171.  
  172.     LuaStack->clear();
  173.     return 0;
  174. }
  175.  
  176. static int luaStack_Size(lua_State *Lua)
  177. {
  178.     if(LuaStack == NULL)
  179.     {
  180.         luaL_error(Lua, "Stack is not created!");
  181.         return 0;
  182.     }
  183.  
  184.     lua_pushnumber(Lua, LuaStack->size());
  185.     return 1;
  186. }
  187.  
  188. static int luaStack_Empty(lua_State *Lua)
  189. {
  190.     if(LuaStack == NULL)
  191.     {
  192.         luaL_error(Lua, "Stack is not created!");
  193.         return 0;
  194.     }
  195.  
  196.     lua_pushboolean(Lua, LuaStack->empty());
  197.     return 1;
  198. }
  199.  
  200. static int luaStack_Length(lua_State *Lua)
  201. {
  202.     if(LuaStack == NULL)
  203.     {
  204.         luaL_error(Lua, "Stack is not created!");
  205.         return 0;
  206.     }
  207.  
  208.     lua_pushnumber(Lua, LuaStack->slots());
  209.     return 1;
  210. }
  211.  
  212. int __declspec(dllexport) luaopen_stack (lua_State *L) {
  213.     printf("Stack loaded!\n");
  214.     struct luaL_reg driver[] = {
  215.         {"push", luaStack_Push},
  216.         {"pop", luaStack_Pop},
  217.         {"clear", luaStack_Clear},
  218.         {"size", luaStack_Size},
  219.         {"empty", luaStack_Empty},
  220.         {"create", luaStack_Create},
  221.         {"close", luaStack_Close},
  222.         {"length", luaStack_Length},
  223.         {NULL, NULL}
  224.     };
  225.  
  226.     luaL_openlib (L, "stack", driver, 0);
  227.     return 1;
  228. };
Advertisement
Add Comment
Please, Sign In to add comment