Guest User

luafunction.h

a guest
Nov 6th, 2013
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.16 KB | None | 0 0
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // This source file is part of the LuaPlus source distribution and is Copyright
  3. // 2001-2010 by Joshua C. Jensen ([email protected]).
  4. //
  5. // The latest version may be obtained from http://luaplus.org/.
  6. //
  7. // The code presented in this file may be used in any environment it is
  8. // acceptable to use Lua.
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #ifndef LUAFUNCTION_H
  11. #define LUAFUNCTION_H
  12.  
  13. #include "LuaPlusInternal.h"
  14. #include "LuaAutoBlock.h"
  15.  
  16. #if LUAPLUS_EXTENSIONS
  17.  
  18. namespace LuaPlus {
  19.  
  20. /**
  21. **/
  22. template <typename RT>
  23. class LuaFunction
  24. {
  25. public:
  26.     LuaFunction(LuaObject& _functionObj)
  27.         : functionObj(_functionObj) {
  28.     }
  29.  
  30.     LuaFunction(LuaState* state, const char* functionName) {
  31.         functionObj = state->GetGlobals()[functionName];
  32.     }
  33.  
  34.     RT operator()() {
  35.         lua_State* L = functionObj.GetCState();
  36.         LuaAutoBlock autoBlock(L);
  37.         functionObj.Push();
  38.  
  39.         if (lua_pcall(L, 0, 1, 0)) {
  40.             const char* errorString = lua_tostring(L, -1);  (void)errorString;
  41.             luaplus_assert(0);
  42.         }
  43.         return LPCD::Get(LPCD::TypeWrapper<RT>(), L, -1);
  44.     }
  45.  
  46.     template <typename P1>
  47.     RT operator()(P1 p1) {
  48.         lua_State* L = functionObj.GetCState();
  49.         LuaAutoBlock autoBlock(L);
  50.         functionObj.Push();
  51.  
  52.         LPCD::Push(L, p1);
  53.  
  54.         if (lua_pcall(L, 1, 1, 0)) {
  55.             const char* errorString = lua_tostring(L, -1);  (void)errorString;
  56.             luaplus_assert(0);
  57.         }
  58.         return LPCD::Get(LPCD::TypeWrapper<RT>(), L, -1);
  59.     }
  60.  
  61.     template <typename P1, typename P2>
  62.     RT operator()(P1 p1, P2 p2) {
  63.         lua_State* L = functionObj.GetCState();
  64.         LuaAutoBlock autoBlock(L);
  65.         functionObj.Push();
  66.  
  67.         LPCD::Push(L, p1);
  68.         LPCD::Push(L, p2);
  69.  
  70.         if (lua_pcall(L, 2, 1, 0)) {
  71.             const char* errorString = lua_tostring(L, -1);  (void)errorString;
  72.             luaplus_assert(0);
  73.         }
  74.         return LPCD::Get(LPCD::TypeWrapper<RT>(), L, -1);
  75.     }
  76.  
  77.     template <typename P1, typename P2, typename P3>
  78.     RT operator()(P1 p1, P2 p2, P3 p3) {
  79.         lua_State* L = functionObj.GetCState();
  80.         LuaAutoBlock autoBlock(L);
  81.         functionObj.Push();
  82.  
  83.         LPCD::Push(L, p1);
  84.         LPCD::Push(L, p2);
  85.         LPCD::Push(L, p3);
  86.  
  87.         if (lua_pcall(L, 3, 1, 0)) {
  88.             const char* errorString = lua_tostring(L, -1);  (void)errorString;
  89.             luaplus_assert(0);
  90.         }
  91.         return LPCD::Get(LPCD::TypeWrapper<RT>(), L, -1);
  92.     }
  93.  
  94.     template <typename P1, typename P2, typename P3, typename P4>
  95.     RT operator()(P1 p1, P2 p2, P3 p3, P4 p4) {
  96.         lua_State* L = functionObj.GetCState();
  97.         LuaAutoBlock autoBlock(L);
  98.         functionObj.Push();
  99.  
  100.         LPCD::Push(L, p1);
  101.         LPCD::Push(L, p2);
  102.         LPCD::Push(L, p3);
  103.         LPCD::Push(L, p4);
  104.  
  105.         if (lua_pcall(L, 4, 1, 0)) {
  106.             const char* errorString = lua_tostring(L, -1);  (void)errorString;
  107.             luaplus_assert(0);
  108.         }
  109.         return LPCD::Get(LPCD::TypeWrapper<RT>(), L, -1);
  110.     }
  111.  
  112.     template <typename P1, typename P2, typename P3, typename P4, typename P5>
  113.     RT operator()(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) {
  114.         lua_State* L = functionObj.GetCState();
  115.         LuaAutoBlock autoBlock(L);
  116.         functionObj.Push();
  117.  
  118.         LPCD::Push(L, p1);
  119.         LPCD::Push(L, p2);
  120.         LPCD::Push(L, p3);
  121.         LPCD::Push(L, p4);
  122.         LPCD::Push(L, p5);
  123.  
  124.         if (lua_pcall(L, 5, 1, 0)) {
  125.             const char* errorString = lua_tostring(L, -1);  (void)errorString;
  126.             luaplus_assert(0);
  127.         }
  128.         return LPCD::Get(LPCD::TypeWrapper<RT>(), L, -1);
  129.     }
  130.  
  131.     template <typename P1, typename P2, typename P3, typename P4, typename P5, typename P6>
  132.     RT operator()(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6) {
  133.         lua_State* L = functionObj.GetCState();
  134.         LuaAutoBlock autoBlock(L);
  135.         functionObj.Push();
  136.  
  137.         LPCD::Push(L, p1);
  138.         LPCD::Push(L, p2);
  139.         LPCD::Push(L, p3);
  140.         LPCD::Push(L, p4);
  141.         LPCD::Push(L, p5);
  142.         LPCD::Push(L, p6);
  143.  
  144.         if (lua_pcall(L, 6, 1, 0)) {
  145.             const char* errorString = lua_tostring(L, -1);  (void)errorString;
  146.             luaplus_assert(0);
  147.         }
  148.         return LPCD::Get(LPCD::TypeWrapper<RT>(), L, -1);
  149.     }
  150.  
  151.     template <typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7>
  152.     RT operator()(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7) {
  153.         lua_State* L = functionObj.GetCState();
  154.         LuaAutoBlock autoBlock(L);
  155.         functionObj.Push();
  156.  
  157.         LPCD::Push(L, p1);
  158.         LPCD::Push(L, p2);
  159.         LPCD::Push(L, p3);
  160.         LPCD::Push(L, p4);
  161.         LPCD::Push(L, p5);
  162.         LPCD::Push(L, p6);
  163.         LPCD::Push(L, p7);
  164.  
  165.         if (lua_pcall(L, 7, 1, 0)) {
  166.             const char* errorString = lua_tostring(L, -1);  (void)errorString;
  167.             luaplus_assert(0);
  168.         }
  169.         return LPCD::Get(LPCD::TypeWrapper<RT>(), L, -1);
  170.     }
  171.  
  172. protected:
  173.     LuaObject functionObj;
  174. };
  175.  
  176.  
  177. /**
  178. **/
  179. class LuaFunctionVoid
  180. {
  181. public:
  182.     LuaFunctionVoid(const LuaObject& functionObj)
  183.         : functionObj(functionObj) {
  184.     }
  185.  
  186.     LuaFunctionVoid(LuaState* state, const char* functionName) {
  187.         functionObj = state->GetGlobals()[functionName];
  188.     }
  189.  
  190.     void operator()() {
  191.         lua_State* L = functionObj.GetCState();
  192.         LuaAutoBlock autoBlock(L);
  193.         functionObj.Push();
  194.  
  195.         if (lua_pcall(L, 0, 0, 0)) {
  196.             const char* errorString = lua_tostring(L, -1);  (void)errorString;
  197.             luaplus_assert(0);
  198.         }
  199.     }
  200.  
  201.     template <typename P1>
  202.     void operator()(P1 p1) {
  203.         lua_State* L = functionObj.GetCState();
  204.         LuaAutoBlock autoBlock(L);
  205.         functionObj.Push();
  206.  
  207.         LPCD::Push(L, p1);
  208.  
  209.         if (lua_pcall(L, 1, 0, 0)) {
  210.             const char* errorString = lua_tostring(L, -1);  (void)errorString;
  211.             luaplus_assert(0);
  212.         }
  213.     }
  214.  
  215.     template <typename P1, typename P2>
  216.     void operator()(P1 p1, P2 p2) {
  217.         lua_State* L = functionObj.GetCState();
  218.         LuaAutoBlock autoBlock(L);
  219.         functionObj.Push();
  220.  
  221.         LPCD::Push(L, p1);
  222.         LPCD::Push(L, p2);
  223.  
  224.         if (lua_pcall(L, 2, 0, 0)) {
  225.             const char* errorString = lua_tostring(L, -1);  (void)errorString;
  226.             luaplus_assert(0);
  227.         }
  228.     }
  229.  
  230.     template <typename P1, typename P2, typename P3>
  231.     void operator()(P1 p1, P2 p2, P3 p3) {
  232.         lua_State* L = functionObj.GetCState();
  233.         LuaAutoBlock autoBlock(L);
  234.         functionObj.Push();
  235.  
  236.         LPCD::Push(L, p1);
  237.         LPCD::Push(L, p2);
  238.         LPCD::Push(L, p3);
  239.  
  240.         if (lua_pcall(L, 3, 0, 0)) {
  241.             const char* errorString = lua_tostring(L, -1);  (void)errorString;
  242.             luaplus_assert(0);
  243.         }
  244.     }
  245.  
  246.     template <typename P1, typename P2, typename P3, typename P4>
  247.     void operator()(P1 p1, P2 p2, P3 p3, P4 p4) {
  248.         lua_State* L = functionObj.GetCState();
  249.         LuaAutoBlock autoBlock(L);
  250.         functionObj.Push();
  251.  
  252.         LPCD::Push(L, p1);
  253.         LPCD::Push(L, p2);
  254.         LPCD::Push(L, p3);
  255.         LPCD::Push(L, p4);
  256.  
  257.         if (lua_pcall(L, 4, 0, 0)) {
  258.             const char* errorString = lua_tostring(L, -1);  (void)errorString;
  259.             luaplus_assert(0);
  260.         }
  261.     }
  262.  
  263.     template <typename P1, typename P2, typename P3, typename P4, typename P5>
  264.     void operator()(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) {
  265.         lua_State* L = functionObj.GetCState();
  266.         LuaAutoBlock autoBlock(L);
  267.         functionObj.Push();
  268.  
  269.         LPCD::Push(L, p1);
  270.         LPCD::Push(L, p2);
  271.         LPCD::Push(L, p3);
  272.         LPCD::Push(L, p4);
  273.         LPCD::Push(L, p5);
  274.  
  275.         if (lua_pcall(L, 5, 0, 0)) {
  276.             const char* errorString = lua_tostring(L, -1);  (void)errorString;
  277.             luaplus_assert(0);
  278.         }
  279.     }
  280.  
  281.     template <typename P1, typename P2, typename P3, typename P4, typename P5, typename P6>
  282.     void operator()(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6) {
  283.         lua_State* L = functionObj.GetCState();
  284.         LuaAutoBlock autoBlock(L);
  285.         functionObj.Push();
  286.  
  287.         LPCD::Push(L, p1);
  288.         LPCD::Push(L, p2);
  289.         LPCD::Push(L, p3);
  290.         LPCD::Push(L, p4);
  291.         LPCD::Push(L, p5);
  292.         LPCD::Push(L, p6);
  293.  
  294.         if (lua_pcall(L, 6, 0, 0)) {
  295.             const char* errorString = lua_tostring(L, -1);  (void)errorString;
  296.             luaplus_assert(0);
  297.         }
  298.     }
  299.  
  300.     template <typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7>
  301.     void operator()(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7) {
  302.         lua_State* L = functionObj.GetCState();
  303.         LuaAutoBlock autoBlock(L);
  304.         functionObj.Push();
  305.  
  306.         LPCD::Push(L, p1);
  307.         LPCD::Push(L, p2);
  308.         LPCD::Push(L, p3);
  309.         LPCD::Push(L, p4);
  310.         LPCD::Push(L, p5);
  311.         LPCD::Push(L, p6);
  312.         LPCD::Push(L, p7);
  313.  
  314.         if (lua_pcall(L, 7, 0, 0)) {
  315.             const char* errorString = lua_tostring(L, -1);  (void)errorString;
  316.             luaplus_assert(0);
  317.         }
  318.     }
  319.  
  320. protected:
  321.     LuaObject functionObj;
  322. };
  323.  
  324. } // namespace LuaPlus
  325.  
  326. #endif // LUAPLUS_EXTENSIONS
  327.  
  328. #endif // LUAFUNCTION_H
Advertisement
Add Comment
Please, Sign In to add comment